Changeset 164:06c681ff53aa


Ignore:
Timestamp:
Sep 3, 2013, 8:47:27 AM (7 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
Message:

Round-tripping through load/save shouldn't discard objects

Location:
nagslang
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • nagslang/level.py

    r145 r164  
    3030        self._glue = go.PuzzleGlue()
    3131        self._drawables = []
     32        self._game_objects = []
    3233
    3334    def _get_data(self):
     
    3536        with resources.get_file('levels', self.name) as f:
    3637            return load(f)
     38
     39    def _dump_data(self, f):
     40        # For manipulation in tests.
     41        dump({
     42            'size': [self.x, self.y],
     43            'base_tile': self.basetile,
     44            'polygons': self.polygons,
     45            'game_objects': self._game_objects,
     46        }, f)
    3747
    3848    def load(self, space):
     
    4454            for point in points:
    4555                self.polygons[i].append(tuple(point))
    46         for game_object_dict in data.get('game_objects', []):
     56        self._game_objects = data.get('game_objects', [])
     57        for game_object_dict in self._game_objects:
    4758            self._create_game_object(space, **game_object_dict)
    4859
     
    8495            return False
    8596        with resources.get_file('levels', self.name, mode='w') as f:
    86             dump({
    87                 'size': [self.x, self.y],
    88                 'base_tile': self.basetile,
    89                 'polygons': self.polygons,
    90             }, f)
     97            self._dump_data(f)
    9198        return True
    9299
  • nagslang/tests/test_level.py

    r145 r164  
    11from unittest import TestCase
     2
     3from StringIO import StringIO
    24
    35from nagslang import game_object as go
    46from nagslang.level import Level
     7from nagslang.yamlish import load
    58
    69
     
    1619        return level
    1720
     21    def roundtrip_level(self, level):
     22        newlevel = Level(level.name)
     23        f = StringIO()
     24        level._dump_data(f)
     25        f.seek(0)
     26        newlevel._get_data = lambda: load(f)
     27        return newlevel
     28
    1829    def test_empty_level(self):
     30        def do_test(level):
     31            level.load(FakeSpace())
     32            self.assertEqual((5, 10), level.get_size())
     33            self.assertEqual([], level.get_walls())
     34            self.assertEqual([], level.get_drawables())
     35
    1936        level = self.make_level('foo', {
    2037            'size': [5, 10],
     
    2239            'polygons': {},
    2340        })
    24         level.load(FakeSpace())
    25         self.assertEqual((5, 10), level.get_size())
    26         self.assertEqual([], level.get_walls())
    27         self.assertEqual([], level.get_drawables())
     41
     42        do_test(level)
     43        level2 = self.roundtrip_level(level)
     44        do_test(level2)
    2845
    2946    def test_level_walls(self):
     47        def do_test(level):
     48            level.load(FakeSpace())
     49            self.assertEqual((5, 10), level.get_size())
     50            self.assertEqual([[(1, 1), (2, 1), (1, 2)]], level.get_walls())
     51            self.assertEqual([], level.get_drawables())
     52
    3053        level = self.make_level('foo', {
    3154            'size': [5, 10],
     
    3558            },
    3659        })
    37         level.load(FakeSpace())
    38         self.assertEqual((5, 10), level.get_size())
    39         self.assertEqual([[(1, 1), (2, 1), (1, 2)]], level.get_walls())
    40         self.assertEqual([], level.get_drawables())
     60        do_test(level)
     61        level2 = self.roundtrip_level(level)
     62        do_test(level2)
    4163
    4264    def test_level_game_objects(self):
     65        def do_test(level):
     66            level.load(FakeSpace())
     67            self.assertEqual((5, 10), level.get_size())
     68            self.assertEqual([], level.get_walls())
     69            [box, switch] = level.get_drawables()
     70            self.assertTrue(isinstance(box, go.Box))
     71            self.assertEqual(box.shape.body.position, (3, 3))
     72            self.assertTrue(isinstance(switch, go.FloorSwitch))
     73            self.assertEqual(switch.shape.body.position, (4, 4))
     74
     75            puzzle_bits = level._glue._components
     76            self.assertEqual(['foo', 'foo_proxy'],
     77                             sorted(puzzle_bits.keys()))
     78            self.assertTrue(
     79                isinstance(puzzle_bits['foo_proxy'], go.StateProxyPuzzler))
     80            self.assertEqual('foo', puzzle_bits['foo_proxy']._state_source)
     81            self.assertTrue(isinstance(puzzle_bits['foo'],
     82                            go.FloorSwitchPuzzler))
     83
    4384        level = self.make_level('foo', {
    4485            'size': [5, 10],
     
    62103            ],
    63104        })
    64         level.load(FakeSpace())
    65         self.assertEqual((5, 10), level.get_size())
    66         self.assertEqual([], level.get_walls())
    67         [box, switch] = level.get_drawables()
    68         self.assertTrue(isinstance(box, go.Box))
    69         self.assertEqual(box.shape.body.position, (3, 3))
    70         self.assertTrue(isinstance(switch, go.FloorSwitch))
    71         self.assertEqual(switch.shape.body.position, (4, 4))
    72105
    73         puzzle_bits = level._glue._components
    74         self.assertEqual(['foo', 'foo_proxy'], sorted(puzzle_bits.keys()))
    75         self.assertTrue(
    76             isinstance(puzzle_bits['foo_proxy'], go.StateProxyPuzzler))
    77         self.assertEqual('foo', puzzle_bits['foo_proxy']._state_source)
    78         self.assertTrue(isinstance(puzzle_bits['foo'], go.FloorSwitchPuzzler))
     106        do_test(level)
     107        level2 = self.roundtrip_level(level)
     108        do_test(level2)
Note: See TracChangeset for help on using the changeset viewer.