示例#1
0
    def test_can_see_body_pieces(self):
        '''Snake.look includes its own body pieces (not including first or second)'''
        self.snake.body.append(Snake.BodyPiece([0, 0]))
        expected_seen_object = Snake.BodyPiece([0, 10])
        self.snake.body.append(expected_seen_object)
        other_objects = []

        output = self.snake.look(other_objects)

        expected_output = expected_seen_object.visual_encoding + [10]
        self.assertListEqual(output, expected_output)
示例#2
0
    def test_calls_pygame_draw_circle_with_the_correct_arguments(
            self, mock_draw):
        '''Snake.draw calls draw on each of its body_pieces, passing the given surface along'''
        self.snake.body = [Snake.BodyPiece([0, 0]) for _ in range(5)]
        surface = MagicMock()
        self.snake.draw(surface)

        expected_calls = [call(surface) for _ in range(5)]
        mock_draw.assert_has_calls(expected_calls)
示例#3
0
    def test_adds_new_body_piece_at_correct_position_with_multiple_body_pieces(
            self):
        '''Snake.grow adds a new body piece with position equal to the oldest position of the last body piece'''
        last_piece = Snake.BodyPiece([1, 1])
        last_piece.history = [[4, 4], [3, 3], [2, 2], [1, 1]]
        self.snake.body.append(last_piece)

        self.snake.grow()

        expected_length = 3
        self.assertEqual(len(self.snake.body), expected_length)
        expected_position = last_piece.history[0]
        self.assertListEqual(self.snake.body[-1].position, expected_position)
示例#4
0
    def test_calls_move_to_on_all_tail_pieces_with_correct_positions(
            self, mock_move_to):
        '''Snake.move calls Snake.BodyPiece.move_to on each but the first of the snake's body_pieces with the oldest position in the preceeding body piece's history'''
        self.snake.body = []
        histories = [
            [[1, 1], [1, 2], [1, 3], [1, 4], [1, 5]],
            [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6]],
            [[1, 3], [1, 4], [1, 5], [1, 6], [1, 7]],
            [[1, 4], [1, 5], [1, 6], [1, 7], [1, 8]],
        ]
        for history in histories:
            new_piece = Snake.BodyPiece(history[-1][:])
            new_piece.history = history[:]
            self.snake.body.append(new_piece)

        self.snake.move()

        expected_calls = [call(history[0]) for history in histories[:-1]]
        mock_move_to.assert_has_calls(expected_calls)
示例#5
0
 def setUp(self):
     init_position = [0, 0]
     self.body_piece = Snake.BodyPiece(init_position)
示例#6
0
 def setUp(self):
     init_position = [0, 0]
     init_direction = 0
     self.snake = Snake(init_position, init_direction)
     for _ in range(5):
         self.snake.body.append(Snake.BodyPiece([0, 0]))