comparison tests/swig_tests/model_tests.py @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children bf7f838e6684
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 #!/usr/bin/env python
2 from swig_test_utils import *
3 import math
4
5 class TestModel(unittest.TestCase):
6
7 def setUp(self):
8 self.model = fife.Model()
9
10 def testModel(self):
11 map1 = self.model.createMap("map001")
12 map2 = self.model.createMap("map002")
13
14 query = self.model.getMap("map001")
15 self.assertEqual(map1.getId(), query.getId())
16
17 query = self.model.getMap("map002")
18 self.assertEqual(map2.getId(), query.getId())
19
20 query = self.model.getMaps()
21 self.assertEqual(len(query), 2)
22
23 self.assertEqual(len(query), self.model.getNumMaps())
24
25 self.model.deleteMap(map2)
26
27 query = self.model.getMaps()
28 self.assertEqual(len(query), 1)
29
30 self.model.createMap("map003")
31 self.model.createMap("map004")
32
33 query = self.model.getMaps()
34 self.assertEqual(len(query), 3)
35 self.assertEqual(self.model.getNumMaps(), 3)
36
37 self.model.deleteMaps()
38 query = self.model.getMaps()
39 self.assertEqual(len(query), 0)
40 self.assertEqual(self.model.getNumMaps(), 0)
41
42 def testMaps(self):
43 map = self.model.createMap("map005")
44
45 self.assertEqual(map.getNumLayers(), 0)
46
47 grid = fife.SquareGrid()
48
49 layer1 = map.createLayer("layer001", grid)
50 layer2 = map.createLayer("layer002", grid)
51
52 self.assertEqual(layer1.getId(), "layer001")
53 self.assertEqual(layer2.getId(), "layer002")
54
55 self.assertEqual(len(map.getLayers()), 2)
56
57 self.assertEqual(map.getNumLayers(), 2)
58 map.deleteLayer(layer2)
59 self.assertEqual(map.getNumLayers(), 1)
60 map.deleteLayers()
61 self.assertEqual(map.getNumLayers(), 0)
62
63 def testLayers(self):
64 map = self.model.createMap("map006")
65
66 grid = fife.SquareGrid()
67 obj1 = self.model.createObject("object001","test_nspace")
68 obj2 = self.model.createObject("object002","test_nspace")
69
70 self.assertEqual(obj1.getId(), "object001")
71 self.assertEqual(obj2.getId(), "object002")
72
73 layer = map.createLayer("layer003", grid)
74
75 self.assertEqual(layer.hasInstances(), 0)
76 self.assertEqual(layer.getMap().getId(), map.getId())
77
78 inst = layer.createInstance(obj1, fife.ModelCoordinate(4,4))
79 layer.createInstance(obj2, fife.ModelCoordinate(5,6))
80 layer.createInstance(obj2, fife.ModelCoordinate(5,4))
81
82 #query = layer.getInstances("Name", "Goon")
83 #self.assertEqual(len(query), 2)
84 self.assertEqual(len(layer.getInstances()), 3)
85
86 #self.assertEqual(query[0].get("Name"), "Goon")
87 #p1 = fife.ModelCoordinate(4,4)
88 #print p1.x, p1.y
89 #p2 = inst.getLocation().getLayerCoordinates()
90 #print p2.x, p2.y
91 #self.assertEqual(inst.getLocation().getLayerCoordinates(), fife.ModelCoordinate(4,4))
92
93 class TestActionAngles(unittest.TestCase):
94 def setUp(self):
95 self.runaction = fife.Action("action001")
96 fife.ActionVisual.create(self.runaction)
97 self.runaction.get2dGfxVisual().addAnimation(90, 1)
98 self.runaction.get2dGfxVisual().addAnimation(0, 0)
99 self.runaction.get2dGfxVisual().addAnimation(270, 3)
100 self.runaction.get2dGfxVisual().addAnimation(180, 2)
101 self.walkaction = fife.Action("action002")
102 fife.ActionVisual.create(self.walkaction)
103 self.walkaction.get2dGfxVisual().addAnimation(70, 1)
104 self.walkaction.get2dGfxVisual().addAnimation(200, 2)
105 self.walkaction.get2dGfxVisual().addAnimation(320, 3)
106
107 def testRunAngle89(self):
108 self.assertEqual(self.runaction.get2dGfxVisual().getAnimationIndexByAngle(89), 1)
109
110 def testRunAngle90(self):
111 self.assertEqual(self.runaction.get2dGfxVisual().getAnimationIndexByAngle(90), 1)
112
113 def testRunAngle91(self):
114 self.assertEqual(self.runaction.get2dGfxVisual().getAnimationIndexByAngle(91), 1)
115
116 def testRunAngle135(self):
117 self.assertEqual(self.runaction.get2dGfxVisual().getAnimationIndexByAngle(135), 2)
118
119 def testRunAngle134(self):
120 self.assertEqual(self.runaction.get2dGfxVisual().getAnimationIndexByAngle(134), 1)
121
122 def testRunAngle136(self):
123 self.assertEqual(self.runaction.get2dGfxVisual().getAnimationIndexByAngle(136), 2)
124
125 def testRunAngle0(self):
126 self.assertEqual(self.runaction.get2dGfxVisual().getAnimationIndexByAngle(0), 0)
127
128 def testRunAngle40(self):
129 self.assertEqual(self.runaction.get2dGfxVisual().getAnimationIndexByAngle(40), 0)
130
131 def testRunAngle45(self):
132 self.assertEqual(self.runaction.get2dGfxVisual().getAnimationIndexByAngle(45), 1)
133
134 def testRunAngle270(self):
135 self.assertEqual(self.runaction.get2dGfxVisual().getAnimationIndexByAngle(270), 3)
136
137 def testRunAngle269(self):
138 self.assertEqual(self.runaction.get2dGfxVisual().getAnimationIndexByAngle(269), 3)
139
140 def testRunAngle271(self):
141 self.assertEqual(self.runaction.get2dGfxVisual().getAnimationIndexByAngle(271), 3)
142
143 def testRunAngle314(self):
144 self.assertEqual(self.runaction.get2dGfxVisual().getAnimationIndexByAngle(314), 3)
145
146 def testRunAngle359(self):
147 self.assertEqual(self.runaction.get2dGfxVisual().getAnimationIndexByAngle(359), 0)
148
149 def testRunAngle400(self):
150 self.assertEqual(self.runaction.get2dGfxVisual().getAnimationIndexByAngle(400), 0)
151
152 def testRunAngle451(self):
153 self.assertEqual(self.runaction.get2dGfxVisual().getAnimationIndexByAngle(451), 1)
154
155 def testWalkAngle0(self):
156 self.assertEqual(self.walkaction.get2dGfxVisual().getAnimationIndexByAngle(0), 3)
157
158 def testWalkAngle60(self):
159 self.assertEqual(self.walkaction.get2dGfxVisual().getAnimationIndexByAngle(60), 1)
160
161 def testWalkAngle199(self):
162 self.assertEqual(self.walkaction.get2dGfxVisual().getAnimationIndexByAngle(199), 2)
163
164 class InstanceListener(fife.InstanceActionListener):
165 def __init__(self):
166 fife.InstanceActionListener.__init__(self)
167 self.finished = False
168
169 def OnActionFinished(self, instance, action):
170 self.finishedInstance = instance
171 self.finishedAction = action
172 self.finished = True
173
174 class ActivityTests(unittest.TestCase):
175 def setUp(self):
176 grid = fife.HexGrid()
177 map = fife.Map("map007")
178 self.layer = map.addLayer("layer010", grid)
179
180 self.target = fife.Location(self.layer)
181 self.target.setPosition(fife.ModelCoordinate(10,10))
182
183 self.obj = fife.Object("object010", '')
184 self.pather = fife.LinearPather()
185 self.obj.setPather(self.pather)
186 self.inst = self.layer.addInstance(self.obj, fife.ModelCoordinate(4,4))
187 self.action = self.obj.addAction('action010', 'run')
188 self.action.addAnimation(0, 1)
189 self.action.thisown = 0
190 self.listener = InstanceListener()
191 self.inst.addActionListener(self.listener)
192
193 def testMovingAction(self):
194 self.inst.move('run', self.target, 0.5)
195 for i in xrange(30):
196 self.inst.update()
197 self.assert_(self.listener.finished)
198
199 def testNonMovingAction(self):
200 self.inst.move('run', fife.ModelCoordinate(0,0))
201 self.inst.update()
202 self.assert_(self.listener.finished)
203
204 class GridTests(unittest.TestCase):
205 def _testgrid(self, grid, curpos, access, cost):
206 for k, v in access.items():
207 print k, v
208 self.assertEqual(grid.isAccessible(fife.ModelCoordinate(*curpos), fife.ModelCoordinate(*k)), v)
209 for k, v in cost.items():
210 self.assertEqual(int(10000 * grid.getAdjacentCost(fife.ModelCoordinate(*curpos), fife.ModelCoordinate(*k))),
211 int(10000 * v))
212
213 curpos = fife.ModelCoordinate(*curpos)
214 accessiblepts = fife.ModelCoordinateVector()
215 grid.getAccessibleCoordinates(curpos, accessiblepts)
216 costpts = [fife.ModelCoordinate(*pt) for pt in cost.keys()]
217 for pt in costpts:
218 self.assert_(pt in accessiblepts)
219 for pt in accessiblepts:
220 self.assert_(pt in costpts)
221
222
223 def testHexGrid(self):
224 grid = fife.HexGrid()
225 curpos = (1,1)
226 access = {
227 (0,0): False,
228 (0,1): True,
229 (0,2): False,
230 (1,0): True,
231 (1,1): True,
232 (1,2): True,
233 (2,0): True,
234 (2,1): True,
235 (2,2): True,
236 }
237 cost = {
238 (0,1): 1,
239 (1,0): 1,
240 (1,1): 0,
241 (1,2): 1,
242 (2,0): 1,
243 (2,1): 1,
244 (2,2): 1,
245 }
246 self._testgrid(grid, curpos, access, cost)
247
248 def testSquareGrid(self):
249 grid = fife.SquareGrid()
250 curpos = (1,1)
251 access = {
252 (0,0): False,
253 (0,1): True,
254 (0,2): False,
255 (1,0): True,
256 (1,1): True,
257 (1,2): True,
258 (2,0): False,
259 (2,1): True,
260 (2,2): False,
261 }
262 cost = {
263 (0,1): 1,
264 (1,0): 1,
265 (1,1): 0,
266 (1,2): 1,
267 (2,1): 1,
268 }
269 self._testgrid(grid, curpos, access, cost)
270
271 def testDiagSquareGrid(self):
272 grid = fife.SquareGrid(True)
273 curpos = (1,1)
274 access = {
275 (0,0): True,
276 (0,1): True,
277 (0,2): True,
278 (1,0): True,
279 (1,1): True,
280 (1,2): True,
281 (2,0): True,
282 (2,1): True,
283 (2,2): True,
284 }
285 cost = {
286 (0,0): math.sqrt(2),
287 (0,1): 1,
288 (0,2): math.sqrt(2),
289 (1,0): 1,
290 (1,1): 0,
291 (1,2): 1,
292 (2,0): math.sqrt(2),
293 (2,1): 1,
294 (2,2): math.sqrt(2),
295 }
296 self._testgrid(grid, curpos, access, cost)
297
298
299 TEST_CLASSES = [TestModel, TestActionAngles, GridTests] #ActivityTests
300
301 if __name__ == '__main__':
302 unittest.main()