Logo Search packages:      
Sourcecode: karrigell version File versions  Download package

NameMapper.py

#!/usr/bin/env python
# $Id: NameMapper.py,v 1.10 2005/01/03 20:11:13 tavis_rudd Exp $
"""NameMapper Tests

Meta-Data
================================================================================
Author: Tavis Rudd <tavis@damnsimple.com>,
Version: $Revision: 1.10 $
Start Date: 2001/10/01
Last Revision Date: $Date: 2005/01/03 20:11:13 $
"""
__author__ = "Tavis Rudd <tavis@damnsimple.com>"
__revision__ = "$Revision: 1.10 $"[11:-2]

from __future__ import generators
import sys
import types
import os
import os.path

import unittest_local_copy as unittest
from Cheetah.NameMapper import NotFound, valueForKey, \
     valueForName, valueFromSearchList, valueFromFrame, valueFromFrameOrSearchList


##################################################
## TEST DATA FOR USE IN THE TEMPLATES ##

00029 class DummyClass:
    classVar1 = 123

    def __init__(self):
        self.instanceVar1 = 123
        
    def __str__(self):
        return 'object'

    def meth(self, arg="arff"):
        return str(arg)

    def meth1(self, arg="doo"):
        return arg

    def meth2(self, arg1="a1", arg2="a2"):
        raise ValueError

00047     def meth3(self):
        """Tests a bug that Jeff Johnson reported on Oct 1, 2001"""
        
        x = 'A string'
        try:
            for i in [1,2,3,4]:
                if x == 2:    
                    pass
                
                if x == 'xx':
                    pass
            return x
        except:
            raise


def dummyFunc(arg="Scooby"):
    return arg

def funcThatRaises():
    raise ValueError

                 
testNamespace = {
    'aStr':'blarg',
    'anInt':1,
    'aFloat':1.5,
    'aDict': {'one':'item1',
              'two':'item2',
              'nestedDict':{'one':'nestedItem1',
                            'two':'nestedItem2',
                            'funcThatRaises':funcThatRaises,
                            'aClass': DummyClass,
                            },
              'nestedFunc':dummyFunc,
              },
    'aClass': DummyClass,    
    'aFunc': dummyFunc,
    'anObj': DummyClass(),
    'aMeth': DummyClass().meth1,
    'none' : None,  
    'emptyString':'',
    'funcThatRaises':funcThatRaises,
    }
    
autoCallResults = {'aFunc':'Scooby',
                   'aMeth':'doo',
                   }

results = testNamespace.copy()
results.update({'anObj.meth1':'doo',
                'aDict.one':'item1',
                'aDict.nestedDict':testNamespace['aDict']['nestedDict'],
                'aDict.nestedDict.one':'nestedItem1',
                'aDict.nestedDict.aClass':DummyClass,
                'aDict.nestedFunc':'Scooby',
                'aClass.classVar1':123,
                'anObj.instanceVar1':123,
                'anObj.meth3':'A string',
                })

for k in testNamespace.keys():
    # put them in the globals for the valueFromFrame tests
    exec '%s = testNamespace[k]'%k

##################################################
## TEST BASE CLASSES

00115 class NameMapperTest(unittest.TestCase):
    failureException = (NotFound,AssertionError)
    _testNamespace = testNamespace
    _results = results
    
    def namespace(self):
        return self._testNamespace

    def VFN(self, name, autocall=True):
        return valueForName(self.namespace(), name, autocall)

    def VFS(self, searchList, name, autocall=True):
        return valueFromSearchList(searchList, name, autocall)

    
    # alias to be overriden later
    get = VFN

    def check(self, name):
        got = self.get(name)
        if autoCallResults.has_key(name):
            expected = autoCallResults[name]
        else:
            expected = self._results[name]
        assert got == expected
        

##################################################
## TEST CASE CLASSES

00145 class VFN(NameMapperTest):

00147     def test1(self):
        """string in dict lookup"""
        self.check('aStr')

00151     def test2(self):
        """string in dict lookup in a loop"""
        for i in range(10):
            self.check('aStr')
            
00156     def test3(self):
        """int in dict lookup"""
        self.check('anInt')

00160     def test4(self):
        """int in dict lookup in a loop"""
        for i in range(10):
            self.check('anInt')

00165     def test5(self):
        """float in dict lookup"""
        self.check('aFloat')

00169     def test6(self):
        """float in dict lookup in a loop"""
        for i in range(10):
            self.check('aFloat')
          
00174     def test7(self):
        """class in dict lookup"""
        self.check('aClass')

00178     def test8(self):
        """class in dict lookup in a loop"""
        for i in range(10):
            self.check('aClass')
            
00183     def test9(self):
        """aFunc in dict lookup"""
        self.check('aFunc')

00187     def test10(self):
        """aFunc in dict lookup in a loop"""
        for i in range(10):
            self.check('aFunc')

