def testSnakeLength(self): game_map = Map2D(2, 1) snake = Snake(game_map, 0, 0, Direction.X_POSITIVE) self.assertEqual(len(snake), 1) Food(game_map) snake.move() self.assertEqual(len(snake), 2)
def testMoveResultHitUnknownObject(self): game_map = Map2D(2, 1) snake = Snake(game_map, 0, 0, Direction.X_POSITIVE) game_map.put('some_object', 1, 0) with self.assertRaises(RuntimeError) as context: snake.move() self.assertIn('Blocking object unknown type', str(context.exception))
def testInsideMapBoundary(self): game_map = Map2D(2, 3) self.assertTrue(game_map.inside_map_boundary(1, 2)) self.assertFalse(game_map.inside_map_boundary(2, 0)) self.assertFalse(game_map.inside_map_boundary(-4, 0)) self.assertFalse(game_map.inside_map_boundary(0, 3)) self.assertFalse(game_map.inside_map_boundary(0, -1))
def testInvalidPosition(self): game_map = Map2D(2, 2) with self.assertRaises(ValueError) as context: game_map.validate_position(2, 2) self.assertIn('outside of the current map dimensions', str(context.exception))
def testMovementUnsuccessfulDirectionChange(self): game_map = Map2D(3, 3) snake = Snake(game_map, 0, 1, Direction.X_POSITIVE) snake.move() snake.set_direction(Direction.X_NEGATIVE) snake.move() self.assertEqual(game_map.peek(2, 1), snake)
def testTriesToPutStringToOccupiedPosition(self): game_map = Map2D(1, 2) game_map.put('blocker', 0, 1) self.assertEqual(game_map.try_put('testString', 0, 1), 'blocker') self.assertEqual(game_map.peek(0, 1), 'blocker') self.assertIsNone(game_map.peek(0, 0))
def testPutsStringToFreePosition(self): game_map = Map2D(2, 2) game_map.put('testString', 0, 1) self.assertEqual(game_map.peek(0, 1), 'testString') self.assertIsNone(game_map.peek(0, 0)) self.assertIsNone(game_map.peek(1, 0)) self.assertIsNone(game_map.peek(1, 1))
def testMoveResultAteFood(self): game_map = Map2D(2, 1) snake = Snake(game_map, 0, 0, Direction.X_POSITIVE) Food(game_map) self.assertEqual(snake.move(), Snake.MoveResult.ATE_FOOD) self.assertIs(game_map.peek(0, 0), snake) self.assertIs(game_map.peek(1, 0), snake) self.assertEqual(snake.head_pos(), (1, 0))
def testPutsStringToOccupiedPosition(self): game_map = Map2D(2, 2) game_map.put('existing', 0, 1) game_map.put('newObject', 0, 1) self.assertEqual(game_map.peek(0, 1), 'newObject') self.assertIsNone(game_map.peek(0, 0)) self.assertIsNone(game_map.peek(1, 0)) self.assertIsNone(game_map.peek(1, 1))
def testMoveSnakeOnCircularPath(self): game_map = Map2D(2, 2) snake = Snake(game_map, 0, 0, Direction.X_POSITIVE) Food(game_map) Food(game_map) Food(game_map) snake.move() snake.set_direction(Direction.Y_POSITIVE) snake.move() snake.set_direction(Direction.X_NEGATIVE) snake.move() snake.set_direction(Direction.Y_NEGATIVE) self.assertEqual(snake.move(), Snake.MoveResult.MOVED) self.assertEqual(snake.head_pos(), (0, 0)) self.assertEqual(snake.end_pos(), (1, 0))
def testMoveResultHitSnake(self): game_map = Map2D(2, 1) snake = Snake(game_map, 0, 0, Direction.X_POSITIVE) Snake(game_map, 1, 0, Direction.X_POSITIVE) self.assertEqual(snake.move(), Snake.MoveResult.HIT_SNAKE) self.assertIs(game_map.peek(0, 0), snake)
def testMoveResultHitWall(self): game_map = Map2D(1, 1) snake = Snake(game_map, 0, 0, Direction.Y_NEGATIVE) self.assertEqual(snake.move(), Snake.MoveResult.HIT_WALL)
def testMoveInUndefinedDirection(self): game_map = Map2D(1, 1) snake = Snake(game_map, 0, 0, 'not_a_direction') with self.assertRaises(RuntimeError) as context: snake.move() self.assertIn('Invalid direction', str(context.exception))
def testMoveYNegative(self): game_map = Map2D(3, 3) snake = Snake(game_map, 1, 1, Direction.Y_NEGATIVE) self.assertEqual(snake.move(), Snake.MoveResult.MOVED) self.assertIs(game_map.peek(1, 0), snake) self.assertIsNone(game_map.peek(1, 1))
def testMoveXPositive(self): game_map = Map2D(3, 3) snake = Snake(game_map, 1, 1, Direction.X_POSITIVE) self.assertEqual(snake.move(), Snake.MoveResult.MOVED) self.assertIs(game_map.peek(2, 1), snake) self.assertIsNone(game_map.peek(1, 1))
def testShape(self): game_map = Map2D(1, 5) self.assertEqual(game_map.shape(), (1, 5))
def testHeadPositionIsCorrectAfterMove(self): game_map = Map2D(3, 3) snake = Snake(game_map, 1, 1, Direction.X_POSITIVE) snake.move() self.assertEqual(snake.head_pos(), (2, 1))
def testReturnsEndPosition(self): game_map = Map2D(2, 1) snake = Snake(game_map, 1, 0, Direction.X_NEGATIVE) Food(game_map) snake.move() self.assertEqual(snake.end_pos(), (1, 0))
def testFailsToPutSnakeOnFilledPosition(self): game_map = Map2D(3, 3) game_map.put('blocker', 1, 2) with self.assertRaises(ValueError) as context: Snake(game_map, 1, 2, Direction.X_POSITIVE) self.assertIn('is blocked by', str(context.exception))
def testPutsSnakeOnEmptyPosition(self): game_map = Map2D(3, 3) snake = Snake(game_map, 1, 2, Direction.X_POSITIVE) self.assertEqual(game_map.peek(1, 2), snake)
def testValidatePosition(self): game_map = Map2D(2, 2) game_map.validate_position(0, 1)
def testReturnsHeadPosition(self): game_map = Map2D(3, 3) snake = Snake(game_map, 1, 2, Direction.X_POSITIVE) self.assertEqual(snake.head_pos(), (1, 2))
def testTriesToPutStringToFreePosition(self): game_map = Map2D(1, 2) self.assertIsNone(game_map.try_put('testString', 0, 1)) self.assertEqual(game_map.peek(0, 1), 'testString') self.assertIsNone(game_map.peek(0, 0))