2 # $Id: NameMapper.py,v 1.11 2006/01/15 20:45:22 tavis_rudd Exp $
6 ================================================================================
7 Author: Tavis Rudd <tavis@damnsimple.com>,
8 Version: $Revision: 1.11 $
10 Last Revision Date: $Date: 2006/01/15 20:45:22 $
12 from __future__
import generators
13 __author__
= "Tavis Rudd <tavis@damnsimple.com>"
14 __revision__
= "$Revision: 1.11 $"[11:-2]
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 ##
32 self
.instanceVar1
= 123
37 def meth(self
, arg
="arff"):
40 def meth1(self
, arg
="doo"):
43 def meth2(self
, arg1
="a1", arg2
="a2"):
47 """Tests a bug that Jeff Johnson reported on Oct 1, 2001"""
62 def dummyFunc(arg
="Scooby"):
73 'aDict': {'one':'item1',
75 'nestedDict':{'one':'nestedItem1',
77 'funcThatRaises':funcThatRaises
,
80 'nestedFunc':dummyFunc
,
84 'anObj': DummyClass(),
85 'aMeth': DummyClass().meth1
,
88 'funcThatRaises':funcThatRaises
,
91 autoCallResults
= {'aFunc':'Scooby',
95 results
= testNamespace
.copy()
96 results
.update({'anObj.meth1':'doo',
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 ##################################################
114 class NameMapperTest(unittest
.TestCase
):
115 failureException
= (NotFound
,AssertionError)
116 _testNamespace
= testNamespace
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
132 def check(self
, name
):
134 if autoCallResults
.has_key(name
):
135 expected
= autoCallResults
[name
]
137 expected
= self
._results
[name
]
138 assert got
== expected
141 ##################################################
144 class VFN(NameMapperTest
):
147 """string in dict lookup"""
151 """string in dict lookup in a loop"""
156 """int in dict lookup"""
160 """int in dict lookup in a loop"""
165 """float in dict lookup"""
169 """float in dict lookup in a loop"""
174 """class in dict lookup"""
178 """class in dict lookup in a loop"""
183 """aFunc in dict lookup"""
187 """aFunc in dict lookup in a loop"""
192 """aMeth in dict lookup"""
196 """aMeth in dict lookup in a loop"""
201 """aMeth in dict lookup"""
205 """aMeth in dict lookup in a loop"""
210 """anObj in dict lookup"""
214 """anObj in dict lookup in a loop"""
219 """aDict in dict lookup"""
223 """aDict in dict lookup in a loop"""
228 """aDict in dict lookup"""
232 """aDict in dict lookup in a loop"""
237 """aClass.classVar1 in dict lookup"""
238 self
.check('aClass.classVar1')
241 """aClass.classVar1 in dict lookup in a loop"""
243 self
.check('aClass.classVar1')
247 """anObj.instanceVar1 in dict lookup"""
248 self
.check('anObj.instanceVar1')
251 """anObj.instanceVar1 in dict lookup in a loop"""
253 self
.check('anObj.instanceVar1')
255 ## tests 22, 25, and 26 removed when the underscored lookup was removed
258 """anObj.meth1 in dict lookup"""
259 self
.check('anObj.meth1')
262 """anObj.meth1 in dict lookup in a loop"""
264 self
.check('anObj.meth1')
267 """aDict.one in dict lookup"""
268 self
.check('aDict.one')
271 """aDict.one in dict lookup in a loop"""
273 self
.check('aDict.one')
276 """aDict.nestedDict in dict lookup"""
277 self
.check('aDict.nestedDict')
280 """aDict.nestedDict in dict lookup in a loop"""
282 self
.check('aDict.nestedDict')
285 """aDict.nestedDict.one in dict lookup"""
286 self
.check('aDict.nestedDict.one')
289 """aDict.nestedDict.one in dict lookup in a loop"""
291 self
.check('aDict.nestedDict.one')
294 """aDict.nestedFunc in dict lookup"""
295 self
.check('aDict.nestedFunc')
298 """aDict.nestedFunc in dict lookup in a loop"""
300 self
.check('aDict.nestedFunc')
303 """aDict.nestedFunc in dict lookup - without autocalling"""
304 assert self
.get('aDict.nestedFunc', False) == dummyFunc
307 """aDict.nestedFunc in dict lookup in a loop - without autocalling"""
309 assert self
.get('aDict.nestedFunc', False) == dummyFunc
312 """aMeth in dict lookup - without autocalling"""
313 assert self
.get('aMeth', False) == self
.namespace()['aMeth']
316 """aMeth in dict lookup in a loop - without autocalling"""
318 assert self
.get('aMeth', False) == self
.namespace()['aMeth']
321 """anObj.meth3 in dict lookup"""
322 self
.check('anObj.meth3')
325 """aMeth in dict lookup in a loop"""
327 self
.check('anObj.meth3')
333 self
.get('anObj.methX')
334 self
.assertRaises(NotFound
,test
)
337 """NotFound test in a loop"""
339 self
.get('anObj.methX')
342 self
.assertRaises(NotFound
,test
)
345 """Other exception from meth test"""
348 self
.get('anObj.meth2')
349 self
.assertRaises(ValueError, test
)
352 """Other exception from meth test in a loop"""
354 self
.get('anObj.meth2')
357 self
.assertRaises(ValueError,test
)
360 """None in dict lookup"""
364 """None in dict lookup in a loop"""
369 """EmptyString in dict lookup"""
370 self
.check('emptyString')
373 """EmptyString in dict lookup in a loop"""
375 self
.check('emptyString')
378 """Other exception from func test"""
381 self
.get('funcThatRaises')
382 self
.assertRaises(ValueError, test
)
385 """Other exception from func test in a loop"""
387 self
.get('funcThatRaises')
390 self
.assertRaises(ValueError,test
)
394 """Other exception from func test"""
397 self
.get('aDict.nestedDict.funcThatRaises')
398 self
.assertRaises(ValueError, test
)
401 """Other exception from func test in a loop"""
403 self
.get('aDict.nestedDict.funcThatRaises')
406 self
.assertRaises(ValueError,test
)
409 """aDict.nestedDict.aClass in dict lookup"""
410 self
.check('aDict.nestedDict.aClass')
413 """aDict.nestedDict.aClass in dict lookup in a loop"""
415 self
.check('aDict.nestedDict.aClass')
418 """aDict.nestedDict.aClass in dict lookup - without autocalling"""
419 assert self
.get('aDict.nestedDict.aClass', False) == DummyClass
422 """aDict.nestedDict.aClass in dict lookup in a loop - without autocalling"""
424 assert self
.get('aDict.nestedDict.aClass', False) == DummyClass
427 """Other exception from func test -- but without autocalling shouldn't raise"""
429 self
.get('aDict.nestedDict.funcThatRaises', False)
432 """Other exception from func test in a loop -- but without autocalling shouldn't raise"""
435 self
.get('aDict.nestedDict.funcThatRaises', False)
438 _searchListLength
= 1
440 def searchList(self
):
441 lng
= self
._searchListLength
443 return [self
.namespace()]
445 return [self
.namespace(),{'dummy':1234}]
448 return ({'dummy':1234}, self
.namespace(),{'dummy':1234})
450 # a generator for more kicks
451 return self
.searchListGenerator()
453 def searchListGenerator(self
):
456 for i
in [Test(),{'dummy':1234}, self
.namespace(),{'dummy':1234}]:
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
472 def get(self
, name
, autocall
=True):
473 ns
= self
._testNamespace
475 aFloat
= ns
['aFloat']
477 return valueFromFrame(name
, autocall
)
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
492 def test_VFF_1(self
):
497 assert self
.get('eval', False)==eval
498 assert self
.get('range', False)==range
501 _searchListLength
= 1
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
512 del ns
['anInt'] # will be picked up by globals
514 def VFFSL(self
, searchList
, name
, autocall
=True):
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__':