00192     def test11(self):
        """aMeth in dict lookup"""
        self.check('aMeth')

00196     def test12(self):
        """aMeth in dict lookup in a loop"""
        for i in range(10):
            self.check('aMeth')

00201     def test13(self):
        """aMeth in dict lookup"""
        self.check('aMeth')

00205     def test14(self):
        """aMeth in dict lookup in a loop"""
        for i in range(10):
            self.check('aMeth')

00210     def test15(self):
        """anObj in dict lookup"""
        self.check('anObj')

00214     def test16(self):
        """anObj in dict lookup in a loop"""
        for i in range(10):
            self.check('anObj')

00219     def test17(self):
        """aDict in dict lookup"""
        self.check('aDict')

00223     def test18(self):
        """aDict in dict lookup in a loop"""
        for i in range(10):
            self.check('aDict')

00228     def test17(self):
        """aDict in dict lookup"""
        self.check('aDict')

00232     def test18(self):
        """aDict in dict lookup in a loop"""
        for i in range(10):
            self.check('aDict')

00237     def test19(self):
        """aClass.classVar1 in dict lookup"""
        self.check('aClass.classVar1')

00241     def test20(self):
        """aClass.classVar1 in dict lookup in a loop"""
        for i in range(10):
            self.check('aClass.classVar1')


00247     def test23(self):
        """anObj.instanceVar1 in dict lookup"""
        self.check('anObj.instanceVar1')

00251     def test24(self):
        """anObj.instanceVar1 in dict lookup in a loop"""
        for i in range(10):
            self.check('anObj.instanceVar1')

    ## tests 22, 25, and 26 removed when the underscored lookup was removed

00258     def test27(self):
        """anObj.meth1 in dict lookup"""
        self.check('anObj.meth1')

00262     def test28(self):
        """anObj.meth1 in dict lookup in a loop"""
        for i in range(10):
            self.check('anObj.meth1')

00267     def test29(self):
        """aDict.one in dict lookup"""
        self.check('aDict.one')

00271     def test30(self):
        """aDict.one in dict lookup in a loop"""
        for i in range(10):
            self.check('aDict.one')

00276     def test31(self):
        """aDict.nestedDict in dict lookup"""
        self.check('aDict.nestedDict')

00280     def test32(self):
        """aDict.nestedDict in dict lookup in a loop"""
        for i in range(10):
            self.check('aDict.nestedDict')
            
00285     def test33(self):
        """aDict.nestedDict.one in dict lookup"""
        self.check('aDict.nestedDict.one')

00289     def test34(self):
        """aDict.nestedDict.one in dict lookup in a loop"""
        for i in range(10):
            self.check('aDict.nestedDict.one')
            
00294     def test35(self):
        """aDict.nestedFunc in dict lookup"""
        self.check('aDict.nestedFunc')

00298     def test36(self):
        """aDict.nestedFunc in dict lookup in a loop"""
        for i in range(10):
            self.check('aDict.nestedFunc')

00303     def test37(self):
        """aDict.nestedFunc in dict lookup - without autocalling"""
        assert self.get('aDict.nestedFunc', False) == dummyFunc

00307     def test38(self):
        """aDict.nestedFunc in dict lookup in a loop - without autocalling"""
        for i in range(10):
            assert self.get('aDict.nestedFunc', False) == dummyFunc

00312     def test39(self):
        """aMeth in dict lookup - without autocalling"""
        assert self.get('aMeth', False) == self.namespace()['aMeth']

00316     def test40(self):
        """aMeth in dict lookup in a loop - without autocalling"""
        for i in range(10):
            assert self.get('aMeth', False) == self.namespace()['aMeth']

00321     def test41(self):
        """anObj.meth3 in dict lookup"""
        self.check('anObj.meth3')

00325     def test42(self):
        """aMeth in dict lookup in a loop"""
        for i in range(10):
            self.check('anObj.meth3')

00330     def test43(self):
        """NotFound test"""

        def test(self=self):
            self.get('anObj.methX')    
        self.assertRaises(NotFound,test)
        
00337     def test44(self):
        """NotFound test in a loop"""
        def test(self=self):
            self.get('anObj.methX')    

        for i in range(10):
            self.assertRaises(NotFound,test)
            
00345     def test45(self):
        """Other exception from meth test"""

        def test(self=self):
            self.get('anObj.meth2')    
        self.assertRaises(ValueError, test)
        
00352     def test46(self):
        """Other exception from meth test in a loop"""
        def test(self=self):
            self.get('anObj.meth2')    

        for i in range(10):
            self.assertRaises(ValueError,test)

00360     def test47(self):
        """None in dict lookup"""
        self.check('none')

00364     def test48(self):
        """None in dict lookup in a loop"""
        for i in range(10):
            self.check('none')
            
