Ignore:
Timestamp:
09/03/13 08:47:27 (8 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
Phase:
public
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.