def setUp(self): self.profile = Profile() self.floor = Floor() self.level = Level(self.profile, 1) self.level.floor = self.floor self.level.load_level = mock.Mock() self.level.is_failed = mock.Mock(return_value=False)
def setUp(self): self.floor = Floor() self.floor.width = 2 self.floor.height = 3 self.warrior = Warrior() self.floor.add(self.warrior, 0, 0) self.listen = Listen(self.warrior)
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 setUp(self): self.floor = Floor() self.floor.width = 2 self.floor.height = 3 self.captive = Captive() self.floor.add(self.captive, 0, 0) self.explode = Explode(self.captive)
class TestExplode(unittest.TestCase): def setUp(self): self.floor = Floor() self.floor.width = 2 self.floor.height = 3 self.captive = Captive() self.floor.add(self.captive, 0, 0) self.explode = Explode(self.captive) 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 test_should_explode_when_bomb_time_reaches_zero(self): self.captive._health = 10 self.explode.time = 3 self.explode.pass_turn() self.explode.pass_turn() self.assertEqual(self.captive.health, 10) self.explode.pass_turn() self.assertEqual(self.captive.health, -90)
class TestPlaying(unittest.TestCase): def setUp(self): self.profile = Profile() self.floor = Floor() self.level = Level(self.profile, 1) self.level.floor = self.floor self.level.load_level = mock.Mock() self.level.is_failed = mock.Mock(return_value=False) def test_load_level_once_when_playing_multiple_turns(self): self.level.play(2) self.level.load_level.assert_called_once_with() def test_prepare_turn_and_play_turn_each_object_specified_amount(self): unit = Warrior() unit.prepare_turn = mock.Mock() unit.perform_turn = mock.Mock() self.floor.add(unit, 0, 0, 'north') self.level.play(2) unit.prepare_turn.assert_called_with() unit.perform_turn.assert_called_with() self.assertEqual(2, unit.prepare_turn.call_count) self.assertEqual(2, unit.perform_turn.call_count) def test_should_return_immediately_when_passed(self): unit = Warrior() unit.turn = mock.Mock() self.floor.add(unit, 0, 0, 'north') self.level.is_passed = mock.Mock(return_value=True) self.level.play(2) def test_call_fxn_in_play_for_each_turn(self): self.i = 0 def foo(): self.i += 1 self.level.play(2, foo) self.assertEqual(self.i, 2) def test_should_count_down_time_bonus_once_each_turn(self): self.level.time_bonus = 10 self.level.play(3) self.assertEqual(self.level.time_bonus, 7) def test_should_count_down_time_bonus_to_zero(self): self.level.time_bonus = 2 self.level.play(5) self.assertEqual(self.level.time_bonus, 0) def test_should_have_a_pretty_score_calculation(self): self.assertEqual(self.level.score_calculation(123, 45), "123 + 45 = 168") def test_should_not_have_score_calc_when_starting_score_is_zero(self): self.assertEqual(self.level.score_calculation(0, 45), "45")
class TestListen(unittest.TestCase): def setUp(self): self.floor = Floor() self.floor.width = 2 self.floor.height = 3 self.warrior = Warrior() self.floor.add(self.warrior, 0, 0) self.listen = Listen(self.warrior) 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()))
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')
class LevelLoader(object): def __init__(self, level): self.floor = Floor() self.level = level self.level.floor = self.floor def description(self, desc): self.level.description = desc def tip(self, level_tip): self.level.tip = level_tip def clue(self, level_clue): self.level.clue = level_clue def time_bonus(self, bonus): self.level.time_bonus = bonus def ace_score(self, score): self.level.ace_score = score def size(self, width, height): self.floor.width = width self.floor.height = height def stairs(self, x, y): self.floor.place_stairs(x, y) def unit(self, a_unit, x, y, facing="north", func=None): if not isinstance(a_unit, UnitBase): a_unit = self._unit_to_constant(a_unit)() self.floor.add(a_unit, x, y, facing) if func: func(a_unit) return a_unit def warrior(self, *args, **kwargs): a_func = kwargs.get('func', None) warrior = Warrior(self.level) return self.level.setup_warrior(self.unit(warrior, *args, func=a_func)) @staticmethod def _unit_to_constant(name): camel = "".join(map(lambda x: x.capitalize(), str(name).split('_'))) return eval(camel)
class LevelLoader(object): def __init__(self, level): self.floor = Floor() self.level = level self.level.floor = self.floor def description(self, desc): self.level.description = desc def tip(self, level_tip): self.level.tip = level_tip def clue(self, level_clue): self.level.clue = level_clue def time_bonus(self, bonus): self.level.time_bonus = bonus def ace_score(self, score): self.level.ace_score = score def size(self, width, height): self.floor.width = width self.floor.height = height def stairs(self, x, y): self.floor.place_stairs(x, y) def unit(self, a_unit, x, y, facing="north", func=None): if not isinstance(a_unit, UnitBase): a_unit = self._unit_to_constant(a_unit)() self.floor.add(a_unit, x, y, facing) if func: func(a_unit) return a_unit def warrior(self, *args, **kwargs): a_func = kwargs.get("func", None) warrior = Warrior(self.level) return self.level.setup_warrior(self.unit(warrior, *args, func=a_func)) @staticmethod def _unit_to_constant(name): camel = "".join(map(lambda x: x.capitalize(), str(name).split("_"))) return eval(camel)
def setUp(self): self.profile = Profile() self.floor = Floor() self.level = Level(self.profile, 1) self.level.floor = self.floor self.level.load_level = mock.Mock() self.level.is_failed = mock.Mock(return_value=False) self.level.floor.other_units = mock.Mock(return_value=[mock.Mock()]) self.warrior = mock.Mock(score=0, abilities={}) self.level.warrior = self.warrior
class TestFloorThreeByOne(unittest.TestCase): def setUp(self): self.floor = Floor() self.floor.width = 3 self.floor.height = 1 def test_should_print_map_with_stairs_and_unit(self): self.floor.add(Warrior(), 0, 0) self.floor.place_stairs(2, 0) self.assertEqual(self.floor.character, ''' --- |@ >| --- ''') 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 __init__(self, level): self.floor = Floor() self.level = level self.level.floor = self.floor
class TestDetonate(unittest.TestCase): 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_deal_8_damage_if_forward_1_space(self): self.floor.add(self.target, 1, 1) self.detonate.perform() self.assertEqual(2, self.target._health) def test_should_deal_4_damage_if_forward_2_spaces(self): self.floor.add(self.target, 2, 1) self.detonate.perform() self.assertEqual(6, self.target._health) def test_should_deal_4_damage_if_diagonal_1_space(self): self.floor.add(self.target, 1, 2) self.detonate.perform() self.assertEqual(6, self.target._health) self.floor.add(self.target, 1, 0) self.detonate.perform() self.assertEqual(2, self.target._health) def test_should_deal_4_damage_to_the_user(self): self.detonate.perform() self.assertEqual(16, self.warrior._health) def test_should_cause_ticking_captives_to_explode(self): mock_explode = mock.Mock() self.target.abilities['explode_'] = mock_explode self.floor.add(self.target, 1, 1) self.detonate.perform() self.assertTrue(mock_explode.perform.called)
def setUp(self): self.floor = Floor() self.floor.width = 2 self.floor.height = 3
class TestFloorTwoByThree(unittest.TestCase): def setUp(self): self.floor = Floor() self.floor.width = 2 self.floor.height = 3 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) 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_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 test_should_not_consider_corner_out_of_bounds(self): self.assertFalse(self.floor.out_of_bounds(0, 0)) self.assertFalse(self.floor.out_of_bounds(1, 0)) self.assertFalse(self.floor.out_of_bounds(1, 2)) self.assertFalse(self.floor.out_of_bounds(0, 2)) def test_should_consider_corner_out_of_bounds_when_beyond_sides(self): self.assertTrue(self.floor.out_of_bounds(-1, 0)) self.assertTrue(self.floor.out_of_bounds(0, -1)) self.assertTrue(self.floor.out_of_bounds(0, 3)) self.assertTrue(self.floor.out_of_bounds(2, 0)) def test_should_return_space_at_specified_location(self): self.assertEqual(self.floor.space(0, 0).__class__.__name__, 'Space') def test_should_place_stairs_and_be_able_to_fetch_the_location(self): self.floor.place_stairs(1, 2) self.assertEqual(self.floor.stairs_location, [1, 2])
class TestLevel(unittest.TestCase): def setUp(self): self.profile = Profile() self.floor = Floor() self.level = Level(self.profile, 1) self.level.floor = self.floor def test_should_consider_passed_When_warrior_is_on_stairs(self): self.level.warrior = Warrior() self.floor.add(self.level.warrior, 0, 0, 'north') self.floor.place_stairs(0, 0) self.assertTrue(self.level.is_passed()) def test_should_default_time_bonus_to_zero(self): self.assertEqual(self.level.time_bonus, 0) def test_should_have_a_grade_relative_to_ace_score(self): self.level.ace_score = 100 self.assertEqual(self.level.grade_for(110), "S") self.assertEqual(self.level.grade_for(100), "S") self.assertEqual(self.level.grade_for(99), "A") self.assertEqual(self.level.grade_for(89), "B") self.assertEqual(self.level.grade_for(79), "C") self.assertEqual(self.level.grade_for(69), "D") self.assertEqual(self.level.grade_for(59), "F") def test_should_have_no_grade_if_there_is_no_ace_score(self): self.assertIsNone(self.level.ace_score) self.assertIsNone(self.level.grade_for(100)) def test_should_load_file_contents_into_level(self): with mock.patch.object(self.level, 'load_path', return_value='path/to/level.py'): mock_file = mock.Mock(read=mock.Mock( return_value="level.description('foo')")) with mock.patch('builtins.open', return_value=mock_file): self.level.load_level() self.assertEqual(self.level.description, 'foo') def test_should_have_a_player_path_from_profile(self): with mock.patch.object(self.profile, '_player_path', 'path/to/player'): self.assertEqual(self.level.player_path(), 'path/to/player') def test_should_have_load_path_from_profile_tower_with_level_number(self): with mock.patch.object(self.profile, 'tower_path', 'path/to/tower'): self.assertEqual( self.level.load_path(), os.path.abspath('pythonwarrior/towers/tower/level_001.py')) @mock.patch('pythonwarrior.level.os.path.exists') def test_should_exist_if_file_exists(self, mock_exists): self.level.load_path = mock.Mock(return_value='/foo/bar') mock_exists.return_value = True self.assertTrue(self.level.exists()) mock_exists.assert_called_once_with('/foo/bar') @mock.patch('pythonwarrior.level.PlayerGenerator') def test_should_generate_player_files(self, mock_pg): generator = mock.Mock() mock_pg.return_value = generator self.level.load_level = mock.Mock() self.level.generate_player_files() generator.generate.assert_called_once_with() self.level.load_level.assert_called_once_with() def test_should_setup_warrior_with_profile_abilities(self): self.profile.abilities = ['foo', 'bar'] warrior = mock.Mock() self.level.setup_warrior(warrior) warrior.add_abilities.assert_called_once_with('foo', 'bar') def test_should_setup_warrior_with_profile_name(self): self.profile.warrior_name = "Joe" warrior = mock.Mock() self.level.setup_warrior(warrior) self.assertEqual(warrior.name_attr, "Joe")
def setUp(self): self.profile = Profile() self.floor = Floor() self.level = Level(self.profile, 1) self.level.floor = self.floor
class TestPosition(unittest.TestCase): 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_at_should_be_true_if_position_matches_x_and_y(self): self.assertTrue(self.position.at(1, 2)) def test_should_rotate_clockwise(self): self.assertEqual('north', self.position.direction) for direction in ['east', 'south', 'west', 'north', 'east']: self.position.rotate(1) self.assertEqual(direction, self.position.direction) def test_should_rotate_counter_clockwise(self): self.assertEqual('north', self.position.direction) for direction in ['west', 'south', 'east', 'north', 'west']: self.position.rotate(-1) self.assertEqual(direction, self.position.direction) 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_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_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_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_move_object_on_floor_relatively(self): self.assertEqual(self.unit, self.floor.get(1, 2)) self.position.move(-1, 2) self.assertEqual(None, self.floor.get(1, 2)) self.assertEqual(self.unit, self.floor.get(3, 3)) self.position.rotate(1) self.position.move(-1) self.assertEqual(None, self.floor.get(3, 3)) self.assertEqual(self.unit, self.floor.get(2, 3)) def test_should_return_distance_from_stairs_as_0_when_on_stairs(self): self.floor.place_stairs(1, 2) self.assertEqual(0, self.position.distance_from_stairs()) def test_should_return_distance_from_stairs_in_both_directions(self): self.floor.place_stairs(0, 3) self.assertEqual(2, self.position.distance_from_stairs()) def test_should_return_relative_direction_of_stairs(self): self.floor.place_stairs(0, 0) self.assertEqual('forward', self.position.relative_direction_of_stairs()) def test_should_return_relative_direction_of_given_space(self): result = self.position.relative_direction_of(self.floor.space(5, 3)) self.assertEqual('right', result) def test_should_be_able_to_determine_relative_direction(self): self.assertEqual('forward', self.position.relative_direction('north')) self.assertEqual('backward', self.position.relative_direction('south')) self.assertEqual('left', self.position.relative_direction('west')) self.assertEqual('right', self.position.relative_direction('east')) self.position.rotate(1) self.assertEqual('left', self.position.relative_direction('north')) self.position.rotate(1) self.assertEqual('backward', self.position.relative_direction('north')) self.position.rotate(1) self.assertEqual('right', self.position.relative_direction('north')) def test_should_return_a_space_at_the_same_location_as_position(self): self.assertEqual([1, 2], self.position.space().location()) def test_should_return_distance_of_given_space(self): self.assertEqual(5, self.position.distance_of(self.floor.space(5, 3))) self.assertEqual(3, self.position.distance_of(self.floor.space(4, 2)))
class TestPosition(unittest.TestCase): 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_at_should_be_true_if_position_matches_x_and_y(self): self.assertTrue(self.position.at(1, 2)) def test_should_rotate_clockwise(self): self.assertEqual("north", self.position.direction) for direction in ["east", "south", "west", "north", "east"]: self.position.rotate(1) self.assertEqual(direction, self.position.direction) def test_should_rotate_counter_clockwise(self): self.assertEqual("north", self.position.direction) for direction in ["west", "south", "east", "north", "west"]: self.position.rotate(-1) self.assertEqual(direction, self.position.direction) 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_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_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_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_move_object_on_floor_relatively(self): self.assertEqual(self.unit, self.floor.get(1, 2)) self.position.move(-1, 2) self.assertEqual(None, self.floor.get(1, 2)) self.assertEqual(self.unit, self.floor.get(3, 3)) self.position.rotate(1) self.position.move(-1) self.assertEqual(None, self.floor.get(3, 3)) self.assertEqual(self.unit, self.floor.get(2, 3)) def test_should_return_distance_from_stairs_as_0_when_on_stairs(self): self.floor.place_stairs(1, 2) self.assertEqual(0, self.position.distance_from_stairs()) def test_should_return_distance_from_stairs_in_both_directions(self): self.floor.place_stairs(0, 3) self.assertEqual(2, self.position.distance_from_stairs()) def test_should_return_relative_direction_of_stairs(self): self.floor.place_stairs(0, 0) self.assertEqual("forward", self.position.relative_direction_of_stairs()) def test_should_return_relative_direction_of_given_space(self): result = self.position.relative_direction_of(self.floor.space(5, 3)) self.assertEqual("right", result) def test_should_be_able_to_determine_relative_direction(self): self.assertEqual("forward", self.position.relative_direction("north")) self.assertEqual("backward", self.position.relative_direction("south")) self.assertEqual("left", self.position.relative_direction("west")) self.assertEqual("right", self.position.relative_direction("east")) self.position.rotate(1) self.assertEqual("left", self.position.relative_direction("north")) self.position.rotate(1) self.assertEqual("backward", self.position.relative_direction("north")) self.position.rotate(1) self.assertEqual("right", self.position.relative_direction("north")) def test_should_return_a_space_at_the_same_location_as_position(self): self.assertEqual([1, 2], self.position.space().location()) def test_should_return_distance_of_given_space(self): self.assertEqual(5, self.position.distance_of(self.floor.space(5, 3))) self.assertEqual(3, self.position.distance_of(self.floor.space(4, 2)))
class TestLevel(unittest.TestCase): def setUp(self): self.profile = Profile() self.floor = Floor() self.level = Level(self.profile, 1) self.level.floor = self.floor def test_should_consider_passed_When_warrior_is_on_stairs(self): self.level.warrior = Warrior() self.floor.add(self.level.warrior, 0, 0, 'north') self.floor.place_stairs(0, 0) self.assertTrue(self.level.is_passed()) def test_should_default_time_bonus_to_zero(self): self.assertEqual(self.level.time_bonus, 0) def test_should_have_a_grade_relative_to_ace_score(self): self.level.ace_score = 100 self.assertEqual(self.level.grade_for(110), "S") self.assertEqual(self.level.grade_for(100), "S") self.assertEqual(self.level.grade_for(99), "A") self.assertEqual(self.level.grade_for(89), "B") self.assertEqual(self.level.grade_for(79), "C") self.assertEqual(self.level.grade_for(69), "D") self.assertEqual(self.level.grade_for(59), "F") def test_should_have_no_grade_if_there_is_no_ace_score(self): self.assertIsNone(self.level.ace_score) self.assertIsNone(self.level.grade_for(100)) def test_should_load_file_contents_into_level(self): with mock.patch.object(self.level, 'load_path', return_value='path/to/level.py'): mock_file = mock.Mock( read=mock.Mock(return_value="level.description('foo')")) with mock.patch('__builtin__.open', return_value=mock_file): self.level.load_level() self.assertEqual(self.level.description, 'foo') def test_should_have_a_player_path_from_profile(self): with mock.patch.object(self.profile, '_player_path', 'path/to/player'): self.assertEqual(self.level.player_path(), 'path/to/player') def test_should_have_load_path_from_profile_tower_with_level_number(self): with mock.patch.object(self.profile, 'tower_path', 'path/to/tower'): self.assertEqual(self.level.load_path(), os.path.abspath( 'pythonwarrior/towers/tower/level_001.py')) @mock.patch('pythonwarrior.level.os.path.exists') def test_should_exist_if_file_exists(self, mock_exists): self.level.load_path = mock.Mock(return_value='/foo/bar') mock_exists.return_value = True self.assertTrue(self.level.exists()) mock_exists.assert_called_once_with('/foo/bar') @mock.patch('pythonwarrior.level.PlayerGenerator') def test_should_generate_player_files(self, mock_pg): generator = mock.Mock() mock_pg.return_value = generator self.level.load_level = mock.Mock() self.level.generate_player_files() generator.generate.assert_called_once_with() self.level.load_level.assert_called_once_with() def test_should_setup_warrior_with_profile_abilities(self): self.profile.abilities = ['foo', 'bar'] warrior = mock.Mock() self.level.setup_warrior(warrior) warrior.add_abilities.assert_called_once_with('foo', 'bar') def test_should_setup_warrior_with_profile_name(self): self.profile.warrior_name = "Joe" warrior = mock.Mock() self.level.setup_warrior(warrior) self.assertEqual(warrior.name_attr, "Joe")