/usr/share/pyshared/genshi/template/tests/directives.py is in python-genshi 0.7-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 | # -*- coding: utf-8 -*-
#
# Copyright (C) 2006-2010 Edgewall Software
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://genshi.edgewall.org/wiki/License.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://genshi.edgewall.org/log/.
import doctest
import re
import sys
import unittest
from genshi.template import directives, MarkupTemplate, TextTemplate, \
TemplateRuntimeError, TemplateSyntaxError
class AttrsDirectiveTestCase(unittest.TestCase):
"""Tests for the `py:attrs` template directive."""
def test_combined_with_loop(self):
"""
Verify that the directive has access to the loop variables.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<elem py:for="item in items" py:attrs="item"/>
</doc>""")
items = [{'id': 1}, {'id': 2}]
self.assertEqual("""<doc>
<elem id="1"/><elem id="2"/>
</doc>""", tmpl.generate(items=items).render(encoding=None))
def test_update_existing_attr(self):
"""
Verify that an attribute value that evaluates to `None` removes an
existing attribute of that name.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<elem class="foo" py:attrs="{'class': 'bar'}"/>
</doc>""")
self.assertEqual("""<doc>
<elem class="bar"/>
</doc>""", tmpl.generate().render(encoding=None))
def test_remove_existing_attr(self):
"""
Verify that an attribute value that evaluates to `None` removes an
existing attribute of that name.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<elem class="foo" py:attrs="{'class': None}"/>
</doc>""")
self.assertEqual("""<doc>
<elem/>
</doc>""", tmpl.generate().render(encoding=None))
class ChooseDirectiveTestCase(unittest.TestCase):
"""Tests for the `py:choose` template directive and the complementary
directives `py:when` and `py:otherwise`."""
def test_multiple_true_whens(self):
"""
Verify that, if multiple `py:when` bodies match, only the first is
output.
"""
tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/" py:choose="">
<span py:when="1 == 1">1</span>
<span py:when="2 == 2">2</span>
<span py:when="3 == 3">3</span>
</div>""")
self.assertEqual("""<div>
<span>1</span>
</div>""", tmpl.generate().render(encoding=None))
def test_otherwise(self):
tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/" py:choose="">
<span py:when="False">hidden</span>
<span py:otherwise="">hello</span>
</div>""")
self.assertEqual("""<div>
<span>hello</span>
</div>""", tmpl.generate().render(encoding=None))
def test_nesting(self):
"""
Verify that `py:choose` blocks can be nested:
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<div py:choose="1">
<div py:when="1" py:choose="3">
<span py:when="2">2</span>
<span py:when="3">3</span>
</div>
</div>
</doc>""")
self.assertEqual("""<doc>
<div>
<div>
<span>3</span>
</div>
</div>
</doc>""", tmpl.generate().render(encoding=None))
def test_complex_nesting(self):
"""
Verify more complex nesting.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<div py:choose="1">
<div py:when="1" py:choose="">
<span py:when="2">OK</span>
<span py:when="1">FAIL</span>
</div>
</div>
</doc>""")
self.assertEqual("""<doc>
<div>
<div>
<span>OK</span>
</div>
</div>
</doc>""", tmpl.generate().render(encoding=None))
def test_complex_nesting_otherwise(self):
"""
Verify more complex nesting using otherwise.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<div py:choose="1">
<div py:when="1" py:choose="2">
<span py:when="1">FAIL</span>
<span py:otherwise="">OK</span>
</div>
</div>
</doc>""")
self.assertEqual("""<doc>
<div>
<div>
<span>OK</span>
</div>
</div>
</doc>""", tmpl.generate().render(encoding=None))
def test_when_with_strip(self):
"""
Verify that a when directive with a strip directive actually strips of
the outer element.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<div py:choose="" py:strip="">
<span py:otherwise="">foo</span>
</div>
</doc>""")
self.assertEqual("""<doc>
<span>foo</span>
</doc>""", tmpl.generate().render(encoding=None))
def test_when_outside_choose(self):
"""
Verify that a `when` directive outside of a `choose` directive is
reported as an error.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<div py:when="xy" />
</doc>""")
self.assertRaises(TemplateRuntimeError, str, tmpl.generate())
def test_otherwise_outside_choose(self):
"""
Verify that an `otherwise` directive outside of a `choose` directive is
reported as an error.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<div py:otherwise="" />
</doc>""")
self.assertRaises(TemplateRuntimeError, str, tmpl.generate())
def test_when_without_test(self):
"""
Verify that an `when` directive that doesn't have a `test` attribute
is reported as an error.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<div py:choose="" py:strip="">
<py:when>foo</py:when>
</div>
</doc>""")
self.assertRaises(TemplateRuntimeError, str, tmpl.generate())
def test_when_without_test_but_with_choose_value(self):
"""
Verify that an `when` directive that doesn't have a `test` attribute
works as expected as long as the parent `choose` directive has a test
expression.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<div py:choose="foo" py:strip="">
<py:when>foo</py:when>
</div>
</doc>""")
self.assertEqual("""<doc>
foo
</doc>""", tmpl.generate(foo='Yeah').render(encoding=None))
def test_otherwise_without_test(self):
"""
Verify that an `otherwise` directive can be used without a `test`
attribute.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<div py:choose="" py:strip="">
<py:otherwise>foo</py:otherwise>
</div>
</doc>""")
self.assertEqual("""<doc>
foo
</doc>""", tmpl.generate().render(encoding=None))
def test_as_element(self):
"""
Verify that the directive can also be used as an element.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<py:choose>
<py:when test="1 == 1">1</py:when>
<py:when test="2 == 2">2</py:when>
<py:when test="3 == 3">3</py:when>
</py:choose>
</doc>""")
self.assertEqual("""<doc>
1
</doc>""", tmpl.generate().render(encoding=None))
def test_in_text_template(self):
"""
Verify that the directive works as expected in a text template.
"""
tmpl = TextTemplate("""#choose
#when 1 == 1
1
#end
#when 2 == 2
2
#end
#when 3 == 3
3
#end
#end""")
self.assertEqual(""" 1\n""",
tmpl.generate().render(encoding=None))
class DefDirectiveTestCase(unittest.TestCase):
"""Tests for the `py:def` template directive."""
def test_function_with_strip(self):
"""
Verify that a named template function with a strip directive actually
strips of the outer element.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<div py:def="echo(what)" py:strip="">
<b>${what}</b>
</div>
${echo('foo')}
</doc>""")
self.assertEqual("""<doc>
<b>foo</b>
</doc>""", tmpl.generate().render(encoding=None))
def test_exec_in_replace(self):
tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/">
<p py:def="echo(greeting, name='world')" class="message">
${greeting}, ${name}!
</p>
<div py:replace="echo('hello')"></div>
</div>""")
self.assertEqual("""<div>
<p class="message">
hello, world!
</p>
</div>""", tmpl.generate().render(encoding=None))
def test_as_element(self):
"""
Verify that the directive can also be used as an element.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<py:def function="echo(what)">
<b>${what}</b>
</py:def>
${echo('foo')}
</doc>""")
self.assertEqual("""<doc>
<b>foo</b>
</doc>""", tmpl.generate().render(encoding=None))
def test_nested_defs(self):
"""
Verify that a template function defined inside a conditional block can
be called from outside that block.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<py:if test="semantic">
<strong py:def="echo(what)">${what}</strong>
</py:if>
<py:if test="not semantic">
<b py:def="echo(what)">${what}</b>
</py:if>
${echo('foo')}
</doc>""")
self.assertEqual("""<doc>
<strong>foo</strong>
</doc>""", tmpl.generate(semantic=True).render(encoding=None))
def test_function_with_default_arg(self):
"""
Verify that keyword arguments work with `py:def` directives.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<b py:def="echo(what, bold=False)" py:strip="not bold">${what}</b>
${echo('foo')}
</doc>""")
self.assertEqual("""<doc>
foo
</doc>""", tmpl.generate().render(encoding=None))
def test_invocation_in_attribute(self):
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<py:def function="echo(what)">${what or 'something'}</py:def>
<p class="${echo('foo')}">bar</p>
</doc>""")
self.assertEqual("""<doc>
<p class="foo">bar</p>
</doc>""", tmpl.generate().render(encoding=None))
def test_invocation_in_attribute_none(self):
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<py:def function="echo()">${None}</py:def>
<p class="${echo()}">bar</p>
</doc>""")
self.assertEqual("""<doc>
<p>bar</p>
</doc>""", tmpl.generate().render(encoding=None))
def test_function_raising_typeerror(self):
def badfunc():
raise TypeError
tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
<div py:def="dobadfunc()">
${badfunc()}
</div>
<div py:content="dobadfunc()"/>
</html>""")
self.assertRaises(TypeError, list, tmpl.generate(badfunc=badfunc))
def test_def_in_matched(self):
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<head py:match="head">${select('*')}</head>
<head>
<py:def function="maketitle(test)"><b py:replace="test" /></py:def>
<title>${maketitle(True)}</title>
</head>
</doc>""")
self.assertEqual("""<doc>
<head><title>True</title></head>
</doc>""", tmpl.generate().render(encoding=None))
def test_in_text_template(self):
"""
Verify that the directive works as expected in a text template.
"""
tmpl = TextTemplate("""
#def echo(greeting, name='world')
${greeting}, ${name}!
#end
${echo('Hi', name='you')}
""")
self.assertEqual("""
Hi, you!
""", tmpl.generate().render(encoding=None))
def test_function_with_star_args(self):
"""
Verify that a named template function using "star arguments" works as
expected.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<div py:def="f(*args, **kwargs)">
${repr(args)}
${repr(sorted(kwargs.items()))}
</div>
${f(1, 2, a=3, b=4)}
</doc>""")
self.assertEqual("""<doc>
<div>
[1, 2]
[('a', 3), ('b', 4)]
</div>
</doc>""", tmpl.generate().render(encoding=None))
class ForDirectiveTestCase(unittest.TestCase):
"""Tests for the `py:for` template directive."""
def test_loop_with_strip(self):
"""
Verify that the combining the `py:for` directive with `py:strip` works
correctly.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<div py:for="item in items" py:strip="">
<b>${item}</b>
</div>
</doc>""")
self.assertEqual("""<doc>
<b>1</b>
<b>2</b>
<b>3</b>
<b>4</b>
<b>5</b>
</doc>""", tmpl.generate(items=range(1, 6)).render(encoding=None))
def test_as_element(self):
"""
Verify that the directive can also be used as an element.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<py:for each="item in items">
<b>${item}</b>
</py:for>
</doc>""")
self.assertEqual("""<doc>
<b>1</b>
<b>2</b>
<b>3</b>
<b>4</b>
<b>5</b>
</doc>""", tmpl.generate(items=range(1, 6)).render(encoding=None))
def test_multi_assignment(self):
"""
Verify that assignment to tuples works correctly.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<py:for each="k, v in items">
<p>key=$k, value=$v</p>
</py:for>
</doc>""")
self.assertEqual("""<doc>
<p>key=a, value=1</p>
<p>key=b, value=2</p>
</doc>""", tmpl.generate(items=(('a', 1), ('b', 2)))
.render(encoding=None))
def test_nested_assignment(self):
"""
Verify that assignment to nested tuples works correctly.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<py:for each="idx, (k, v) in items">
<p>$idx: key=$k, value=$v</p>
</py:for>
</doc>""")
self.assertEqual("""<doc>
<p>0: key=a, value=1</p>
<p>1: key=b, value=2</p>
</doc>""", tmpl.generate(items=enumerate([('a', 1), ('b', 2)]))
.render(encoding=None))
def test_not_iterable(self):
"""
Verify that assignment to nested tuples works correctly.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<py:for each="item in foo">
$item
</py:for>
</doc>""", filename='test.html')
try:
list(tmpl.generate(foo=12))
self.fail('Expected TemplateRuntimeError')
except TypeError, e:
assert (str(e) == "iteration over non-sequence" or
str(e) == "'int' object is not iterable")
exc_type, exc_value, exc_traceback = sys.exc_info()
frame = exc_traceback.tb_next
frames = []
while frame.tb_next:
frame = frame.tb_next
frames.append(frame)
self.assertEqual("<Expression u'iter(foo)'>",
frames[-1].tb_frame.f_code.co_name)
self.assertEqual('test.html',
frames[-1].tb_frame.f_code.co_filename)
self.assertEqual(2, frames[-1].tb_lineno)
def test_for_with_empty_value(self):
"""
Verify an empty 'for' value is an error
"""
try:
MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<py:for each="">
empty
</py:for>
</doc>""", filename='test.html').generate()
self.fail('ExpectedTemplateSyntaxError')
except TemplateSyntaxError, e:
self.assertEqual('test.html', e.filename)
if sys.version_info[:2] > (2,4):
self.assertEqual(2, e.lineno)
class IfDirectiveTestCase(unittest.TestCase):
"""Tests for the `py:if` template directive."""
def test_loop_with_strip(self):
"""
Verify that the combining the `py:if` directive with `py:strip` works
correctly.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<b py:if="foo" py:strip="">${bar}</b>
</doc>""")
self.assertEqual("""<doc>
Hello
</doc>""", tmpl.generate(foo=True, bar='Hello').render(encoding=None))
def test_as_element(self):
"""
Verify that the directive can also be used as an element.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<py:if test="foo">${bar}</py:if>
</doc>""")
self.assertEqual("""<doc>
Hello
</doc>""", tmpl.generate(foo=True, bar='Hello').render(encoding=None))
class MatchDirectiveTestCase(unittest.TestCase):
"""Tests for the `py:match` template directive."""
def test_with_strip(self):
"""
Verify that a match template can produce the same kind of element that
it matched without entering an infinite recursion.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<elem py:match="elem" py:strip="">
<div class="elem">${select('text()')}</div>
</elem>
<elem>Hey Joe</elem>
</doc>""")
self.assertEqual("""<doc>
<div class="elem">Hey Joe</div>
</doc>""", tmpl.generate().render(encoding=None))
def test_without_strip(self):
"""
Verify that a match template can produce the same kind of element that
it matched without entering an infinite recursion.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<elem py:match="elem">
<div class="elem">${select('text()')}</div>
</elem>
<elem>Hey Joe</elem>
</doc>""")
self.assertEqual("""<doc>
<elem>
<div class="elem">Hey Joe</div>
</elem>
</doc>""", tmpl.generate().render(encoding=None))
def test_as_element(self):
"""
Verify that the directive can also be used as an element.
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<py:match path="elem">
<div class="elem">${select('text()')}</div>
</py:match>
<elem>Hey Joe</elem>
</doc>""")
self.assertEqual("""<doc>
<div class="elem">Hey Joe</div>
</doc>""", tmpl.generate().render(encoding=None))
def test_recursive_match_1(self):
"""
Match directives are applied recursively, meaning that they are also
applied to any content they may have produced themselves:
"""
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<elem py:match="elem">
<div class="elem">
${select('*')}
</div>
</elem>
<elem>
<subelem>
<elem/>
</subelem>
</elem>
</doc>""")
self.assertEqual("""<doc>
<elem>
<div class="elem">
<subelem>
<elem>
<div class="elem">
</div>
</elem>
</subelem>
</div>
</elem>
</doc>""", tmpl.generate().render(encoding=None))
def test_recursive_match_2(self):
"""
When two or more match templates match the same element and also
themselves output the element they match, avoiding recursion is even
more complex, but should work.
"""
tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
<body py:match="body">
<div id="header"/>
${select('*')}
</body>
<body py:match="body">
${select('*')}
<div id="footer"/>
</body>
<body>
<h1>Foo</h1>
</body>
</html>""")
self.assertEqual("""<html>
<body>
<div id="header"/><h1>Foo</h1>
<div id="footer"/>
</body>
</html>""", tmpl.generate().render(encoding=None))
def test_recursive_match_3(self):
tmpl = MarkupTemplate("""<test xmlns:py="http://genshi.edgewall.org/">
<py:match path="b[@type='bullet']">
<bullet>${select('*|text()')}</bullet>
</py:match>
<py:match path="group[@type='bullet']">
<ul>${select('*')}</ul>
</py:match>
<py:match path="b">
<generic>${select('*|text()')}</generic>
</py:match>
<b>
<group type="bullet">
<b type="bullet">1</b>
<b type="bullet">2</b>
</group>
</b>
</test>
""")
self.assertEqual("""<test>
<generic>
<ul><bullet>1</bullet><bullet>2</bullet></ul>
</generic>
</test>""", tmpl.generate().render(encoding=None))
def test_not_match_self(self):
"""
See http://genshi.edgewall.org/ticket/77
"""
tmpl = MarkupTemplate("""<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://genshi.edgewall.org/">
<body py:match="body" py:content="select('*')" />
<h1 py:match="h1">
${select('text()')}
Goodbye!
</h1>
<body>
<h1>Hello!</h1>
</body>
</html>""")
self.assertEqual("""<html xmlns="http://www.w3.org/1999/xhtml">
<body><h1>
Hello!
Goodbye!
</h1></body>
</html>""", tmpl.generate().render(encoding=None))
def test_select_text_in_element(self):
"""
See http://genshi.edgewall.org/ticket/77#comment:1
"""
tmpl = MarkupTemplate("""<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://genshi.edgewall.org/">
<body py:match="body" py:content="select('*')" />
<h1 py:match="h1">
<text>
${select('text()')}
</text>
Goodbye!
</h1>
<body>
<h1>Hello!</h1>
</body>
</html>""")
self.assertEqual("""<html xmlns="http://www.w3.org/1999/xhtml">
<body><h1>
<text>
Hello!
</text>
Goodbye!
</h1></body>
</html>""", tmpl.generate().render(encoding=None))
def test_select_all_attrs(self):
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<div py:match="elem" py:attrs="select('@*')">
${select('text()')}
</div>
<elem id="joe">Hey Joe</elem>
</doc>""")
self.assertEqual("""<doc>
<div id="joe">
Hey Joe
</div>
</doc>""", tmpl.generate().render(encoding=None))
def test_select_all_attrs_empty(self):
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<div py:match="elem" py:attrs="select('@*')">
${select('text()')}
</div>
<elem>Hey Joe</elem>
</doc>""")
self.assertEqual("""<doc>
<div>
Hey Joe
</div>
</doc>""", tmpl.generate().render(encoding=None))
def test_select_all_attrs_in_body(self):
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<div py:match="elem">
Hey ${select('text()')} ${select('@*')}
</div>
<elem title="Cool">Joe</elem>
</doc>""")
self.assertEqual("""<doc>
<div>
Hey Joe Cool
</div>
</doc>""", tmpl.generate().render(encoding=None))
def test_def_in_match(self):
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<py:def function="maketitle(test)"><b py:replace="test" /></py:def>
<head py:match="head">${select('*')}</head>
<head><title>${maketitle(True)}</title></head>
</doc>""")
self.assertEqual("""<doc>
<head><title>True</title></head>
</doc>""", tmpl.generate().render(encoding=None))
def test_match_with_xpath_variable(self):
tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/">
<span py:match="*[name()=$tagname]">
Hello ${select('@name')}
</span>
<greeting name="Dude"/>
</div>""")
self.assertEqual("""<div>
<span>
Hello Dude
</span>
</div>""", tmpl.generate(tagname='greeting').render(encoding=None))
self.assertEqual("""<div>
<greeting name="Dude"/>
</div>""", tmpl.generate(tagname='sayhello').render(encoding=None))
def test_content_directive_in_match(self):
tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
<div py:match="foo">I said <q py:content="select('text()')">something</q>.</div>
<foo>bar</foo>
</html>""")
self.assertEqual("""<html>
<div>I said <q>bar</q>.</div>
</html>""", tmpl.generate().render(encoding=None))
def test_cascaded_matches(self):
tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
<body py:match="body">${select('*')}</body>
<head py:match="head">${select('title')}</head>
<body py:match="body">${select('*')}<hr /></body>
<head><title>Welcome to Markup</title></head>
<body><h2>Are you ready to mark up?</h2></body>
</html>""")
self.assertEqual("""<html>
<head><title>Welcome to Markup</title></head>
<body><h2>Are you ready to mark up?</h2><hr/></body>
</html>""", tmpl.generate().render(encoding=None))
def test_multiple_matches(self):
tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
<input py:match="form//input" py:attrs="select('@*')"
value="${values[str(select('@name'))]}" />
<form><p py:for="field in fields">
<label>${field.capitalize()}</label>
<input type="text" name="${field}" />
</p></form>
</html>""")
fields = ['hello_%s' % i for i in range(5)]
values = dict([('hello_%s' % i, i) for i in range(5)])
self.assertEqual("""<html>
<form><p>
<label>Hello_0</label>
<input value="0" type="text" name="hello_0"/>
</p><p>
<label>Hello_1</label>
<input value="1" type="text" name="hello_1"/>
</p><p>
<label>Hello_2</label>
<input value="2" type="text" name="hello_2"/>
</p><p>
<label>Hello_3</label>
<input value="3" type="text" name="hello_3"/>
</p><p>
<label>Hello_4</label>
<input value="4" type="text" name="hello_4"/>
</p></form>
</html>""", tmpl.generate(fields=fields, values=values)
.render(encoding=None))
def test_namespace_context(self):
tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"
xmlns:x="http://www.example.org/">
<div py:match="x:foo">Foo</div>
<foo xmlns="http://www.example.org/"/>
</html>""")
# FIXME: there should be a way to strip out unwanted/unused namespaces,
# such as the "x" in this example
self.assertEqual("""<html xmlns:x="http://www.example.org/">
<div>Foo</div>
</html>""", tmpl.generate().render(encoding=None))
def test_match_with_position_predicate(self):
tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
<p py:match="body/p[1]" class="first">${select('*|text()')}</p>
<body>
<p>Foo</p>
<p>Bar</p>
</body>
</html>""")
self.assertEqual("""<html>
<body>
<p class="first">Foo</p>
<p>Bar</p>
</body>
</html>""", tmpl.generate().render(encoding=None))
def test_match_with_closure(self):
tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
<p py:match="body//p" class="para">${select('*|text()')}</p>
<body>
<p>Foo</p>
<div><p>Bar</p></div>
</body>
</html>""")
self.assertEqual("""<html>
<body>
<p class="para">Foo</p>
<div><p class="para">Bar</p></div>
</body>
</html>""", tmpl.generate().render(encoding=None))
def test_match_without_closure(self):
tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
<p py:match="body/p" class="para">${select('*|text()')}</p>
<body>
<p>Foo</p>
<div><p>Bar</p></div>
</body>
</html>""")
self.assertEqual("""<html>
<body>
<p class="para">Foo</p>
<div><p>Bar</p></div>
</body>
</html>""", tmpl.generate().render(encoding=None))
def test_match_with_once_attribute(self):
tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
<py:match path="body" once="true"><body>
<div id="wrap">
${select("*")}
</div>
</body></py:match>
<body>
<p>Foo</p>
</body>
<body>
<p>Bar</p>
</body>
</html>""")
self.assertEqual("""<html>
<body>
<div id="wrap">
<p>Foo</p>
</div>
</body>
<body>
<p>Bar</p>
</body>
</html>""", tmpl.generate().render(encoding=None))
def test_match_with_recursive_attribute(self):
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<py:match path="elem" recursive="false"><elem>
<div class="elem">
${select('*')}
</div>
</elem></py:match>
<elem>
<subelem>
<elem/>
</subelem>
</elem>
</doc>""")
self.assertEqual("""<doc>
<elem>
<div class="elem">
<subelem>
<elem/>
</subelem>
</div>
</elem>
</doc>""", tmpl.generate().render(encoding=None))
# See http://genshi.edgewall.org/ticket/254/
def test_triple_match_produces_no_duplicate_items(self):
tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<div py:match="div[@id='content']" py:attrs="select('@*')" once="true">
<ul id="tabbed_pane" />
${select('*')}
</div>
<body py:match="body" once="true" buffer="false">
${select('*|text()')}
</body>
<body py:match="body" once="true" buffer="false">
${select('*|text()')}
</body>
<body>
<div id="content">
<h1>Ticket X</h1>
</div>
</body>
</doc>""")
output = tmpl.generate().render('xhtml', doctype='xhtml')
matches = re.findall("tabbed_pane", output)
self.assertNotEqual(None, matches)
self.assertEqual(1, len(matches))
def test_match_multiple_times1(self):
# See http://genshi.edgewall.org/ticket/370
tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
<py:match path="body[@id='content']/h2" />
<head py:match="head" />
<head py:match="head" />
<head />
<body />
</html>""")
self.assertEqual("""<html>
<head/>
<body/>
</html>""", tmpl.generate().render())
def test_match_multiple_times2(self):
# See http://genshi.edgewall.org/ticket/370
tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
<py:match path="body/div[@id='properties']" />
<head py:match="head" />
<head py:match="head" />
<head/>
<body>
<div id="properties">Foo</div>
</body>
</html>""")
self.assertEqual("""<html>
<head/>
<body>
</body>
</html>""", tmpl.generate().render())
def test_match_multiple_times3(self):
# See http://genshi.edgewall.org/ticket/370#comment:12
tmpl = MarkupTemplate("""<?xml version="1.0"?>
<root xmlns:py="http://genshi.edgewall.org/">
<py:match path="foo/bar">
<zzzzz/>
</py:match>
<foo>
<bar/>
<bar/>
</foo>
<bar/>
</root>""")
self.assertEqual("""<?xml version="1.0"?>\n<root>
<foo>
<zzzzz/>
<zzzzz/>
</foo>
<bar/>
</root>""", tmpl.generate().render())
# FIXME
#def test_match_after_step(self):
# tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/">
# <span py:match="div/greeting">
# Hello ${select('@name')}
# </span>
# <greeting name="Dude" />
# </div>""")
# self.assertEqual("""<div>
# <span>
# Hello Dude
# </span>
# </div>""", tmpl.generate().render(encoding=None))
class ContentDirectiveTestCase(unittest.TestCase):
"""Tests for the `py:content` template directive."""
def test_as_element(self):
try:
MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<py:content foo="">Foo</py:content>
</doc>""", filename='test.html').generate()
self.fail('Expected TemplateSyntaxError')
except TemplateSyntaxError, e:
self.assertEqual('test.html', e.filename)
self.assertEqual(2, e.lineno)
class ReplaceDirectiveTestCase(unittest.TestCase):
"""Tests for the `py:replace` template directive."""
def test_replace_with_empty_value(self):
"""
Verify that the directive raises an apprioriate exception when an empty
expression is supplied.
"""
try:
MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/">
<elem py:replace="">Foo</elem>
</doc>""", filename='test.html').generate()
self.fail('Expected TemplateSyntaxError')
except TemplateSyntaxError, e:
self.assertEqual('test.html', e.filename)
self.assertEqual(2, e.lineno)
def test_as_element(self):
tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/">
<py:replace value="title" />
</div>""", filename='test.html')
self.assertEqual("""<div>
Test
</div>""", tmpl.generate(title='Test').render(encoding=None))
class StripDirectiveTestCase(unittest.TestCase):
"""Tests for the `py:strip` template directive."""
def test_strip_false(self):
tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/">
<div py:strip="False"><b>foo</b></div>
</div>""")
self.assertEqual("""<div>
<div><b>foo</b></div>
</div>""", tmpl.generate().render(encoding=None))
def test_strip_empty(self):
tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/">
<div py:strip=""><b>foo</b></div>
</div>""")
self.assertEqual("""<div>
<b>foo</b>
</div>""", tmpl.generate().render(encoding=None))
class WithDirectiveTestCase(unittest.TestCase):
"""Tests for the `py:with` template directive."""
def test_shadowing(self):
tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/">
${x}
<span py:with="x = x * 2" py:replace="x"/>
${x}
</div>""")
self.assertEqual("""<div>
42
84
42
</div>""", tmpl.generate(x=42).render(encoding=None))
def test_as_element(self):
tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/">
<py:with vars="x = x * 2">${x}</py:with>
</div>""")
self.assertEqual("""<div>
84
</div>""", tmpl.generate(x=42).render(encoding=None))
def test_multiple_vars_same_name(self):
tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/">
<py:with vars="
foo = 'bar';
foo = foo.replace('r', 'z')
">
$foo
</py:with>
</div>""")
self.assertEqual("""<div>
baz
</div>""", tmpl.generate(x=42).render(encoding=None))
def test_multiple_vars_single_assignment(self):
tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/">
<py:with vars="x = y = z = 1">${x} ${y} ${z}</py:with>
</div>""")
self.assertEqual("""<div>
1 1 1
</div>""", tmpl.generate(x=42).render(encoding=None))
def test_nested_vars_single_assignment(self):
tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/">
<py:with vars="x, (y, z) = (1, (2, 3))">${x} ${y} ${z}</py:with>
</div>""")
self.assertEqual("""<div>
1 2 3
</div>""", tmpl.generate(x=42).render(encoding=None))
def test_multiple_vars_trailing_semicolon(self):
tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/">
<py:with vars="x = x * 2; y = x / 2;">${x} ${y}</py:with>
</div>""")
self.assertEqual("""<div>
84 %s
</div>""" % (84 / 2), tmpl.generate(x=42).render(encoding=None))
def test_semicolon_escape(self):
tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/">
<py:with vars="x = 'here is a semicolon: ;'; y = 'here are two semicolons: ;;' ;">
${x}
${y}
</py:with>
</div>""")
self.assertEqual("""<div>
here is a semicolon: ;
here are two semicolons: ;;
</div>""", tmpl.generate().render(encoding=None))
def test_ast_transformation(self):
"""
Verify that the usual template expression AST transformations are
applied despite the code being compiled to a `Suite` object.
"""
tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/">
<span py:with="bar=foo.bar">
$bar
</span>
</div>""")
self.assertEqual("""<div>
<span>
42
</span>
</div>""", tmpl.generate(foo={'bar': 42}).render(encoding=None))
def test_unicode_expr(self):
tmpl = MarkupTemplate(u"""<div xmlns:py="http://genshi.edgewall.org/">
<span py:with="weeks=(u'一', u'二', u'三', u'四', u'五', u'六', u'日')">
$weeks
</span>
</div>""")
self.assertEqual(u"""<div>
<span>
一二三四五六日
</span>
</div>""", tmpl.generate().render(encoding=None))
def test_with_empty_value(self):
"""
Verify that an empty py:with works (useless, but legal)
"""
tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/">
<span py:with="">Text</span></div>""")
self.assertEqual("""<div>
<span>Text</span></div>""", tmpl.generate().render(encoding=None))
def suite():
suite = unittest.TestSuite()
suite.addTest(doctest.DocTestSuite(directives))
suite.addTest(unittest.makeSuite(AttrsDirectiveTestCase, 'test'))
suite.addTest(unittest.makeSuite(ChooseDirectiveTestCase, 'test'))
suite.addTest(unittest.makeSuite(DefDirectiveTestCase, 'test'))
suite.addTest(unittest.makeSuite(ForDirectiveTestCase, 'test'))
suite.addTest(unittest.makeSuite(IfDirectiveTestCase, 'test'))
suite.addTest(unittest.makeSuite(MatchDirectiveTestCase, 'test'))
suite.addTest(unittest.makeSuite(ContentDirectiveTestCase, 'test'))
suite.addTest(unittest.makeSuite(ReplaceDirectiveTestCase, 'test'))
suite.addTest(unittest.makeSuite(StripDirectiveTestCase, 'test'))
suite.addTest(unittest.makeSuite(WithDirectiveTestCase, 'test'))
return suite
if __name__ == '__main__':
unittest.main(defaultTest='suite')
|