source: nagslang/tests/test_level.py@ 202:63d7ab8ede83

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

Level doesn't gave a get_drawables() getter any more, that was rubbish

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.level import Level
7from nagslang.yamlish import load
8
9
10class FakeSpace(object):
11 def add(self, *objs):
12 pass
13
14
15class TestLevel(TestCase):
16 def make_level(self, name, data):
17 level = Level(name)
18 level._get_data = lambda: data
19 return level
20
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
29 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.drawables)
35
36 level = self.make_level('foo', {
37 'size': [5, 10],
38 'base_tile': 'tiles/floor.png',
39 'polygons': {},
40 })
41
42 do_test(level)
43 level2 = self.roundtrip_level(level)
44 do_test(level2)
45
46 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.drawables)
52
53 level = self.make_level('foo', {
54 'size': [5, 10],
55 'base_tile': 'tiles/floor.png',
56 'polygons': {
57 1: [[1, 1], [2, 1], [1, 2]],
58 },
59 })
60 do_test(level)
61 level2 = self.roundtrip_level(level)
62 do_test(level2)
63
64 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.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'], go.CollidePuzzler))
82
83 level = self.make_level('foo', {
84 'size': [5, 10],
85 'base_tile': 'tiles/floor.png',
86 'polygons': {},
87 'game_objects': [
88 {
89 'classname': 'Box',
90 'args': [[3, 3]],
91 },
92 {
93 'name': 'foo',
94 'classname': 'FloorSwitch',
95 'args': [[4, 4]],
96 },
97 {
98 'name': 'foo_proxy',
99 'classname': 'StateProxyPuzzler',
100 'args': ['foo'],
101 },
102 ],
103 })
104
105 do_test(level)
106 level2 = self.roundtrip_level(level)
107 do_test(level2)
Note: See TracBrowser for help on using the repository browser.