source: nagslang/tests/test_level.py@ 684:f3e79d6225b5

Last change on this file since 684:f3e79d6225b5 was 684:f3e79d6225b5, checked in by Neil Muller <drnlmuller@…>, 9 years ago

Add fake world for the level tests

File size: 3.6 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 FakeWorld(object):
17
18 def __init__(self):
19 self.level_state = {}
20
21
22class TestLevel(TestCase):
23 def make_level(self, name, data):
24 world = FakeWorld()
25 level = Level(name, world)
26 level._get_data = lambda: data
27 return level
28
29 def roundtrip_level(self, level):
30 world = FakeWorld()
31 newlevel = Level(level.name, world)
32 f = StringIO()
33 level._dump_data(f)
34 f.seek(0)
35 newlevel._get_data = lambda: load(f)
36 return newlevel
37
38 def test_empty_level(self):
39 def do_test(level):
40 level.load(FakeSpace())
41 self.assertEqual((5, 10), level.get_size())
42 self.assertEqual([], level.get_walls())
43 self.assertEqual([], level.drawables)
44 self.assertEqual('test.ogg', level.music)
45
46 level = self.make_level('foo', {
47 'size': [5, 10],
48 'base_tile': 'tiles/floor.png',
49 'polygons': {},
50 'music': 'test.ogg'
51 })
52
53 do_test(level)
54 level2 = self.roundtrip_level(level)
55 do_test(level2)
56
57 def test_level_walls(self):
58 def do_test(level):
59 level.load(FakeSpace())
60 self.assertEqual((5, 10), level.get_size())
61 self.assertEqual([[(1, 1), (2, 1), (1, 2)]], level.get_walls())
62 self.assertEqual([], level.drawables)
63
64 level = self.make_level('foo', {
65 'size': [5, 10],
66 'base_tile': 'tiles/floor.png',
67 'polygons': {
68 1: [[1, 1], [2, 1], [1, 2]],
69 },
70 'music': 'test.ogg'
71 })
72 do_test(level)
73 level2 = self.roundtrip_level(level)
74 do_test(level2)
75
76 def test_level_game_objects(self):
77 def do_test(level):
78 level.load(FakeSpace())
79 self.assertEqual((5, 10), level.get_size())
80 self.assertEqual([], level.get_walls())
81 [box, switch] = level.drawables
82 self.assertTrue(isinstance(box, go.Box))
83 self.assertEqual(box.shape.body.position, (3, 3))
84 self.assertTrue(isinstance(switch, go.FloorSwitch))
85 self.assertEqual(switch.shape.body.position, (4, 4))
86
87 puzzle_bits = level._glue._components
88 self.assertEqual(['foo', 'foo_proxy'],
89 sorted(puzzle_bits.keys()))
90 self.assertTrue(
91 isinstance(puzzle_bits['foo_proxy'], puzzle.StateProxyPuzzler))
92 self.assertEqual('foo', puzzle_bits['foo_proxy']._state_source)
93 self.assertTrue(isinstance(puzzle_bits['foo'],
94 puzzle.CollidePuzzler))
95
96 level = self.make_level('foo', {
97 'size': [5, 10],
98 'music': 'test.ogg',
99 'base_tile': 'tiles/floor.png',
100 'polygons': {},
101 'game_objects': [
102 {
103 'classname': 'Box',
104 'args': [[3, 3]],
105 },
106 {
107 'name': 'foo',
108 'classname': 'FloorSwitch',
109 'args': [[4, 4]],
110 },
111 {
112 'name': 'foo_proxy',
113 'classname': 'puzzle.StateProxyPuzzler',
114 'args': ['foo'],
115 },
116 ],
117 })
118
119 do_test(level)
120 level2 = self.roundtrip_level(level)
121 do_test(level2)
Note: See TracBrowser for help on using the repository browser.