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