Example #1
0
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)
Example #2
0
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 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 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])
Example #5
0
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])
Example #6
0
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])
Example #7
0
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)))
Example #8
0
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)))
Example #9
0
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")
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")