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)
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)
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)
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)
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)
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()
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)
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)
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)
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)
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)
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()
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)
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)
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)))
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()
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)
def test_whenRemovingElement_thenElementIsRemoved(self): world_elements = WorldElements({A_MONSTER}) world_elements.remove(A_MONSTER) self.assertNotIn(A_MONSTER, world_elements)