root/galaxy-central/eggs/Cheetah-2.2.2-py2.6-macosx-10.6-universal-ucs2.egg/Cheetah/Tests/NameMapper.py

リビジョン 3, 13.9 KB (コミッタ: kohda, 14 年 前)

Install Unix tools  http://hannonlab.cshl.edu/galaxy_unix_tools/galaxy.html

行番号 
1#!/usr/bin/env python
2
3from __future__ import generators
4import sys
5import types
6import os
7import os.path
8
9import unittest
10from Cheetah.NameMapper import NotFound, valueForKey, \
11     valueForName, valueFromSearchList, valueFromFrame, valueFromFrameOrSearchList
12
13
14class DummyClass:
15    classVar1 = 123
16
17    def __init__(self):
18        self.instanceVar1 = 123
19       
20    def __str__(self):
21        return 'object'
22
23    def meth(self, arg="arff"):
24        return str(arg)
25
26    def meth1(self, arg="doo"):
27        return arg
28
29    def meth2(self, arg1="a1", arg2="a2"):
30        raise ValueError
31
32    def meth3(self):
33        """Tests a bug that Jeff Johnson reported on Oct 1, 2001"""
34       
35        x = 'A string'
36        try:
37            for i in [1,2,3,4]:
38                if x == 2:     
39                    pass
40               
41                if x == 'xx':
42                    pass
43            return x
44        except:
45            raise
46
47
48def dummyFunc(arg="Scooby"):
49    return arg
50
51def funcThatRaises():
52    raise ValueError
53
54                 
55testNamespace = {
56    'aStr':'blarg',
57    'anInt':1,
58    'aFloat':1.5,
59    'aDict': {'one':'item1',
60              'two':'item2',
61              'nestedDict':{'one':'nestedItem1',
62                            'two':'nestedItem2',
63                            'funcThatRaises':funcThatRaises,
64                            'aClass': DummyClass,
65                            },
66              'nestedFunc':dummyFunc,
67              },
68    'aClass': DummyClass,   
69    'aFunc': dummyFunc,
70    'anObj': DummyClass(),
71    'aMeth': DummyClass().meth1,
72    'none' : None, 
73    'emptyString':'',
74    'funcThatRaises':funcThatRaises,
75    }
76   
77autoCallResults = {'aFunc':'Scooby',
78                   'aMeth':'doo',
79                   }
80
81results = testNamespace.copy()
82results.update({'anObj.meth1':'doo',
83                'aDict.one':'item1',
84                'aDict.nestedDict':testNamespace['aDict']['nestedDict'],
85                'aDict.nestedDict.one':'nestedItem1',
86                'aDict.nestedDict.aClass':DummyClass,
87                'aDict.nestedFunc':'Scooby',
88                'aClass.classVar1':123,
89                'anObj.instanceVar1':123,
90                'anObj.meth3':'A string',
91                })
92
93for k in testNamespace.keys():
94    # put them in the globals for the valueFromFrame tests
95    exec '%s = testNamespace[k]'%k
96
97##################################################
98## TEST BASE CLASSES
99
100class NameMapperTest(unittest.TestCase):
101    failureException = (NotFound,AssertionError)
102    _testNamespace = testNamespace
103    _results = results
104   
105    def namespace(self):
106        return self._testNamespace
107
108    def VFN(self, name, autocall=True):
109        return valueForName(self.namespace(), name, autocall)
110
111    def VFS(self, searchList, name, autocall=True):
112        return valueFromSearchList(searchList, name, autocall)
113
114   
115    # alias to be overriden later
116    get = VFN
117
118    def check(self, name):
119        got = self.get(name)
120        if autoCallResults.has_key(name):
121            expected = autoCallResults[name]
122        else:
123            expected = self._results[name]
124        assert got == expected
125       
126
127##################################################
128## TEST CASE CLASSES
129
130class VFN(NameMapperTest):
131
132    def test1(self):
133        """string in dict lookup"""
134        self.check('aStr')
135
136    def test2(self):
137        """string in dict lookup in a loop"""
138        for i in range(10):
139            self.check('aStr')
140           
141    def test3(self):
142        """int in dict lookup"""
143        self.check('anInt')
144
145    def test4(self):
146        """int in dict lookup in a loop"""
147        for i in range(10):
148            self.check('anInt')
149
150    def test5(self):
151        """float in dict lookup"""
152        self.check('aFloat')
153
154    def test6(self):
155        """float in dict lookup in a loop"""
156        for i in range(10):
157            self.check('aFloat')
158         
159    def test7(self):
160        """class in dict lookup"""
161        self.check('aClass')
162
163    def test8(self):
164        """class in dict lookup in a loop"""
165        for i in range(10):
166            self.check('aClass')
167           
168    def test9(self):
169        """aFunc in dict lookup"""
170        self.check('aFunc')
171
172    def test10(self):
173        """aFunc in dict lookup in a loop"""
174        for i in range(10):
175            self.check('aFunc')
176
177    def test11(self):
178        """aMeth in dict lookup"""
179        self.check('aMeth')
180
181    def test12(self):
182        """aMeth in dict lookup in a loop"""
183        for i in range(10):
184            self.check('aMeth')
185
186    def test13(self):
187        """aMeth in dict lookup"""
188        self.check('aMeth')
189
190    def test14(self):
191        """aMeth in dict lookup in a loop"""
192        for i in range(10):
193            self.check('aMeth')
194
195    def test15(self):
196        """anObj in dict lookup"""
197        self.check('anObj')
198
199    def test16(self):
200        """anObj in dict lookup in a loop"""
201        for i in range(10):
202            self.check('anObj')
203
204    def test17(self):
205        """aDict in dict lookup"""
206        self.check('aDict')
207
208    def test18(self):
209        """aDict in dict lookup in a loop"""
210        for i in range(10):
211            self.check('aDict')
212
213    def test17(self):
214        """aDict in dict lookup"""
215        self.check('aDict')
216
217    def test18(self):
218        """aDict in dict lookup in a loop"""
219        for i in range(10):
220            self.check('aDict')
221
222    def test19(self):
223        """aClass.classVar1 in dict lookup"""
224        self.check('aClass.classVar1')
225
226    def test20(self):
227        """aClass.classVar1 in dict lookup in a loop"""
228        for i in range(10):
229            self.check('aClass.classVar1')
230
231
232    def test23(self):
233        """anObj.instanceVar1 in dict lookup"""
234        self.check('anObj.instanceVar1')
235
236    def test24(self):
237        """anObj.instanceVar1 in dict lookup in a loop"""
238        for i in range(10):
239            self.check('anObj.instanceVar1')
240
241    ## tests 22, 25, and 26 removed when the underscored lookup was removed
242
243    def test27(self):
244        """anObj.meth1 in dict lookup"""
245        self.check('anObj.meth1')
246
247    def test28(self):
248        """anObj.meth1 in dict lookup in a loop"""
249        for i in range(10):
250            self.check('anObj.meth1')
251
252    def test29(self):
253        """aDict.one in dict lookup"""
254        self.check('aDict.one')
255
256    def test30(self):
257        """aDict.one in dict lookup in a loop"""
258        for i in range(10):
259            self.check('aDict.one')
260
261    def test31(self):
262        """aDict.nestedDict in dict lookup"""
263        self.check('aDict.nestedDict')
264
265    def test32(self):
266        """aDict.nestedDict in dict lookup in a loop"""
267        for i in range(10):
268            self.check('aDict.nestedDict')
269           
270    def test33(self):
271        """aDict.nestedDict.one in dict lookup"""
272        self.check('aDict.nestedDict.one')
273
274    def test34(self):
275        """aDict.nestedDict.one in dict lookup in a loop"""
276        for i in range(10):
277            self.check('aDict.nestedDict.one')
278           
279    def test35(self):
280        """aDict.nestedFunc in dict lookup"""
281        self.check('aDict.nestedFunc')
282
283    def test36(self):
284        """aDict.nestedFunc in dict lookup in a loop"""
285        for i in range(10):
286            self.check('aDict.nestedFunc')
287
288    def test37(self):
289        """aDict.nestedFunc in dict lookup - without autocalling"""
290        assert self.get('aDict.nestedFunc', False) == dummyFunc
291
292    def test38(self):
293        """aDict.nestedFunc in dict lookup in a loop - without autocalling"""
294        for i in range(10):
295            assert self.get('aDict.nestedFunc', False) == dummyFunc
296
297    def test39(self):
298        """aMeth in dict lookup - without autocalling"""
299        assert self.get('aMeth', False) == self.namespace()['aMeth']
300
301    def test40(self):
302        """aMeth in dict lookup in a loop - without autocalling"""
303        for i in range(10):
304            assert self.get('aMeth', False) == self.namespace()['aMeth']
305
306    def test41(self):
307        """anObj.meth3 in dict lookup"""
308        self.check('anObj.meth3')
309
310    def test42(self):
311        """aMeth in dict lookup in a loop"""
312        for i in range(10):
313            self.check('anObj.meth3')
314
315    def test43(self):
316        """NotFound test"""
317
318        def test(self=self):
319            self.get('anObj.methX')   
320        self.assertRaises(NotFound,test)
321       
322    def test44(self):
323        """NotFound test in a loop"""
324        def test(self=self):
325            self.get('anObj.methX')   
326
327        for i in range(10):
328            self.assertRaises(NotFound,test)
329           
330    def test45(self):
331        """Other exception from meth test"""
332
333        def test(self=self):
334            self.get('anObj.meth2')   
335        self.assertRaises(ValueError, test)
336       
337    def test46(self):
338        """Other exception from meth test in a loop"""
339        def test(self=self):
340            self.get('anObj.meth2')   
341
342        for i in range(10):
343            self.assertRaises(ValueError,test)
344
345    def test47(self):
346        """None in dict lookup"""
347        self.check('none')
348
349    def test48(self):
350        """None in dict lookup in a loop"""
351        for i in range(10):
352            self.check('none')
353           
354    def test49(self):
355        """EmptyString in dict lookup"""
356        self.check('emptyString')
357
358    def test50(self):
359        """EmptyString in dict lookup in a loop"""
360        for i in range(10):
361            self.check('emptyString')
362
363    def test51(self):
364        """Other exception from func test"""
365
366        def test(self=self):
367            self.get('funcThatRaises')   
368        self.assertRaises(ValueError, test)
369       
370    def test52(self):
371        """Other exception from func test in a loop"""
372        def test(self=self):
373            self.get('funcThatRaises')   
374
375        for i in range(10):
376            self.assertRaises(ValueError,test)
377
378
379    def test53(self):
380        """Other exception from func test"""
381
382        def test(self=self):
383            self.get('aDict.nestedDict.funcThatRaises')   
384        self.assertRaises(ValueError, test)
385       
386    def test54(self):
387        """Other exception from func test in a loop"""
388        def test(self=self):
389            self.get('aDict.nestedDict.funcThatRaises')   
390
391        for i in range(10):
392            self.assertRaises(ValueError,test)
393
394    def test55(self):
395        """aDict.nestedDict.aClass in dict lookup"""
396        self.check('aDict.nestedDict.aClass')
397
398    def test56(self):
399        """aDict.nestedDict.aClass in dict lookup in a loop"""
400        for i in range(10):
401            self.check('aDict.nestedDict.aClass')
402
403    def test57(self):
404        """aDict.nestedDict.aClass in dict lookup - without autocalling"""
405        assert self.get('aDict.nestedDict.aClass', False) == DummyClass
406
407    def test58(self):
408        """aDict.nestedDict.aClass in dict lookup in a loop - without autocalling"""
409        for i in range(10):
410            assert self.get('aDict.nestedDict.aClass', False) == DummyClass
411
412    def test59(self):
413        """Other exception from func test -- but without autocalling shouldn't raise"""
414
415        self.get('aDict.nestedDict.funcThatRaises', False)   
416       
417    def test60(self):
418        """Other exception from func test in a loop -- but without autocalling shouldn't raise"""
419
420        for i in range(10):
421            self.get('aDict.nestedDict.funcThatRaises', False)   
422
423class VFS(VFN):
424    _searchListLength = 1
425   
426    def searchList(self):
427        lng = self._searchListLength
428        if lng == 1:
429            return [self.namespace()]
430        elif lng == 2:
431            return [self.namespace(),{'dummy':1234}]
432        elif lng == 3:
433            # a tuple for kicks
434            return ({'dummy':1234}, self.namespace(),{'dummy':1234})
435        elif lng == 4:
436            # a generator for more kicks
437            return self.searchListGenerator()
438
439    def searchListGenerator(self):
440        class Test:
441            pass
442        for i in [Test(),{'dummy':1234}, self.namespace(),{'dummy':1234}]:
443            yield i
444 
445    def get(self, name, autocall=True):
446        return self.VFS(self.searchList(), name, autocall)
447       
448class VFS_2namespaces(VFS):
449    _searchListLength = 2
450   
451class VFS_3namespaces(VFS):
452    _searchListLength = 3
453
454class VFS_4namespaces(VFS):
455    _searchListLength = 4
456   
457class VFF(VFN):
458    def get(self, name, autocall=True):
459        ns = self._testNamespace
460        aStr = ns['aStr']
461        aFloat = ns['aFloat']
462        none = 'some'
463        return valueFromFrame(name, autocall)
464
465    def setUp(self):
466        """Mod some of the data
467        """
468        self._testNamespace = ns = self._testNamespace.copy()
469        self._results = res = self._results.copy()
470        ns['aStr'] = res['aStr'] = 'BLARG'
471        ns['aFloat'] = res['aFloat'] = 0.1234
472        res['none'] = 'some'
473        res['True'] = True
474        res['False'] = False
475        res['None'] = None
476        res['eval'] = eval
477
478    def test_VFF_1(self):
479        """Builtins"""
480        self.check('True')
481        self.check('None')
482        self.check('False')
483        assert self.get('eval', False)==eval
484        assert self.get('range', False)==range
485
486class VFFSL(VFS):
487    _searchListLength = 1
488
489    def setUp(self):
490        """Mod some of the data
491        """
492        self._testNamespace = ns = self._testNamespace.copy()
493        self._results = res = self._results.copy()
494        ns['aStr'] = res['aStr'] = 'BLARG'
495        ns['aFloat'] = res['aFloat'] = 0.1234
496        res['none'] = 'some'
497       
498        del ns['anInt'] # will be picked up by globals
499       
500    def VFFSL(self, searchList, name, autocall=True):
501        anInt = 1
502        none = 'some'
503        return valueFromFrameOrSearchList(searchList, name, autocall)
504   
505    def get(self, name, autocall=True):
506        return self.VFFSL(self.searchList(), name, autocall)
507
508class VFFSL_2(VFFSL):
509    _searchListLength = 2
510
511class VFFSL_3(VFFSL):
512    _searchListLength = 3
513
514class VFFSL_4(VFFSL):
515    _searchListLength = 4
516
517if sys.platform.startswith('java'):
518    del VFF, VFFSL, VFFSL_2, VFFSL_3, VFFSL_4
519
520
521##################################################
522## if run from the command line ##
523       
524if __name__ == '__main__':
525    unittest.main()
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。