Пример #1
0
    def test_whenMovingElement_thenElementIsRemovedFromPreviousPoisition(self):
        energy = create_autospec(Energy)
        world_elements = WorldElements({A_MONSTER})

        world_elements.move(A_MONSTER, A_DELTA_POSITION, energy)

        self.assertNotIn(A_MONSTER, world_elements)
Пример #2
0
    def test_whenMovingElement_thenSquaredMagnitudeOfMovementIsRemovedFromEnergy(
            self):
        energy = create_autospec(Energy)
        world_elements = WorldElements({A_MONSTER})

        world_elements.move(A_MONSTER, A_DELTA_POSITION, energy)

        energy.remove.assert_called_once_with(A_DELTA_POSITION.magnitude()**2)
Пример #3
0
    def test_givenFoodAtMonsterPosition_whenPlayingTurn_thenFoodIsRemoved(
            self):
        food = Food(SOME_POSITION, SOME_ENERGY)
        foods = WorldElements({food})

        A_MONSTER.play_turn(WorldElements({A_MONSTER}), foods)

        self.assertNotIn(food, foods)
Пример #4
0
    def test_whenMovingElement_thenElementIsmovedToNewPosition(self):
        energy = create_autospec(Energy)
        world_elements = WorldElements({A_MONSTER})

        world_elements.move(A_MONSTER, A_DELTA_POSITION, energy)

        expected_element = NormalMonster(A_POSITION + A_DELTA_POSITION,
                                         SOME_ENERGY, RANGE_OF_MOTION,
                                         MONSTER_NAME)
        self.assertIn(expected_element, world_elements)
Пример #5
0
    def test_givenElementNotInRange_whenGettingSurroundingElements_thenElementNotInIterable(
            self):
        range_of_motion = 2
        a_monster_in_range = NormalMonster(
            A_POSITION + Position(range_of_motion + 1, 0),
            SOME_ENERGY,
            range_of_motion,
            MONSTER_NAME,
        )
        world_elements = WorldElements({a_monster_in_range})

        surrouding_elements = world_elements.elements_surrounding(
            A_POSITION, range_of_motion)

        self.assertNotIn(a_monster_in_range, surrouding_elements)
Пример #6
0
    def test_givenPositionAlreadyOccupied_whenMovingElement_thenNoEnergyIsWithdrawned(
            self):
        energy = create_autospec(Energy)
        world_elements = WorldElements({A_MONSTER})

        silence(world_elements.move)(A_MONSTER, Position(0, 0), energy)

        energy.remove.assert_not_called()
Пример #7
0
    def test_givenPositionAlreadyOccupied_whenMovingElement_thenExceptionIsRaised(
            self):
        energy = create_autospec(Energy)
        world_elements = WorldElements({A_MONSTER})
        arguments = (A_MONSTER, Position(0, 0), energy)

        self.assertRaises(PositionAlreadyOccupied, world_elements.move,
                          *arguments)
Пример #8
0
    def test_givenMonsterDoesntHaveEnoughEnergyToMove_whenPlayingTurn_thenMonsterDies(
            self):
        monster = NormalMonster(SOME_POSITION, SOME_ENERGY, RANGE_OF_MOTION,
                                MONSTER_NAME)
        self.monsters_mock.move.side_effect = NotEnoughEnergyToMove

        monster.play_turn(self.monsters_mock, WorldElements(set()))

        self.monsters_mock.remove.assert_called_once_with(monster)
Пример #9
0
    def test_givenNoFoodsSurroundingMonster_whenPlayingTurn_thenMonsterMovesAnyWhere(
            self):
        monster = NormalMonster(SOME_POSITION, SOME_ENERGY, RANGE_OF_MOTION,
                                MONSTER_NAME)

        monster.play_turn(self.monsters_mock, WorldElements(set()))

        self.monsters_mock.move.assert_called_once_with(
            monster, Not(Position(0, 0)), SOME_ENERGY)
Пример #10
0
    def test_givenNotEnoughEnergy_whenMovingElement_thenElementIsNotMoved(
            self):
        energy = create_autospec(Energy)
        energy.remove.side_effect = NoMoreEnergy
        world_elements = WorldElements({A_MONSTER})

        silence(world_elements.move)(A_MONSTER, A_DELTA_POSITION, energy)

        self.assertIn(A_MONSTER, world_elements)
