1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
|
# Copyright 2008-2014 Jaap Karssenberg <jaap.karssenberg@gmail.com>
'''Test cases for the zim.templates module.'''
import tests
from zim.base import MovingWindowIter
from zim.newfs import FileNotFoundError
from zim.parse.simpletree import SimpleTreeElement, SimpleTreeBuilder
from zim.templates import *
from zim.templates.parser import *
from zim.templates.expression import *
from zim.templates.expressionparser import *
from zim.templates.processor import *
E = SimpleTreeElement
class TestExpressionParser(tests.TestCase):
def runTest(self):
## Test atoms
p = ExpressionParser()
for text, wanted in (
('True', ExpressionLiteral(True)),
('False', ExpressionLiteral(False)),
('None', ExpressionLiteral(None)),
('"foo\\tbar"', ExpressionLiteral("foo\tbar")),
('123', ExpressionLiteral(123)),
('1.2', ExpressionLiteral(1.2)),
('1E+3', ExpressionLiteral(1E+3)),
('x', ExpressionParameter('x')),
('foo.bar', ExpressionParameter('foo.bar')),
):
self.assertEqual(p.parse(text), wanted)
## Test compound expressions
p = ExpressionParser()
for text, wanted in (
('x or y', ExpressionOperator(
operator.or_,
ExpressionParameter('x'),
ExpressionParameter('y')
)),
('x == y', ExpressionOperator(
operator.eq,
ExpressionParameter('x'),
ExpressionParameter('y')
)),
('not x', ExpressionUnaryOperator(
operator.not_,
ExpressionParameter('x')
)),
('[1, a, True]', ExpressionList([
ExpressionLiteral(1),
ExpressionParameter('a'),
ExpressionLiteral(True),
])),
('[[1, a], [True, False]]', ExpressionList([
ExpressionList([
ExpressionLiteral(1),
ExpressionParameter('a'),
]),
ExpressionList([
ExpressionLiteral(True),
ExpressionLiteral(False),
])
])),
('func(1, a)', ExpressionFunctionCall(
ExpressionParameter('func'),
ExpressionList([
ExpressionLiteral(1),
ExpressionParameter('a'),
])
)),
('func([1, a])', ExpressionFunctionCall(
ExpressionParameter('func'),
ExpressionList([
ExpressionList([
ExpressionLiteral(1),
ExpressionParameter('a'),
])
])
)),
('func(1, func(a))', ExpressionFunctionCall(
ExpressionParameter('func'),
ExpressionList([
ExpressionLiteral(1),
ExpressionFunctionCall(
ExpressionParameter('func'),
ExpressionList([
ExpressionParameter('a'),
])
)
])
)),
('[func(1, a), x == y]', ExpressionList([
ExpressionFunctionCall(
ExpressionParameter('func'),
ExpressionList([
ExpressionLiteral(1),
ExpressionParameter('a'),
])
),
ExpressionOperator(
operator.eq,
ExpressionParameter('x'),
ExpressionParameter('y')
)
])),
):
self.assertEqual(p.parse(text), wanted)
## Test operator precedence
expr = ExpressionParser().parse('a or b and not c < d and f or x')
# Read as: '(a or ((b and ((not (c < d)) and f)) or x))'
wanted = ExpressionOperator(
operator.or_,
ExpressionParameter('a'),
ExpressionOperator(
operator.or_,
ExpressionOperator(
operator.and_,
ExpressionParameter('b'),
ExpressionOperator(
operator.and_,
ExpressionUnaryOperator(
operator.not_,
ExpressionOperator(
operator.lt,
ExpressionParameter('c'),
ExpressionParameter('d')
)
),
ExpressionParameter('f')
)
),
ExpressionParameter('x')
)
)
#~ print('\nEXPRESSION:', expr)
self.assertEqual(expr, wanted)
## Invalid syntaxes
p = ExpressionParser()
for t in (
'x > y > z', # chaining comparison operators not allowed
'x > not y', # 'not' has higher precendence, can not appear here
'not not x', # double operator
'x and and y', # double operator
'[x,,y]', # double "," - missing element
'(1,2)', # Tuple not supported
'1 2', # Two expressions, instead of one
'1, 2', # Two expressions, instead of one
'1.2.3', # Invalid literal
'<>', # just an operator
'', # empty expression has no meaning
):
self.assertRaises(ExpressionSyntaxError, p.parse, t)
# TODO check for meaningfull error messages for these
# TODO any edge cases ?
class TestExpression(tests.TestCase):
def runTest(self):
expr = ExpressionList([
ExpressionLiteral('foooo'),
ExpressionParameter('foo'),
ExpressionParameter('a.b'),
ExpressionOperator(
operator.le,
ExpressionParameter('n'),
ExpressionLiteral(2)
),
ExpressionFunctionCall(
ExpressionParameter('addone'),
ExpressionList([
ExpressionParameter('n')
])
),
])
result = expr({
'foo': 'FOO',
'a': {
'b': 'BAR'
},
'n': 1,
'addone': ExpressionFunction(lambda a: a + 1)
})
wanted = ['foooo', 'FOO', 'BAR', True, 2]
self.assertEqual(result, wanted)
class TestExpressionFunctionCall(tests.TestCase):
def runTest(self):
class Foo(object):
def __init__(self, prefix):
self.prefix = prefix
@ExpressionFunction
def string(self, string):
return self.prefix + string
# Test ExpressionFunction works as decorator (bound method)
foo = Foo('FOO')
self.assertIsInstance(foo.string, ExpressionFunction)
self.assertEqual(foo.string('bar'), 'FOObar')
# Test get builtin from dict
mydict = {
'len': ExpressionFunction(lambda o: len(o)),
'mylist': ['a', 'b', 'c'],
}
args = ExpressionList([ExpressionParameter('mylist')])
var = ExpressionParameter('len')
func = ExpressionFunctionCall(var, args)
self.assertEqual(func(mydict), 3)
# Test get object method from attr
mydict = {'foo': foo}
args = ExpressionList([ExpressionLiteral('BAR')])
var = ExpressionParameter('foo.string')
func = ExpressionFunctionCall(var, args)
self.assertEqual(func(mydict), 'FOOBAR')
# Test implicit types
mydict = {
'somedict': {'a': 'AAA', 'b': 'BBB', 'c': 'CCC'},
'somelist': ['x', 'y', 'z'],
'somestring': 'FOOBAR',
}
args = ExpressionList() # empty args
for name, wanted in (
('somedict.sorted', ['a', 'b', 'c']),
('somelist.len', 3),
('somestring.lower', 'foobar'),
('somedict.b.lower', 'bbb'),
('somelist.1.upper', 'Y'),
):
var = ExpressionParameter(name)
func = ExpressionFunctionCall(var, args)
self.assertEqual(func(mydict), wanted)
class TestExpressionObjects(tests.TestCase):
def runTest(self):
# Test proper object type for attributes
for obj in (
ExpressionStringObject('foo'),
ExpressionDictObject({'foo': 'bar'}),
ExpressionListObject(['a', 'b', 'c']),
):
for name in obj._fmethods:
self.assertTrue(hasattr(obj, name))
function = getattr(obj, name)
self.assertIsInstance(function, ExpressionFunction)
# Test getitem, iter, len, str
# and one or two functions of each type
data = {'a': 'b', 'c': 'd', 'e': 'f'}
mydict = ExpressionDictObject(data)
self.assertEqual(mydict['c'], data['c'])
self.assertEqual(list(mydict), list(data))
self.assertEqual(len(mydict), len(data))
self.assertEqual(str(mydict), str(data))
self.assertEqual(mydict.get('c'), data.get('c'))
mylist = ExpressionListObject(['a', 'b', 'c'])
self.assertEqual(mylist[1], 'b')
self.assertEqual(mylist.get(1), 'b')
self.assertIsNone(mylist.get(5))
mystring = ExpressionStringObject('foo')
self.assertEqual(mystring.upper(), "FOO")
class TestTemplateBuilderTextBuffer(tests.TestCase):
def runTest(self):
builder = SimpleTreeBuilder()
buffer = TemplateBuilderTextBuffer(builder)
buffer.start('FOO')
buffer.text('foo\n\t\t')
buffer.rstrip()
buffer.append('BAR')
buffer.lstrip()
buffer.text(' \n\n\t\tdus\n\n')
buffer.rstrip()
buffer.append('BAR')
buffer.lstrip()
buffer.text('\n')
buffer.end('FOO')
result = builder.get_root()
#~ print result
self.assertEqual(result,
E('FOO', None, [
'foo',
E('BAR', None, []),
'\n\t\tdus\n',
E('BAR', None, []),
])
)
class TestTemplateParser(tests.TestCase):
# Include all elements recognized by parser and various forms
# of whitespace stripping, no need to excersize all expressions
# - ExpressionParser is tested separately
TEMPLATE = '''\
[% foo %]
[% GET foo %]
[% bar = "test" %]
[% SET bar = "test" %]
<!--[% IF foo %]-->
DO SOMETHING
<!--[% ELIF foo -%]-->
SOMETHING ELSE
[%- ELSE %]
YET SOMETHING ELSE
[% END %]
Switch: [% IF foo %]AAA[% ELSE %]BBB[% END %]
[% BLOCK bar -%]
BAR
[% END %]
[% FOR a IN b %]
AAA
[% END %]
[% FOREACH a IN b %]
AAA
[% END %]
[% FOREACH a = b %]
AAA
[% END %]
<!--[% BLOCK foo %]-->
FOO
<!--[% END %]-->
'''
WANTED = E('TEMPLATE', None, [
E('MAIN', None, [
E('GET', {'expr': ExpressionParameter('foo')}, []),
'\n', # whitespace around GET remains intact
E('GET', {'expr': ExpressionParameter('foo')}, []),
'\n',
E('SET', {
'var': ExpressionParameter('bar'),
'expr': ExpressionLiteral('test')
}, []), # no whitespace here - SET chomps
E('SET', {
'var': ExpressionParameter('bar'),
'expr': ExpressionLiteral('test')
}, []),
'\n', # only one "\n" here!
# no indenting before block level items like IF
E('IF', {'expr': ExpressionParameter('foo')}, [
'\tDO SOMETHING\n' # indenting intact
]),
E('ELIF', {'expr': ExpressionParameter('foo')}, [
'SOMETHING ELSE' # stripped on both sides
]),
E('ELSE', None, [
'\tYET SOMETHING ELSE\n' # indenting intact
]),
'\nSwitch:\t',
E('IF', {'expr': ExpressionParameter('foo')}, [
'AAA'
]),
E('ELSE', None, [
'BBB'
]),
'\n\n', # two "\n" here because IF .. ELSE is inline
'\n', # another empty line after block is taken out
# 3 times same loop by different syntax
E('FOR', {
'var': ExpressionParameter('a'),
'expr': ExpressionParameter('b'),
}, [
'\tAAA\n'
]),
'\n',
E('FOR', {
'var': ExpressionParameter('a'),
'expr': ExpressionParameter('b'),
}, [
'\tAAA\n'
]),
'\n',
E('FOR', {
'var': ExpressionParameter('a'),
'expr': ExpressionParameter('b'),
}, [
'\tAAA\n'
]),
'\n',
]),
E('BLOCK', {'name': 'bar'}, ['BAR\n']),
# indenting before "[% BLOCK .." and before "BAR" both gone
E('BLOCK', {'name': 'foo'}, ['\tFOO\n']),
# indenting intact
])
def runTest(self):
parser = TemplateParser()
root = parser.parse(self.TEMPLATE)
#~ print root
self.assertEqual(root, self.WANTED)
# TODO Test exceptions
# - invalid expression
# - lower case keyword
# - invalide sequence IF / ELSE
class TestTemplateContextDict(tests.TestCase):
def runTest(self):
data = {'a': 'AAA', 'b': 'BBB', 'c': 'CCC'}
context = TemplateContextDict(data)
for name in context._fmethods:
func = getattr(context, name)
self.assertIsInstance(func, ExpressionFunction)
# make sure we can use as regular dict
context['d'] = 'DDD'
self.assertEqual(context.pop('d'), 'DDD')
class TestTemplateLoopState(tests.TestCase):
def runTest(self):
items = ['aaa', 'bbb', 'ccc']
loop = TemplateLoopState(len(items), None)
myiter = MovingWindowIter(items)
for i, stateitems in enumerate(myiter):
loop._update(i, myiter)
self.assertEqual(loop.size, 3)
self.assertEqual(loop.max, 2)
self.assertEqual(loop.prev, None if i == 0 else items[i - 1])
self.assertEqual(loop.current, items[i])
self.assertEqual(loop.next, None if i == 2 else items[i + 1])
self.assertEqual(loop.index, i)
self.assertEqual(loop.count, i + 1)
self.assertEqual(loop.first, True if i == 0 else False)
self.assertEqual(loop.last, True if i == 2 else False)
self.assertEqual(loop.parity, 'odd' if i % 2 else 'even')
self.assertEqual(loop.even, False if i % 2 else True)
self.assertEqual(loop.odd, True if i % 2 else False)
self.assertEqual(i, 2)
class TestTemplateProcessor(tests.TestCase):
def testGetSet(self):
# test 'GET', 'SET'
processor = TemplateProcessor(
E('TEMPLATE', None, [
E('MAIN', None, [
E('SET', {
'var': ExpressionParameter('aaa.bbb'),
'expr': ExpressionLiteral('foo')
}),
E('GET', {'expr': ExpressionParameter('aaa.bbb')}),
])
])
)
output = []
context = TemplateContextDict({'aaa': TemplateContextDict({})})
processor.process(output, context)
self.assertEqual(output, ['foo'])
output = []
context = TemplateContextDict({'aaa': {}})
with self.assertRaises(AssertionError):
processor.process(output, context)
def testIfElifElse(self):
# test 'IF', 'ELIF', 'ELSE',
processor = TemplateProcessor(
E('TEMPLATE', None, [
E('MAIN', None, [
E('IF', {'expr': ExpressionParameter('a')}, ['A']),
E('ELIF', {'expr': ExpressionParameter('b')}, ['B']),
E('ELIF', {'expr': ExpressionParameter('c')}, ['C']),
E('ELSE', {}, ['D']),
])
])
)
for context, wanted in (
({'a': True}, ['A']),
({'a': False, 'b': True}, ['B']),
({'a': False, 'b': False, 'c': True}, ['C']),
({'a': False, 'b': False, 'c': False}, ['D']),
):
lines = []
processor.process(lines, TemplateContextDict(context))
self.assertEqual(lines, wanted)
def testFor(self):
# test 'FOR'
processor = TemplateProcessor(
E('TEMPLATE', None, [
E('MAIN', None, [
E('FOR', {
'var': ExpressionParameter('iter'),
'expr': ExpressionParameter('items'),
}, [
E('GET', {'expr': ExpressionParameter('loop.count')}),
': ',
E('GET', {'expr': ExpressionParameter('iter')}),
'\n',
])
])
])
)
context = {'items': ['aaa', 'bbb', 'ccc']}
lines = []
processor.process(lines, TemplateContextDict(context))
self.assertEqual(''.join(lines), '1: aaa\n2: bbb\n3: ccc\n')
def testIncludeName(self):
# test 'INCLUDE name',
# parameter "foo" in the context is ignored
processor = TemplateProcessor(
E('TEMPLATE', None, [
E('MAIN', None, [
E('INCLUDE', {'expr': ExpressionParameter('foo')}),
E('INCLUDE', {'expr': ExpressionParameter('foo')}),
E('INCLUDE', {'expr': ExpressionParameter('foo')}),
]),
E('BLOCK', {'name': 'foo'}, 'FOO\n'),
])
)
lines = []
processor.process(lines, TemplateContextDict({'foo': 'bar'}))
self.assertEqual(''.join(lines), 'FOO\nFOO\nFOO\n')
def testIncludeNameExpr(self):
# test 'INCLUDE expression' where expression evals to name
# parameter "foo" points to block "bar"
processor = TemplateProcessor(
E('TEMPLATE', None, [
E('MAIN', None, [
E('INCLUDE', {'expr': ExpressionParameter('foo')}),
E('INCLUDE', {'expr': ExpressionParameter('foo')}),
E('INCLUDE', {'expr': ExpressionParameter('foo')}),
]),
E('BLOCK', {'name': 'bar'}, 'FOO\n'),
])
)
lines = []
processor.process(lines, TemplateContextDict({'foo': 'bar'}))
self.assertEqual(''.join(lines), 'FOO\nFOO\nFOO\n')
def testIncludePath(self):
# test 'INCLUDE path'
def parse_included_file_func(path):
self.assertEqual(path, 'include.txt')
return E('TEMPLATE', None, [
E('MAIN', None, ['INCLUDED TEXT\n'])
])
processor = TemplateProcessor(
E('TEMPLATE', None, [
E('MAIN', None, [
E('INCLUDE', {'expr': ExpressionLiteral('include.txt')}),
]),
]), parse_included_file_func=parse_included_file_func)
lines = []
processor.process(lines, TemplateContextDict({}))
self.assertEqual(''.join(lines), 'INCLUDED TEXT\n')
def testIncludePathExpr(self):
# test 'INCLUDE expression' where expression evals to path
def parse_included_file_func(path):
self.assertEqual(path, 'include.txt')
return E('TEMPLATE', None, [
E('MAIN', None, ['INCLUDED TEXT\n'])
])
processor = TemplateProcessor(
E('TEMPLATE', None, [
E('MAIN', None, [
E('INCLUDE', {'expr': ExpressionParameter('path')}),
]),
]), parse_included_file_func=parse_included_file_func)
lines = []
processor.process(lines, TemplateContextDict({'path': 'include.txt'}))
self.assertEqual(''.join(lines), 'INCLUDED TEXT\n')
class TestTemplateList(tests.TestCase):
def runTest(self):
categories = list_template_categories()
self.assertIn('html', categories)
self.assertIn('wiki', categories)
for cat in categories:
templates = list_templates(cat)
#~ print('>>', cat, templates)
self.assertGreater(len(templates), 0)
for name, filename in templates:
template = get_template(cat, name)
self.assertIsInstance(template, Template)
class TestTemplateFunctions(tests.TestCase):
def testFuncLen(self):
func = build_template_functions()['len']
self.assertIsInstance(func, ExpressionFunction)
self.assertEqual(
func([1, 2, 3]),
3
)
def testFuncSorted(self):
func = build_template_functions()['sorted']
self.assertIsInstance(func, ExpressionFunction)
self.assertEqual(
func(['bbb', 'aaa', 'ccc']),
['aaa', 'bbb', 'ccc']
)
def testFuncReversed(self):
func = build_template_functions()['reversed']
self.assertIsInstance(func, ExpressionFunction)
self.assertEqual(
func(['bbb', 'aaa', 'ccc']),
['ccc', 'aaa', 'bbb']
)
def testFuncRange(self):
func = build_template_functions()['range']
self.assertIsInstance(func, ExpressionFunction)
self.assertEqual(
list(func(1, 10)),
[1, 2, 3, 4, 5, 6, 7, 8, 9]
)
def testFuncStrftime(self):
from datetime import date
func = build_template_functions()['strftime']
self.assertIsInstance(func, ExpressionFunction)
self.assertTrue(func('%Y %m %d'))
self.assertEqual(
func('%Y %m %d', date(2014, 5, 26)),
'2014 05 26'
)
def testFuncStrfcal(self):
from datetime import date
func = build_template_functions()['strfcal']
self.assertIsInstance(func, ExpressionFunction)
self.assertTrue(func('%Y %W'))
self.assertEqual(
func('%Y %W', date(2014, 5, 26)),
'2014 22'
)
def testHTMLEncode(self):
func = build_template_functions()['html_encode']
self.assertIsInstance(func, ExpressionFunction)
self.assertEqual(func('<a>foo</a>'), '<a>foo</a>')
def testURLEncode(self):
func = build_template_functions()['url_encode']
self.assertIsInstance(func, ExpressionFunction)
self.assertEqual(func('/foo/bar baz'), '%2Ffoo%2Fbar%20baz')
class TestTemplate(tests.TestCase):
def runTest(self):
from pprint import pprint
file = tests.TEST_DATA_FOLDER.file('TestTemplate.html')
templ = Template(file)
#~ pprint(templ.parts) # parser output
output = []
templ.process(output, {
'title': 'THIS IS THE TITLE',
'generator': {
'name': 'ZIM VERSION',
},
'navigation': {
'prev': None,
'next': None,
},
'links': {},
'pages': [
{ # page
'name': 'page',
'heading': 'HEAD',
'body': 'BODY',
'properties': {
'type': 'PAGE',
},
'backlinks': [
{'name': 'LINK1'},
{'name': 'LINK2'},
{'name': 'LINK3'},
],
'attachments': [
{'name': 'FILE1', 'basename': 'FILE1', 'size': '1k'},
{'name': 'FILE2', 'basename': 'FILE2', 'size': '1k'},
],
},
],
'uri': ExpressionFunction(lambda l: "URL:%s" % l['name']),
'anchor': ExpressionFunction(lambda l: "ANCHOR:%s" % l['name']),
})
#~ print(''.join(output))
# TODO assert something
### Test empty template OK as well
folder = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL)
file = folder.file('empty.html')
self.assertRaises(FileNotFoundError, Template, file)
file.touch()
templ = Template(file)
output = []
templ.process(output, {})
self.assertEqual(output, [])
class TestTemplateInclude(tests.TestCase):
def setUp(self):
folder = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL) # Can be converted to virual after removal of "zim.fs"
self.file = folder.file('template.html')
include = folder.file('template/include.html')
include.write('INCLUDED TEXT [% foo %]')
protected = folder.file('passwd.txt')
protected.write('FAIL')
def testInclude(self):
# Test inclusion & parsing of template content
# see tests in TestTemplateProcessor for tests of syntax variants
self.file.write('[% foo="Test" %][% INCLUDE "include.html" %]')
templ = Template(self.file)
output = []
templ.process(output, {})
self.assertEqual(output, ['INCLUDED TEXT ', 'Test'])
def testIncludeFromParentDirNotAllowed(self):
# test 'INCLUDE path' does not allow include from ../../ something
self.file.write('[% foo="Test" %][% INCLUDE "../passwd.txt" %]')
templ = Template(self.file)
output = []
with tests.LoggingFilter('zim'):
templ.process(output, {})
self.assertNotIn('FAIL', output)
|