source: nagslang/tests/test_level.py@ 209:ad1d3de210cd

Last change on this file since 209:ad1d3de210cd was 209:ad1d3de210cd, checked in by Stefano Rivera <stefano@…>, 8 years ago

Drop compatibility imports, and allow an optional module on classnames

File size: 3.3 KB
Line 
1from unittest import TestCase
2
3from StringIO import StringIO
4
5from nagslang import game_object as go
6from nagslang import puzzle
7from nagslang.level import Level
8from nagslang.yamlish import load
9
10
11class FakeSpace(object):
12 def add(self, *objs):
13 pass
14
15
16class TestLevel(TestCase):
17 def make_level(self, name, data):
18 level = Level(name)
19 level._get_data = lambda: data
20 return level
21
22 def roundtrip_level(self, level):
23 newlevel = Level(level.name)
24 f = StringIO()
25 level._dump_data(f)
26 f.seek(0)
27 newlevel._get_data = lambda: load(f)
28 return newlevel
29
30 def test_empty_level(self):
31 def do_test(level):
32 level.load(FakeSpace())
33 self.assertEqual((5, 10), level.get_size())
34 self.assertEqual([], level.get_walls())
35 self.assertEqual([], level.drawables)
36
37 level = self.make_level('foo', {
38 'size': [5, 10],
39 'base_tile': 'tiles/floor.png',
40 'polygons': {},
41 })
42
43 do_test(level)
44 level2 = self.roundtrip_level(level)
45 do_test(level2)
46
47 def test_level_walls(self):
48 def do_test(level):
49 level.load(FakeSpace())
50 self.assertEqual((5, 10), level.get_size())
51 self.assertEqual([[(1, 1), (2, 1), (1, 2)]], level.get_walls())
52 self.assertEqual([], level.drawables)
53
54 level = self.make_level('foo', {
55 'size': [5, 10],
56 'base_tile': 'tiles/floor.png',
57 'polygons': {
58 1: [[1, 1], [2, 1], [1, 2]],
59 },
60 })
61 do_test(level)
62 level2 = self.roundtrip_level(level)
63 do_test(level2)
64
65 def test_level_game_objects(self):
66 def do_test(level):
67 level.load(FakeSpace())
68 self.assertEqual((5, 10), level.get_size())
69 self.assertEqual([], level.get_walls())
70 [box, switch] = level.drawables
71 self.assertTrue(isinstance(box, go.Box))
72 self.assertEqual(box.shape.body.position, (3, 3))
73 self.assertTrue(isinstance(switch, go.FloorSwitch))
74 self.assertEqual(switch.shape.body.position, (4, 4))
75
76 puzzle_bits = level._glue._components
77 self.assertEqual(['foo', 'foo_proxy'],
78 sorted(puzzle_bits.keys()))
79 self.assertTrue(
80 isinstance(puzzle_bits['foo_proxy'], puzzle.StateProxyPuzzler))
81 self.assertEqual('foo', puzzle_bits['foo_proxy']._state_source)
82 self.assertTrue(isinstance(puzzle_bits['foo'],
83 puzzle.CollidePuzzler))
84
85 level = self.make_level('foo', {
86 'size': [5, 10],
87 'base_tile': 'tiles/floor.png',
88 'polygons': {},
89 'game_objects': [
90 {
91 'classname': 'Box',
92 'args': [[3, 3]],
93 },
94 {
95 'name': 'foo',
96 'classname': 'FloorSwitch',
97 'args': [[4, 4]],
98 },
99 {
100 'name': 'foo_proxy',
101 'classname': 'puzzle.StateProxyPuzzler',
102 'args': ['foo'],
103 },
104 ],
105 })
106
107 do_test(level)
108 level2 = self.roundtrip_level(level)
109 do_test(level2)
Note: See TracBrowser for help on using the repository browser.