Debian Codegraph @

Cheetah/Tests/Regressions.py

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