00369     def test49(self):
        """EmptyString in dict lookup"""
        self.check('emptyString')

00373     def test50(self):
        """EmptyString in dict lookup in a loop"""
        for i in range(10):
            self.check('emptyString')

00378     def test51(self):
        """Other exception from func test"""

        def test(self=self):
            self.get('funcThatRaises')    
        self.assertRaises(ValueError, test)
        
00385     def test52(self):
        """Other exception from func test in a loop"""
        def test(self=self):
            self.get('funcThatRaises')    

        for i in range(10):
            self.assertRaises(ValueError,test)


00394     def test53(self):
        """Other exception from func test"""

        def test(self=self):
            self.get('aDict.nestedDict.funcThatRaises')    
        self.assertRaises(ValueError, test)
        
00401     def test54(self):
        """Other exception from func test in a loop"""
        def test(self=self):
            self.get('aDict.nestedDict.funcThatRaises')    

        for i in range(10):
            self.assertRaises(ValueError,test)

00409     def test55(self):
        """aDict.nestedDict.aClass in dict lookup"""
        self.check('aDict.nestedDict.aClass')

00413     def test56(self):
        """aDict.nestedDict.aClass in dict lookup in a loop"""
        for i in range(10):
            self.check('aDict.nestedDict.aClass')

00418     def test57(self):
        """aDict.nestedDict.aClass in dict lookup - without autocalling"""
        assert self.get('aDict.nestedDict.aClass', False) == DummyClass

00422     def test58(self):
        """aDict.nestedDict.aClass in dict lookup in a loop - without autocalling"""
        for i in range(10):
            assert self.get('aDict.nestedDict.aClass', False) == DummyClass

00427     def test59(self):
        """Other exception from func test -- but without autocalling shouldn't raise"""

        self.get('aDict.nestedDict.funcThatRaises', False)    
        
00432     def test60(self):
        """Other exception from func test in a loop -- but without autocalling shouldn't raise"""

        for i in range(10):
            self.get('aDict.nestedDict.funcThatRaises', False)    

class VFS(VFN):
    _searchListLength = 1
    
    def searchList(self):
        lng = self._searchListLength
        if lng == 1:
            return [self.namespace()]
        elif lng == 2:
            return [self.namespace(),{'dummy':1234}]
        elif lng == 3:
            # a tuple for kicks
            return ({'dummy':1234}, self.namespace(),{'dummy':1234})
        elif lng == 4:
            # a generator for more kicks
            return self.searchListGenerator()

    def searchListGenerator(self):
        class Test:
            pass
        for i in [Test(),{'dummy':1234}, self.namespace(),{'dummy':1234}]:
            yield i
  
    def get(self, name, autocall=True):
        return self.VFS(self.searchList(), name, autocall)
        
class VFS_2namespaces(VFS):
    _searchListLength = 2
    
class VFS_3namespaces(VFS):
    _searchListLength = 3

class VFS_4namespaces(VFS):
    _searchListLength = 4
    
class VFF(VFN): 
    def get(self, name, autocall=True):
        ns = self._testNamespace
        aStr = ns['aStr'] 
        aFloat = ns['aFloat']
        none = 'some'
        return valueFromFrame(name, autocall)

    def setUp(self):
        """Mod some of the data
        """
        self._testNamespace = ns = self._testNamespace.copy()
        self._results = res = self._results.copy()
        ns['aStr'] = res['aStr'] = 'BLARG'
        ns['aFloat'] = res['aFloat'] = 0.1234
        res['none'] = 'some'
        res['True'] = True
        res['False'] = False
        res['None'] = None
        res['eval'] = eval

    def test_VFF_1(self):
        """Builtins"""
        self.check('True')
        self.check('None')
        self.check('False')
        assert self.get('eval', False)==eval
        assert self.get('range', False)==range

class VFFSL(VFS):
    _searchListLength = 1

    def setUp(self):
        """Mod some of the data
        """
        self._testNamespace = ns = self._testNamespace.copy()
        self._results = res = self._results.copy()
        ns['aStr'] = res['aStr'] = 'BLARG'
        ns['aFloat'] = res['aFloat'] = 0.1234
        res['none'] = 'some'
        
        del ns['anInt'] # will be picked up by globals
        
    def VFFSL(self, searchList, name, autocall=True):
        anInt = 1
        none = 'some'
        return valueFromFrameOrSearchList(searchList, name, autocall)
    
    def get(self, name, autocall=True):
        return self.VFFSL(self.searchList(), name, autocall)

class VFFSL_2(VFFSL):
    _searchListLength = 2

class VFFSL_3(VFFSL):
    _searchListLength = 3

class VFFSL_4(VFFSL):
    _searchListLength = 4

##################################################
## if run from the command line ##
        
if __name__ == '__main__':
    unittest.main()

Generated by  Doxygen 1.6.0   Back to index