def test_should_subtract_attack_power_amount_from_health(self, mock_unit): receiver = UnitBase() receiver.position = mock.Mock() receiver._health = 5 mock_unit.return_value = receiver self.attack.perform() self.assertEqual(receiver.health, 2)
def test_should_subtract_100_health_from_each_unit_on_the_floor(self): unit = UnitBase() unit._health = 20 self.floor.add(unit, 0, 1) self.captive._health = 10 self.explode.perform() self.assertEqual(self.captive.health, -90) self.assertEqual(unit.health, -80)
def setUp(self): self.unit = UnitBase() self.floor = Floor() self.floor.width = 6 self.floor.height = 5 self.floor.add(self.unit, 1, 2, "north") self.position = self.unit.position
def test_should_fetch_other_units_not_warrior(self): unit = UnitBase() warrior = Warrior() self.floor.add(unit, 0, 0, 'north') self.floor.add(warrior, 1, 0, 'north') self.assertNotIn(warrior, self.floor.other_units()) self.assertIn(unit, self.floor.other_units())
def setUp(self): self.floor = Floor() self.floor.width = 4 self.floor.height = 3 self.warrior = Warrior() self.warrior._health = 20 self.detonate = Detonate(self.warrior) self.target = UnitBase() self.target._health = 10 self.floor.add(self.warrior, 0, 1, 'east')
def test_should_get_relative_object_diagonally_when_rotating(self): unit = UnitBase() self.floor.add(unit, 0, 1) self.position.rotate(2) self.assertFalse(self.position.relative_space(-1, 1).is_empty())
def test_should_get_relative_object_diagonally(self): unit = UnitBase() self.floor.add(unit, 0, 1) self.assertFalse(self.position.relative_space(1, -1).is_empty())
def test_should_get_relative_object_in_front_when_rotated(self): unit = UnitBase() self.floor.add(unit, 2, 2) self.position.rotate(1) self.assertFalse(self.position.relative_space(1).is_empty())
def test_should_get_relative_space_in_front(self): unit = UnitBase() self.floor.add(unit, 1, 1) self.assertFalse(self.position.relative_space(1).is_empty())
def test_should_not_consider_unit_on_floor_if_no_position(self): unit = UnitBase() self.floor.add(unit, 0, 1, "north") unit.position = None self.assertEqual(self.floor.units, [])
def test_should_return_space_with_units_but_not_main_unit(self): self.floor.add(UnitBase(), 0, 1) self.assertEqual(1, len(self.listen.perform()))
class TestUnitBase(unittest.TestCase): def setUp(self): self.unit = UnitBase() def test_should_have_attack_of_zero(self): self.assertEqual(self.unit.attack_power, 0) def test_should_be_dead_if_no_position(self): self.assertEqual(self.unit.position, None) self.assertFalse(self.unit.is_alive()) def test_should_be_alive_if_has_position(self): self.unit.position = mock.Mock() self.assertTrue(self.unit.is_alive()) def test_should_default_max_health_to_0(self): self.assertEqual(self.unit.max_health, 0) def test_should_default_health_to_max_health(self): self.unit.max_health = 10 self.assertEqual(self.unit.health, 10) def test_should_subtract_health_when_taking_damage(self): self.unit.max_health = 10 self.unit.take_damage(3) self.assertEqual(self.unit.health, 7) def test_should_set_position_to_none_when_out_of_health(self): self.max_health = 10 self.unit.take_damage(10) self.assertEqual(self.unit.position, None) def test_should_return_name_as_string_representation(self): self.assertEqual(self.unit.name(), 'UnitBase') self.assertEqual(str(self.unit), 'UnitBase') @mock.patch('pythonwarrior.units.base.UnitBase.next_turn') @mock.patch('pythonwarrior.units.base.UnitBase.play_turn') def test_should_prepare_turn_by_calling_play_turn_with_next_turn_object(self, mock_play_turn, mock_next_turn): mock_next_turn.return_value = "next_turn" self.unit.prepare_turn() mock_play_turn.assert_called_with('next_turn') @mock.patch('pythonwarrior.units.base.UnitBase.next_turn') @mock.patch('pythonwarrior.units.base.Walk') def test_should_perform_action_when_calling_perform_on_turn(self, mock_walk, mock_next_turn): self.unit.position = mock.Mock() walk_object = mock.Mock(name='asdfs') mock_walk.return_value = walk_object self.unit.add_abilities('walk_') turn = mock.Mock(action=['walk_', 'backward']) mock_next_turn.return_value = turn self.unit.prepare_turn() self.unit.perform_turn() walk_object.perform.assert_called_once_with('backward') @mock.patch('pythonwarrior.units.base.UnitBase.next_turn') @mock.patch('pythonwarrior.units.base.Walk') def test_should_not_form_action_when_dead(self, mock_walk, mock_next_turn): self.unit.position = None walk_object = mock.Mock() mock_walk.return_value = walk_object walk_object.perform.side_effect = Exception('shouldnt be called') self.unit.add_abilities('walk_') turn = mock.Mock(action=['walk_', 'backward']) mock_next_turn.return_value = turn self.unit.prepare_turn() self.unit.perform_turn() def test_should_not_raise_an_exception_when_calling_perform_turn_with_no_action(self): self.unit.prepare_turn() self.unit.perform_turn() @mock.patch('pythonwarrior.units.base.UnitBase.abilities') @mock.patch('pythonwarrior.units.base.Turn') def test_should_pass_abilities_to_new_turn_when_calling_next_turn(self, mock_turn, mock_abilities): mock_turn.return_value = 'turn' self.assertEqual(self.unit.next_turn(), 'turn') mock_turn.assert_called_once_with(mock_abilities) @mock.patch('pythonwarrior.units.base.Walk') def test_should_add_abilities_to_abilities_dict(self, mock_walk): mock_walk.return_value = 'walk' self.unit.add_abilities("walk_") self.assertEqual(self.unit.abilities, {'walk_': 'walk'}) def test_should_appear_as_question_mark_on_map(self): self.assertEqual(self.unit.character, "?") def test_should_be_released_from_bonds_when_taking_damage(self): self.unit.max_health = 10 self.unit.bind() self.assertTrue(self.unit.is_bound()) self.unit.take_damage(2) self.assertFalse(self.unit.is_bound()) def test_should_be_released_from_bonds_when_calling_release(self): self.unit.bind() self.unit.unbind() self.assertFalse(self.unit.is_bound()) @mock.patch('pythonwarrior.units.base.UnitBase.next_turn') def test_should_not_perform_action_when_bound(self, mock_next_turn): self.unit.position = mock.Mock() self.unit.bind() self.unit.add_abilities("walk_") turn = mock.Mock() turn.action = ["walk_", "backward"] mock_next_turn.return_value = turn self.unit.prepare_turn() self.unit.perform_turn()
def setUp(self): self.unit = UnitBase()
class TestUnitBase(unittest.TestCase): def setUp(self): self.unit = UnitBase() def test_should_have_attack_of_zero(self): self.assertEqual(self.unit.attack_power, 0) def test_should_be_dead_if_no_position(self): self.assertEqual(self.unit.position, None) self.assertFalse(self.unit.is_alive()) def test_should_be_alive_if_has_position(self): self.unit.position = mock.Mock() self.assertTrue(self.unit.is_alive()) def test_should_default_max_health_to_0(self): self.assertEqual(self.unit.max_health, 0) def test_should_default_health_to_max_health(self): self.unit.max_health = 10 self.assertEqual(self.unit.health, 10) def test_should_subtract_health_when_taking_damage(self): self.unit.max_health = 10 self.unit.take_damage(3) self.assertEqual(self.unit.health, 7) def test_should_set_position_to_none_when_out_of_health(self): self.max_health = 10 self.unit.take_damage(10) self.assertEqual(self.unit.position, None) def test_should_return_name_as_string_representation(self): self.assertEqual(self.unit.name(), 'UnitBase') self.assertEqual(str(self.unit), 'UnitBase') @mock.patch('pythonwarrior.units.base.UnitBase.next_turn') @mock.patch('pythonwarrior.units.base.UnitBase.play_turn') def test_should_prepare_turn_by_calling_play_turn_with_next_turn_object( self, mock_play_turn, mock_next_turn): mock_next_turn.return_value = "next_turn" self.unit.prepare_turn() mock_play_turn.assert_called_with('next_turn') @mock.patch('pythonwarrior.units.base.UnitBase.next_turn') @mock.patch('pythonwarrior.units.base.Walk') def test_should_perform_action_when_calling_perform_on_turn( self, mock_walk, mock_next_turn): self.unit.position = mock.Mock() walk_object = mock.Mock(name='asdfs') mock_walk.return_value = walk_object self.unit.add_abilities('walk_') turn = mock.Mock(action=['walk_', 'backward']) mock_next_turn.return_value = turn self.unit.prepare_turn() self.unit.perform_turn() walk_object.perform.assert_called_once_with('backward') @mock.patch('pythonwarrior.units.base.UnitBase.next_turn') @mock.patch('pythonwarrior.units.base.Walk') def test_should_not_form_action_when_dead(self, mock_walk, mock_next_turn): self.unit.position = None walk_object = mock.Mock() mock_walk.return_value = walk_object walk_object.perform.side_effect = Exception('shouldnt be called') self.unit.add_abilities('walk_') turn = mock.Mock(action=['walk_', 'backward']) mock_next_turn.return_value = turn self.unit.prepare_turn() self.unit.perform_turn() def test_should_not_raise_exception_when_perform_turn_with_no_action(self): self.unit.prepare_turn() self.unit.perform_turn() @mock.patch('pythonwarrior.units.base.UnitBase.abilities') @mock.patch('pythonwarrior.units.base.Turn') def test_should_pass_abilities_to_new_turn_when_calling_next_turn( self, mock_turn, mock_abilities): mock_turn.return_value = 'turn' self.assertEqual(self.unit.next_turn(), 'turn') mock_turn.assert_called_once_with(mock_abilities) @mock.patch('pythonwarrior.units.base.Walk') def test_should_add_abilities_to_abilities_dict(self, mock_walk): mock_walk.return_value = 'walk' self.unit.add_abilities("walk_") self.assertEqual(self.unit.abilities, {'walk_': 'walk'}) def test_should_appear_as_question_mark_on_map(self): self.assertEqual(self.unit.character, "?") def test_should_be_released_from_bonds_when_taking_damage(self): self.unit.max_health = 10 self.unit.bind() self.assertTrue(self.unit.is_bound()) self.unit.take_damage(2) self.assertFalse(self.unit.is_bound()) def test_should_be_released_from_bonds_when_calling_release(self): self.unit.bind() self.unit.unbind() self.assertFalse(self.unit.is_bound()) @mock.patch('pythonwarrior.units.base.UnitBase.next_turn') def test_should_not_perform_action_when_bound(self, mock_next_turn): self.unit.position = mock.Mock() self.unit.bind() self.unit.add_abilities("walk_") turn = mock.Mock() turn.action = ["walk_", "backward"] mock_next_turn.return_value = turn self.unit.prepare_turn() self.unit.perform_turn()
def test_should_return_unique_units(self): u1 = UnitBase() self.floor.add(u1, 0, 0) self.floor.add(UnitBase(), 1, 0) self.assertEqual(self.floor.unique_units, [u1])
def test_should_bind_recipient(self): receiver = UnitBase() self.bind.unit = mock.Mock(return_value=receiver) self.bind.perform() self.assertTrue(receiver.is_bound())
def test_should_be_able_to_add_and_fetch_a_unit(self): unit = UnitBase() self.floor.add(unit, 0, 1, "north") self.assertEqual(self.floor.get(0, 1), unit)