示例#1
0
    def runTest(self):
        t = Template.compile(source="""Main file with |$v|

        $other""")

        otherT = Template.compile(source="Other template with |$v|")
        other = otherT()
        t.other = other

        t.v = u'Unicode String with eacute é'
        t.other.v = u'Unicode String and an eacute é'

        assert unicode(t())
示例#2
0
文件: Unicode.py 项目: Tendrid/pyrt
    def runTest(self):
        t = Template.compile(source="""Main file with |$v|

        $other""")

        otherT = Template.compile(source="Other template with |$v|")
        other = otherT()
        t.other = other

        t.v = u'Unicode String with eacute é'
        t.other.v = u'Unicode String and an eacute é'

        assert unicode(t())
示例#3
0
文件: Template.py 项目: Tendrid/pyrt
    def test_moduleNameArg(self):
        klass = Template.compile(source='$foo', moduleName='foo99')
        mod = sys.modules['foo99']
        assert klass.__name__=='foo99'
        t = klass(namespaces={'foo':1234})
        assert str(t)=='1234'


        klass = Template.compile(source='$foo',
                                 moduleName='foo1',
                                 className='foo2')
        mod = sys.modules['foo1']
        assert klass.__name__=='foo2'
        t = klass(namespaces={'foo':1234})
        assert str(t)=='1234'
示例#4
0
文件: Template.py 项目: Tendrid/pyrt
    def test_moduleGlobalsArg(self):
        klass = Template.compile(source='$foo',
                                 moduleGlobals={'foo':1234})
        t = klass()
        assert str(t)=='1234'

        klass2 = Template.compile(source='$foo', baseclass='Test1',
                                  moduleGlobals={'Test1':dict})
        t = klass2({'foo':1234})
        assert str(t)=='1234'

        klass3 = Template.compile(source='$foo', baseclass='Test1',
                                  moduleGlobals={'Test1':dict, 'foo':1234})
        t = klass3()
        assert str(t)=='1234'
示例#5
0
文件: Template.py 项目: Tendrid/pyrt
    def test_baseclassArg(self):
        klass = Template.compile(source='$foo', baseclass=dict)
        t = klass({'foo':1234})
        assert str(t)=='1234'

        klass2 = Template.compile(source='$foo', baseclass=klass)
        t = klass2({'foo':1234})
        assert str(t)=='1234'

        klass3 = Template.compile(source='#implements dummy\n$bar', baseclass=klass2)
        t = klass3({'foo':1234})
        assert str(t)=='1234'

        klass4 = Template.compile(source='$foo', baseclass='dict')
        t = klass4({'foo':1234})
        assert str(t)=='1234'
示例#6
0
    def runTest(self):
        t = Template.compile(source="""#encoding utf-8
        Main file with |$v| and eacute in the template é""")

        t.v = u'Unicode String'

        assert unicode(t())
示例#7
0
文件: Template.py 项目: Tendrid/pyrt
    def test_normalizePreprocessorArgVariants(self):
        src='%set foo = 12\n%%comment\n$(@foo*10)'

        class Settings1: tokens = '@ %' 
        Settings1 = Settings1()
            
        from modules.Cheetah.Template import TemplatePreprocessor
        settings = Template._normalizePreprocessorSettings(Settings1)
        preprocObj = TemplatePreprocessor(settings)

        def preprocFunc(source, file):
            return '$(12*10)', None

        class TemplateSubclass(Template):
            pass

        compilerSettings = {'cheetahVarStartToken': '@',
                            'directiveStartToken': '%',
                            'commentStartToken': '%%',
                            }
        
        for arg in ['@ %',
                    {'tokens':'@ %'},
                    {'compilerSettings':compilerSettings},
                    {'compilerSettings':compilerSettings,
                     'templateInitArgs':{}},
                    {'tokens':'@ %',
                     'templateAPIClass':TemplateSubclass},
                    Settings1,
                    preprocObj,
                    preprocFunc,                    
                    ]:
            
            klass = Template.compile(src, preprocessors=arg)
            assert str(klass())=='120'
