[3] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | import Cheetah.NameMapper |
---|
| 4 | import Cheetah.Template |
---|
| 5 | |
---|
| 6 | import sys |
---|
| 7 | import unittest |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | majorVer, minorVer = sys.version_info[0], sys.version_info[1] |
---|
| 11 | versionTuple = (majorVer, minorVer) |
---|
| 12 | |
---|
| 13 | def isPython23(): |
---|
| 14 | ''' Python 2.3 is still supported by Cheetah, but doesn't support decorators ''' |
---|
| 15 | return majorVer == 2 and minorVer < 4 |
---|
| 16 | |
---|
| 17 | class GetAttrException(Exception): |
---|
| 18 | pass |
---|
| 19 | |
---|
| 20 | class CustomGetAttrClass(object): |
---|
| 21 | def __getattr__(self, name): |
---|
| 22 | raise GetAttrException('FAIL, %s' % name) |
---|
| 23 | |
---|
| 24 | class GetAttrTest(unittest.TestCase): |
---|
| 25 | ''' |
---|
| 26 | Test for an issue occurring when __getatttr__() raises an exception |
---|
| 27 | causing NameMapper to raise a NotFound exception |
---|
| 28 | ''' |
---|
| 29 | def test_ValidException(self): |
---|
| 30 | o = CustomGetAttrClass() |
---|
| 31 | try: |
---|
| 32 | print o.attr |
---|
| 33 | except GetAttrException, e: |
---|
| 34 | # expected |
---|
| 35 | return |
---|
| 36 | except: |
---|
| 37 | self.fail('Invalid exception raised: %s' % e) |
---|
| 38 | self.fail('Should have had an exception raised') |
---|
| 39 | |
---|
| 40 | def test_NotFoundException(self): |
---|
| 41 | template = ''' |
---|
| 42 | #def raiseme() |
---|
| 43 | $obj.attr |
---|
| 44 | #end def''' |
---|
| 45 | |
---|
| 46 | template = Cheetah.Template.Template.compile(template, compilerSettings={}, keepRefToGeneratedCode=True) |
---|
| 47 | template = template(searchList=[{'obj' : CustomGetAttrClass()}]) |
---|
| 48 | assert template, 'We should have a valid template object by now' |
---|
| 49 | |
---|
| 50 | self.failUnlessRaises(GetAttrException, template.raiseme) |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | class InlineImportTest(unittest.TestCase): |
---|
| 54 | def test_FromFooImportThing(self): |
---|
| 55 | ''' |
---|
| 56 | Verify that a bug introduced in v2.1.0 where an inline: |
---|
| 57 | #from module import class |
---|
| 58 | would result in the following code being generated: |
---|
| 59 | import class |
---|
| 60 | ''' |
---|
| 61 | template = ''' |
---|
| 62 | #def myfunction() |
---|
| 63 | #if True |
---|
| 64 | #from os import path |
---|
| 65 | #return 17 |
---|
| 66 | Hello! |
---|
| 67 | #end if |
---|
| 68 | #end def |
---|
| 69 | ''' |
---|
| 70 | template = Cheetah.Template.Template.compile(template, compilerSettings={'useLegacyImportMode' : False}, keepRefToGeneratedCode=True) |
---|
| 71 | template = template(searchList=[{}]) |
---|
| 72 | |
---|
| 73 | assert template, 'We should have a valid template object by now' |
---|
| 74 | |
---|
| 75 | rc = template.myfunction() |
---|
| 76 | assert rc == 17, (template, 'Didn\'t get a proper return value') |
---|
| 77 | |
---|
| 78 | def test_ImportFailModule(self): |
---|
| 79 | template = ''' |
---|
| 80 | #try |
---|
| 81 | #import invalidmodule |
---|
| 82 | #except |
---|
| 83 | #set invalidmodule = dict(FOO='BAR!') |
---|
| 84 | #end try |
---|
| 85 | |
---|
| 86 | $invalidmodule.FOO |
---|
| 87 | ''' |
---|
| 88 | template = Cheetah.Template.Template.compile(template, compilerSettings={'useLegacyImportMode' : False}, keepRefToGeneratedCode=True) |
---|
| 89 | template = template(searchList=[{}]) |
---|
| 90 | |
---|
| 91 | assert template, 'We should have a valid template object by now' |
---|
| 92 | assert str(template), 'We weren\'t able to properly generate the result from the template' |
---|
| 93 | |
---|
| 94 | def test_ProperImportOfBadModule(self): |
---|
| 95 | template = ''' |
---|
| 96 | #from invalid import fail |
---|
| 97 | |
---|
| 98 | This should totally $fail |
---|
| 99 | ''' |
---|
| 100 | self.failUnlessRaises(ImportError, Cheetah.Template.Template.compile, template, compilerSettings={'useLegacyImportMode' : False}, keepRefToGeneratedCode=True) |
---|
| 101 | |
---|
| 102 | def test_AutoImporting(self): |
---|
| 103 | template = ''' |
---|
| 104 | #extends FakeyTemplate |
---|
| 105 | |
---|
| 106 | Boo! |
---|
| 107 | ''' |
---|
| 108 | self.failUnlessRaises(ImportError, Cheetah.Template.Template.compile, template) |
---|
| 109 | |
---|
| 110 | def test_StuffBeforeImport_Legacy(self): |
---|
| 111 | template = ''' |
---|
| 112 | ### |
---|
| 113 | ### I like comments before import |
---|
| 114 | ### |
---|
| 115 | #extends Foo |
---|
| 116 | Bar |
---|
| 117 | ''' |
---|
| 118 | self.failUnlessRaises(ImportError, Cheetah.Template.Template.compile, template, compilerSettings={'useLegacyImportMode' : True}, keepRefToGeneratedCode=True) |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | class Mantis_Issue_11_Regression_Test(unittest.TestCase): |
---|
| 122 | ''' |
---|
| 123 | Test case for bug outlined in Mantis issue #11: |
---|
| 124 | |
---|
| 125 | Output: |
---|
| 126 | Traceback (most recent call last): |
---|
| 127 | File "test.py", line 12, in <module> |
---|
| 128 | t.respond() |
---|
| 129 | File "DynamicallyCompiledCheetahTemplate.py", line 86, in respond |
---|
| 130 | File "/usr/lib64/python2.6/cgi.py", line 1035, in escape |
---|
| 131 | s = s.replace("&", "&") # Must be done first! |
---|
| 132 | ''' |
---|
| 133 | def test_FailingBehavior(self): |
---|
| 134 | import cgi |
---|
| 135 | template = Cheetah.Template.Template("$escape($request)", searchList=[{'escape' : cgi.escape, 'request' : 'foobar'}]) |
---|
| 136 | assert template |
---|
| 137 | self.failUnlessRaises(AttributeError, template.respond) |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | def test_FailingBehaviorWithSetting(self): |
---|
| 141 | import cgi |
---|
| 142 | template = Cheetah.Template.Template("$escape($request)", |
---|
| 143 | searchList=[{'escape' : cgi.escape, 'request' : 'foobar'}], |
---|
| 144 | compilerSettings={'prioritizeSearchListOverSelf' : True}) |
---|
| 145 | assert template |
---|
| 146 | assert template.respond() |
---|
| 147 | |
---|
| 148 | class Mantis_Issue_21_Regression_Test(unittest.TestCase): |
---|
| 149 | ''' |
---|
| 150 | Test case for bug outlined in issue #21 |
---|
| 151 | |
---|
| 152 | Effectively @staticmethod and @classmethod |
---|
| 153 | decorated methods in templates don't |
---|
| 154 | properly define the _filter local, which breaks |
---|
| 155 | when using the NameMapper |
---|
| 156 | ''' |
---|
| 157 | def runTest(self): |
---|
| 158 | if isPython23(): |
---|
| 159 | return |
---|
| 160 | template = ''' |
---|
| 161 | #@staticmethod |
---|
| 162 | #def testMethod() |
---|
| 163 | This is my $output |
---|
| 164 | #end def |
---|
| 165 | ''' |
---|
| 166 | template = Cheetah.Template.Template.compile(template) |
---|
| 167 | assert template |
---|
| 168 | assert template.testMethod(output='bug') # raises a NameError: global name '_filter' is not defined |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | class Mantis_Issue_22_Regression_Test(unittest.TestCase): |
---|
| 172 | ''' |
---|
| 173 | Test case for bug outlined in issue #22 |
---|
| 174 | |
---|
| 175 | When using @staticmethod and @classmethod |
---|
| 176 | in conjunction with the #filter directive |
---|
| 177 | the generated code for the #filter is reliant |
---|
| 178 | on the `self` local, breaking the function |
---|
| 179 | ''' |
---|
| 180 | def test_NoneFilter(self): |
---|
| 181 | # XXX: Disabling this test for now |
---|
| 182 | return |
---|
| 183 | if isPython23(): |
---|
| 184 | return |
---|
| 185 | template = ''' |
---|
| 186 | #@staticmethod |
---|
| 187 | #def testMethod() |
---|
| 188 | #filter None |
---|
| 189 | This is my $output |
---|
| 190 | #end filter |
---|
| 191 | #end def |
---|
| 192 | ''' |
---|
| 193 | template = Cheetah.Template.Template.compile(template) |
---|
| 194 | assert template |
---|
| 195 | assert template.testMethod(output='bug') |
---|
| 196 | |
---|
| 197 | def test_DefinedFilter(self): |
---|
| 198 | # XXX: Disabling this test for now |
---|
| 199 | return |
---|
| 200 | if isPython23(): |
---|
| 201 | return |
---|
| 202 | template = ''' |
---|
| 203 | #@staticmethod |
---|
| 204 | #def testMethod() |
---|
| 205 | #filter Filter |
---|
| 206 | This is my $output |
---|
| 207 | #end filter |
---|
| 208 | #end def |
---|
| 209 | ''' |
---|
| 210 | # The generated code for the template's testMethod() should look something |
---|
| 211 | # like this in the 'error' case: |
---|
| 212 | ''' |
---|
| 213 | @staticmethod |
---|
| 214 | def testMethod(**KWS): |
---|
| 215 | ## CHEETAH: generated from #def testMethod() at line 3, col 13. |
---|
| 216 | trans = DummyTransaction() |
---|
| 217 | _dummyTrans = True |
---|
| 218 | write = trans.response().write |
---|
| 219 | SL = [KWS] |
---|
| 220 | _filter = lambda x, **kwargs: unicode(x) |
---|
| 221 | |
---|
| 222 | ######################################## |
---|
| 223 | ## START - generated method body |
---|
| 224 | |
---|
| 225 | _orig_filter_18517345 = _filter |
---|
| 226 | filterName = u'Filter' |
---|
| 227 | if self._CHEETAH__filters.has_key("Filter"): |
---|
| 228 | _filter = self._CHEETAH__currentFilter = self._CHEETAH__filters[filterName] |
---|
| 229 | else: |
---|
| 230 | _filter = self._CHEETAH__currentFilter = \ |
---|
| 231 | self._CHEETAH__filters[filterName] = getattr(self._CHEETAH__filtersLib, filterName)(self).filter |
---|
| 232 | write(u' This is my ') |
---|
| 233 | _v = VFFSL(SL,"output",True) # u'$output' on line 5, col 32 |
---|
| 234 | if _v is not None: write(_filter(_v, rawExpr=u'$output')) # from line 5, col 32. |
---|
| 235 | |
---|
| 236 | ######################################## |
---|
| 237 | ## END - generated method body |
---|
| 238 | |
---|
| 239 | return _dummyTrans and trans.response().getvalue() or "" |
---|
| 240 | ''' |
---|
| 241 | template = Cheetah.Template.Template.compile(template) |
---|
| 242 | assert template |
---|
| 243 | assert template.testMethod(output='bug') |
---|
| 244 | |
---|
| 245 | |
---|
| 246 | if __name__ == '__main__': |
---|
| 247 | unittest.main() |
---|