Initial Commit
[Projects.git] / pkgbuilds / pytivo / pkg / usr / share / pyTivo / Cheetah / Tests / NameMapper.py
blob2782463e6ba5d661067bfda3182f6a177af4cb3e
1 #!/usr/bin/env python
2 # $Id: NameMapper.py,v 1.11 2006/01/15 20:45:22 tavis_rudd Exp $
3 """NameMapper Tests
5 Meta-Data
6 ================================================================================
7 Author: Tavis Rudd <tavis@damnsimple.com>,
8 Version: $Revision: 1.11 $
9 Start Date: 2001/10/01
10 Last Revision Date: $Date: 2006/01/15 20:45:22 $
11 """
12 from __future__ import generators
13 __author__ = "Tavis Rudd <tavis@damnsimple.com>"
14 __revision__ = "$Revision: 1.11 $"[11:-2]
15 import sys
16 import types
17 import os
18 import os.path
20 import unittest_local_copy as unittest
21 from Cheetah.NameMapper import NotFound, valueForKey, \
22 valueForName, valueFromSearchList, valueFromFrame, valueFromFrameOrSearchList
25 ##################################################
26 ## TEST DATA FOR USE IN THE TEMPLATES ##
28 class DummyClass:
29 classVar1 = 123
31 def __init__(self):
32 self.instanceVar1 = 123
34 def __str__(self):
35 return 'object'
37 def meth(self, arg="arff"):
38 return str(arg)
40 def meth1(self, arg="doo"):
41 return arg
43 def meth2(self, arg1="a1", arg2="a2"):
44 raise ValueError
46 def meth3(self):
47 """Tests a bug that Jeff Johnson reported on Oct 1, 2001"""
49 x = 'A string'
50 try:
51 for i in [1,2,3,4]:
52 if x == 2:
53 pass
55 if x == 'xx':
56 pass
57 return x
58 except:
59 raise
62 def dummyFunc(arg="Scooby"):
63 return arg
65 def funcThatRaises():
66 raise ValueError
69 testNamespace = {
70 'aStr':'blarg',
71 'anInt':1,
72 'aFloat':1.5,
73 'aDict': {'one':'item1',
74 'two':'item2',
75 'nestedDict':{'one':'nestedItem1',
76 'two':'nestedItem2',
77 'funcThatRaises':funcThatRaises,
78 'aClass': DummyClass,
80 'nestedFunc':dummyFunc,
82 'aClass': DummyClass,
83 'aFunc': dummyFunc,
84 'anObj': DummyClass(),
85 'aMeth': DummyClass().meth1,
86 'none' : None,
87 'emptyString':'',
88 'funcThatRaises':funcThatRaises,
91 autoCallResults = {'aFunc':'Scooby',
92 'aMeth':'doo',
95 results = testNamespace.copy()
96 results.update({'anObj.meth1':'doo',
97 'aDict.one':'item1',
98 'aDict.nestedDict':testNamespace['aDict']['nestedDict'],
99 'aDict.nestedDict.one':'nestedItem1',
100 'aDict.nestedDict.aClass':DummyClass,
101 'aDict.nestedFunc':'Scooby',
102 'aClass.classVar1':123,
103 'anObj.instanceVar1':123,
104 'anObj.meth3':'A string',
107 for k in testNamespace.keys():
108 # put them in the globals for the valueFromFrame tests
109 exec '%s = testNamespace[k]'%k
111 ##################################################
112 ## TEST BASE CLASSES
114 class NameMapperTest(unittest.TestCase):
115 failureException = (NotFound,AssertionError)
116 _testNamespace = testNamespace
117 _results = results
119 def namespace(self):
120 return self._testNamespace
122 def VFN(self, name, autocall=True):
123 return valueForName(self.namespace(), name, autocall)
125 def VFS(self, searchList, name, autocall=True):
126 return valueFromSearchList(searchList, name, autocall)
129 # alias to be overriden later
130 get = VFN
132 def check(self, name):
133 got = self.get(name)
134 if autoCallResults.has_key(name):
135 expected = autoCallResults[name]
136 else:
137 expected = self._results[name]
138 assert got == expected
141 ##################################################
142 ## TEST CASE CLASSES
144 class VFN(NameMapperTest):
146 def test1(self):
147 """string in dict lookup"""
148 self.check('aStr')
150 def test2(self):
151 """string in dict lookup in a loop"""
152 for i in range(10):
153 self.check('aStr')
155 def test3(self):
156 """int in dict lookup"""
157 self.check('anInt')
159 def test4(self):
160 """int in dict lookup in a loop"""
161 for i in range(10):
162 self.check('anInt')
164 def test5(self):
165 """float in dict lookup"""
166 self.check('aFloat')
168 def test6(self):
169 """float in dict lookup in a loop"""
170 for i in range(10):
171 self.check('aFloat')
173 def test7(self):
174 """class in dict lookup"""
175 self.check('aClass')
177 def test8(self):
178 """class in dict lookup in a loop"""
179 for i in range(10):
180 self.check('aClass')
182 def test9(self):
183 """aFunc in dict lookup"""
184 self.check('aFunc')
186 def test10(self):
187 """aFunc in dict lookup in a loop"""
188 for i in range(10):
189 self.check('aFunc')
191 def test11(self):
192 """aMeth in dict lookup"""
193 self.check('aMeth')
195 def test12(self):
196 """aMeth in dict lookup in a loop"""
197 for i in range(10):
198 self.check('aMeth')
200 def test13(self):
201 """aMeth in dict lookup"""
202 self.check('aMeth')
204 def test14(self):
205 """aMeth in dict lookup in a loop"""
206 for i in range(10):
207 self.check('aMeth')
209 def test15(self):
210 """anObj in dict lookup"""
211 self.check('anObj')
213 def test16(self):
214 """anObj in dict lookup in a loop"""
215 for i in range(10):
216 self.check('anObj')
218 def test17(self):
219 """aDict in dict lookup"""
220 self.check('aDict')
222 def test18(self):
223 """aDict in dict lookup in a loop"""
224 for i in range(10):
225 self.check('aDict')
227 def test17(self):
228 """aDict in dict lookup"""
229 self.check('aDict')
231 def test18(self):
232 """aDict in dict lookup in a loop"""
233 for i in range(10):
234 self.check('aDict')
236 def test19(self):
237 """aClass.classVar1 in dict lookup"""
238 self.check('aClass.classVar1')
240 def test20(self):
241 """aClass.classVar1 in dict lookup in a loop"""
242 for i in range(10):
243 self.check('aClass.classVar1')
246 def test23(self):
247 """anObj.instanceVar1 in dict lookup"""
248 self.check('anObj.instanceVar1')
250 def test24(self):
251 """anObj.instanceVar1 in dict lookup in a loop"""
252 for i in range(10):
253 self.check('anObj.instanceVar1')
255 ## tests 22, 25, and 26 removed when the underscored lookup was removed
257 def test27(self):
258 """anObj.meth1 in dict lookup"""
259 self.check('anObj.meth1')
261 def test28(self):
262 """anObj.meth1 in dict lookup in a loop"""
263 for i in range(10):
264 self.check('anObj.meth1')
266 def test29(self):
267 """aDict.one in dict lookup"""
268 self.check('aDict.one')
270 def test30(self):
271 """aDict.one in dict lookup in a loop"""
272 for i in range(10):
273 self.check('aDict.one')
275 def test31(self):
276 """aDict.nestedDict in dict lookup"""
277 self.check('aDict.nestedDict')
279 def test32(self):
280 """aDict.nestedDict in dict lookup in a loop"""
281 for i in range(10):
282 self.check('aDict.nestedDict')
284 def test33(self):
285 """aDict.nestedDict.one in dict lookup"""
286 self.check('aDict.nestedDict.one')
288 def test34(self):
289 """aDict.nestedDict.one in dict lookup in a loop"""
290 for i in range(10):
291 self.check('aDict.nestedDict.one')
293 def test35(self):
294 """aDict.nestedFunc in dict lookup"""
295 self.check('aDict.nestedFunc')
297 def test36(self):
298 """aDict.nestedFunc in dict lookup in a loop"""
299 for i in range(10):
300 self.check('aDict.nestedFunc')
302 def test37(self):
303 """aDict.nestedFunc in dict lookup - without autocalling"""
304 assert self.get('aDict.nestedFunc', False) == dummyFunc
306 def test38(self):
307 """aDict.nestedFunc in dict lookup in a loop - without autocalling"""
308 for i in range(10):
309 assert self.get('aDict.nestedFunc', False) == dummyFunc
311 def test39(self):
312 """aMeth in dict lookup - without autocalling"""
313 assert self.get('aMeth', False) == self.namespace()['aMeth']
315 def test40(self):
316 """aMeth in dict lookup in a loop - without autocalling"""
317 for i in range(10):
318 assert self.get('aMeth', False) == self.namespace()['aMeth']
320 def test41(self):
321 """anObj.meth3 in dict lookup"""
322 self.check('anObj.meth3')
324 def test42(self):
325 """aMeth in dict lookup in a loop"""
326 for i in range(10):
327 self.check('anObj.meth3')
329 def test43(self):
330 """NotFound test"""
332 def test(self=self):
333 self.get('anObj.methX')
334 self.assertRaises(NotFound,test)
336 def test44(self):
337 """NotFound test in a loop"""
338 def test(self=self):
339 self.get('anObj.methX')
341 for i in range(10):
342 self.assertRaises(NotFound,test)
344 def test45(self):
345 """Other exception from meth test"""
347 def test(self=self):
348 self.get('anObj.meth2')
349 self.assertRaises(ValueError, test)
351 def test46(self):
352 """Other exception from meth test in a loop"""
353 def test(self=self):
354 self.get('anObj.meth2')
356 for i in range(10):
357 self.assertRaises(ValueError,test)
359 def test47(self):
360 """None in dict lookup"""
361 self.check('none')
363 def test48(self):
364 """None in dict lookup in a loop"""
365 for i in range(10):
366 self.check('none')
368 def test49(self):
369 """EmptyString in dict lookup"""
370 self.check('emptyString')
372 def test50(self):
373 """EmptyString in dict lookup in a loop"""
374 for i in range(10):
375 self.check('emptyString')
377 def test51(self):
378 """Other exception from func test"""
380 def test(self=self):
381 self.get('funcThatRaises')
382 self.assertRaises(ValueError, test)
384 def test52(self):
385 """Other exception from func test in a loop"""
386 def test(self=self):
387 self.get('funcThatRaises')
389 for i in range(10):
390 self.assertRaises(ValueError,test)
393 def test53(self):
394 """Other exception from func test"""
396 def test(self=self):
397 self.get('aDict.nestedDict.funcThatRaises')
398 self.assertRaises(ValueError, test)
400 def test54(self):
401 """Other exception from func test in a loop"""
402 def test(self=self):
403 self.get('aDict.nestedDict.funcThatRaises')
405 for i in range(10):
406 self.assertRaises(ValueError,test)
408 def test55(self):
409 """aDict.nestedDict.aClass in dict lookup"""
410 self.check('aDict.nestedDict.aClass')
412 def test56(self):
413 """aDict.nestedDict.aClass in dict lookup in a loop"""
414 for i in range(10):
415 self.check('aDict.nestedDict.aClass')
417 def test57(self):
418 """aDict.nestedDict.aClass in dict lookup - without autocalling"""
419 assert self.get('aDict.nestedDict.aClass', False) == DummyClass
421 def test58(self):
422 """aDict.nestedDict.aClass in dict lookup in a loop - without autocalling"""
423 for i in range(10):
424 assert self.get('aDict.nestedDict.aClass', False) == DummyClass
426 def test59(self):
427 """Other exception from func test -- but without autocalling shouldn't raise"""
429 self.get('aDict.nestedDict.funcThatRaises', False)
431 def test60(self):
432 """Other exception from func test in a loop -- but without autocalling shouldn't raise"""
434 for i in range(10):
435 self.get('aDict.nestedDict.funcThatRaises', False)
437 class VFS(VFN):
438 _searchListLength = 1
440 def searchList(self):
441 lng = self._searchListLength
442 if lng == 1:
443 return [self.namespace()]
444 elif lng == 2:
445 return [self.namespace(),{'dummy':1234}]
446 elif lng == 3:
447 # a tuple for kicks
448 return ({'dummy':1234}, self.namespace(),{'dummy':1234})
449 elif lng == 4:
450 # a generator for more kicks
451 return self.searchListGenerator()
453 def searchListGenerator(self):
454 class Test:
455 pass
456 for i in [Test(),{'dummy':1234}, self.namespace(),{'dummy':1234}]:
457 yield i
459 def get(self, name, autocall=True):
460 return self.VFS(self.searchList(), name, autocall)
462 class VFS_2namespaces(VFS):
463 _searchListLength = 2
465 class VFS_3namespaces(VFS):
466 _searchListLength = 3
468 class VFS_4namespaces(VFS):
469 _searchListLength = 4
471 class VFF(VFN):
472 def get(self, name, autocall=True):
473 ns = self._testNamespace
474 aStr = ns['aStr']
475 aFloat = ns['aFloat']
476 none = 'some'
477 return valueFromFrame(name, autocall)
479 def setUp(self):
480 """Mod some of the data
482 self._testNamespace = ns = self._testNamespace.copy()
483 self._results = res = self._results.copy()
484 ns['aStr'] = res['aStr'] = 'BLARG'
485 ns['aFloat'] = res['aFloat'] = 0.1234
486 res['none'] = 'some'
487 res['True'] = True
488 res['False'] = False
489 res['None'] = None
490 res['eval'] = eval
492 def test_VFF_1(self):
493 """Builtins"""
494 self.check('True')
495 self.check('None')
496 self.check('False')
497 assert self.get('eval', False)==eval
498 assert self.get('range', False)==range
500 class VFFSL(VFS):
501 _searchListLength = 1
503 def setUp(self):
504 """Mod some of the data
506 self._testNamespace = ns = self._testNamespace.copy()
507 self._results = res = self._results.copy()
508 ns['aStr'] = res['aStr'] = 'BLARG'
509 ns['aFloat'] = res['aFloat'] = 0.1234
510 res['none'] = 'some'
512 del ns['anInt'] # will be picked up by globals
514 def VFFSL(self, searchList, name, autocall=True):
515 anInt = 1
516 none = 'some'
517 return valueFromFrameOrSearchList(searchList, name, autocall)
519 def get(self, name, autocall=True):
520 return self.VFFSL(self.searchList(), name, autocall)
522 class VFFSL_2(VFFSL):
523 _searchListLength = 2
525 class VFFSL_3(VFFSL):
526 _searchListLength = 3
528 class VFFSL_4(VFFSL):
529 _searchListLength = 4
531 if sys.platform.startswith('java'):
532 del VFF, VFFSL, VFFSL_2, VFFSL_3, VFFSL_4
535 ##################################################
536 ## if run from the command line ##
538 if __name__ == '__main__':
539 unittest.main()