示例#8
0
文件: Unicode.py 项目: Tendrid/pyrt
    def runTest(self):
        t = Template.compile(source="""#encoding utf-8
        Main file with |$v| and eacute in the template é""")

        t.v = u'Unicode String'

        assert unicode(t())
示例#9
0
    def runTest(self):
        source = """#encoding utf-8
        #set $someUnicodeString = u"Bébé"
        Main file with |$v| and eacute in the template é"""
        t = Template.compile(source=source)

        t.v = u'Unicode String'

        assert unicode(t())
示例#10
0
文件: Unicode.py 项目: Tendrid/pyrt
    def runTest(self):
        source = """#encoding utf-8
        #set $someUnicodeString = u"Bébé"
        Main file with |$v| and eacute in the template é"""
        t = Template.compile(source=source)

        t.v = u'Unicode String'

        assert unicode(t())
示例#11
0
文件: Template.py 项目: Tendrid/pyrt
 def test_i18n(self):
     src='''\
     %i18n: This is a $string that needs translation
     %i18n id="foo", domain="root": This is a $string that needs translation
     '''
     src = '\n'.join([ln.strip() for ln in src.splitlines()])
     klass = Template.compile(src, preprocessors='@ %', baseclass=dict)
     t = klass({'string':'bit of text'})
     #print str(t), repr(str(t))
     assert str(t)==('This is a bit of text that needs translation\n'*2)[:-1]
示例#12
0
文件: Template.py 项目: Tendrid/pyrt
 def test_basicUsage1(self):
     src='''\
     %set foo = @a
     $(@foo*10)
     @a'''
     src = '\n'.join([ln.strip() for ln in src.splitlines()])
     preprocessors = {'tokens':'@ %',
                      'namespaces':{'a':99}
                      }
     klass = Template.compile(src, preprocessors=preprocessors)
     assert str(klass())=='990\n99'
示例#13
0
文件: Template.py 项目: Tendrid/pyrt
    def test_mainMethodNameArg(self):
        klass = Template.compile(source='$foo',
                                 className='foo123',
                                 mainMethodName='testMeth')
        assert klass.__name__=='foo123'
        t = klass(namespaces={'foo':1234})
        #print t.generatedClassCode()
        assert str(t)=='1234'
        assert t.testMeth()=='1234'

        klass = Template.compile(source='$foo',
                                 moduleName='fooXXX',                                 
                                 className='foo123',
                                 mainMethodName='testMeth',
                                 baseclass=dict)
        assert klass.__name__=='foo123'
        t = klass({'foo':1234})
        #print t.generatedClassCode()
        assert str(t)=='1234'
        assert t.testMeth()=='1234'
示例#14
0
文件: Template.py 项目: Tendrid/pyrt
    def test_basicUsage(self):
        klass = Template.compile(source='$foo', baseclass=dict)
        t = klass({'foo':1234})
        assert str(t)=='1234'

        klass2 = klass.subclass(source='$foo')
        t = klass2({'foo':1234})
        assert str(t)=='1234'

        klass3 = klass2.subclass(source='#implements dummy\n$bar')
        t = klass3({'foo':1234})
        assert str(t)=='1234'
示例#15
0
文件: Template.py 项目: Tendrid/pyrt
 def runTest(self):
     template = '''
         #extends Template, Useless
         #def foo()
             #return [4,5] + $boink()
         #end def
     '''
     template = Template.compile(template,
             moduleGlobals={'Useless' : Useless},
             compilerSettings={'autoImportForExtendsDirective' : False})
     template = template()
     result = template.foo()
     assert result == [4, 5, 1, 2, 3], (result, 'Unexpected result')
示例#16
0
文件: Template.py 项目: Tendrid/pyrt
    def test_compilationCache(self):
        klass = Template.compile(source='$foo',
                                 className='unique111',
                                 cacheCompilationResults=False)
        t = klass(namespaces={'foo':1234})
        assert str(t)=='1234'
        assert not klass._CHEETAH_isInCompilationCache


        # this time it will place it in the cache
        klass = Template.compile(source='$foo',
                                 className='unique111',
                                 cacheCompilationResults=True)
        t = klass(namespaces={'foo':1234})
        assert str(t)=='1234'
        assert klass._CHEETAH_isInCompilationCache

        # by default it will be in the cache
        klass = Template.compile(source='$foo',
                                 className='unique999099')
        t = klass(namespaces={'foo':1234})
        assert str(t)=='1234'
        assert klass._CHEETAH_isInCompilationCache