Пример #11
0
    def test_givenNotEnoughEnergy_whenMovingElement_thenExceptionIsRaised(
            self):
        energy = create_autospec(Energy)
        energy.remove.side_effect = NoMoreEnergy
        world_elements = WorldElements({A_MONSTER})
        arguments = (A_MONSTER, A_DELTA_POSITION, energy)

        self.assertRaises(NotEnoughEnergyToMove, world_elements.move,
                          *arguments)
Пример #12
0
    def test_givenMonsterWantsToMoveIntoAnOccupiedPosition_whenPlayingTurn_thenMonsterDoesntMove(
            self):
        monster = NormalMonster(SOME_POSITION, SOME_ENERGY, RANGE_OF_MOTION,
                                MONSTER_NAME)
        self.monsters_mock.move.side_effect = PositionAlreadyOccupied

        monster.play_turn(self.monsters_mock, WorldElements(set()))

        self.monsters_mock.move.assert_called_once()
Пример #13
0
    def test_givenPositionAlreadyOccupied_whenMovingElement_thenNoMovementHappen(
            self):
        energy = create_autospec(Energy)
        another_monster = A_MONSTER.move_by(A_DELTA_POSITION)
        world_elements = WorldElements({A_MONSTER, another_monster})

        silence(world_elements.move)(A_MONSTER, A_DELTA_POSITION, energy)

        self.assertIn(A_MONSTER, world_elements)
Пример #14
0
    def test_givenFoodSurroundingMonster_whenPlayingTurn_thenMonsterMoveTowardFood(
            self):
        food = Food(SOME_POSITION + DELTA_POSITION_WITHIN_RANGE, SOME_ENERGY)
        monster = NormalMonster(SOME_POSITION, SOME_ENERGY, RANGE_OF_MOTION,
                                MONSTER_NAME)

        monster.play_turn(self.monsters_mock, WorldElements({food}))

        expected_movement = food.delta_position_from(monster.position)
        self.monsters_mock.move.assert_called_once_with(
            monster, expected_movement, SOME_ENERGY)
Пример #15
0
def generate_world_elements(
    element_constructor: Callable[[Tuple[Position, Energy]], WorldElement],
    number_of_elements: int,
) -> WorldElements:
    position_range = (5, 50)
    world_element_positions = [(Position(randint(*position_range),
                                         randint(*position_range)),
                                Energy(200))
                               for _ in range(number_of_elements)]
    return WorldElements(set(map(element_constructor,
                                 world_element_positions)))
Пример #16
0
    def test_givenMonsterDoesntHaveChanceToReproduce_whenPlayingTurn_thenMonsterDoesntReproduces(
            self, generate_fraction):
        generate_fraction.return_value = 2 * SOME_REPRODUCTION_PROBABILITY
        enough_energy_to_reproduce = Energy(10 - 1)
        monster = NormalMonster(
            SOME_POSITION,
            enough_energy_to_reproduce,
            RANGE_OF_MOTION,
            MONSTER_NAME,
            SOME_REPRODUCTION_PROBABILITY,
        )

        monster.play_turn(self.monsters_mock, WorldElements(set()))

        self.monsters_mock.add.assert_not_called()
Пример #17
0
    def test_givenNoFoodsAtMonsterPosition_whenPlayingTurn_thenMonsterDoesnGainEnergy(
            self, generate_fraction):
        generate_fraction.return_value = 2 * SOME_REPRODUCTION_PROBABILITY
        food = Food(SOME_POSITION + SOME_POSITION, SOME_ENERGY)
        monster_energy = Energy(SOME_ENERGY_VALUE)
        monster = NormalMonster(
            SOME_POSITION,
            monster_energy,
            RANGE_OF_MOTION,
            MONSTER_NAME,
            SOME_REPRODUCTION_PROBABILITY,
        )

        monster.play_turn(self.monsters_mock, WorldElements({food}))

        expected_energy = Energy(SOME_ENERGY_VALUE)
        self.assertEqual(expected_energy, monster_energy)
Пример #18
0
    def test_whenRemovingElement_thenElementIsRemoved(self):
        world_elements = WorldElements({A_MONSTER})

        world_elements.remove(A_MONSTER)

        self.assertNotIn(A_MONSTER, world_elements)