1 | #!/usr/bin/env python |
---|
2 | # -*- coding: latin-1 -*- |
---|
3 | |
---|
4 | ''' |
---|
5 | Syntax and Output tests. |
---|
6 | |
---|
7 | TODO |
---|
8 | - #finally |
---|
9 | - #filter |
---|
10 | - #errorCatcher |
---|
11 | - #echo |
---|
12 | - #silent |
---|
13 | ''' |
---|
14 | |
---|
15 | |
---|
16 | ################################################## |
---|
17 | ## DEPENDENCIES ## |
---|
18 | |
---|
19 | import sys |
---|
20 | import types |
---|
21 | import re |
---|
22 | from copy import deepcopy |
---|
23 | import os |
---|
24 | import os.path |
---|
25 | import new |
---|
26 | import warnings |
---|
27 | import unittest |
---|
28 | |
---|
29 | from Cheetah.NameMapper import NotFound |
---|
30 | from Cheetah.NameMapper import C_VERSION as NameMapper_C_VERSION |
---|
31 | from Cheetah.Template import Template |
---|
32 | from Cheetah.Parser import ParseError |
---|
33 | from Cheetah.Compiler import Compiler, DEFAULT_COMPILER_SETTINGS |
---|
34 | |
---|
35 | class Unspecified(object): |
---|
36 | pass |
---|
37 | |
---|
38 | majorVer, minorVer = sys.version_info[0], sys.version_info[1] |
---|
39 | versionTuple = (majorVer, minorVer) |
---|
40 | |
---|
41 | def testdecorator(func): |
---|
42 | return func |
---|
43 | |
---|
44 | class DummyClass: |
---|
45 | _called = False |
---|
46 | def __str__(self): |
---|
47 | return 'object' |
---|
48 | |
---|
49 | def meth(self, arg="arff"): |
---|
50 | return str(arg) |
---|
51 | |
---|
52 | def meth1(self, arg="doo"): |
---|
53 | return arg |
---|
54 | |
---|
55 | def meth2(self, arg1="a1", arg2="a2"): |
---|
56 | return str(arg1) + str(arg2) |
---|
57 | |
---|
58 | def methWithPercentSignDefaultArg(self, arg1="110%"): |
---|
59 | return str(arg1) |
---|
60 | |
---|
61 | def callIt(self, arg=1234): |
---|
62 | self._called = True |
---|
63 | self._callArg = arg |
---|
64 | |
---|
65 | |
---|
66 | def dummyFunc(arg="Scooby"): |
---|
67 | return arg |
---|
68 | |
---|
69 | defaultTestNameSpace = { |
---|
70 | 'aStr':'blarg', |
---|
71 | 'anInt':1, |
---|
72 | 'aFloat':1.5, |
---|
73 | 'aList': ['item0','item1','item2'], |
---|
74 | 'aDict': {'one':'item1', |
---|
75 | 'two':'item2', |
---|
76 | 'nestedDict':{1:'nestedItem1', |
---|
77 | 'two':'nestedItem2' |
---|
78 | }, |
---|
79 | 'nestedFunc':dummyFunc, |
---|
80 | }, |
---|
81 | 'aFunc': dummyFunc, |
---|
82 | 'anObj': DummyClass(), |
---|
83 | 'aMeth': DummyClass().meth1, |
---|
84 | 'aStrToBeIncluded': "$aStr $anInt", |
---|
85 | 'none' : None, |
---|
86 | 'emptyString':'', |
---|
87 | 'numOne':1, |
---|
88 | 'numTwo':2, |
---|
89 | 'zero':0, |
---|
90 | 'tenDigits': 1234567890, |
---|
91 | 'webSafeTest': 'abc <=> &', |
---|
92 | 'strip1': ' \t strippable whitespace \t\t \n', |
---|
93 | 'strip2': ' \t strippable whitespace \t\t ', |
---|
94 | 'strip3': ' \t strippable whitespace \t\t\n1 2 3\n', |
---|
95 | |
---|
96 | 'blockToBeParsed':"""$numOne $numTwo""", |
---|
97 | 'includeBlock2':"""$numOne $numTwo $aSetVar""", |
---|
98 | |
---|
99 | 'includeFileName':'parseTest.txt', |
---|
100 | 'listOfLambdas':[lambda x: x, lambda x: x, lambda x: x,], |
---|
101 | 'list': [ |
---|
102 | {'index': 0, 'numOne': 1, 'numTwo': 2}, |
---|
103 | {'index': 1, 'numOne': 1, 'numTwo': 2}, |
---|
104 | ], |
---|
105 | 'nameList': [('john', 'doe'), ('jane', 'smith')], |
---|
106 | 'letterList': ['a', 'b', 'c'], |
---|
107 | '_': lambda x: 'Translated: ' + x, |
---|
108 | 'unicodeData':u'aoeu12345\u1234', |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | ################################################## |
---|
113 | ## TEST BASE CLASSES |
---|
114 | |
---|
115 | class OutputTest(unittest.TestCase): |
---|
116 | report = ''' |
---|
117 | Template output mismatch: |
---|
118 | |
---|
119 | Input Template = |
---|
120 | %(template)s%(end)s |
---|
121 | |
---|
122 | Expected Output = |
---|
123 | %(expected)s%(end)s |
---|
124 | |
---|
125 | Actual Output = |
---|
126 | %(actual)s%(end)s''' |
---|
127 | |
---|
128 | convertEOLs = True |
---|
129 | _EOLreplacement = None |
---|
130 | _debugEOLReplacement = False |
---|
131 | |
---|
132 | DEBUGLEV = 0 |
---|
133 | _searchList = [defaultTestNameSpace] |
---|
134 | |
---|
135 | _useNewStyleCompilation = True |
---|
136 | #_useNewStyleCompilation = False |
---|
137 | |
---|
138 | _extraCompileKwArgs = None |
---|
139 | |
---|
140 | def searchList(self): |
---|
141 | return self._searchList |
---|
142 | |
---|
143 | def verify(self, input, expectedOutput, |
---|
144 | inputEncoding=None, |
---|
145 | outputEncoding=None, |
---|
146 | convertEOLs=Unspecified): |
---|
147 | if self._EOLreplacement: |
---|
148 | if convertEOLs is Unspecified: |
---|
149 | convertEOLs = self.convertEOLs |
---|
150 | if convertEOLs: |
---|
151 | input = input.replace('\n', self._EOLreplacement) |
---|
152 | expectedOutput = expectedOutput.replace('\n', self._EOLreplacement) |
---|
153 | |
---|
154 | self._input = input |
---|
155 | if self._useNewStyleCompilation: |
---|
156 | extraKwArgs = self._extraCompileKwArgs or {} |
---|
157 | |
---|
158 | templateClass = Template.compile( |
---|
159 | source=input, |
---|
160 | compilerSettings=self._getCompilerSettings(), |
---|
161 | keepRefToGeneratedCode=True, |
---|
162 | **extraKwArgs |
---|
163 | ) |
---|
164 | moduleCode = templateClass._CHEETAH_generatedModuleCode |
---|
165 | self.template = templateObj = templateClass(searchList=self.searchList()) |
---|
166 | else: |
---|
167 | self.template = templateObj = Template( |
---|
168 | input, |
---|
169 | searchList=self.searchList(), |
---|
170 | compilerSettings=self._getCompilerSettings(), |
---|
171 | ) |
---|
172 | moduleCode = templateObj._CHEETAH_generatedModuleCode |
---|
173 | if self.DEBUGLEV >= 1: |
---|
174 | print moduleCode |
---|
175 | try: |
---|
176 | output = templateObj.respond() # rather than __str__, because of unicode |
---|
177 | assert output==expectedOutput, self._outputMismatchReport(output, expectedOutput) |
---|
178 | finally: |
---|
179 | templateObj.shutdown() |
---|
180 | |
---|
181 | def _getCompilerSettings(self): |
---|
182 | return {} |
---|
183 | |
---|
184 | def _outputMismatchReport(self, output, expectedOutput): |
---|
185 | if self._debugEOLReplacement and self._EOLreplacement: |
---|
186 | EOLrepl = self._EOLreplacement |
---|
187 | marker = '*EOL*' |
---|
188 | return self.report % {'template': self._input.replace(EOLrepl,marker), |
---|
189 | 'expected': expectedOutput.replace(EOLrepl,marker), |
---|
190 | 'actual': output.replace(EOLrepl,marker), |
---|
191 | 'end': '(end)'} |
---|
192 | else: |
---|
193 | return self.report % {'template': self._input, |
---|
194 | 'expected': expectedOutput, |
---|
195 | 'actual': output, |
---|
196 | 'end': '(end)'} |
---|
197 | |
---|
198 | def genClassCode(self): |
---|
199 | if hasattr(self, 'template'): |
---|
200 | return self.template.generatedClassCode() |
---|
201 | |
---|
202 | def genModuleCode(self): |
---|
203 | if hasattr(self, 'template'): |
---|
204 | return self.template.generatedModuleCode() |
---|
205 | |
---|
206 | ################################################## |
---|
207 | ## TEST CASE CLASSES |
---|
208 | |
---|
209 | class EmptyTemplate(OutputTest): |
---|
210 | convertEOLs = False |
---|
211 | def test1(self): |
---|
212 | """an empty string for the template""" |
---|
213 | |
---|
214 | warnings.filterwarnings('error', |
---|
215 | 'You supplied an empty string for the source!', |
---|
216 | UserWarning) |
---|
217 | try: |
---|
218 | self.verify("", "") |
---|
219 | except UserWarning: |
---|
220 | pass |
---|
221 | else: |
---|
222 | self.fail("Should warn about empty source strings.") |
---|
223 | |
---|
224 | try: |
---|
225 | self.verify("#implements foo", "") |
---|
226 | except NotImplementedError: |
---|
227 | pass |
---|
228 | else: |
---|
229 | self.fail("This should barf about respond() not being implemented.") |
---|
230 | |
---|
231 | self.verify("#implements respond", "") |
---|
232 | |
---|
233 | self.verify("#implements respond(foo=1234)", "") |
---|
234 | |
---|
235 | |
---|
236 | class Backslashes(OutputTest): |
---|
237 | convertEOLs = False |
---|
238 | |
---|
239 | def setUp(self): |
---|
240 | fp = open('backslashes.txt','w') |
---|
241 | fp.write(r'\ #LogFormat "%h %l %u %t \"%r\" %>s %b"' + '\n\n\n\n\n\n\n') |
---|
242 | fp.flush() |
---|
243 | fp.close |
---|
244 | |
---|
245 | def tearDown(self): |
---|
246 | if os.path.exists('backslashes.txt'): |
---|
247 | os.remove('backslashes.txt') |
---|
248 | |
---|
249 | def test1(self): |
---|
250 | """ a single \\ using rawstrings""" |
---|
251 | self.verify(r"\ ", |
---|
252 | r"\ ") |
---|
253 | |
---|
254 | def test2(self): |
---|
255 | """ a single \\ using rawstrings and lots of lines""" |
---|
256 | self.verify(r"\ " + "\n\n\n\n\n\n\n\n\n", |
---|
257 | r"\ " + "\n\n\n\n\n\n\n\n\n") |
---|
258 | |
---|
259 | def test3(self): |
---|
260 | """ a single \\ without using rawstrings""" |
---|
261 | self.verify("\ \ ", |
---|
262 | "\ \ ") |
---|
263 | |
---|
264 | def test4(self): |
---|
265 | """ single line from an apache conf file""" |
---|
266 | self.verify(r'#LogFormat "%h %l %u %t \"%r\" %>s %b"', |
---|
267 | r'#LogFormat "%h %l %u %t \"%r\" %>s %b"') |
---|
268 | |
---|
269 | def test5(self): |
---|
270 | """ single line from an apache conf file with many NEWLINES |
---|
271 | |
---|
272 | The NEWLINES are used to make sure that MethodCompiler.commitStrConst() |
---|
273 | is handling long and short strings in the same fashion. It uses |
---|
274 | triple-quotes for strings with lots of \\n in them and repr(theStr) for |
---|
275 | shorter strings with only a few newlines.""" |
---|
276 | |
---|
277 | self.verify(r'#LogFormat "%h %l %u %t \"%r\" %>s %b"' + '\n\n\n\n\n\n\n', |
---|
278 | r'#LogFormat "%h %l %u %t \"%r\" %>s %b"' + '\n\n\n\n\n\n\n') |
---|
279 | |
---|
280 | def test6(self): |
---|
281 | """ test backslash handling in an included file""" |
---|
282 | self.verify(r'#include "backslashes.txt"', |
---|
283 | r'\ #LogFormat "%h %l %u %t \"%r\" %>s %b"' + '\n\n\n\n\n\n\n') |
---|
284 | |
---|
285 | def test7(self): |
---|
286 | """ a single \\ without using rawstrings plus many NEWLINES""" |
---|
287 | self.verify("\ \ " + "\n\n\n\n\n\n\n\n\n", |
---|
288 | "\ \ " + "\n\n\n\n\n\n\n\n\n") |
---|
289 | |
---|
290 | def test8(self): |
---|
291 | """ single line from an apache conf file with single quotes and many NEWLINES |
---|
292 | """ |
---|
293 | |
---|
294 | self.verify(r"""#LogFormat '%h %l %u %t \"%r\" %>s %b'""" + '\n\n\n\n\n\n\n', |
---|
295 | r"""#LogFormat '%h %l %u %t \"%r\" %>s %b'""" + '\n\n\n\n\n\n\n') |
---|
296 | |
---|
297 | class NonTokens(OutputTest): |
---|
298 | def test1(self): |
---|
299 | """dollar signs not in Cheetah $vars""" |
---|
300 | self.verify("$ $$ $5 $. $ test", |
---|
301 | "$ $$ $5 $. $ test") |
---|
302 | |
---|
303 | def test2(self): |
---|
304 | """hash not in #directives""" |
---|
305 | self.verify("# \# #5 ", |
---|
306 | "# # #5 ") |
---|
307 | |
---|
308 | def test3(self): |
---|
309 | """escapted comments""" |
---|
310 | self.verify(" \##escaped comment ", |
---|
311 | " ##escaped comment ") |
---|
312 | |
---|
313 | def test4(self): |
---|
314 | """escapted multi-line comments""" |
---|
315 | self.verify(" \#*escaped comment \n*# ", |
---|
316 | " #*escaped comment \n*# ") |
---|
317 | |
---|
318 | def test5(self): |
---|
319 | """1 dollar sign""" |
---|
320 | self.verify("$", |
---|
321 | "$") |
---|
322 | def _X_test6(self): |
---|
323 | """1 dollar sign followed by hash""" |
---|
324 | self.verify("\n$#\n", |
---|
325 | "\n$#\n") |
---|
326 | |
---|
327 | def test6(self): |
---|
328 | """1 dollar sign followed by EOL Slurp Token""" |
---|
329 | if DEFAULT_COMPILER_SETTINGS['EOLSlurpToken']: |
---|
330 | self.verify("\n$%s\n"%DEFAULT_COMPILER_SETTINGS['EOLSlurpToken'], |
---|
331 | "\n$") |
---|
332 | else: |
---|
333 | self.verify("\n$#\n", |
---|
334 | "\n$#\n") |
---|
335 | |
---|
336 | class Comments_SingleLine(OutputTest): |
---|
337 | def test1(self): |
---|
338 | """## followed by WS""" |
---|
339 | self.verify("## ", |
---|
340 | "") |
---|
341 | |
---|
342 | def test2(self): |
---|
343 | """## followed by NEWLINE""" |
---|
344 | self.verify("##\n", |
---|
345 | "") |
---|
346 | |
---|
347 | def test3(self): |
---|
348 | """## followed by text then NEWLINE""" |
---|
349 | self.verify("## oeuao aoe uaoe \n", |
---|
350 | "") |
---|
351 | def test4(self): |
---|
352 | """## gobbles leading WS""" |
---|
353 | self.verify(" ## oeuao aoe uaoe \n", |
---|
354 | "") |
---|
355 | |
---|
356 | def test5(self): |
---|
357 | """## followed by text then NEWLINE, + leading WS""" |
---|
358 | self.verify(" ## oeuao aoe uaoe \n", |
---|
359 | "") |
---|
360 | |
---|
361 | def test6(self): |
---|
362 | """## followed by EOF""" |
---|
363 | self.verify("##", |
---|
364 | "") |
---|
365 | |
---|
366 | def test7(self): |
---|
367 | """## followed by EOF with leading WS""" |
---|
368 | self.verify(" ##", |
---|
369 | "") |
---|
370 | |
---|
371 | def test8(self): |
---|
372 | """## gobble line |
---|
373 | with text on previous and following lines""" |
---|
374 | self.verify("line1\n ## aoeu 1234 \nline2", |
---|
375 | "line1\nline2") |
---|
376 | |
---|
377 | def test9(self): |
---|
378 | """## don't gobble line |
---|
379 | with text on previous and following lines""" |
---|
380 | self.verify("line1\n 12 ## aoeu 1234 \nline2", |
---|
381 | "line1\n 12 \nline2") |
---|
382 | |
---|
383 | def test10(self): |
---|
384 | """## containing $placeholders |
---|
385 | """ |
---|
386 | self.verify("##$a$b $c($d)", |
---|
387 | "") |
---|
388 | |
---|
389 | def test11(self): |
---|
390 | """## containing #for directive |
---|
391 | """ |
---|
392 | self.verify("##for $i in range(15)", |
---|
393 | "") |
---|
394 | |
---|
395 | |
---|
396 | class Comments_MultiLine_NoGobble(OutputTest): |
---|
397 | """ |
---|
398 | Multiline comments used to not gobble whitespace. They do now, but this can |
---|
399 | be turned off with a compilerSetting |
---|
400 | """ |
---|
401 | |
---|
402 | def _getCompilerSettings(self): |
---|
403 | return {'gobbleWhitespaceAroundMultiLineComments':False} |
---|
404 | |
---|
405 | def test1(self): |
---|
406 | """#* *# followed by WS |
---|
407 | Shouldn't gobble WS |
---|
408 | """ |
---|
409 | self.verify("#* blarg *# ", |
---|
410 | " ") |
---|
411 | |
---|
412 | def test2(self): |
---|
413 | """#* *# preceded and followed by WS |
---|
414 | Shouldn't gobble WS |
---|
415 | """ |
---|
416 | self.verify(" #* blarg *# ", |
---|
417 | " ") |
---|
418 | |
---|
419 | def test3(self): |
---|
420 | """#* *# followed by WS, with NEWLINE |
---|
421 | Shouldn't gobble WS |
---|
422 | """ |
---|
423 | self.verify("#* \nblarg\n *# ", |
---|
424 | " ") |
---|
425 | |
---|
426 | def test4(self): |
---|
427 | """#* *# preceded and followed by WS, with NEWLINE |
---|
428 | Shouldn't gobble WS |
---|
429 | """ |
---|
430 | self.verify(" #* \nblarg\n *# ", |
---|
431 | " ") |
---|
432 | |
---|
433 | class Comments_MultiLine(OutputTest): |
---|
434 | """ |
---|
435 | Note: Multiline comments don't gobble whitespace! |
---|
436 | """ |
---|
437 | |
---|
438 | def test1(self): |
---|
439 | """#* *# followed by WS |
---|
440 | Should gobble WS |
---|
441 | """ |
---|
442 | self.verify("#* blarg *# ", |
---|
443 | "") |
---|
444 | |
---|
445 | def test2(self): |
---|
446 | """#* *# preceded and followed by WS |
---|
447 | Should gobble WS |
---|
448 | """ |
---|
449 | self.verify(" #* blarg *# ", |
---|
450 | "") |
---|
451 | |
---|
452 | def test3(self): |
---|
453 | """#* *# followed by WS, with NEWLINE |
---|
454 | Shouldn't gobble WS |
---|
455 | """ |
---|
456 | self.verify("#* \nblarg\n *# ", |
---|
457 | "") |
---|
458 | |
---|
459 | def test4(self): |
---|
460 | """#* *# preceded and followed by WS, with NEWLINE |
---|
461 | Shouldn't gobble WS |
---|
462 | """ |
---|
463 | self.verify(" #* \nblarg\n *# ", |
---|
464 | "") |
---|
465 | |
---|
466 | def test5(self): |
---|
467 | """#* *# containing nothing |
---|
468 | """ |
---|
469 | self.verify("#**#", |
---|
470 | "") |
---|
471 | |
---|
472 | def test6(self): |
---|
473 | """#* *# containing only NEWLINES |
---|
474 | """ |
---|
475 | self.verify(" #*\n\n\n\n\n\n\n\n*# ", |
---|
476 | "") |
---|
477 | |
---|
478 | def test7(self): |
---|
479 | """#* *# containing $placeholders |
---|
480 | """ |
---|
481 | self.verify("#* $var $var(1234*$c) *#", |
---|
482 | "") |
---|
483 | |
---|
484 | def test8(self): |
---|
485 | """#* *# containing #for directive |
---|
486 | """ |
---|
487 | self.verify("#* #for $i in range(15) *#", |
---|
488 | "") |
---|
489 | |
---|
490 | def test9(self): |
---|
491 | """ text around #* *# containing #for directive |
---|
492 | """ |
---|
493 | self.verify("foo\nfoo bar #* #for $i in range(15) *# foo\n", |
---|
494 | "foo\nfoo bar foo\n") |
---|
495 | |
---|
496 | def test9(self): |
---|
497 | """ text around #* *# containing #for directive and trailing whitespace |
---|
498 | which should be gobbled |
---|
499 | """ |
---|
500 | self.verify("foo\nfoo bar #* #for $i in range(15) *# \ntest", |
---|
501 | "foo\nfoo bar \ntest") |
---|
502 | |
---|
503 | def test10(self): |
---|
504 | """ text around #* *# containing #for directive and newlines: trailing whitespace |
---|
505 | which should be gobbled. |
---|
506 | """ |
---|
507 | self.verify("foo\nfoo bar #* \n\n#for $i in range(15) \n\n*# \ntest", |
---|
508 | "foo\nfoo bar \ntest") |
---|
509 | |
---|
510 | class Placeholders(OutputTest): |
---|
511 | def test1(self): |
---|
512 | """1 placeholder""" |
---|
513 | self.verify("$aStr", "blarg") |
---|
514 | |
---|
515 | def test2(self): |
---|
516 | """2 placeholders""" |
---|
517 | self.verify("$aStr $anInt", "blarg 1") |
---|
518 | |
---|
519 | def test3(self): |
---|
520 | """2 placeholders, back-to-back""" |
---|
521 | self.verify("$aStr$anInt", "blarg1") |
---|
522 | |
---|
523 | def test4(self): |
---|
524 | """1 placeholder enclosed in ()""" |
---|
525 | self.verify("$(aStr)", "blarg") |
---|
526 | |
---|
527 | def test5(self): |
---|
528 | """1 placeholder enclosed in {}""" |
---|
529 | self.verify("${aStr}", "blarg") |
---|
530 | |
---|
531 | def test6(self): |
---|
532 | """1 placeholder enclosed in []""" |
---|
533 | self.verify("$[aStr]", "blarg") |
---|
534 | |
---|
535 | def test7(self): |
---|
536 | """1 placeholder enclosed in () + WS |
---|
537 | |
---|
538 | Test to make sure that $(<WS><identifier>.. matches |
---|
539 | """ |
---|
540 | self.verify("$( aStr )", "blarg") |
---|
541 | |
---|
542 | def test8(self): |
---|
543 | """1 placeholder enclosed in {} + WS""" |
---|
544 | self.verify("${ aStr }", "blarg") |
---|
545 | |
---|
546 | def test9(self): |
---|
547 | """1 placeholder enclosed in [] + WS""" |
---|
548 | self.verify("$[ aStr ]", "blarg") |
---|
549 | |
---|
550 | def test10(self): |
---|
551 | """1 placeholder enclosed in () + WS + * cache |
---|
552 | |
---|
553 | Test to make sure that $*(<WS><identifier>.. matches |
---|
554 | """ |
---|
555 | self.verify("$*( aStr )", "blarg") |
---|
556 | |
---|
557 | def test11(self): |
---|
558 | """1 placeholder enclosed in {} + WS + *cache""" |
---|
559 | self.verify("$*{ aStr }", "blarg") |
---|
560 | |
---|
561 | def test12(self): |
---|
562 | """1 placeholder enclosed in [] + WS + *cache""" |
---|
563 | self.verify("$*[ aStr ]", "blarg") |
---|
564 | |
---|
565 | def test13(self): |
---|
566 | """1 placeholder enclosed in {} + WS + *<int>*cache""" |
---|
567 | self.verify("$*5*{ aStr }", "blarg") |
---|
568 | |
---|
569 | def test14(self): |
---|
570 | """1 placeholder enclosed in [] + WS + *<int>*cache""" |
---|
571 | self.verify("$*5*[ aStr ]", "blarg") |
---|
572 | |
---|
573 | def test15(self): |
---|
574 | """1 placeholder enclosed in {} + WS + *<float>*cache""" |
---|
575 | self.verify("$*0.5d*{ aStr }", "blarg") |
---|
576 | |
---|
577 | def test16(self): |
---|
578 | """1 placeholder enclosed in [] + WS + *<float>*cache""" |
---|
579 | self.verify("$*.5*[ aStr ]", "blarg") |
---|
580 | |
---|
581 | def test17(self): |
---|
582 | """1 placeholder + *<int>*cache""" |
---|
583 | self.verify("$*5*aStr", "blarg") |
---|
584 | |
---|
585 | def test18(self): |
---|
586 | """1 placeholder *<float>*cache""" |
---|
587 | self.verify("$*0.5h*aStr", "blarg") |
---|
588 | |
---|
589 | def test19(self): |
---|
590 | """1 placeholder surrounded by single quotes and multiple newlines""" |
---|
591 | self.verify("""'\n\n\n\n'$aStr'\n\n\n\n'""", |
---|
592 | """'\n\n\n\n'blarg'\n\n\n\n'""") |
---|
593 | |
---|
594 | def test20(self): |
---|
595 | """silent mode $!placeholders """ |
---|
596 | self.verify("$!aStr$!nonExistant$!*nonExistant$!{nonExistant}", "blarg") |
---|
597 | |
---|
598 | try: |
---|
599 | self.verify("$!aStr$nonExistant", |
---|
600 | "blarg") |
---|
601 | except NotFound: |
---|
602 | pass |
---|
603 | else: |
---|
604 | self.fail('should raise NotFound exception') |
---|
605 | |
---|
606 | def test21(self): |
---|
607 | """Make sure that $*caching is actually working""" |
---|
608 | namesStr = 'You Me Them Everyone' |
---|
609 | names = namesStr.split() |
---|
610 | |
---|
611 | tmpl = Template.compile('#for name in $names: $name ', baseclass=dict) |
---|
612 | assert str(tmpl({'names':names})).strip()==namesStr |
---|
613 | |
---|
614 | tmpl = tmpl.subclass('#for name in $names: $*name ') |
---|
615 | assert str(tmpl({'names':names}))=='You '*len(names) |
---|
616 | |
---|
617 | tmpl = tmpl.subclass('#for name in $names: $*1*name ') |
---|
618 | assert str(tmpl({'names':names}))=='You '*len(names) |
---|
619 | |
---|
620 | tmpl = tmpl.subclass('#for name in $names: $*1*(name) ') |
---|
621 | assert str(tmpl({'names':names}))=='You '*len(names) |
---|
622 | |
---|
623 | if versionTuple > (2,2): |
---|
624 | tmpl = tmpl.subclass('#for name in $names: $*1*(name) ') |
---|
625 | assert str(tmpl(names=names))=='You '*len(names) |
---|
626 | |
---|
627 | class Placeholders_Vals(OutputTest): |
---|
628 | convertEOLs = False |
---|
629 | def test1(self): |
---|
630 | """string""" |
---|
631 | self.verify("$aStr", "blarg") |
---|
632 | |
---|
633 | def test2(self): |
---|
634 | """string - with whitespace""" |
---|
635 | self.verify(" $aStr ", " blarg ") |
---|
636 | |
---|
637 | def test3(self): |
---|
638 | """empty string - with whitespace""" |
---|
639 | self.verify("$emptyString", "") |
---|
640 | |
---|
641 | def test4(self): |
---|
642 | """int""" |
---|
643 | self.verify("$anInt", "1") |
---|
644 | |
---|
645 | def test5(self): |
---|
646 | """float""" |
---|
647 | self.verify("$aFloat", "1.5") |
---|
648 | |
---|
649 | def test6(self): |
---|
650 | """list""" |
---|
651 | self.verify("$aList", "['item0', 'item1', 'item2']") |
---|
652 | |
---|
653 | def test7(self): |
---|
654 | """None |
---|
655 | |
---|
656 | The default output filter is ReplaceNone. |
---|
657 | """ |
---|
658 | self.verify("$none", "") |
---|
659 | |
---|
660 | def test8(self): |
---|
661 | """True, False |
---|
662 | """ |
---|
663 | self.verify("$True $False", "%s %s"%(repr(True), repr(False))) |
---|
664 | |
---|
665 | def test9(self): |
---|
666 | """$_ |
---|
667 | """ |
---|
668 | self.verify("$_('foo')", "Translated: foo") |
---|
669 | |
---|
670 | class PlaceholderStrings(OutputTest): |
---|
671 | def test1(self): |
---|
672 | """some c'text $placeholder text' strings""" |
---|
673 | self.verify("$str(c'$aStr')", "blarg") |
---|
674 | |
---|
675 | def test2(self): |
---|
676 | """some c'text $placeholder text' strings""" |
---|
677 | self.verify("$str(c'$aStr.upper')", "BLARG") |
---|
678 | |
---|
679 | def test3(self): |
---|
680 | """some c'text $placeholder text' strings""" |
---|
681 | self.verify("$str(c'$(aStr.upper.replace(c\"A$str()\",\"\"))')", "BLRG") |
---|
682 | |
---|
683 | def test4(self): |
---|
684 | """some c'text $placeholder text' strings""" |
---|
685 | self.verify("#echo $str(c'$(aStr.upper)')", "BLARG") |
---|
686 | |
---|
687 | def test5(self): |
---|
688 | """some c'text $placeholder text' strings""" |
---|
689 | self.verify("#if 1 then $str(c'$(aStr.upper)') else 0", "BLARG") |
---|
690 | |
---|
691 | def test6(self): |
---|
692 | """some c'text $placeholder text' strings""" |
---|
693 | self.verify("#if 1\n$str(c'$(aStr.upper)')#slurp\n#else\n0#end if", "BLARG") |
---|
694 | |
---|
695 | def test7(self): |
---|
696 | """some c'text $placeholder text' strings""" |
---|
697 | self.verify("#def foo(arg=c'$(\"BLARG\")')\n" |
---|
698 | "$arg#slurp\n" |
---|
699 | "#end def\n" |
---|
700 | "$foo()$foo(c'$anInt')#slurp", |
---|
701 | |
---|
702 | "BLARG1") |
---|
703 | |
---|
704 | |
---|
705 | |
---|
706 | class UnicodeStrings(OutputTest): |
---|
707 | def test1(self): |
---|
708 | """unicode data in placeholder |
---|
709 | """ |
---|
710 | #self.verify(u"$unicodeData", defaultTestNameSpace['unicodeData'], outputEncoding='utf8') |
---|
711 | self.verify(u"$unicodeData", defaultTestNameSpace['unicodeData']) |
---|
712 | |
---|
713 | def test2(self): |
---|
714 | """unicode data in body |
---|
715 | """ |
---|
716 | self.verify(u"aoeu12345\u1234", u"aoeu12345\u1234") |
---|
717 | #self.verify(u"#encoding utf8#aoeu12345\u1234", u"aoeu12345\u1234") |
---|
718 | |
---|
719 | class EncodingDirective(OutputTest): |
---|
720 | def test1(self): |
---|
721 | """basic #encoding """ |
---|
722 | self.verify("#encoding utf-8\n1234", |
---|
723 | "1234") |
---|
724 | |
---|
725 | def test2(self): |
---|
726 | """basic #encoding """ |
---|
727 | self.verify("#encoding ascii\n1234", |
---|
728 | "1234") |
---|
729 | |
---|
730 | def test3(self): |
---|
731 | """basic #encoding """ |
---|
732 | self.verify("#encoding utf-8\n\xe1\x88\xb4", |
---|
733 | u'\u1234', outputEncoding='utf8') |
---|
734 | |
---|
735 | def test4(self): |
---|
736 | """basic #encoding """ |
---|
737 | self.verify("#encoding latin-1\n\xe1\x88\xb4", |
---|
738 | u"\xe1\x88\xb4") |
---|
739 | |
---|
740 | def test5(self): |
---|
741 | """basic #encoding """ |
---|
742 | self.verify("#encoding latin-1\nAndr\202", |
---|
743 | u'Andr\202') |
---|
744 | |
---|
745 | class UnicodeDirective(OutputTest): |
---|
746 | def test1(self): |
---|
747 | """basic #unicode """ |
---|
748 | self.verify("#unicode utf-8\n1234", |
---|
749 | u"1234") |
---|
750 | |
---|
751 | self.verify("#unicode ascii\n1234", |
---|
752 | u"1234") |
---|
753 | |
---|
754 | self.verify("#unicode latin-1\n1234", |
---|
755 | u"1234") |
---|
756 | |
---|
757 | self.verify("#unicode latin-1\n1234�, |
---|
758 | u"1234�) |
---|
759 | self.verify("#unicode: latin-1\n1234�, |
---|
760 | u"1234�) |
---|
761 | self.verify("# unicode : latin-1\n1234�, |
---|
762 | u"1234�) |
---|
763 | |
---|
764 | self.verify(u"#unicode latin-1\n1234�, |
---|
765 | u"1234�) |
---|
766 | |
---|
767 | self.verify("#encoding latin-1\n1234�, |
---|
768 | u"1234�) |
---|
769 | |
---|
770 | class Placeholders_Esc(OutputTest): |
---|
771 | convertEOLs = False |
---|
772 | def test1(self): |
---|
773 | """1 escaped placeholder""" |
---|
774 | self.verify("\$var", |
---|
775 | "$var") |
---|
776 | |
---|
777 | def test2(self): |
---|
778 | """2 escaped placeholders""" |
---|
779 | self.verify("\$var \$_", |
---|
780 | "$var $_") |
---|
781 | |
---|
782 | def test3(self): |
---|
783 | """2 escaped placeholders - back to back""" |
---|
784 | self.verify("\$var\$_", |
---|
785 | "$var$_") |
---|
786 | |
---|
787 | def test4(self): |
---|
788 | """2 escaped placeholders - nested""" |
---|
789 | self.verify("\$var(\$_)", |
---|
790 | "$var($_)") |
---|
791 | |
---|
792 | def test5(self): |
---|
793 | """2 escaped placeholders - nested and enclosed""" |
---|
794 | self.verify("\$(var(\$_)", |
---|
795 | "$(var($_)") |
---|
796 | |
---|
797 | |
---|
798 | class Placeholders_Calls(OutputTest): |
---|
799 | def test1(self): |
---|
800 | """func placeholder - no ()""" |
---|
801 | self.verify("$aFunc", |
---|
802 | "Scooby") |
---|
803 | |
---|
804 | def test2(self): |
---|
805 | """func placeholder - with ()""" |
---|
806 | self.verify("$aFunc()", |
---|
807 | "Scooby") |
---|
808 | |
---|
809 | def test3(self): |
---|
810 | r"""func placeholder - with (\n\n)""" |
---|
811 | self.verify("$aFunc(\n\n)", |
---|
812 | "Scooby", convertEOLs=False) |
---|
813 | |
---|
814 | def test4(self): |
---|
815 | r"""func placeholder - with (\n\n) and $() enclosure""" |
---|
816 | self.verify("$(aFunc(\n\n))", |
---|
817 | "Scooby", convertEOLs=False) |
---|
818 | |
---|
819 | def test5(self): |
---|
820 | r"""func placeholder - with (\n\n) and ${} enclosure""" |
---|
821 | self.verify("${aFunc(\n\n)}", |
---|
822 | "Scooby", convertEOLs=False) |
---|
823 | |
---|
824 | def test6(self): |
---|
825 | """func placeholder - with (int)""" |
---|
826 | self.verify("$aFunc(1234)", |
---|
827 | "1234") |
---|
828 | |
---|
829 | def test7(self): |
---|
830 | r"""func placeholder - with (\nint\n)""" |
---|
831 | self.verify("$aFunc(\n1234\n)", |
---|
832 | "1234", convertEOLs=False) |
---|
833 | def test8(self): |
---|
834 | """func placeholder - with (string)""" |
---|
835 | self.verify("$aFunc('aoeu')", |
---|
836 | "aoeu") |
---|
837 | |
---|
838 | def test9(self): |
---|
839 | """func placeholder - with ('''string''')""" |
---|
840 | self.verify("$aFunc('''aoeu''')", |
---|
841 | "aoeu") |
---|
842 | def test10(self): |
---|
843 | r"""func placeholder - with ('''\nstring\n''')""" |
---|
844 | self.verify("$aFunc('''\naoeu\n''')", |
---|
845 | "\naoeu\n") |
---|
846 | |
---|
847 | def test11(self): |
---|
848 | r"""func placeholder - with ('''\nstring'\n''')""" |
---|
849 | self.verify("$aFunc('''\naoeu'\n''')", |
---|
850 | "\naoeu'\n") |
---|
851 | |
---|
852 | def test12(self): |
---|
853 | r'''func placeholder - with ("""\nstring\n""")''' |
---|
854 | self.verify('$aFunc("""\naoeu\n""")', |
---|
855 | "\naoeu\n") |
---|
856 | |
---|
857 | def test13(self): |
---|
858 | """func placeholder - with (string*int)""" |
---|
859 | self.verify("$aFunc('aoeu'*2)", |
---|
860 | "aoeuaoeu") |
---|
861 | |
---|
862 | def test14(self): |
---|
863 | """func placeholder - with (int*int)""" |
---|
864 | self.verify("$aFunc(2*2)", |
---|
865 | "4") |
---|
866 | |
---|
867 | def test15(self): |
---|
868 | """func placeholder - with (int*float)""" |
---|
869 | self.verify("$aFunc(2*2.0)", |
---|
870 | "4.0") |
---|
871 | |
---|
872 | def test16(self): |
---|
873 | r"""func placeholder - with (int\n*\nfloat)""" |
---|
874 | self.verify("$aFunc(2\n*\n2.0)", |
---|
875 | "4.0", convertEOLs=False) |
---|
876 | |
---|
877 | def test17(self): |
---|
878 | """func placeholder - with ($arg=float)""" |
---|
879 | self.verify("$aFunc($arg=4.0)", |
---|
880 | "4.0") |
---|
881 | |
---|
882 | def test18(self): |
---|
883 | """func placeholder - with (arg=float)""" |
---|
884 | self.verify("$aFunc(arg=4.0)", |
---|
885 | "4.0") |
---|
886 | |
---|
887 | def test19(self): |
---|
888 | """deeply nested argstring, no enclosure""" |
---|
889 | self.verify("$aFunc($arg=$aMeth($arg=$aFunc(1)))", |
---|
890 | "1") |
---|
891 | |
---|
892 | def test20(self): |
---|
893 | """deeply nested argstring, no enclosure + with WS""" |
---|
894 | self.verify("$aFunc( $arg = $aMeth( $arg = $aFunc( 1 ) ) )", |
---|
895 | "1") |
---|
896 | def test21(self): |
---|
897 | """deeply nested argstring, () enclosure + with WS""" |
---|
898 | self.verify("$(aFunc( $arg = $aMeth( $arg = $aFunc( 1 ) ) ) )", |
---|
899 | "1") |
---|
900 | |
---|
901 | def test22(self): |
---|
902 | """deeply nested argstring, {} enclosure + with WS""" |
---|
903 | self.verify("${aFunc( $arg = $aMeth( $arg = $aFunc( 1 ) ) ) }", |
---|
904 | "1") |
---|
905 | |
---|
906 | def test23(self): |
---|
907 | """deeply nested argstring, [] enclosure + with WS""" |
---|
908 | self.verify("$[aFunc( $arg = $aMeth( $arg = $aFunc( 1 ) ) ) ]", |
---|
909 | "1") |
---|
910 | |
---|
911 | def test24(self): |
---|
912 | """deeply nested argstring, () enclosure + *cache""" |
---|
913 | self.verify("$*(aFunc( $arg = $aMeth( $arg = $aFunc( 1 ) ) ) )", |
---|
914 | "1") |
---|
915 | def test25(self): |
---|
916 | """deeply nested argstring, () enclosure + *15*cache""" |
---|
917 | self.verify("$*15*(aFunc( $arg = $aMeth( $arg = $aFunc( 1 ) ) ) )", |
---|
918 | "1") |
---|
919 | |
---|
920 | def test26(self): |
---|
921 | """a function call with the Python None kw.""" |
---|
922 | self.verify("$aFunc(None)", |
---|
923 | "") |
---|
924 | |
---|
925 | class NameMapper(OutputTest): |
---|
926 | def test1(self): |
---|
927 | """autocalling""" |
---|
928 | self.verify("$aFunc! $aFunc().", |
---|
929 | "Scooby! Scooby.") |
---|
930 | |
---|
931 | def test2(self): |
---|
932 | """nested autocalling""" |
---|
933 | self.verify("$aFunc($aFunc).", |
---|
934 | "Scooby.") |
---|
935 | |
---|
936 | def test3(self): |
---|
937 | """list subscription""" |
---|
938 | self.verify("$aList[0]", |
---|
939 | "item0") |
---|
940 | |
---|
941 | def test4(self): |
---|
942 | """list slicing""" |
---|
943 | self.verify("$aList[:2]", |
---|
944 | "['item0', 'item1']") |
---|
945 | |
---|
946 | def test5(self): |
---|
947 | """list slicing and subcription combined""" |
---|
948 | self.verify("$aList[:2][0]", |
---|
949 | "item0") |
---|
950 | |
---|
951 | def test6(self): |
---|
952 | """dictionary access - NameMapper style""" |
---|
953 | self.verify("$aDict.one", |
---|
954 | "item1") |
---|
955 | |
---|
956 | def test7(self): |
---|
957 | """dictionary access - Python style""" |
---|
958 | self.verify("$aDict['one']", |
---|
959 | "item1") |
---|
960 | |
---|
961 | def test8(self): |
---|
962 | """dictionary access combined with autocalled string method""" |
---|
963 | self.verify("$aDict.one.upper", |
---|
964 | "ITEM1") |
---|
965 | |
---|
966 | def test9(self): |
---|
967 | """dictionary access combined with string method""" |
---|
968 | self.verify("$aDict.one.upper()", |
---|
969 | "ITEM1") |
---|
970 | |
---|
971 | def test10(self): |
---|
972 | """nested dictionary access - NameMapper style""" |
---|
973 | self.verify("$aDict.nestedDict.two", |
---|
974 | "nestedItem2") |
---|
975 | |
---|
976 | def test11(self): |
---|
977 | """nested dictionary access - Python style""" |
---|
978 | self.verify("$aDict['nestedDict']['two']", |
---|
979 | "nestedItem2") |
---|
980 | |
---|
981 | def test12(self): |
---|
982 | """nested dictionary access - alternating style""" |
---|
983 | self.verify("$aDict['nestedDict'].two", |
---|
984 | "nestedItem2") |
---|
985 | |
---|
986 | def test13(self): |
---|
987 | """nested dictionary access using method - alternating style""" |
---|
988 | self.verify("$aDict.get('nestedDict').two", |
---|
989 | "nestedItem2") |
---|
990 | |
---|
991 | def test14(self): |
---|
992 | """nested dictionary access - NameMapper style - followed by method""" |
---|
993 | self.verify("$aDict.nestedDict.two.upper", |
---|
994 | "NESTEDITEM2") |
---|
995 | |
---|
996 | def test15(self): |
---|
997 | """nested dictionary access - alternating style - followed by method""" |
---|
998 | self.verify("$aDict['nestedDict'].two.upper", |
---|
999 | "NESTEDITEM2") |
---|
1000 | |
---|
1001 | def test16(self): |
---|
1002 | """nested dictionary access - NameMapper style - followed by method, then slice""" |
---|
1003 | self.verify("$aDict.nestedDict.two.upper[:4]", |
---|
1004 | "NEST") |
---|
1005 | |
---|
1006 | def test17(self): |
---|
1007 | """nested dictionary access - Python style using a soft-coded key""" |
---|
1008 | self.verify("$aDict[$anObj.meth('nestedDict')].two", |
---|
1009 | "nestedItem2") |
---|
1010 | |
---|
1011 | def test18(self): |
---|
1012 | """object method access""" |
---|
1013 | self.verify("$anObj.meth1", |
---|
1014 | "doo") |
---|
1015 | |
---|
1016 | def test19(self): |
---|
1017 | """object method access, followed by complex slice""" |
---|
1018 | self.verify("$anObj.meth1[0: ((4/4*2)*2)/$anObj.meth1(2) ]", |
---|
1019 | "do") |
---|
1020 | |
---|
1021 | def test20(self): |
---|
1022 | """object method access, followed by a very complex slice |
---|
1023 | If it can pass this one, it's safe to say it works!!""" |
---|
1024 | self.verify("$( anObj.meth1[0:\n (\n(4/4*2)*2)/$anObj.meth1(2)\n ] )", |
---|
1025 | "do") |
---|
1026 | |
---|
1027 | def test21(self): |
---|
1028 | """object method access with % in the default arg for the meth. |
---|
1029 | |
---|
1030 | This tests a bug that Jeff Johnson found and submitted a patch to SF |
---|
1031 | for.""" |
---|
1032 | |
---|
1033 | self.verify("$anObj.methWithPercentSignDefaultArg", |
---|
1034 | "110%") |
---|
1035 | |
---|
1036 | |
---|
1037 | #class NameMapperDict(OutputTest): |
---|
1038 | # |
---|
1039 | # _searchList = [{"update": "Yabba dabba doo!"}] |
---|
1040 | # |
---|
1041 | # def test1(self): |
---|
1042 | # if NameMapper_C_VERSION: |
---|
1043 | # return # This feature is not in the C version yet. |
---|
1044 | # self.verify("$update", "Yabba dabba doo!") |
---|
1045 | # |
---|
1046 | |
---|
1047 | class CacheDirective(OutputTest): |
---|
1048 | |
---|
1049 | def test1(self): |
---|
1050 | r"""simple #cache """ |
---|
1051 | self.verify("#cache:$anInt", |
---|
1052 | "1") |
---|
1053 | |
---|
1054 | def test2(self): |
---|
1055 | r"""simple #cache + WS""" |
---|
1056 | self.verify(" #cache \n$anInt#end cache", |
---|
1057 | "1") |
---|
1058 | |
---|
1059 | def test3(self): |
---|
1060 | r"""simple #cache ... #end cache""" |
---|
1061 | self.verify("""#cache id='cache1', timer=150m |
---|
1062 | $anInt |
---|
1063 | #end cache |
---|
1064 | $aStr""", |
---|
1065 | "1\nblarg") |
---|
1066 | |
---|
1067 | def test4(self): |
---|
1068 | r"""2 #cache ... #end cache blocks""" |
---|
1069 | self.verify("""#slurp |
---|
1070 | #def foo |
---|
1071 | #cache ID='cache1', timer=150m |
---|
1072 | $anInt |
---|
1073 | #end cache |
---|
1074 | #cache id='cache2', timer=15s |
---|
1075 | #for $i in range(5) |
---|
1076 | $i#slurp |
---|
1077 | #end for |
---|
1078 | #end cache |
---|
1079 | $aStr#slurp |
---|
1080 | #end def |
---|
1081 | $foo$foo$foo$foo$foo""", |
---|
1082 | "1\n01234blarg"*5) |
---|
1083 | |
---|
1084 | |
---|
1085 | def test5(self): |
---|
1086 | r"""nested #cache blocks""" |
---|
1087 | self.verify("""#slurp |
---|
1088 | #def foo |
---|
1089 | #cache ID='cache1', timer=150m |
---|
1090 | $anInt |
---|
1091 | #cache id='cache2', timer=15s |
---|
1092 | #for $i in range(5) |
---|
1093 | $i#slurp |
---|
1094 | #end for |
---|
1095 | $*(6)#slurp |
---|
1096 | #end cache |
---|
1097 | #end cache |
---|
1098 | $aStr#slurp |
---|
1099 | #end def |
---|
1100 | $foo$foo$foo$foo$foo""", |
---|
1101 | "1\n012346blarg"*5) |
---|
1102 | |
---|
1103 | def test6(self): |
---|
1104 | r"""Make sure that partial directives don't match""" |
---|
1105 | self.verify("#cache_foo", |
---|
1106 | "#cache_foo") |
---|
1107 | self.verify("#cached", |
---|
1108 | "#cached") |
---|
1109 | |
---|
1110 | class CallDirective(OutputTest): |
---|
1111 | |
---|
1112 | def test1(self): |
---|
1113 | r"""simple #call """ |
---|
1114 | self.verify("#call int\n$anInt#end call", |
---|
1115 | "1") |
---|
1116 | # single line version |
---|
1117 | self.verify("#call int: $anInt", |
---|
1118 | "1") |
---|
1119 | self.verify("#call int: 10\n$aStr", |
---|
1120 | "10\nblarg") |
---|
1121 | |
---|
1122 | def test2(self): |
---|
1123 | r"""simple #call + WS""" |
---|
1124 | self.verify("#call int\n$anInt #end call", |
---|
1125 | "1") |
---|
1126 | |
---|
1127 | def test3(self): |
---|
1128 | r"""a longer #call""" |
---|
1129 | self.verify('''\ |
---|
1130 | #def meth(arg) |
---|
1131 | $arg.upper()#slurp |
---|
1132 | #end def |
---|
1133 | #call $meth |
---|
1134 | $(1234+1) foo#slurp |
---|
1135 | #end call''', |
---|
1136 | "1235 FOO") |
---|
1137 | |
---|
1138 | def test4(self): |
---|
1139 | r"""#call with keyword #args""" |
---|
1140 | self.verify('''\ |
---|
1141 | #def meth(arg1, arg2) |
---|
1142 | $arg1.upper() - $arg2.lower()#slurp |
---|
1143 | #end def |
---|
1144 | #call self.meth |
---|
1145 | #arg arg1 |
---|
1146 | $(1234+1) foo#slurp |
---|
1147 | #arg arg2 |
---|
1148 | UPPER#slurp |
---|
1149 | #end call''', |
---|
1150 | "1235 FOO - upper") |
---|
1151 | |
---|
1152 | def test5(self): |
---|
1153 | r"""#call with single-line keyword #args """ |
---|
1154 | self.verify('''\ |
---|
1155 | #def meth(arg1, arg2) |
---|
1156 | $arg1.upper() - $arg2.lower()#slurp |
---|
1157 | #end def |
---|
1158 | #call self.meth |
---|
1159 | #arg arg1:$(1234+1) foo#slurp |
---|
1160 | #arg arg2:UPPER#slurp |
---|
1161 | #end call''', |
---|
1162 | "1235 FOO - upper") |
---|
1163 | |
---|
1164 | def test6(self): |
---|
1165 | """#call with python kwargs and cheetah output for the 1s positional |
---|
1166 | arg""" |
---|
1167 | |
---|
1168 | self.verify('''\ |
---|
1169 | #def meth(arg1, arg2) |
---|
1170 | $arg1.upper() - $arg2.lower()#slurp |
---|
1171 | #end def |
---|
1172 | #call self.meth arg2="UPPER" |
---|
1173 | $(1234+1) foo#slurp |
---|
1174 | #end call''', |
---|
1175 | "1235 FOO - upper") |
---|
1176 | |
---|
1177 | def test7(self): |
---|
1178 | """#call with python kwargs and #args""" |
---|
1179 | self.verify('''\ |
---|
1180 | #def meth(arg1, arg2, arg3) |
---|
1181 | $arg1.upper() - $arg2.lower() - $arg3#slurp |
---|
1182 | #end def |
---|
1183 | #call self.meth arg2="UPPER", arg3=999 |
---|
1184 | #arg arg1:$(1234+1) foo#slurp |
---|
1185 | #end call''', |
---|
1186 | "1235 FOO - upper - 999") |
---|
1187 | |
---|
1188 | def test8(self): |
---|
1189 | """#call with python kwargs and #args, and using a function to get the |
---|
1190 | function that will be called""" |
---|
1191 | self.verify('''\ |
---|
1192 | #def meth(arg1, arg2, arg3) |
---|
1193 | $arg1.upper() - $arg2.lower() - $arg3#slurp |
---|
1194 | #end def |
---|
1195 | #call getattr(self, "meth") arg2="UPPER", arg3=999 |
---|
1196 | #arg arg1:$(1234+1) foo#slurp |
---|
1197 | #end call''', |
---|
1198 | "1235 FOO - upper - 999") |
---|
1199 | |
---|
1200 | def test9(self): |
---|
1201 | """nested #call directives""" |
---|
1202 | self.verify('''\ |
---|
1203 | #def meth(arg1) |
---|
1204 | $arg1#slurp |
---|
1205 | #end def |
---|
1206 | #def meth2(x,y) |
---|
1207 | $x$y#slurp |
---|
1208 | #end def |
---|
1209 | ## |
---|
1210 | #call self.meth |
---|
1211 | 1#slurp |
---|
1212 | #call self.meth |
---|
1213 | 2#slurp |
---|
1214 | #call self.meth |
---|
1215 | 3#slurp |
---|
1216 | #end call 3 |
---|
1217 | #set two = 2 |
---|
1218 | #call self.meth2 y=c"$(10/$two)" |
---|
1219 | #arg x |
---|
1220 | 4#slurp |
---|
1221 | #end call 4 |
---|
1222 | #end call 2 |
---|
1223 | #end call 1''', |
---|
1224 | "12345") |
---|
1225 | |
---|
1226 | |
---|
1227 | |
---|
1228 | class I18nDirective(OutputTest): |
---|
1229 | def test1(self): |
---|
1230 | r"""simple #call """ |
---|
1231 | self.verify("#i18n \n$anInt#end i18n", |
---|
1232 | "1") |
---|
1233 | |
---|
1234 | # single line version |
---|
1235 | self.verify("#i18n: $anInt", |
---|
1236 | "1") |
---|
1237 | self.verify("#i18n: 10\n$aStr", |
---|
1238 | "10\nblarg") |
---|
1239 | |
---|
1240 | |
---|
1241 | class CaptureDirective(OutputTest): |
---|
1242 | def test1(self): |
---|
1243 | r"""simple #capture""" |
---|
1244 | self.verify('''\ |
---|
1245 | #capture cap1 |
---|
1246 | $(1234+1) foo#slurp |
---|
1247 | #end capture |
---|
1248 | $cap1#slurp |
---|
1249 | ''', |
---|
1250 | "1235 foo") |
---|
1251 | |
---|
1252 | |
---|
1253 | def test2(self): |
---|
1254 | r"""slightly more complex #capture""" |
---|
1255 | self.verify('''\ |
---|
1256 | #def meth(arg) |
---|
1257 | $arg.upper()#slurp |
---|
1258 | #end def |
---|
1259 | #capture cap1 |
---|
1260 | $(1234+1) $anInt $meth("foo")#slurp |
---|
1261 | #end capture |
---|
1262 | $cap1#slurp |
---|
1263 | ''', |
---|
1264 | "1235 1 FOO") |
---|
1265 | |
---|
1266 | |
---|
1267 | class SlurpDirective(OutputTest): |
---|
1268 | def test1(self): |
---|
1269 | r"""#slurp with 1 \n """ |
---|
1270 | self.verify("#slurp\n", |
---|
1271 | "") |
---|
1272 | |
---|
1273 | def test2(self): |
---|
1274 | r"""#slurp with 1 \n, leading whitespace |
---|
1275 | Should gobble""" |
---|
1276 | self.verify(" #slurp\n", |
---|
1277 | "") |
---|
1278 | |
---|
1279 | def test3(self): |
---|
1280 | r"""#slurp with 1 \n, leading content |
---|
1281 | Shouldn't gobble""" |
---|
1282 | self.verify(" 1234 #slurp\n", |
---|
1283 | " 1234 ") |
---|
1284 | |
---|
1285 | def test4(self): |
---|
1286 | r"""#slurp with WS then \n, leading content |
---|
1287 | Shouldn't gobble""" |
---|
1288 | self.verify(" 1234 #slurp \n", |
---|
1289 | " 1234 ") |
---|
1290 | |
---|
1291 | def test5(self): |
---|
1292 | r"""#slurp with garbage chars then \n, leading content |
---|
1293 | Should eat the garbage""" |
---|
1294 | self.verify(" 1234 #slurp garbage \n", |
---|
1295 | " 1234 ") |
---|
1296 | |
---|
1297 | |
---|
1298 | |
---|
1299 | class EOLSlurpToken(OutputTest): |
---|
1300 | _EOLSlurpToken = DEFAULT_COMPILER_SETTINGS['EOLSlurpToken'] |
---|
1301 | def test1(self): |
---|
1302 | r"""#slurp with 1 \n """ |
---|
1303 | self.verify("%s\n"%self._EOLSlurpToken, |
---|
1304 | "") |
---|
1305 | |
---|
1306 | def test2(self): |
---|
1307 | r"""#slurp with 1 \n, leading whitespace |
---|
1308 | Should gobble""" |
---|
1309 | self.verify(" %s\n"%self._EOLSlurpToken, |
---|
1310 | "") |
---|
1311 | def test3(self): |
---|
1312 | r"""#slurp with 1 \n, leading content |
---|
1313 | Shouldn't gobble""" |
---|
1314 | self.verify(" 1234 %s\n"%self._EOLSlurpToken, |
---|
1315 | " 1234 ") |
---|
1316 | |
---|
1317 | def test4(self): |
---|
1318 | r"""#slurp with WS then \n, leading content |
---|
1319 | Shouldn't gobble""" |
---|
1320 | self.verify(" 1234 %s \n"%self._EOLSlurpToken, |
---|
1321 | " 1234 ") |
---|
1322 | |
---|
1323 | def test5(self): |
---|
1324 | r"""#slurp with garbage chars then \n, leading content |
---|
1325 | Should NOT eat the garbage""" |
---|
1326 | self.verify(" 1234 %s garbage \n"%self._EOLSlurpToken, |
---|
1327 | " 1234 %s garbage \n"%self._EOLSlurpToken) |
---|
1328 | |
---|
1329 | if not DEFAULT_COMPILER_SETTINGS['EOLSlurpToken']: |
---|
1330 | del EOLSlurpToken |
---|
1331 | |
---|
1332 | class RawDirective(OutputTest): |
---|
1333 | def test1(self): |
---|
1334 | """#raw till EOF""" |
---|
1335 | self.verify("#raw\n$aFunc().\n\n", |
---|
1336 | "$aFunc().\n\n") |
---|
1337 | |
---|
1338 | def test2(self): |
---|
1339 | """#raw till #end raw""" |
---|
1340 | self.verify("#raw\n$aFunc().\n#end raw\n$anInt", |
---|
1341 | "$aFunc().\n1") |
---|
1342 | |
---|
1343 | def test3(self): |
---|
1344 | """#raw till #end raw gobble WS""" |
---|
1345 | self.verify(" #raw \n$aFunc().\n #end raw \n$anInt", |
---|
1346 | "$aFunc().\n1") |
---|
1347 | |
---|
1348 | def test4(self): |
---|
1349 | """#raw till #end raw using explicit directive closure |
---|
1350 | Shouldn't gobble""" |
---|
1351 | self.verify(" #raw #\n$aFunc().\n #end raw #\n$anInt", |
---|
1352 | " \n$aFunc().\n\n1") |
---|
1353 | |
---|
1354 | def test5(self): |
---|
1355 | """single-line short form #raw: """ |
---|
1356 | self.verify("#raw: $aFunc().\n\n", |
---|
1357 | "$aFunc().\n\n") |
---|
1358 | |
---|
1359 | self.verify("#raw: $aFunc().\n$anInt", |
---|
1360 | "$aFunc().\n1") |
---|
1361 | |
---|
1362 | class BreakpointDirective(OutputTest): |
---|
1363 | def test1(self): |
---|
1364 | """#breakpoint part way through source code""" |
---|
1365 | self.verify("$aFunc(2).\n#breakpoint\n$anInt", |
---|
1366 | "2.\n") |
---|
1367 | |
---|
1368 | def test2(self): |
---|
1369 | """#breakpoint at BOF""" |
---|
1370 | self.verify("#breakpoint\n$anInt", |
---|
1371 | "") |
---|
1372 | |
---|
1373 | def test3(self): |
---|
1374 | """#breakpoint at EOF""" |
---|
1375 | self.verify("$anInt\n#breakpoint", |
---|
1376 | "1\n") |
---|
1377 | |
---|
1378 | |
---|
1379 | class StopDirective(OutputTest): |
---|
1380 | def test1(self): |
---|
1381 | """#stop part way through source code""" |
---|
1382 | self.verify("$aFunc(2).\n#stop\n$anInt", |
---|
1383 | "2.\n") |
---|
1384 | |
---|
1385 | def test2(self): |
---|
1386 | """#stop at BOF""" |
---|
1387 | self.verify("#stop\n$anInt", |
---|
1388 | "") |
---|
1389 | |
---|
1390 | def test3(self): |
---|
1391 | """#stop at EOF""" |
---|
1392 | self.verify("$anInt\n#stop", |
---|
1393 | "1\n") |
---|
1394 | |
---|
1395 | def test4(self): |
---|
1396 | """#stop in pos test block""" |
---|
1397 | self.verify("""$anInt |
---|
1398 | #if 1 |
---|
1399 | inside the if block |
---|
1400 | #stop |
---|
1401 | #end if |
---|
1402 | blarg""", |
---|
1403 | "1\ninside the if block\n") |
---|
1404 | |
---|
1405 | def test5(self): |
---|
1406 | """#stop in neg test block""" |
---|
1407 | self.verify("""$anInt |
---|
1408 | #if 0 |
---|
1409 | inside the if block |
---|
1410 | #stop |
---|
1411 | #end if |
---|
1412 | blarg""", |
---|
1413 | "1\nblarg") |
---|
1414 | |
---|
1415 | |
---|
1416 | class ReturnDirective(OutputTest): |
---|
1417 | |
---|
1418 | def test1(self): |
---|
1419 | """#return'ing an int """ |
---|
1420 | self.verify("""1 |
---|
1421 | $str($test-6) |
---|
1422 | 3 |
---|
1423 | #def test |
---|
1424 | #if 1 |
---|
1425 | #return (3 *2) \ |
---|
1426 | + 2 |
---|
1427 | #else |
---|
1428 | aoeuoaeu |
---|
1429 | #end if |
---|
1430 | #end def |
---|
1431 | """, |
---|
1432 | "1\n2\n3\n") |
---|
1433 | |
---|
1434 | def test2(self): |
---|
1435 | """#return'ing an string """ |
---|
1436 | self.verify("""1 |
---|
1437 | $str($test[1]) |
---|
1438 | 3 |
---|
1439 | #def test |
---|
1440 | #if 1 |
---|
1441 | #return '123' |
---|
1442 | #else |
---|
1443 | aoeuoaeu |
---|
1444 | #end if |
---|
1445 | #end def |
---|
1446 | """, |
---|
1447 | "1\n2\n3\n") |
---|
1448 | |
---|
1449 | def test3(self): |
---|
1450 | """#return'ing an string AND streaming other output via the transaction""" |
---|
1451 | self.verify("""1 |
---|
1452 | $str($test(trans=trans)[1]) |
---|
1453 | 3 |
---|
1454 | #def test |
---|
1455 | 1.5 |
---|
1456 | #if 1 |
---|
1457 | #return '123' |
---|
1458 | #else |
---|
1459 | aoeuoaeu |
---|
1460 | #end if |
---|
1461 | #end def |
---|
1462 | """, |
---|
1463 | "1\n1.5\n2\n3\n") |
---|
1464 | |
---|
1465 | |
---|
1466 | class YieldDirective(OutputTest): |
---|
1467 | convertEOLs = False |
---|
1468 | def test1(self): |
---|
1469 | """simple #yield """ |
---|
1470 | |
---|
1471 | src1 = """#for i in range(10)\n#yield i\n#end for""" |
---|
1472 | src2 = """#for i in range(10)\n$i#slurp\n#yield\n#end for""" |
---|
1473 | src3 = ("#def iterator\n" |
---|
1474 | "#for i in range(10)\n#yield i\n#end for\n" |
---|
1475 | "#end def\n" |
---|
1476 | "#for i in $iterator\n$i#end for" |
---|
1477 | ) |
---|
1478 | |
---|
1479 | |
---|
1480 | for src in (src1,src2,src3): |
---|
1481 | klass = Template.compile(src, keepRefToGeneratedCode=True) |
---|
1482 | #print klass._CHEETAH_generatedModuleCode |
---|
1483 | iter = klass().respond() |
---|
1484 | output = [str(i) for i in iter] |
---|
1485 | assert ''.join(output)=='0123456789' |
---|
1486 | #print ''.join(output) |
---|
1487 | |
---|
1488 | # @@TR: need to expand this to cover error conditions etc. |
---|
1489 | |
---|
1490 | if versionTuple < (2,3): |
---|
1491 | del YieldDirective |
---|
1492 | |
---|
1493 | class ForDirective(OutputTest): |
---|
1494 | |
---|
1495 | def test1(self): |
---|
1496 | """#for loop with one local var""" |
---|
1497 | self.verify("#for $i in range(5)\n$i\n#end for", |
---|
1498 | "0\n1\n2\n3\n4\n") |
---|
1499 | |
---|
1500 | self.verify("#for $i in range(5):\n$i\n#end for", |
---|
1501 | "0\n1\n2\n3\n4\n") |
---|
1502 | |
---|
1503 | self.verify("#for $i in range(5): ##comment\n$i\n#end for", |
---|
1504 | "0\n1\n2\n3\n4\n") |
---|
1505 | |
---|
1506 | self.verify("#for $i in range(5) ##comment\n$i\n#end for", |
---|
1507 | "0\n1\n2\n3\n4\n") |
---|
1508 | |
---|
1509 | |
---|
1510 | def test2(self): |
---|
1511 | """#for loop with WS in loop""" |
---|
1512 | self.verify("#for $i in range(5)\n$i \n#end for", |
---|
1513 | "0 \n1 \n2 \n3 \n4 \n") |
---|
1514 | |
---|
1515 | def test3(self): |
---|
1516 | """#for loop gobble WS""" |
---|
1517 | self.verify(" #for $i in range(5) \n$i \n #end for ", |
---|
1518 | "0 \n1 \n2 \n3 \n4 \n") |
---|
1519 | |
---|
1520 | def test4(self): |
---|
1521 | """#for loop over list""" |
---|
1522 | self.verify("#for $i, $j in [(0,1),(2,3)]\n$i,$j\n#end for", |
---|
1523 | "0,1\n2,3\n") |
---|
1524 | |
---|
1525 | def test5(self): |
---|
1526 | """#for loop over list, with #slurp""" |
---|
1527 | self.verify("#for $i, $j in [(0,1),(2,3)]\n$i,$j#slurp\n#end for", |
---|
1528 | "0,12,3") |
---|
1529 | |
---|
1530 | def test6(self): |
---|
1531 | """#for loop with explicit closures""" |
---|
1532 | self.verify("#for $i in range(5)#$i#end for#", |
---|
1533 | "01234") |
---|
1534 | |
---|
1535 | def test7(self): |
---|
1536 | """#for loop with explicit closures and WS""" |
---|
1537 | self.verify(" #for $i in range(5)#$i#end for# ", |
---|
1538 | " 01234 ") |
---|
1539 | |
---|
1540 | def test8(self): |
---|
1541 | """#for loop using another $var""" |
---|
1542 | self.verify(" #for $i in range($aFunc(5))#$i#end for# ", |
---|
1543 | " 01234 ") |
---|
1544 | |
---|
1545 | def test9(self): |
---|
1546 | """test methods in for loops""" |
---|
1547 | self.verify("#for $func in $listOfLambdas\n$func($anInt)\n#end for", |
---|
1548 | "1\n1\n1\n") |
---|
1549 | |
---|
1550 | |
---|
1551 | def test10(self): |
---|
1552 | """#for loop over list, using methods of the items""" |
---|
1553 | self.verify("#for i, j in [('aa','bb'),('cc','dd')]\n$i.upper,$j.upper\n#end for", |
---|
1554 | "AA,BB\nCC,DD\n") |
---|
1555 | self.verify("#for $i, $j in [('aa','bb'),('cc','dd')]\n$i.upper,$j.upper\n#end for", |
---|
1556 | "AA,BB\nCC,DD\n") |
---|
1557 | |
---|
1558 | def test11(self): |
---|
1559 | """#for loop over list, using ($i,$j) style target list""" |
---|
1560 | self.verify("#for (i, j) in [('aa','bb'),('cc','dd')]\n$i.upper,$j.upper\n#end for", |
---|
1561 | "AA,BB\nCC,DD\n") |
---|
1562 | self.verify("#for ($i, $j) in [('aa','bb'),('cc','dd')]\n$i.upper,$j.upper\n#end for", |
---|
1563 | "AA,BB\nCC,DD\n") |
---|
1564 | |
---|
1565 | def test12(self): |
---|
1566 | """#for loop over list, using i, (j,k) style target list""" |
---|
1567 | self.verify("#for i, (j, k) in enumerate([('aa','bb'),('cc','dd')])\n$j.upper,$k.upper\n#end for", |
---|
1568 | "AA,BB\nCC,DD\n") |
---|
1569 | self.verify("#for $i, ($j, $k) in enumerate([('aa','bb'),('cc','dd')])\n$j.upper,$k.upper\n#end for", |
---|
1570 | "AA,BB\nCC,DD\n") |
---|
1571 | |
---|
1572 | def test13(self): |
---|
1573 | """single line #for""" |
---|
1574 | self.verify("#for $i in range($aFunc(5)): $i", |
---|
1575 | "01234") |
---|
1576 | |
---|
1577 | def test14(self): |
---|
1578 | """single line #for with 1 extra leading space""" |
---|
1579 | self.verify("#for $i in range($aFunc(5)): $i", |
---|
1580 | " 0 1 2 3 4") |
---|
1581 | |
---|
1582 | def test15(self): |
---|
1583 | """2 times single line #for""" |
---|
1584 | self.verify("#for $i in range($aFunc(5)): $i#slurp\n"*2, |
---|
1585 | "01234"*2) |
---|
1586 | |
---|
1587 | def test16(self): |
---|
1588 | """false single line #for """ |
---|
1589 | self.verify("#for $i in range(5): \n$i\n#end for", |
---|
1590 | "0\n1\n2\n3\n4\n") |
---|
1591 | |
---|
1592 | if versionTuple < (2,3): |
---|
1593 | del ForDirective.test12 |
---|
1594 | |
---|
1595 | class RepeatDirective(OutputTest): |
---|
1596 | |
---|
1597 | def test1(self): |
---|
1598 | """basic #repeat""" |
---|
1599 | self.verify("#repeat 3\n1\n#end repeat", |
---|
1600 | "1\n1\n1\n") |
---|
1601 | self.verify("#repeat 3: \n1\n#end repeat", |
---|
1602 | "1\n1\n1\n") |
---|
1603 | |
---|
1604 | self.verify("#repeat 3 ##comment\n1\n#end repeat", |
---|
1605 | "1\n1\n1\n") |
---|
1606 | |
---|
1607 | self.verify("#repeat 3: ##comment\n1\n#end repeat", |
---|
1608 | "1\n1\n1\n") |
---|
1609 | |
---|
1610 | def test2(self): |
---|
1611 | """#repeat with numeric expression""" |
---|
1612 | self.verify("#repeat 3*3/3\n1\n#end repeat", |
---|
1613 | "1\n1\n1\n") |
---|
1614 | |
---|
1615 | def test3(self): |
---|
1616 | """#repeat with placeholder""" |
---|
1617 | self.verify("#repeat $numTwo\n1\n#end repeat", |
---|
1618 | "1\n1\n") |
---|
1619 | |
---|
1620 | def test4(self): |
---|
1621 | """#repeat with placeholder * num""" |
---|
1622 | self.verify("#repeat $numTwo*1\n1\n#end repeat", |
---|
1623 | "1\n1\n") |
---|
1624 | |
---|
1625 | def test5(self): |
---|
1626 | """#repeat with placeholder and WS""" |
---|
1627 | self.verify(" #repeat $numTwo \n1\n #end repeat ", |
---|
1628 | "1\n1\n") |
---|
1629 | |
---|
1630 | def test6(self): |
---|
1631 | """single-line #repeat""" |
---|
1632 | self.verify("#repeat $numTwo: 1", |
---|
1633 | "11") |
---|
1634 | self.verify("#repeat $numTwo: 1\n"*2, |
---|
1635 | "1\n1\n"*2) |
---|
1636 | |
---|
1637 | #false single-line |
---|
1638 | self.verify("#repeat 3: \n1\n#end repeat", |
---|
1639 | "1\n1\n1\n") |
---|
1640 | |
---|
1641 | |
---|
1642 | class AttrDirective(OutputTest): |
---|
1643 | |
---|
1644 | def test1(self): |
---|
1645 | """#attr with int""" |
---|
1646 | self.verify("#attr $test = 1234\n$test", |
---|
1647 | "1234") |
---|
1648 | |
---|
1649 | def test2(self): |
---|
1650 | """#attr with string""" |
---|
1651 | self.verify("#attr $test = 'blarg'\n$test", |
---|
1652 | "blarg") |
---|
1653 | |
---|
1654 | def test3(self): |
---|
1655 | """#attr with expression""" |
---|
1656 | self.verify("#attr $test = 'blarg'.upper()*2\n$test", |
---|
1657 | "BLARGBLARG") |
---|
1658 | |
---|
1659 | def test4(self): |
---|
1660 | """#attr with string + WS |
---|
1661 | Should gobble""" |
---|
1662 | self.verify(" #attr $test = 'blarg' \n$test", |
---|
1663 | "blarg") |
---|
1664 | |
---|
1665 | def test5(self): |
---|
1666 | """#attr with string + WS + leading text |
---|
1667 | Shouldn't gobble""" |
---|
1668 | self.verify(" -- #attr $test = 'blarg' \n$test", |
---|
1669 | " -- \nblarg") |
---|
1670 | |
---|
1671 | |
---|
1672 | class DefDirective(OutputTest): |
---|
1673 | |
---|
1674 | def test1(self): |
---|
1675 | """#def without argstring""" |
---|
1676 | self.verify("#def testMeth\n1234\n#end def\n$testMeth", |
---|
1677 | "1234\n") |
---|
1678 | |
---|
1679 | self.verify("#def testMeth ## comment\n1234\n#end def\n$testMeth", |
---|
1680 | "1234\n") |
---|
1681 | |
---|
1682 | self.verify("#def testMeth: ## comment\n1234\n#end def\n$testMeth", |
---|
1683 | "1234\n") |
---|
1684 | |
---|
1685 | def test2(self): |
---|
1686 | """#def without argstring, gobble WS""" |
---|
1687 | self.verify(" #def testMeth \n1234\n #end def \n$testMeth", |
---|
1688 | "1234\n") |
---|
1689 | |
---|
1690 | def test3(self): |
---|
1691 | """#def with argstring, gobble WS""" |
---|
1692 | self.verify(" #def testMeth($a=999) \n1234-$a\n #end def\n$testMeth", |
---|
1693 | "1234-999\n") |
---|
1694 | |
---|
1695 | def test4(self): |
---|
1696 | """#def with argstring, gobble WS, string used in call""" |
---|
1697 | self.verify(" #def testMeth($a=999) \n1234-$a\n #end def\n$testMeth('ABC')", |
---|
1698 | "1234-ABC\n") |
---|
1699 | |
---|
1700 | def test5(self): |
---|
1701 | """#def with argstring, gobble WS, list used in call""" |
---|
1702 | self.verify(" #def testMeth($a=999) \n1234-$a\n #end def\n$testMeth([1,2,3])", |
---|
1703 | "1234-[1, 2, 3]\n") |
---|
1704 | |
---|
1705 | def test6(self): |
---|
1706 | """#def with 2 args, gobble WS, list used in call""" |
---|
1707 | self.verify(" #def testMeth($a, $b='default') \n1234-$a$b\n #end def\n$testMeth([1,2,3])", |
---|
1708 | "1234-[1, 2, 3]default\n") |
---|
1709 | |
---|
1710 | def test7(self): |
---|
1711 | """#def with *args, gobble WS""" |
---|
1712 | self.verify(" #def testMeth($*args) \n1234-$args\n #end def\n$testMeth", |
---|
1713 | "1234-()\n") |
---|
1714 | |
---|
1715 | def test8(self): |
---|
1716 | """#def with **KWs, gobble WS""" |
---|
1717 | self.verify(" #def testMeth($**KWs) \n1234-$KWs\n #end def\n$testMeth", |
---|
1718 | "1234-{}\n") |
---|
1719 | |
---|
1720 | def test9(self): |
---|
1721 | """#def with *args + **KWs, gobble WS""" |
---|
1722 | self.verify(" #def testMeth($*args, $**KWs) \n1234-$args-$KWs\n #end def\n$testMeth", |
---|
1723 | "1234-()-{}\n") |
---|
1724 | |
---|
1725 | def test10(self): |
---|
1726 | """#def with *args + **KWs, gobble WS""" |
---|
1727 | self.verify( |
---|
1728 | " #def testMeth($*args, $**KWs) \n1234-$args-$KWs.a\n #end def\n$testMeth(1,2, a=1)", |
---|
1729 | "1234-(1, 2)-1\n") |
---|
1730 | |
---|
1731 | |
---|
1732 | def test11(self): |
---|
1733 | """single line #def with extra WS""" |
---|
1734 | self.verify( |
---|
1735 | "#def testMeth: aoeuaoeu\n- $testMeth -", |
---|
1736 | "- aoeuaoeu -") |
---|
1737 | |
---|
1738 | def test12(self): |
---|
1739 | """single line #def with extra WS and nested $placeholders""" |
---|
1740 | self.verify( |
---|
1741 | "#def testMeth: $anInt $aFunc(1234)\n- $testMeth -", |
---|
1742 | "- 1 1234 -") |
---|
1743 | |
---|
1744 | def test13(self): |
---|
1745 | """single line #def escaped $placeholders""" |
---|
1746 | self.verify( |
---|
1747 | "#def testMeth: \$aFunc(\$anInt)\n- $testMeth -", |
---|
1748 | "- $aFunc($anInt) -") |
---|
1749 | |
---|
1750 | def test14(self): |
---|
1751 | """single line #def 1 escaped $placeholders""" |
---|
1752 | self.verify( |
---|
1753 | "#def testMeth: \$aFunc($anInt)\n- $testMeth -", |
---|
1754 | "- $aFunc(1) -") |
---|
1755 | |
---|
1756 | def test15(self): |
---|
1757 | """single line #def 1 escaped $placeholders + more WS""" |
---|
1758 | self.verify( |
---|
1759 | "#def testMeth : \$aFunc($anInt)\n- $testMeth -", |
---|
1760 | "- $aFunc(1) -") |
---|
1761 | |
---|
1762 | def test16(self): |
---|
1763 | """multiline #def with $ on methodName""" |
---|
1764 | self.verify("#def $testMeth\n1234\n#end def\n$testMeth", |
---|
1765 | "1234\n") |
---|
1766 | |
---|
1767 | def test17(self): |
---|
1768 | """single line #def with $ on methodName""" |
---|
1769 | self.verify("#def $testMeth:1234\n$testMeth", |
---|
1770 | "1234") |
---|
1771 | |
---|
1772 | def test18(self): |
---|
1773 | """single line #def with an argument""" |
---|
1774 | self.verify("#def $testMeth($arg=1234):$arg\n$testMeth", |
---|
1775 | "1234") |
---|
1776 | |
---|
1777 | def test19(self): |
---|
1778 | """#def that extends over two lines with arguments""" |
---|
1779 | self.verify("#def $testMeth($arg=1234,\n" |
---|
1780 | +" $arg2=5678)\n" |
---|
1781 | +"$arg $arg2\n" |
---|
1782 | +"#end def\n" |
---|
1783 | +"$testMeth", |
---|
1784 | "1234 5678\n") |
---|
1785 | |
---|
1786 | class DecoratorDirective(OutputTest): |
---|
1787 | def test1(self): |
---|
1788 | """single line #def with decorator""" |
---|
1789 | |
---|
1790 | self.verify("#@ blah", "#@ blah") |
---|
1791 | self.verify("#@23 blah", "#@23 blah") |
---|
1792 | self.verify("#@@TR: comment", "#@@TR: comment") |
---|
1793 | |
---|
1794 | self.verify("#from Cheetah.Tests.SyntaxAndOutput import testdecorator\n" |
---|
1795 | +"#@testdecorator" |
---|
1796 | +"\n#def $testMeth():1234\n$testMeth", |
---|
1797 | |
---|
1798 | "1234") |
---|
1799 | |
---|
1800 | self.verify("#from Cheetah.Tests.SyntaxAndOutput import testdecorator\n" |
---|
1801 | +"#@testdecorator" |
---|
1802 | +"\n#block $testMeth():1234", |
---|
1803 | |
---|
1804 | "1234") |
---|
1805 | |
---|
1806 | try: |
---|
1807 | self.verify( |
---|
1808 | "#from Cheetah.Tests.SyntaxAndOutput import testdecorator\n" |
---|
1809 | +"#@testdecorator\n sdf" |
---|
1810 | +"\n#def $testMeth():1234\n$testMeth", |
---|
1811 | |
---|
1812 | "1234") |
---|
1813 | except ParseError: |
---|
1814 | pass |
---|
1815 | else: |
---|
1816 | self.fail('should raise a ParseError') |
---|
1817 | |
---|
1818 | def test2(self): |
---|
1819 | """#def with multiple decorators""" |
---|
1820 | self.verify("#from Cheetah.Tests.SyntaxAndOutput import testdecorator\n" |
---|
1821 | +"#@testdecorator\n" |
---|
1822 | +"#@testdecorator\n" |
---|
1823 | +"#def testMeth\n" |
---|
1824 | +"1234\n" |
---|
1825 | "#end def\n" |
---|
1826 | "$testMeth", |
---|
1827 | "1234\n") |
---|
1828 | |
---|
1829 | if versionTuple < (2,4): |
---|
1830 | del DecoratorDirective |
---|
1831 | |
---|
1832 | class BlockDirective(OutputTest): |
---|
1833 | |
---|
1834 | def test1(self): |
---|
1835 | """#block without argstring""" |
---|
1836 | self.verify("#block testBlock\n1234\n#end block", |
---|
1837 | "1234\n") |
---|
1838 | |
---|
1839 | self.verify("#block testBlock ##comment\n1234\n#end block", |
---|
1840 | "1234\n") |
---|
1841 | |
---|
1842 | def test2(self): |
---|
1843 | """#block without argstring, gobble WS""" |
---|
1844 | self.verify(" #block testBlock \n1234\n #end block ", |
---|
1845 | "1234\n") |
---|
1846 | |
---|
1847 | def test3(self): |
---|
1848 | """#block with argstring, gobble WS |
---|
1849 | |
---|
1850 | Because blocks can be reused in multiple parts of the template arguments |
---|
1851 | (!!with defaults!!) can be given.""" |
---|
1852 | |
---|
1853 | self.verify(" #block testBlock($a=999) \n1234-$a\n #end block ", |
---|
1854 | "1234-999\n") |
---|
1855 | |
---|
1856 | def test4(self): |
---|
1857 | """#block with 2 args, gobble WS""" |
---|
1858 | self.verify(" #block testBlock($a=999, $b=444) \n1234-$a$b\n #end block ", |
---|
1859 | "1234-999444\n") |
---|
1860 | |
---|
1861 | |
---|
1862 | def test5(self): |
---|
1863 | """#block with 2 nested blocks |
---|
1864 | |
---|
1865 | Blocks can be nested to any depth and the name of the block is optional |
---|
1866 | for the #end block part: #end block OR #end block [name] """ |
---|
1867 | |
---|
1868 | self.verify("""#block testBlock |
---|
1869 | this is a test block |
---|
1870 | #block outerNest |
---|
1871 | outer |
---|
1872 | #block innerNest |
---|
1873 | inner |
---|
1874 | #end block innerNest |
---|
1875 | #end block outerNest |
---|
1876 | --- |
---|
1877 | #end block testBlock |
---|
1878 | """, |
---|
1879 | "this is a test block\nouter\ninner\n---\n") |
---|
1880 | |
---|
1881 | |
---|
1882 | def test6(self): |
---|
1883 | """single line #block """ |
---|
1884 | self.verify( |
---|
1885 | "#block testMeth: This is my block", |
---|
1886 | "This is my block") |
---|
1887 | |
---|
1888 | def test7(self): |
---|
1889 | """single line #block with WS""" |
---|
1890 | self.verify( |
---|
1891 | "#block testMeth: This is my block", |
---|
1892 | "This is my block") |
---|
1893 | |
---|
1894 | def test8(self): |
---|
1895 | """single line #block 1 escaped $placeholders""" |
---|
1896 | self.verify( |
---|
1897 | "#block testMeth: \$aFunc($anInt)", |
---|
1898 | "$aFunc(1)") |
---|
1899 | |
---|
1900 | def test9(self): |
---|
1901 | """single line #block 1 escaped $placeholders + WS""" |
---|
1902 | self.verify( |
---|
1903 | "#block testMeth: \$aFunc( $anInt )", |
---|
1904 | "$aFunc( 1 )") |
---|
1905 | |
---|
1906 | def test10(self): |
---|
1907 | """single line #block 1 escaped $placeholders + more WS""" |
---|
1908 | self.verify( |
---|
1909 | "#block testMeth : \$aFunc( $anInt )", |
---|
1910 | "$aFunc( 1 )") |
---|
1911 | |
---|
1912 | def test11(self): |
---|
1913 | """multiline #block $ on argstring""" |
---|
1914 | self.verify("#block $testBlock\n1234\n#end block", |
---|
1915 | "1234\n") |
---|
1916 | |
---|
1917 | def test12(self): |
---|
1918 | """single line #block with $ on methodName """ |
---|
1919 | self.verify( |
---|
1920 | "#block $testMeth: This is my block", |
---|
1921 | "This is my block") |
---|
1922 | |
---|
1923 | def test13(self): |
---|
1924 | """single line #block with an arg """ |
---|
1925 | self.verify( |
---|
1926 | "#block $testMeth($arg='This is my block'): $arg", |
---|
1927 | "This is my block") |
---|
1928 | |
---|
1929 | def test14(self): |
---|
1930 | """single line #block with None for content""" |
---|
1931 | self.verify( |
---|
1932 | """#block $testMeth: $None\ntest $testMeth-""", |
---|
1933 | "test -") |
---|
1934 | |
---|
1935 | def test15(self): |
---|
1936 | """single line #block with nothing for content""" |
---|
1937 | self.verify( |
---|
1938 | """#block $testMeth: \nfoo\n#end block\ntest $testMeth-""", |
---|
1939 | "foo\ntest foo\n-") |
---|
1940 | |
---|
1941 | class IncludeDirective(OutputTest): |
---|
1942 | |
---|
1943 | def setUp(self): |
---|
1944 | fp = open('parseTest.txt','w') |
---|
1945 | fp.write("$numOne $numTwo") |
---|
1946 | fp.flush() |
---|
1947 | fp.close |
---|
1948 | |
---|
1949 | def tearDown(self): |
---|
1950 | if os.path.exists('parseTest.txt'): |
---|
1951 | os.remove('parseTest.txt') |
---|
1952 | |
---|
1953 | def test1(self): |
---|
1954 | """#include raw of source $emptyString""" |
---|
1955 | self.verify("#include raw source=$emptyString", |
---|
1956 | "") |
---|
1957 | |
---|
1958 | def test2(self): |
---|
1959 | """#include raw of source $blockToBeParsed""" |
---|
1960 | self.verify("#include raw source=$blockToBeParsed", |
---|
1961 | "$numOne $numTwo") |
---|
1962 | |
---|
1963 | def test3(self): |
---|
1964 | """#include raw of 'parseTest.txt'""" |
---|
1965 | self.verify("#include raw 'parseTest.txt'", |
---|
1966 | "$numOne $numTwo") |
---|
1967 | |
---|
1968 | def test4(self): |
---|
1969 | """#include raw of $includeFileName""" |
---|
1970 | self.verify("#include raw $includeFileName", |
---|
1971 | "$numOne $numTwo") |
---|
1972 | |
---|
1973 | def test5(self): |
---|
1974 | """#include raw of $includeFileName, with WS""" |
---|
1975 | self.verify(" #include raw $includeFileName ", |
---|
1976 | "$numOne $numTwo") |
---|
1977 | |
---|
1978 | def test6(self): |
---|
1979 | """#include raw of source= , with WS""" |
---|
1980 | self.verify(" #include raw source='This is my $Source '*2 ", |
---|
1981 | "This is my $Source This is my $Source ") |
---|
1982 | |
---|
1983 | def test7(self): |
---|
1984 | """#include of $blockToBeParsed""" |
---|
1985 | self.verify("#include source=$blockToBeParsed", |
---|
1986 | "1 2") |
---|
1987 | |
---|
1988 | def test8(self): |
---|
1989 | """#include of $blockToBeParsed, with WS""" |
---|
1990 | self.verify(" #include source=$blockToBeParsed ", |
---|
1991 | "1 2") |
---|
1992 | |
---|
1993 | def test9(self): |
---|
1994 | """#include of 'parseTest.txt', with WS""" |
---|
1995 | self.verify(" #include source=$blockToBeParsed ", |
---|
1996 | "1 2") |
---|
1997 | |
---|
1998 | def test10(self): |
---|
1999 | """#include of "parseTest.txt", with WS""" |
---|
2000 | self.verify(" #include source=$blockToBeParsed ", |
---|
2001 | "1 2") |
---|
2002 | |
---|
2003 | def test11(self): |
---|
2004 | """#include of 'parseTest.txt', with WS and surrounding text""" |
---|
2005 | self.verify("aoeu\n #include source=$blockToBeParsed \naoeu", |
---|
2006 | "aoeu\n1 2aoeu") |
---|
2007 | |
---|
2008 | def test12(self): |
---|
2009 | """#include of 'parseTest.txt', with WS and explicit closure""" |
---|
2010 | self.verify(" #include source=$blockToBeParsed# ", |
---|
2011 | " 1 2 ") |
---|
2012 | |
---|
2013 | |
---|
2014 | class SilentDirective(OutputTest): |
---|
2015 | |
---|
2016 | def test1(self): |
---|
2017 | """simple #silent""" |
---|
2018 | self.verify("#silent $aFunc", |
---|
2019 | "") |
---|
2020 | |
---|
2021 | def test2(self): |
---|
2022 | """simple #silent""" |
---|
2023 | self.verify("#silent $anObj.callIt\n$anObj.callArg", |
---|
2024 | "1234") |
---|
2025 | |
---|
2026 | self.verify("#silent $anObj.callIt ##comment\n$anObj.callArg", |
---|
2027 | "1234") |
---|
2028 | |
---|
2029 | def test3(self): |
---|
2030 | """simple #silent""" |
---|
2031 | self.verify("#silent $anObj.callIt(99)\n$anObj.callArg", |
---|
2032 | "99") |
---|
2033 | |
---|
2034 | class SetDirective(OutputTest): |
---|
2035 | |
---|
2036 | def test1(self): |
---|
2037 | """simple #set""" |
---|
2038 | self.verify("#set $testVar = 'blarg'\n$testVar", |
---|
2039 | "blarg") |
---|
2040 | self.verify("#set testVar = 'blarg'\n$testVar", |
---|
2041 | "blarg") |
---|
2042 | |
---|
2043 | |
---|
2044 | self.verify("#set testVar = 'blarg'##comment\n$testVar", |
---|
2045 | "blarg") |
---|
2046 | |
---|
2047 | def test2(self): |
---|
2048 | """simple #set with no WS between operands""" |
---|
2049 | self.verify("#set $testVar='blarg'", |
---|
2050 | "") |
---|
2051 | def test3(self): |
---|
2052 | """#set + use of var""" |
---|
2053 | self.verify("#set $testVar = 'blarg'\n$testVar", |
---|
2054 | "blarg") |
---|
2055 | |
---|
2056 | def test4(self): |
---|
2057 | """#set + use in an #include""" |
---|
2058 | self.verify("#set global $aSetVar = 1234\n#include source=$includeBlock2", |
---|
2059 | "1 2 1234") |
---|
2060 | |
---|
2061 | def test5(self): |
---|
2062 | """#set with a dictionary""" |
---|
2063 | self.verify( """#set $testDict = {'one':'one1','two':'two2','three':'three3'} |
---|
2064 | $testDict.one |
---|
2065 | $testDict.two""", |
---|
2066 | "one1\ntwo2") |
---|
2067 | |
---|
2068 | def test6(self): |
---|
2069 | """#set with string, then used in #if block""" |
---|
2070 | |
---|
2071 | self.verify("""#set $test='a string'\n#if $test#blarg#end if""", |
---|
2072 | "blarg") |
---|
2073 | |
---|
2074 | def test7(self): |
---|
2075 | """simple #set, gobble WS""" |
---|
2076 | self.verify(" #set $testVar = 'blarg' ", |
---|
2077 | "") |
---|
2078 | |
---|
2079 | def test8(self): |
---|
2080 | """simple #set, don't gobble WS""" |
---|
2081 | self.verify(" #set $testVar = 'blarg'#---", |
---|
2082 | " ---") |
---|
2083 | |
---|
2084 | def test9(self): |
---|
2085 | """simple #set with a list""" |
---|
2086 | self.verify(" #set $testVar = [1, 2, 3] \n$testVar", |
---|
2087 | "[1, 2, 3]") |
---|
2088 | |
---|
2089 | def test10(self): |
---|
2090 | """simple #set global with a list""" |
---|
2091 | self.verify(" #set global $testVar = [1, 2, 3] \n$testVar", |
---|
2092 | "[1, 2, 3]") |
---|
2093 | |
---|
2094 | def test11(self): |
---|
2095 | """simple #set global with a list and *cache |
---|
2096 | |
---|
2097 | Caching only works with global #set vars. Local vars are not accesible |
---|
2098 | to the cache namespace. |
---|
2099 | """ |
---|
2100 | |
---|
2101 | self.verify(" #set global $testVar = [1, 2, 3] \n$*testVar", |
---|
2102 | "[1, 2, 3]") |
---|
2103 | |
---|
2104 | def test12(self): |
---|
2105 | """simple #set global with a list and *<int>*cache""" |
---|
2106 | self.verify(" #set global $testVar = [1, 2, 3] \n$*5*testVar", |
---|
2107 | "[1, 2, 3]") |
---|
2108 | |
---|
2109 | def test13(self): |
---|
2110 | """simple #set with a list and *<float>*cache""" |
---|
2111 | self.verify(" #set global $testVar = [1, 2, 3] \n$*.5*testVar", |
---|
2112 | "[1, 2, 3]") |
---|
2113 | |
---|
2114 | def test14(self): |
---|
2115 | """simple #set without NameMapper on""" |
---|
2116 | self.verify("""#compiler useNameMapper = 0\n#set $testVar = 1 \n$testVar""", |
---|
2117 | "1") |
---|
2118 | |
---|
2119 | def test15(self): |
---|
2120 | """simple #set without $""" |
---|
2121 | self.verify("""#set testVar = 1 \n$testVar""", |
---|
2122 | "1") |
---|
2123 | |
---|
2124 | def test16(self): |
---|
2125 | """simple #set global without $""" |
---|
2126 | self.verify("""#set global testVar = 1 \n$testVar""", |
---|
2127 | "1") |
---|
2128 | |
---|
2129 | def test17(self): |
---|
2130 | """simple #set module without $""" |
---|
2131 | self.verify("""#set module __foo__ = 'bar'\n$__foo__""", |
---|
2132 | "bar") |
---|
2133 | |
---|
2134 | def test18(self): |
---|
2135 | """#set with i,j=list style assignment""" |
---|
2136 | self.verify("""#set i,j = [1,2]\n$i$j""", |
---|
2137 | "12") |
---|
2138 | self.verify("""#set $i,$j = [1,2]\n$i$j""", |
---|
2139 | "12") |
---|
2140 | |
---|
2141 | def test19(self): |
---|
2142 | """#set with (i,j)=list style assignment""" |
---|
2143 | self.verify("""#set (i,j) = [1,2]\n$i$j""", |
---|
2144 | "12") |
---|
2145 | self.verify("""#set ($i,$j) = [1,2]\n$i$j""", |
---|
2146 | "12") |
---|
2147 | |
---|
2148 | def test20(self): |
---|
2149 | """#set with i, (j,k)=list style assignment""" |
---|
2150 | self.verify("""#set i, (j,k) = [1,(2,3)]\n$i$j$k""", |
---|
2151 | "123") |
---|
2152 | self.verify("""#set $i, ($j,$k) = [1,(2,3)]\n$i$j$k""", |
---|
2153 | "123") |
---|
2154 | |
---|
2155 | |
---|
2156 | class IfDirective(OutputTest): |
---|
2157 | |
---|
2158 | def test1(self): |
---|
2159 | """simple #if block""" |
---|
2160 | self.verify("#if 1\n$aStr\n#end if\n", |
---|
2161 | "blarg\n") |
---|
2162 | |
---|
2163 | self.verify("#if 1:\n$aStr\n#end if\n", |
---|
2164 | "blarg\n") |
---|
2165 | |
---|
2166 | self.verify("#if 1: \n$aStr\n#end if\n", |
---|
2167 | "blarg\n") |
---|
2168 | |
---|
2169 | self.verify("#if 1: ##comment \n$aStr\n#end if\n", |
---|
2170 | "blarg\n") |
---|
2171 | |
---|
2172 | self.verify("#if 1 ##comment \n$aStr\n#end if\n", |
---|
2173 | "blarg\n") |
---|
2174 | |
---|
2175 | self.verify("#if 1##for i in range(10)#$i#end for##end if", |
---|
2176 | '0123456789') |
---|
2177 | |
---|
2178 | self.verify("#if 1: #for i in range(10)#$i#end for", |
---|
2179 | '0123456789') |
---|
2180 | |
---|
2181 | self.verify("#if 1: #for i in range(10):$i", |
---|
2182 | '0123456789') |
---|
2183 | |
---|
2184 | def test2(self): |
---|
2185 | """simple #if block, with WS""" |
---|
2186 | self.verify(" #if 1\n$aStr\n #end if \n", |
---|
2187 | "blarg\n") |
---|
2188 | def test3(self): |
---|
2189 | """simple #if block, with WS and explicit closures""" |
---|
2190 | self.verify(" #if 1#\n$aStr\n #end if #--\n", |
---|
2191 | " \nblarg\n --\n") |
---|
2192 | |
---|
2193 | def test4(self): |
---|
2194 | """#if block using $numOne""" |
---|
2195 | self.verify("#if $numOne\n$aStr\n#end if\n", |
---|
2196 | "blarg\n") |
---|
2197 | |
---|
2198 | def test5(self): |
---|
2199 | """#if block using $zero""" |
---|
2200 | self.verify("#if $zero\n$aStr\n#end if\n", |
---|
2201 | "") |
---|
2202 | def test6(self): |
---|
2203 | """#if block using $emptyString""" |
---|
2204 | self.verify("#if $emptyString\n$aStr\n#end if\n", |
---|
2205 | "") |
---|
2206 | def test7(self): |
---|
2207 | """#if ... #else ... block using a $emptyString""" |
---|
2208 | self.verify("#if $emptyString\n$anInt\n#else\n$anInt - $anInt\n#end if", |
---|
2209 | "1 - 1\n") |
---|
2210 | |
---|
2211 | def test8(self): |
---|
2212 | """#if ... #elif ... #else ... block using a $emptyString""" |
---|
2213 | self.verify("#if $emptyString\n$c\n#elif $numOne\n$numOne\n#else\n$c - $c\n#end if", |
---|
2214 | "1\n") |
---|
2215 | |
---|
2216 | def test9(self): |
---|
2217 | """#if 'not' test, with #slurp""" |
---|
2218 | self.verify("#if not $emptyString\n$aStr#slurp\n#end if\n", |
---|
2219 | "blarg") |
---|
2220 | |
---|
2221 | def test10(self): |
---|
2222 | """#if block using $*emptyString |
---|
2223 | |
---|
2224 | This should barf |
---|
2225 | """ |
---|
2226 | try: |
---|
2227 | self.verify("#if $*emptyString\n$aStr\n#end if\n", |
---|
2228 | "") |
---|
2229 | except ParseError: |
---|
2230 | pass |
---|
2231 | else: |
---|
2232 | self.fail('This should barf') |
---|
2233 | |
---|
2234 | def test11(self): |
---|
2235 | """#if block using invalid top-level $(placeholder) syntax - should barf""" |
---|
2236 | |
---|
2237 | for badSyntax in ("#if $*5*emptyString\n$aStr\n#end if\n", |
---|
2238 | "#if ${emptyString}\n$aStr\n#end if\n", |
---|
2239 | "#if $(emptyString)\n$aStr\n#end if\n", |
---|
2240 | "#if $[emptyString]\n$aStr\n#end if\n", |
---|
2241 | "#if $!emptyString\n$aStr\n#end if\n", |
---|
2242 | ): |
---|
2243 | try: |
---|
2244 | self.verify(badSyntax, "") |
---|
2245 | except ParseError: |
---|
2246 | pass |
---|
2247 | else: |
---|
2248 | self.fail('This should barf') |
---|
2249 | |
---|
2250 | def test12(self): |
---|
2251 | """#if ... #else if ... #else ... block using a $emptyString |
---|
2252 | Same as test 8 but using else if instead of elif""" |
---|
2253 | self.verify("#if $emptyString\n$c\n#else if $numOne\n$numOne\n#else\n$c - $c\n#end if", |
---|
2254 | "1\n") |
---|
2255 | |
---|
2256 | |
---|
2257 | def test13(self): |
---|
2258 | """#if# ... #else # ... block using a $emptyString with """ |
---|
2259 | self.verify("#if $emptyString# $anInt#else#$anInt - $anInt#end if", |
---|
2260 | "1 - 1") |
---|
2261 | |
---|
2262 | def test14(self): |
---|
2263 | """single-line #if: simple""" |
---|
2264 | self.verify("#if $emptyString then 'true' else 'false'", |
---|
2265 | "false") |
---|
2266 | |
---|
2267 | def test15(self): |
---|
2268 | """single-line #if: more complex""" |
---|
2269 | self.verify("#if $anInt then 'true' else 'false'", |
---|
2270 | "true") |
---|
2271 | |
---|
2272 | def test16(self): |
---|
2273 | """single-line #if: with the words 'else' and 'then' in the output """ |
---|
2274 | self.verify("#if ($anInt and not $emptyString==''' else ''') then $str('then') else 'else'", |
---|
2275 | "then") |
---|
2276 | |
---|
2277 | def test17(self): |
---|
2278 | """single-line #if: """ |
---|
2279 | self.verify("#if 1: foo\n#if 0: bar\n#if 1: foo", |
---|
2280 | "foo\nfoo") |
---|
2281 | |
---|
2282 | |
---|
2283 | self.verify("#if 1: foo\n#if 0: bar\n#if 1: foo", |
---|
2284 | "foo\nfoo") |
---|
2285 | |
---|
2286 | def test18(self): |
---|
2287 | """single-line #if: \n#else: """ |
---|
2288 | self.verify("#if 1: foo\n#elif 0: bar", |
---|
2289 | "foo\n") |
---|
2290 | |
---|
2291 | self.verify("#if 1: foo\n#elif 0: bar\n#else: blarg\n", |
---|
2292 | "foo\n") |
---|
2293 | |
---|
2294 | self.verify("#if 0: foo\n#elif 0: bar\n#else: blarg\n", |
---|
2295 | "blarg\n") |
---|
2296 | |
---|
2297 | class UnlessDirective(OutputTest): |
---|
2298 | |
---|
2299 | def test1(self): |
---|
2300 | """#unless 1""" |
---|
2301 | self.verify("#unless 1\n 1234 \n#end unless", |
---|
2302 | "") |
---|
2303 | |
---|
2304 | self.verify("#unless 1:\n 1234 \n#end unless", |
---|
2305 | "") |
---|
2306 | |
---|
2307 | self.verify("#unless 1: ##comment\n 1234 \n#end unless", |
---|
2308 | "") |
---|
2309 | |
---|
2310 | self.verify("#unless 1 ##comment\n 1234 \n#end unless", |
---|
2311 | "") |
---|
2312 | |
---|
2313 | |
---|
2314 | def test2(self): |
---|
2315 | """#unless 0""" |
---|
2316 | self.verify("#unless 0\n 1234 \n#end unless", |
---|
2317 | " 1234 \n") |
---|
2318 | |
---|
2319 | def test3(self): |
---|
2320 | """#unless $none""" |
---|
2321 | self.verify("#unless $none\n 1234 \n#end unless", |
---|
2322 | " 1234 \n") |
---|
2323 | |
---|
2324 | def test4(self): |
---|
2325 | """#unless $numTwo""" |
---|
2326 | self.verify("#unless $numTwo\n 1234 \n#end unless", |
---|
2327 | "") |
---|
2328 | |
---|
2329 | def test5(self): |
---|
2330 | """#unless $numTwo with WS""" |
---|
2331 | self.verify(" #unless $numTwo \n 1234 \n #end unless ", |
---|
2332 | "") |
---|
2333 | |
---|
2334 | def test6(self): |
---|
2335 | """single-line #unless""" |
---|
2336 | self.verify("#unless 1: 1234", "") |
---|
2337 | self.verify("#unless 0: 1234", "1234") |
---|
2338 | self.verify("#unless 0: 1234\n"*2, "1234\n"*2) |
---|
2339 | |
---|
2340 | class PSP(OutputTest): |
---|
2341 | |
---|
2342 | def test1(self): |
---|
2343 | """simple <%= [int] %>""" |
---|
2344 | self.verify("<%= 1234 %>", "1234") |
---|
2345 | |
---|
2346 | def test2(self): |
---|
2347 | """simple <%= [string] %>""" |
---|
2348 | self.verify("<%= 'blarg' %>", "blarg") |
---|
2349 | |
---|
2350 | def test3(self): |
---|
2351 | """simple <%= None %>""" |
---|
2352 | self.verify("<%= None %>", "") |
---|
2353 | def test4(self): |
---|
2354 | """simple <%= [string] %> + $anInt""" |
---|
2355 | self.verify("<%= 'blarg' %>$anInt", "blarg1") |
---|
2356 | |
---|
2357 | def test5(self): |
---|
2358 | """simple <%= [EXPR] %> + $anInt""" |
---|
2359 | self.verify("<%= ('blarg'*2).upper() %>$anInt", "BLARGBLARG1") |
---|
2360 | |
---|
2361 | def test6(self): |
---|
2362 | """for loop in <%%>""" |
---|
2363 | self.verify("<% for i in range(5):%>1<%end%>", "11111") |
---|
2364 | |
---|
2365 | def test7(self): |
---|
2366 | """for loop in <%%> and using <%=i%>""" |
---|
2367 | self.verify("<% for i in range(5):%><%=i%><%end%>", "01234") |
---|
2368 | |
---|
2369 | def test8(self): |
---|
2370 | """for loop in <% $%> and using <%=i%>""" |
---|
2371 | self.verify("""<% for i in range(5): |
---|
2372 | i=i*2$%><%=i%><%end%>""", "02468") |
---|
2373 | |
---|
2374 | def test9(self): |
---|
2375 | """for loop in <% $%> and using <%=i%> plus extra text""" |
---|
2376 | self.verify("""<% for i in range(5): |
---|
2377 | i=i*2$%><%=i%>-<%end%>""", "0-2-4-6-8-") |
---|
2378 | |
---|
2379 | |
---|
2380 | class WhileDirective(OutputTest): |
---|
2381 | def test1(self): |
---|
2382 | """simple #while with a counter""" |
---|
2383 | self.verify("#set $i = 0\n#while $i < 5\n$i#slurp\n#set $i += 1\n#end while", |
---|
2384 | "01234") |
---|
2385 | |
---|
2386 | class ContinueDirective(OutputTest): |
---|
2387 | def test1(self): |
---|
2388 | """#continue with a #while""" |
---|
2389 | self.verify("""#set $i = 0 |
---|
2390 | #while $i < 5 |
---|
2391 | #if $i == 3 |
---|
2392 | #set $i += 1 |
---|
2393 | #continue |
---|
2394 | #end if |
---|
2395 | $i#slurp |
---|
2396 | #set $i += 1 |
---|
2397 | #end while""", |
---|
2398 | "0124") |
---|
2399 | |
---|
2400 | def test2(self): |
---|
2401 | """#continue with a #for""" |
---|
2402 | self.verify("""#for $i in range(5) |
---|
2403 | #if $i == 3 |
---|
2404 | #continue |
---|
2405 | #end if |
---|
2406 | $i#slurp |
---|
2407 | #end for""", |
---|
2408 | "0124") |
---|
2409 | |
---|
2410 | class BreakDirective(OutputTest): |
---|
2411 | def test1(self): |
---|
2412 | """#break with a #while""" |
---|
2413 | self.verify("""#set $i = 0 |
---|
2414 | #while $i < 5 |
---|
2415 | #if $i == 3 |
---|
2416 | #break |
---|
2417 | #end if |
---|
2418 | $i#slurp |
---|
2419 | #set $i += 1 |
---|
2420 | #end while""", |
---|
2421 | "012") |
---|
2422 | |
---|
2423 | def test2(self): |
---|
2424 | """#break with a #for""" |
---|
2425 | self.verify("""#for $i in range(5) |
---|
2426 | #if $i == 3 |
---|
2427 | #break |
---|
2428 | #end if |
---|
2429 | $i#slurp |
---|
2430 | #end for""", |
---|
2431 | "012") |
---|
2432 | |
---|
2433 | |
---|
2434 | class TryDirective(OutputTest): |
---|
2435 | |
---|
2436 | def test1(self): |
---|
2437 | """simple #try |
---|
2438 | """ |
---|
2439 | self.verify("#try\n1234\n#except\nblarg\n#end try", |
---|
2440 | "1234\n") |
---|
2441 | |
---|
2442 | def test2(self): |
---|
2443 | """#try / #except with #raise |
---|
2444 | """ |
---|
2445 | self.verify("#try\n#raise ValueError\n#except\nblarg\n#end try", |
---|
2446 | "blarg\n") |
---|
2447 | |
---|
2448 | def test3(self): |
---|
2449 | """#try / #except with #raise + WS |
---|
2450 | |
---|
2451 | Should gobble |
---|
2452 | """ |
---|
2453 | self.verify(" #try \n #raise ValueError \n #except \nblarg\n #end try", |
---|
2454 | "blarg\n") |
---|
2455 | |
---|
2456 | |
---|
2457 | def test4(self): |
---|
2458 | """#try / #except with #raise + WS and leading text |
---|
2459 | |
---|
2460 | Shouldn't gobble |
---|
2461 | """ |
---|
2462 | self.verify("--#try \n #raise ValueError \n #except \nblarg\n #end try#--", |
---|
2463 | "--\nblarg\n --") |
---|
2464 | |
---|
2465 | def test5(self): |
---|
2466 | """nested #try / #except with #raise |
---|
2467 | """ |
---|
2468 | self.verify( |
---|
2469 | """#try |
---|
2470 | #raise ValueError |
---|
2471 | #except |
---|
2472 | #try |
---|
2473 | #raise ValueError |
---|
2474 | #except |
---|
2475 | blarg |
---|
2476 | #end try |
---|
2477 | #end try""", |
---|
2478 | "blarg\n") |
---|
2479 | |
---|
2480 | class PassDirective(OutputTest): |
---|
2481 | def test1(self): |
---|
2482 | """#pass in a #try / #except block |
---|
2483 | """ |
---|
2484 | self.verify("#try\n#raise ValueError\n#except\n#pass\n#end try", |
---|
2485 | "") |
---|
2486 | |
---|
2487 | def test2(self): |
---|
2488 | """#pass in a #try / #except block + WS |
---|
2489 | """ |
---|
2490 | self.verify(" #try \n #raise ValueError \n #except \n #pass \n #end try", |
---|
2491 | "") |
---|
2492 | |
---|
2493 | |
---|
2494 | class AssertDirective(OutputTest): |
---|
2495 | def test1(self): |
---|
2496 | """simple #assert |
---|
2497 | """ |
---|
2498 | self.verify("#set $x = 1234\n#assert $x == 1234", |
---|
2499 | "") |
---|
2500 | |
---|
2501 | def test2(self): |
---|
2502 | """simple #assert that fails |
---|
2503 | """ |
---|
2504 | def test(self=self): |
---|
2505 | self.verify("#set $x = 1234\n#assert $x == 999", |
---|
2506 | ""), |
---|
2507 | self.failUnlessRaises(AssertionError, test) |
---|
2508 | |
---|
2509 | def test3(self): |
---|
2510 | """simple #assert with WS |
---|
2511 | """ |
---|
2512 | self.verify("#set $x = 1234\n #assert $x == 1234 ", |
---|
2513 | "") |
---|
2514 | |
---|
2515 | |
---|
2516 | class RaiseDirective(OutputTest): |
---|
2517 | def test1(self): |
---|
2518 | """simple #raise ValueError |
---|
2519 | |
---|
2520 | Should raise ValueError |
---|
2521 | """ |
---|
2522 | def test(self=self): |
---|
2523 | self.verify("#raise ValueError", |
---|
2524 | ""), |
---|
2525 | self.failUnlessRaises(ValueError, test) |
---|
2526 | |
---|
2527 | def test2(self): |
---|
2528 | """#raise ValueError in #if block |
---|
2529 | |
---|
2530 | Should raise ValueError |
---|
2531 | """ |
---|
2532 | def test(self=self): |
---|
2533 | self.verify("#if 1\n#raise ValueError\n#end if\n", |
---|
2534 | "") |
---|
2535 | self.failUnlessRaises(ValueError, test) |
---|
2536 | |
---|
2537 | |
---|
2538 | def test3(self): |
---|
2539 | """#raise ValueError in #if block |
---|
2540 | |
---|
2541 | Shouldn't raise ValueError |
---|
2542 | """ |
---|
2543 | self.verify("#if 0\n#raise ValueError\n#else\nblarg#end if\n", |
---|
2544 | "blarg\n") |
---|
2545 | |
---|
2546 | |
---|
2547 | |
---|
2548 | class ImportDirective(OutputTest): |
---|
2549 | def test1(self): |
---|
2550 | """#import math |
---|
2551 | """ |
---|
2552 | self.verify("#import math", |
---|
2553 | "") |
---|
2554 | |
---|
2555 | def test2(self): |
---|
2556 | """#import math + WS |
---|
2557 | |
---|
2558 | Should gobble |
---|
2559 | """ |
---|
2560 | self.verify(" #import math ", |
---|
2561 | "") |
---|
2562 | |
---|
2563 | def test3(self): |
---|
2564 | """#import math + WS + leading text |
---|
2565 | |
---|
2566 | Shouldn't gobble |
---|
2567 | """ |
---|
2568 | self.verify(" -- #import math ", |
---|
2569 | " -- ") |
---|
2570 | |
---|
2571 | def test4(self): |
---|
2572 | """#from math import syn |
---|
2573 | """ |
---|
2574 | self.verify("#from math import cos", |
---|
2575 | "") |
---|
2576 | |
---|
2577 | def test5(self): |
---|
2578 | """#from math import cos + WS |
---|
2579 | Should gobble |
---|
2580 | """ |
---|
2581 | self.verify(" #from math import cos ", |
---|
2582 | "") |
---|
2583 | |
---|
2584 | def test6(self): |
---|
2585 | """#from math import cos + WS + leading text |
---|
2586 | Shouldn't gobble |
---|
2587 | """ |
---|
2588 | self.verify(" -- #from math import cos ", |
---|
2589 | " -- ") |
---|
2590 | |
---|
2591 | def test7(self): |
---|
2592 | """#from math import cos -- use it |
---|
2593 | """ |
---|
2594 | self.verify("#from math import cos\n$cos(0)", |
---|
2595 | "1.0") |
---|
2596 | |
---|
2597 | def test8(self): |
---|
2598 | """#from math import cos,tan,sin -- and use them |
---|
2599 | """ |
---|
2600 | self.verify("#from math import cos, tan, sin\n$cos(0)-$tan(0)-$sin(0)", |
---|
2601 | "1.0-0.0-0.0") |
---|
2602 | |
---|
2603 | def test9(self): |
---|
2604 | """#import os.path -- use it |
---|
2605 | """ |
---|
2606 | |
---|
2607 | self.verify("#import os.path\n$os.path.exists('.')", |
---|
2608 | repr(True)) |
---|
2609 | |
---|
2610 | def test10(self): |
---|
2611 | """#import os.path -- use it with NameMapper turned off |
---|
2612 | """ |
---|
2613 | self.verify("""## |
---|
2614 | #compiler-settings |
---|
2615 | useNameMapper=False |
---|
2616 | #end compiler-settings |
---|
2617 | #import os.path |
---|
2618 | $os.path.exists('.')""", |
---|
2619 | repr(True)) |
---|
2620 | |
---|
2621 | def test11(self): |
---|
2622 | """#from math import * |
---|
2623 | """ |
---|
2624 | |
---|
2625 | self.verify("#from math import *\n$pow(1,2) $log10(10)", |
---|
2626 | "1.0 1.0") |
---|
2627 | |
---|
2628 | class CompilerDirective(OutputTest): |
---|
2629 | def test1(self): |
---|
2630 | """overriding the commentStartToken |
---|
2631 | """ |
---|
2632 | self.verify("""$anInt##comment |
---|
2633 | #compiler commentStartToken = '//' |
---|
2634 | $anInt//comment |
---|
2635 | """, |
---|
2636 | "1\n1\n") |
---|
2637 | |
---|
2638 | def test2(self): |
---|
2639 | """overriding and resetting the commentStartToken |
---|
2640 | """ |
---|
2641 | self.verify("""$anInt##comment |
---|
2642 | #compiler commentStartToken = '//' |
---|
2643 | $anInt//comment |
---|
2644 | #compiler reset |
---|
2645 | $anInt//comment |
---|
2646 | """, |
---|
2647 | "1\n1\n1//comment\n") |
---|
2648 | |
---|
2649 | |
---|
2650 | class CompilerSettingsDirective(OutputTest): |
---|
2651 | |
---|
2652 | def test1(self): |
---|
2653 | """overriding the cheetahVarStartToken |
---|
2654 | """ |
---|
2655 | self.verify("""$anInt |
---|
2656 | #compiler-settings |
---|
2657 | cheetahVarStartToken = @ |
---|
2658 | #end compiler-settings |
---|
2659 | @anInt |
---|
2660 | #compiler-settings reset |
---|
2661 | $anInt |
---|
2662 | """, |
---|
2663 | "1\n1\n1\n") |
---|
2664 | |
---|
2665 | def test2(self): |
---|
2666 | """overriding the directiveStartToken |
---|
2667 | """ |
---|
2668 | self.verify("""#set $x = 1234 |
---|
2669 | $x |
---|
2670 | #compiler-settings |
---|
2671 | directiveStartToken = @ |
---|
2672 | #end compiler-settings |
---|
2673 | @set $x = 1234 |
---|
2674 | $x |
---|
2675 | """, |
---|
2676 | "1234\n1234\n") |
---|
2677 | |
---|
2678 | def test3(self): |
---|
2679 | """overriding the commentStartToken |
---|
2680 | """ |
---|
2681 | self.verify("""$anInt##comment |
---|
2682 | #compiler-settings |
---|
2683 | commentStartToken = // |
---|
2684 | #end compiler-settings |
---|
2685 | $anInt//comment |
---|
2686 | """, |
---|
2687 | "1\n1\n") |
---|
2688 | |
---|
2689 | if sys.platform.startswith('java'): |
---|
2690 | del CompilerDirective |
---|
2691 | del CompilerSettingsDirective |
---|
2692 | |
---|
2693 | class ExtendsDirective(OutputTest): |
---|
2694 | |
---|
2695 | def test1(self): |
---|
2696 | """#extends Cheetah.Templates._SkeletonPage""" |
---|
2697 | self.verify("""#from Cheetah.Templates._SkeletonPage import _SkeletonPage |
---|
2698 | #extends _SkeletonPage |
---|
2699 | #implements respond |
---|
2700 | $spacer() |
---|
2701 | """, |
---|
2702 | '<img src="spacer.gif" width="1" height="1" alt="" />\n') |
---|
2703 | |
---|
2704 | |
---|
2705 | self.verify("""#from Cheetah.Templates._SkeletonPage import _SkeletonPage |
---|
2706 | #extends _SkeletonPage |
---|
2707 | #implements respond(foo=1234) |
---|
2708 | $spacer()$foo |
---|
2709 | """, |
---|
2710 | '<img src="spacer.gif" width="1" height="1" alt="" />1234\n') |
---|
2711 | |
---|
2712 | def test2(self): |
---|
2713 | """#extends Cheetah.Templates.SkeletonPage without #import""" |
---|
2714 | self.verify("""#extends Cheetah.Templates.SkeletonPage |
---|
2715 | #implements respond |
---|
2716 | $spacer() |
---|
2717 | """, |
---|
2718 | '<img src="spacer.gif" width="1" height="1" alt="" />\n') |
---|
2719 | |
---|
2720 | def test3(self): |
---|
2721 | """#extends Cheetah.Templates.SkeletonPage.SkeletonPage without #import""" |
---|
2722 | self.verify("""#extends Cheetah.Templates.SkeletonPage.SkeletonPage |
---|
2723 | #implements respond |
---|
2724 | $spacer() |
---|
2725 | """, |
---|
2726 | '<img src="spacer.gif" width="1" height="1" alt="" />\n') |
---|
2727 | |
---|
2728 | def test4(self): |
---|
2729 | """#extends with globals and searchList test""" |
---|
2730 | self.verify("""#extends Cheetah.Templates.SkeletonPage |
---|
2731 | #set global g="Hello" |
---|
2732 | #implements respond |
---|
2733 | $g $numOne |
---|
2734 | """, |
---|
2735 | 'Hello 1\n') |
---|
2736 | |
---|
2737 | |
---|
2738 | class SuperDirective(OutputTest): |
---|
2739 | def test1(self): |
---|
2740 | tmpl1 = Template.compile('''$foo $bar(99) |
---|
2741 | #def foo: this is base foo |
---|
2742 | #def bar(arg): super-$arg''') |
---|
2743 | |
---|
2744 | tmpl2 = tmpl1.subclass(''' |
---|
2745 | #implements dummy |
---|
2746 | #def foo |
---|
2747 | #super |
---|
2748 | This is child foo |
---|
2749 | #super(trans=trans) |
---|
2750 | $bar(1234) |
---|
2751 | #end def |
---|
2752 | #def bar(arg): #super($arg) |
---|
2753 | ''') |
---|
2754 | expected = ('this is base foo ' |
---|
2755 | 'This is child foo\nthis is base foo ' |
---|
2756 | 'super-1234\n super-99') |
---|
2757 | assert str(tmpl2()).strip()==expected |
---|
2758 | |
---|
2759 | |
---|
2760 | class ImportantExampleCases(OutputTest): |
---|
2761 | def test1(self): |
---|
2762 | """how to make a comma-delimited list""" |
---|
2763 | self.verify("""#set $sep = '' |
---|
2764 | #for $letter in $letterList |
---|
2765 | $sep$letter#slurp |
---|
2766 | #set $sep = ', ' |
---|
2767 | #end for |
---|
2768 | """, |
---|
2769 | "a, b, c") |
---|
2770 | |
---|
2771 | class FilterDirective(OutputTest): |
---|
2772 | convertEOLs=False |
---|
2773 | |
---|
2774 | def _getCompilerSettings(self): |
---|
2775 | return {'useFilterArgsInPlaceholders':True} |
---|
2776 | |
---|
2777 | def test1(self): |
---|
2778 | """#filter Filter |
---|
2779 | """ |
---|
2780 | self.verify("#filter Filter\n$none#end filter", |
---|
2781 | "") |
---|
2782 | |
---|
2783 | self.verify("#filter Filter: $none", |
---|
2784 | "") |
---|
2785 | |
---|
2786 | def test2(self): |
---|
2787 | """#filter ReplaceNone with WS |
---|
2788 | """ |
---|
2789 | self.verify("#filter Filter \n$none#end filter", |
---|
2790 | "") |
---|
2791 | |
---|
2792 | def test3(self): |
---|
2793 | """#filter MaxLen -- maxlen of 5""" |
---|
2794 | |
---|
2795 | self.verify("#filter MaxLen \n${tenDigits, $maxlen=5}#end filter", |
---|
2796 | "12345") |
---|
2797 | |
---|
2798 | def test4(self): |
---|
2799 | """#filter MaxLen -- no maxlen |
---|
2800 | """ |
---|
2801 | self.verify("#filter MaxLen \n${tenDigits}#end filter", |
---|
2802 | "1234567890") |
---|
2803 | |
---|
2804 | def test5(self): |
---|
2805 | """#filter WebSafe -- basic usage |
---|
2806 | """ |
---|
2807 | self.verify("#filter WebSafe \n$webSafeTest#end filter", |
---|
2808 | "abc <=> &") |
---|
2809 | |
---|
2810 | def test6(self): |
---|
2811 | """#filter WebSafe -- also space |
---|
2812 | """ |
---|
2813 | self.verify("#filter WebSafe \n${webSafeTest, $also=' '}#end filter", |
---|
2814 | "abc <=> &") |
---|
2815 | |
---|
2816 | def test7(self): |
---|
2817 | """#filter WebSafe -- also space, without $ on the args |
---|
2818 | """ |
---|
2819 | self.verify("#filter WebSafe \n${webSafeTest, also=' '}#end filter", |
---|
2820 | "abc <=> &") |
---|
2821 | |
---|
2822 | def test8(self): |
---|
2823 | """#filter Strip -- trailing newline |
---|
2824 | """ |
---|
2825 | self.verify("#filter Strip\n$strip1#end filter", |
---|
2826 | "strippable whitespace\n") |
---|
2827 | |
---|
2828 | def test9(self): |
---|
2829 | """#filter Strip -- no trailing newine |
---|
2830 | """ |
---|
2831 | self.verify("#filter Strip\n$strip2#end filter", |
---|
2832 | "strippable whitespace") |
---|
2833 | |
---|
2834 | def test10(self): |
---|
2835 | """#filter Strip -- multi-line |
---|
2836 | """ |
---|
2837 | self.verify("#filter Strip\n$strip3#end filter", |
---|
2838 | "strippable whitespace\n1 2 3\n") |
---|
2839 | |
---|
2840 | def test11(self): |
---|
2841 | """#filter StripSqueeze -- canonicalize all whitespace to ' ' |
---|
2842 | """ |
---|
2843 | self.verify("#filter StripSqueeze\n$strip3#end filter", |
---|
2844 | "strippable whitespace 1 2 3") |
---|
2845 | |
---|
2846 | |
---|
2847 | class EchoDirective(OutputTest): |
---|
2848 | def test1(self): |
---|
2849 | """#echo 1234 |
---|
2850 | """ |
---|
2851 | self.verify("#echo 1234", |
---|
2852 | "1234") |
---|
2853 | |
---|
2854 | class SilentDirective(OutputTest): |
---|
2855 | def test1(self): |
---|
2856 | """#silent 1234 |
---|
2857 | """ |
---|
2858 | self.verify("#silent 1234", |
---|
2859 | "") |
---|
2860 | |
---|
2861 | class ErrorCatcherDirective(OutputTest): |
---|
2862 | pass |
---|
2863 | |
---|
2864 | |
---|
2865 | class VarExists(OutputTest): # Template.varExists() |
---|
2866 | |
---|
2867 | def test1(self): |
---|
2868 | """$varExists('$anInt') |
---|
2869 | """ |
---|
2870 | self.verify("$varExists('$anInt')", |
---|
2871 | repr(True)) |
---|
2872 | |
---|
2873 | def test2(self): |
---|
2874 | """$varExists('anInt') |
---|
2875 | """ |
---|
2876 | self.verify("$varExists('anInt')", |
---|
2877 | repr(True)) |
---|
2878 | |
---|
2879 | def test3(self): |
---|
2880 | """$varExists('$anInt') |
---|
2881 | """ |
---|
2882 | self.verify("$varExists('$bogus')", |
---|
2883 | repr(False)) |
---|
2884 | |
---|
2885 | def test4(self): |
---|
2886 | """$varExists('$anInt') combined with #if false |
---|
2887 | """ |
---|
2888 | self.verify("#if $varExists('$bogus')\n1234\n#else\n999\n#end if", |
---|
2889 | "999\n") |
---|
2890 | |
---|
2891 | def test5(self): |
---|
2892 | """$varExists('$anInt') combined with #if true |
---|
2893 | """ |
---|
2894 | self.verify("#if $varExists('$anInt')\n1234\n#else\n999#end if", |
---|
2895 | "1234\n") |
---|
2896 | |
---|
2897 | class GetVar(OutputTest): # Template.getVar() |
---|
2898 | def test1(self): |
---|
2899 | """$getVar('$anInt') |
---|
2900 | """ |
---|
2901 | self.verify("$getVar('$anInt')", |
---|
2902 | "1") |
---|
2903 | |
---|
2904 | def test2(self): |
---|
2905 | """$getVar('anInt') |
---|
2906 | """ |
---|
2907 | self.verify("$getVar('anInt')", |
---|
2908 | "1") |
---|
2909 | |
---|
2910 | def test3(self): |
---|
2911 | """$self.getVar('anInt') |
---|
2912 | """ |
---|
2913 | self.verify("$self.getVar('anInt')", |
---|
2914 | "1") |
---|
2915 | |
---|
2916 | def test4(self): |
---|
2917 | """$getVar('bogus', 1234) |
---|
2918 | """ |
---|
2919 | self.verify("$getVar('bogus', 1234)", |
---|
2920 | "1234") |
---|
2921 | |
---|
2922 | def test5(self): |
---|
2923 | """$getVar('$bogus', 1234) |
---|
2924 | """ |
---|
2925 | self.verify("$getVar('$bogus', 1234)", |
---|
2926 | "1234") |
---|
2927 | |
---|
2928 | |
---|
2929 | class MiscComplexSyntax(OutputTest): |
---|
2930 | def test1(self): |
---|
2931 | """Complex use of {},[] and () in a #set expression |
---|
2932 | ---- |
---|
2933 | #set $c = {'A':0}[{}.get('a', {'a' : 'A'}['a'])] |
---|
2934 | $c |
---|
2935 | """ |
---|
2936 | self.verify("#set $c = {'A':0}[{}.get('a', {'a' : 'A'}['a'])]\n$c", |
---|
2937 | "0") |
---|
2938 | |
---|
2939 | |
---|
2940 | class CGI(OutputTest): |
---|
2941 | """CGI scripts with(out) the CGI environment and with(out) GET variables. |
---|
2942 | """ |
---|
2943 | convertEOLs=False |
---|
2944 | |
---|
2945 | def _beginCGI(self): |
---|
2946 | os.environ['REQUEST_METHOD'] = "GET" |
---|
2947 | def _endCGI(self): |
---|
2948 | try: |
---|
2949 | del os.environ['REQUEST_METHOD'] |
---|
2950 | except KeyError: |
---|
2951 | pass |
---|
2952 | _guaranteeNoCGI = _endCGI |
---|
2953 | |
---|
2954 | |
---|
2955 | def test1(self): |
---|
2956 | """A regular template.""" |
---|
2957 | self._guaranteeNoCGI() |
---|
2958 | source = "#extends Cheetah.Tools.CGITemplate\n" + \ |
---|
2959 | "#implements respond\n" + \ |
---|
2960 | "$cgiHeaders#slurp\n" + \ |
---|
2961 | "Hello, world!" |
---|
2962 | self.verify(source, "Hello, world!") |
---|
2963 | |
---|
2964 | |
---|
2965 | def test2(self): |
---|
2966 | """A CGI script.""" |
---|
2967 | self._beginCGI() |
---|
2968 | source = "#extends Cheetah.Tools.CGITemplate\n" + \ |
---|
2969 | "#implements respond\n" + \ |
---|
2970 | "$cgiHeaders#slurp\n" + \ |
---|
2971 | "Hello, world!" |
---|
2972 | self.verify(source, "Content-type: text/html\n\nHello, world!") |
---|
2973 | self._endCGI() |
---|
2974 | |
---|
2975 | |
---|
2976 | def test3(self): |
---|
2977 | """A (pseudo) Webware servlet. |
---|
2978 | |
---|
2979 | This uses the Python syntax escape to set |
---|
2980 | self._CHEETAH__isControlledByWebKit. |
---|
2981 | We could instead do '#silent self._CHEETAH__isControlledByWebKit = True', |
---|
2982 | taking advantage of the fact that it will compile unchanged as long |
---|
2983 | as there's no '$' in the statement. (It won't compile with an '$' |
---|
2984 | because that would convert to a function call, and you can't assign |
---|
2985 | to a function call.) Because this isn't really being called from |
---|
2986 | Webware, we'd better not use any Webware services! Likewise, we'd |
---|
2987 | better not call $cgiImport() because it would be misled. |
---|
2988 | """ |
---|
2989 | self._beginCGI() |
---|
2990 | source = "#extends Cheetah.Tools.CGITemplate\n" + \ |
---|
2991 | "#implements respond\n" + \ |
---|
2992 | "<% self._CHEETAH__isControlledByWebKit = True %>#slurp\n" + \ |
---|
2993 | "$cgiHeaders#slurp\n" + \ |
---|
2994 | "Hello, world!" |
---|
2995 | self.verify(source, "Hello, world!") |
---|
2996 | self._endCGI() |
---|
2997 | |
---|
2998 | |
---|
2999 | def test4(self): |
---|
3000 | """A CGI script with a GET variable.""" |
---|
3001 | self._beginCGI() |
---|
3002 | os.environ['QUERY_STRING'] = "cgiWhat=world" |
---|
3003 | source = "#extends Cheetah.Tools.CGITemplate\n" + \ |
---|
3004 | "#implements respond\n" + \ |
---|
3005 | "$cgiHeaders#slurp\n" + \ |
---|
3006 | "#silent $webInput(['cgiWhat'])##slurp\n" + \ |
---|
3007 | "Hello, $cgiWhat!" |
---|
3008 | self.verify(source, |
---|
3009 | "Content-type: text/html\n\nHello, world!") |
---|
3010 | del os.environ['QUERY_STRING'] |
---|
3011 | self._endCGI() |
---|
3012 | |
---|
3013 | |
---|
3014 | |
---|
3015 | class WhitespaceAfterDirectiveTokens(OutputTest): |
---|
3016 | def _getCompilerSettings(self): |
---|
3017 | return {'allowWhitespaceAfterDirectiveStartToken':True} |
---|
3018 | |
---|
3019 | def test1(self): |
---|
3020 | self.verify("# for i in range(10): $i", |
---|
3021 | "0123456789") |
---|
3022 | self.verify("# for i in range(10)\n$i# end for", |
---|
3023 | "0123456789") |
---|
3024 | self.verify("# for i in range(10)#$i#end for", |
---|
3025 | "0123456789") |
---|
3026 | |
---|
3027 | |
---|
3028 | |
---|
3029 | class DefmacroDirective(OutputTest): |
---|
3030 | def _getCompilerSettings(self): |
---|
3031 | def aMacro(src): |
---|
3032 | return '$aStr' |
---|
3033 | |
---|
3034 | return {'macroDirectives':{'aMacro':aMacro |
---|
3035 | }} |
---|
3036 | |
---|
3037 | def test1(self): |
---|
3038 | self.verify("""\ |
---|
3039 | #defmacro inc: #set @src +=1 |
---|
3040 | #set i = 1 |
---|
3041 | #inc: $i |
---|
3042 | $i""", |
---|
3043 | "2") |
---|
3044 | |
---|
3045 | |
---|
3046 | |
---|
3047 | self.verify("""\ |
---|
3048 | #defmacro test |
---|
3049 | #for i in range(10): @src |
---|
3050 | #end defmacro |
---|
3051 | #test: $i-foo#slurp |
---|
3052 | #for i in range(3): $i""", |
---|
3053 | "0-foo1-foo2-foo3-foo4-foo5-foo6-foo7-foo8-foo9-foo012") |
---|
3054 | |
---|
3055 | self.verify("""\ |
---|
3056 | #defmacro test |
---|
3057 | #for i in range(10): @src |
---|
3058 | #end defmacro |
---|
3059 | #test: $i-foo |
---|
3060 | #for i in range(3): $i""", |
---|
3061 | "0-foo\n1-foo\n2-foo\n3-foo\n4-foo\n5-foo\n6-foo\n7-foo\n8-foo\n9-foo\n012") |
---|
3062 | |
---|
3063 | |
---|
3064 | self.verify("""\ |
---|
3065 | #defmacro test: #for i in range(10): @src |
---|
3066 | #test: $i-foo#slurp |
---|
3067 | -#for i in range(3): $i""", |
---|
3068 | "0-foo1-foo2-foo3-foo4-foo5-foo6-foo7-foo8-foo9-foo-012") |
---|
3069 | |
---|
3070 | self.verify("""\ |
---|
3071 | #defmacro test##for i in range(10): @src#end defmacro##slurp |
---|
3072 | #test: $i-foo#slurp |
---|
3073 | -#for i in range(3): $i""", |
---|
3074 | "0-foo1-foo2-foo3-foo4-foo5-foo6-foo7-foo8-foo9-foo-012") |
---|
3075 | |
---|
3076 | self.verify("""\ |
---|
3077 | #defmacro testFoo: nothing |
---|
3078 | #defmacro test(foo=1234): #for i in range(10): @src |
---|
3079 | #test foo=234: $i-foo#slurp |
---|
3080 | -#for i in range(3): $i""", |
---|
3081 | "0-foo1-foo2-foo3-foo4-foo5-foo6-foo7-foo8-foo9-foo-012") |
---|
3082 | |
---|
3083 | self.verify("""\ |
---|
3084 | #defmacro testFoo: nothing |
---|
3085 | #defmacro test(foo=1234): #for i in range(10): @src@foo |
---|
3086 | #test foo='-foo'#$i#end test#-#for i in range(3): $i""", |
---|
3087 | "0-foo1-foo2-foo3-foo4-foo5-foo6-foo7-foo8-foo9-foo-012") |
---|
3088 | |
---|
3089 | self.verify("""\ |
---|
3090 | #defmacro testFoo: nothing |
---|
3091 | #defmacro test(foo=1234): #for i in range(10): @src.strip()@foo |
---|
3092 | #test foo='-foo': $i |
---|
3093 | -#for i in range(3): $i""", |
---|
3094 | "0-foo1-foo2-foo3-foo4-foo5-foo6-foo7-foo8-foo9-foo-012") |
---|
3095 | |
---|
3096 | def test2(self): |
---|
3097 | self.verify("#aMacro: foo", |
---|
3098 | "blarg") |
---|
3099 | self.verify("#defmacro nested: @macros.aMacro(@src)\n#nested: foo", |
---|
3100 | "blarg") |
---|
3101 | |
---|
3102 | |
---|
3103 | class Indenter(OutputTest): |
---|
3104 | convertEOLs=False |
---|
3105 | |
---|
3106 | source = """ |
---|
3107 | public class X |
---|
3108 | { |
---|
3109 | #for $method in $methods |
---|
3110 | $getMethod($method) |
---|
3111 | |
---|
3112 | #end for |
---|
3113 | } |
---|
3114 | //end of class |
---|
3115 | |
---|
3116 | #def getMethod($method) |
---|
3117 | #indent ++ |
---|
3118 | public $getType($method) ${method.Name}($getParams($method.Params)); |
---|
3119 | #indent -- |
---|
3120 | #end def |
---|
3121 | |
---|
3122 | #def getParams($params) |
---|
3123 | #indent off |
---|
3124 | |
---|
3125 | #for $counter in $range($len($params)) |
---|
3126 | #if $counter == len($params) - 1 |
---|
3127 | $params[$counter]#slurp |
---|
3128 | #else: |
---|
3129 | $params[$counter], |
---|
3130 | #end if |
---|
3131 | #end for |
---|
3132 | #indent on |
---|
3133 | #end def |
---|
3134 | |
---|
3135 | #def getType($method) |
---|
3136 | #indent push |
---|
3137 | #indent=0 |
---|
3138 | #if $method.Type == "VT_VOID" |
---|
3139 | void#slurp |
---|
3140 | #elif $method.Type == "VT_INT" |
---|
3141 | int#slurp |
---|
3142 | #elif $method.Type == "VT_VARIANT" |
---|
3143 | Object#slurp |
---|
3144 | #end if |
---|
3145 | #indent pop |
---|
3146 | #end def |
---|
3147 | """ |
---|
3148 | |
---|
3149 | control = """ |
---|
3150 | public class X |
---|
3151 | { |
---|
3152 | public void Foo( |
---|
3153 | _input, |
---|
3154 | _output); |
---|
3155 | |
---|
3156 | |
---|
3157 | public int Bar( |
---|
3158 | _str1, |
---|
3159 | str2, |
---|
3160 | _str3); |
---|
3161 | |
---|
3162 | |
---|
3163 | public Object Add( |
---|
3164 | value1, |
---|
3165 | value); |
---|
3166 | |
---|
3167 | |
---|
3168 | } |
---|
3169 | //end of class |
---|
3170 | |
---|
3171 | |
---|
3172 | |
---|
3173 | """ |
---|
3174 | def _getCompilerSettings(self): |
---|
3175 | return {'useFilterArgsInPlaceholders':True} |
---|
3176 | |
---|
3177 | def searchList(self): # Inside Indenter class. |
---|
3178 | class Method: |
---|
3179 | def __init__(self, _name, _type, *_params): |
---|
3180 | self.Name = _name |
---|
3181 | self.Type = _type |
---|
3182 | self.Params = _params |
---|
3183 | methods = [Method("Foo", "VT_VOID", "_input", "_output"), |
---|
3184 | Method("Bar", "VT_INT", "_str1", "str2", "_str3"), |
---|
3185 | Method("Add", "VT_VARIANT", "value1", "value")] |
---|
3186 | return [{"methods": methods}] |
---|
3187 | |
---|
3188 | def test1(self): # Inside Indenter class. |
---|
3189 | self.verify(self.source, self.control) |
---|
3190 | |
---|
3191 | |
---|
3192 | ################################################## |
---|
3193 | ## CREATE CONVERTED EOL VERSIONS OF THE TEST CASES |
---|
3194 | |
---|
3195 | if OutputTest._useNewStyleCompilation and versionTuple >= (2,3): |
---|
3196 | extraCompileKwArgsForDiffBaseclass = {'baseclass':dict} |
---|
3197 | else: |
---|
3198 | extraCompileKwArgsForDiffBaseclass = {'baseclass':object} |
---|
3199 | |
---|
3200 | |
---|
3201 | def install_eols(): |
---|
3202 | klasses = [v for v in globals().values() if isinstance(v, (types.ClassType, types.TypeType)) and issubclass(v, unittest.TestCase)] |
---|
3203 | for klass in klasses: |
---|
3204 | name = klass.__name__ |
---|
3205 | if hasattr(klass,'convertEOLs') and klass.convertEOLs: |
---|
3206 | win32Src = r"class %(name)s_Win32EOL(%(name)s): _EOLreplacement = '\r\n'"%locals() |
---|
3207 | macSrc = r"class %(name)s_MacEOL(%(name)s): _EOLreplacement = '\r'"%locals() |
---|
3208 | exec win32Src in globals() |
---|
3209 | exec macSrc in globals() |
---|
3210 | |
---|
3211 | if versionTuple >= (2,3): |
---|
3212 | src = r"class %(name)s_DiffBaseClass(%(name)s): "%locals() |
---|
3213 | src += " _extraCompileKwArgs = extraCompileKwArgsForDiffBaseclass" |
---|
3214 | exec src in globals() |
---|
3215 | |
---|
3216 | del name |
---|
3217 | del klass |
---|
3218 | |
---|
3219 | ################################################## |
---|
3220 | ## if run from the command line ## |
---|
3221 | |
---|
3222 | if __name__ == '__main__': |
---|
3223 | install_eols() |
---|
3224 | unittest.main() |
---|
3225 | |
---|
3226 | # vim: shiftwidth=4 tabstop=4 expandtab |
---|