示例#17
0
文件: Template.py 项目: Tendrid/pyrt
    def test_keepRefToGeneratedCodeArg(self):
        klass = Template.compile(source='$foo',
                                 className='unique58',
                                 cacheCompilationResults=False,
                                 keepRefToGeneratedCode=False)
        t = klass(namespaces={'foo':1234})
        assert str(t)=='1234'
        assert not t.generatedModuleCode()


        klass2 = Template.compile(source='$foo',
                                 className='unique58',
                                 keepRefToGeneratedCode=True)
        t = klass2(namespaces={'foo':1234})
        assert str(t)=='1234'
        assert t.generatedModuleCode()

        klass3 = Template.compile(source='$foo',
                                 className='unique58',
                                 keepRefToGeneratedCode=False)
        t = klass3(namespaces={'foo':1234})
        assert str(t)=='1234'
        # still there as this class came from the cache
        assert t.generatedModuleCode() 
示例#18
0
文件: Template.py 项目: Tendrid/pyrt
 def test_FailCase(self):
     ''' Test situation where an inline #import statement will get relocated '''
     source = '''
         #def myFunction()
             Ahoy!
             #try
                 #import sys
             #except ImportError
                 $print "This will never happen!"
             #end try
         #end def
         '''
     # This should raise an IndentationError (if the bug exists)
     klass = Template.compile(source=source, compilerSettings={'useLegacyImportMode' : False})
     t = klass(namespaces={'foo' : 1234})
示例#19
0
文件: Template.py 项目: Tendrid/pyrt
 def test_moduleFileCaching(self):
     if versionTuple < (2, 3):
         return
     tmpDir = tempfile.mkdtemp()
     try:
         #print tmpDir
         assert os.path.exists(tmpDir)
         klass = Template.compile(source='$foo',
                                  cacheModuleFilesForTracebacks=True,
                                  cacheDirForModuleFiles=tmpDir)
         mod = sys.modules[klass.__module__]
         #print mod.__file__
         assert os.path.exists(mod.__file__)
         assert os.path.dirname(mod.__file__)==tmpDir
     finally:
         shutil.rmtree(tmpDir, True)
示例#20
0
文件: Template.py 项目: Tendrid/pyrt
 def test_BasicDecorator(self):
     if sys.version_info[0] == 2 and sys.version_info[1] == 3:
             print('This version of Python doesn\'t support decorators, skipping tests')
             return
     template = '''
         #@staticmethod
         #def myStaticMethod()
             #return '$foo = %s' % $foo
         #end def
     '''
     template = Template.compile(source=template)
     try:
         rc = template.myStaticMethod(foo='bar')
         assert rc == '$foo = bar', (rc, 'Template class method didn\'t return what I expected')
     except AttributeError, ex:
         self.fail(ex)
示例#21
0
文件: Template.py 项目: Tendrid/pyrt
    def test_complexUsage(self):
        src='''\
        %set foo = @a
        %def func1: #def func(arg): $arg("***")
        %% comment
        $(@foo*10)
        @func1
        $func(lambda x:c"--$x--@a")'''
        src = '\n'.join([ln.strip() for ln in src.splitlines()])

        
        for arg in [{'tokens':'@ %', 'namespaces':{'a':99} },
                    {'tokens':'@ %', 'namespaces':{'a':99} },
                    ]:
            klass = Template.compile(src, preprocessors=arg)
            t = klass()
            assert str(t)=='990\n--***--99'
示例#22
0
文件: Template.py 项目: Tendrid/pyrt
 def test_classNameArg(self):
     klass = Template.compile(source='$foo', className='foo123')
     assert klass.__name__=='foo123'
     t = klass(namespaces={'foo':1234})
     assert str(t)=='1234'
示例#23
0
文件: Template.py 项目: Tendrid/pyrt
 def test_basicUsage(self):
     klass = Template.compile(source='$foo')
     t = klass(namespaces={'foo':1234})
     assert str(t)=='1234'