Source

webstring / webstring / tests / test_webstring.py

   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
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
# -*- coding: utf-8 -*-
'''Unit tests for lwebstring'''

import pickle
import unittest

from stuf.six import u
try:
    import lxml.etree as _etree
except ImportError:
    raise
_Element = _etree.Element

from webstring import Template, HTMLTemplate


class Testwebstring(unittest.TestCase):

    '''Test class for webstring'''

    # Add tests
    groupend = u(
        '<tr class="tr1"><td id="td1"/><td id="td2"/>'
        '</tr><tr class="tr2"><td id="td1"/><td id="td2"/></tr>'
    )
    altgroupend = u('<tr class="tr2"><td id="td1"/>'
        '<td id="td2"/></tr><tr class="tr1"><td id="td1"/>'
        '<td id="td2"/></tr>')
    subgroupend = u('<td id="td1"/><td id="td1"/>')
    addtest = u('<html><head><title id="title"/></head><html><body>'
        '<p id="content"/></body></html></html>')
    altaddtest = u('<html><body><p id="content"/></body><head>'
        '<title id="title"/></head></html>')
    field_add = u('<p id="content"/><title id="title"/>')
    field_group_add = u('<title id="title"/><tr class="tr1"><td id="td1"/>'
        '<td id="td2"/></tr>')
    root_group_add = u('<html><head><title id="title"/></head>'
        '<tr class="tr1"><td id="td1"/><td id="td2"/></tr></html>')
    root_element_add = u('<html><head><title id="title"/></head><p/></html>')
    field_element_add = u('<title id="title"/><p/>')
    group_element_add = u('<tr class="tr1"><td id="td1"/>'
        '<td id="td2"/></tr><p/>')
    subfield_element_add = u('<td id="td1"/><p/>')
    # Mul tests
    multest = '<p id="content"/><p id="content"/><p id="content"/>'
    root_mul = u('<html><head><title id="title"/></head><html><body>'
        '<p id="content"/></body></html><html><head><title id="title"/>'
        '</head><html><body><p id="content"/></body></html></html><html>'
        '<head><title id="title"/></head><html><body><p id="content"/>'
        '</body></html></html></html>')
    root_group_mul = u('<tr class="tr1"><td id="td1"/>'
        '<td id="td2"/></tr><tr class="tr1"><td id="td1"/>'
        '<td id="td2"/></tr><tr class="tr1"><td id="td1"/>'
        '<td id="td2"/></tr>')
    subfield_group_mul = u('<td id="td1"/><td id="td1"/><td id="td1"/>')
    # Mod tests
    modtest1 = u('<html><head><title id="title">Test Page</title>'
        '</head><body/></html>')
    modtest2 = u('<html><head><title id="title">Test Page</title></head>'
    '<html><body><p id="content">Content Goes Here</p></body></html></html>')
    modtest3 = u('<html><head><title id="title">Title</title></head>'
        '<body><p id="content">Test1</p><p id="content">Test2</p>'
        '<p id="content2">Test1</p><p id="content2">Test2</p>'
        '</body></html>')
    modtest5 = u('<title id="title">Test Page</title>')
    groupmod = u('<td id="td1">Test Page</td>')
    groupmod2 = u('<table><tr class="tr1"><td id="td1">Test Page</td>'
        '<td id="td2">Content Goes Here</td></tr></table>')
    groupmod3 = u('<table><tr class="tr1"><td id="td1">Test1</td><td '
        'id="td1">Test2</td><td id="td2">Test1</td><td id="td2">Test2'
        '</td></tr></table>')
    # Pow tets
    powtest1 = u('<p id="content">Test1</p><p id="content">Test2</p>')
    groupow = u('<tr class="tr1"><td id="td1">1</td>'
        '<td id="td2">2</td></tr><tr class="tr1"><td id="td1">3'
        '</td><td id="td2">4</td></tr><tr class="tr1"><td id="td1">5'
        '</td><td id="td2">6</td></tr>')
    # Templates
    modtemplate1 = Template('<html><head><title id="title"/></head>'
        '<body/></html>')
    modtemplate3 = Template('<html><head><title id="title"></title>'
        '</head><body><p id="content"/><p id="content2"/></body>'
        '</html>')
    gentemplate = Template(addtest)
    atemplate = Template('<html><head><title id="title"></title></head>'
        '</html>')
    btemplate = Template(
        '<html><body><p id="content"></p></body></html>')
    powtemplate = Template('<html><head/><body><p id="content"/>'
        '</body></html>')
    agrouptest = Template('<table><tr class="tr1"><td id="td1"/>'
        '<td id="td2"/></tr></table>')
    bgrouptest = Template('<table><tr class="tr2"><td id="td1"/>'
        '<td id="td2"/></tr></table>')
    subcombotest = Template('<body><a id="fieldtest" href="atest"/>'
        '<tr class="grouptest"><td id="groupfield1"/>'
        '<td id="groupfield2"/></tr></body>')
    rss = Template('''<rss version="2.0">
<channel>
<title>Example</title>
<link>http://www.example.org/</link>
<description>RSS Example</description>
<language>en-us</language>
<pubDate id="cpubdate">$month $day, $year</pubDate>
<lastBuildDate id="lastbuilddate">%(month)s %(day)s, %(year)s</lastBuildDate>
<item class="item">
<title id="title"/>
<link id="link"/>
<guid id="guid" isPermaLink="true"/>
<description id="description"/>
<pubDate id="ipubdate"/>
</item>
</channel>
</rss>''')
    htmlt = HTMLTemplate(
        '<html><head><link href="test.css" '
        'type="text/css"><title id="title"><body><p id="content">'
        '</body></head></html>'
    )
    # Data structures
    powdict = {'content1': 'Test1', 'content2': 'Test2'}
    powtup = ('Test1', 'Test2')
    moddict = {'content': 'Content Goes Here', 'title': 'Test Page'}
    groupdict = {'td1': 'Test Page', 'td2': 'Content Goes Here'}
    modtup = ('Test Page', 'Content Goes Here')
    grouptup = (('1', '2'), ('3', '4'), ('5', '6'))
    groupowdict = {'2': ['1', '2'], '4': ['3', '4'], '6': ['5', '6']}

    def test_lroot_add(self):
        '''Tests addition of two root Templates.'''
        atst = self.atemplate + self.btemplate
        self.assertEqual(self.addtest, atst.render())

    def test_lroot_radd(self):
        '''Tests the right-side addition of two root Templates.'''
        final = u('<html><body><p id="content"/></body><html><head>'
            '<title id="title"/></head></html></html>')
        atst = self.btemplate + self.atemplate
        self.assertEqual(final, atst.render())

    def test_lroot_iadd(self):
        '''Tests the modifying addition of two root Templates.'''
        self.atemplate += self.btemplate
        self.assertEqual(self.addtest, self.atemplate.pipe())

    def test_lfield_add(self):
        '''Tests the addition of two fields.'''
        final = '<title id="title"/><p id="content"/>'
        atst = self.atemplate.title + self.btemplate.content
        self.assertEqual(final, atst.render())

    def test_lfield_radd(self):
        '''Tests the right-side addition of two fields.'''
        atst = self.btemplate.content + self.atemplate.title
        self.assertEqual(self.field_add, atst.render())

    def test_lfield_iadd(self):
        '''Tests the modifying addition of two fields.'''
        self.btemplate.content += self.atemplate.title
        self.assertEqual(self.field_add, self.btemplate.content.pipe())

    def test_lgroup_add(self):
        '''Tests the addition of two groups.'''
        atst = self.agrouptest.tr1 + self.bgrouptest.tr2
        self.assertEqual(self.groupend, atst.render())

    def test_lgroup_radd(self):
        '''Tests the right-side addition of two groups.'''
        atst = self.bgrouptest.tr2 + self.agrouptest.tr1
        self.assertEqual(self.altgroupend, atst.render())

    def test_lgroup_iadd(self):
        '''Tests the modifying addition of two groups.'''
        self.agrouptest.tr1 += self.bgrouptest.tr2
        self.assertEqual(self.groupend, self.agrouptest.tr1.pipe())

    def test_lsubfield_add(self):
        '''Tests the addition of two subfields.'''
        atst = self.agrouptest.tr1.td1 + self.bgrouptest.tr2.td1
        self.assertEqual(self.subgroupend, atst.render())

    def test_lsubfield_radd(self):
        '''Tests the right-side addition of two subfields.'''
        final = u('<td id="td1"/><td id="td1"/>')
        atst = self.bgrouptest.tr2.td1 + self.agrouptest.tr1.td1
        self.assertEqual(final, atst.render())

    def test_lsubfield_iadd(self):
        '''Tests the modifying addition of two subfields.'''
        self.agrouptest.tr1.td1 += self.bgrouptest.tr2.td1
        self.assertEqual(self.subgroupend, self.agrouptest.tr1.td1.pipe())

    def test_lfield_root_iadd(self):
        '''Tests the modifying addition of a field and a root Template.'''
        final = u('<title id="title"/><html><body><p id="content"/>'
            '</body></html>')
        self.atemplate.title += self.btemplate
        self.assertEqual(final, self.atemplate.title.pipe())

    def test_lfield_group_iadd(self):
        '''Tests the modifying addition of a field and group.'''
        self.atemplate.title += self.agrouptest.tr1
        self.assertEqual(self.field_group_add,
            self.atemplate.title.pipe())

    def test_lfield_subfield_iadd(self):
        '''Tests the modifying addition of a field and subfield.'''
        final = u('<title id="title"/><td id="td1"/>')
        self.atemplate.title += self.agrouptest.tr1.td1
        self.assertEqual(final, self.atemplate.title.pipe())

    def test_lroot_field_iadd(self):
        '''Tests the modifying addition of a root Template and a field.'''
        final = u('<html><body><p id="content"/></body>'
        '<title id="title"/></html>')
        self.btemplate += self.atemplate.title
        self.assertEqual(final, self.btemplate.pipe())

    def test_lroot_group_iadd(self):
        '''Tests the modifying addition of a root Template and a group.'''
        self.atemplate += self.agrouptest.tr1
        self.assertEqual(self.root_group_add, self.atemplate.pipe())

    def test_lroot_subfield_iadd(self):
        '''Tests the modifying addition of a root Template and subfield.'''
        final = u(
            '<html><head><title id="title"/></head><td id="td1"/></html>'
        )
        self.atemplate += self.agrouptest.tr1.td1
        self.assertEqual(final, self.atemplate.pipe())

    def test_lgroup_subfield_iadd(self):
        '''Tests the modifying addition of one group and one subfield.'''
        final = u('<tr class="tr1"><td id="td1"/><td id="td2"/>'
            '</tr><td id="td1"/>')
        self.agrouptest.tr1 += self.bgrouptest.tr2.td1
        self.assertEqual(final, self.agrouptest.tr1.pipe())

    def test_lsubfield_group_iadd(self):
        '''Tests the modifying addition of one subfield and one group.'''
        final = u('<td id="td1"/><tr class="tr1">'
            '<td id="td1"/><td id="td2"/></tr>')
        self.bgrouptest.tr2.td1 += self.agrouptest.tr1
        self.assertEqual(final, self.bgrouptest.tr2.td1.pipe())

    def test_lroot_element_radd(self):
        '''Tests the right-side addition of a root Template and an Element.'''
        atst = _Element('p') + self.atemplate
        self.assertEqual(self.root_element_add, atst.render())

    def test_lroot_element_iadd(self):
        '''Tests the modifying addition of a root Template and an Element.'''
        self.atemplate += _Element('p')
        self.assertEqual(self.root_element_add, self.atemplate.pipe())

    def test_lfield_element_radd(self):
        '''Tests the right-side addition of a field and an Element.'''
        atst = _Element('p') + self.atemplate.title
        self.assertEqual(self.field_element_add, atst.render())

    def test_lfield_element_iadd(self):
        '''Tests the modifying addition of a field and an Element.'''
        self.atemplate.title += _Element('p')
        self.assertEqual(self.field_element_add,
            self.atemplate.title.pipe())

    def test_lgroup_element_radd(self):
        '''Tests the right-side addition of a group and an Element.'''
        atst = _Element('p') + self.agrouptest.tr1
        self.assertEqual(self.group_element_add, atst.render())

    def test_lgroup_element_iadd(self):
        '''Tests the modifying addition of a group and an Element.'''
        self.agrouptest.tr1 += _Element('p')
        self.assertEqual(self.group_element_add,
            self.agrouptest.tr1.pipe())

    def test_lsubfield_element_radd(self):
        '''Tests the right-side addition of a subfield and an Element.'''
        atst = _Element('p') + self.agrouptest.tr1.td1
        self.assertEqual(self.subfield_element_add, atst.render())

    def test_lsubfield_element_iadd(self):
        '''Tests the modifying addition of a subfield and an Element.'''
        self.agrouptest.tr1.td1 += _Element('p')
        self.assertEqual(self.subfield_element_add,
            self.agrouptest.tr1.td1.pipe())

    def test_lroot_radd_raise(self):
        '''Raises TypeError if wrong type right-side added to root Template.'''
        def tempfunc():
            return [1, 1, 1] + self.atemplate
        self.assertRaises(TypeError, tempfunc)

    def test_lroot_iadd_raise(self):
        '''Raises TypeError if wrong type is added to root Template.'''
        def tempfunc():
            atst = self.atemplate.default
            atst += [1, 1, 1]
        self.assertRaises(TypeError, tempfunc)

    def test_lfield_radd_raise(self):
        '''Raises TypeError if wrong type is right-side added to field.'''
        def tempfunc():
            return [1, 1, 1] + self.atemplate.title
        self.assertRaises(TypeError, tempfunc)

    def test_lfield_iadd_raise(self):
        '''Raises TypeError if wrong type is modifyingly added to field.'''
        def tempfunc():
            atst = self.atemplate.default
            atst.title += [1, 1, 1]
        self.assertRaises(TypeError, tempfunc)

    def test_lgroup_radd_raise(self):
        '''Raises TypeError if wrong type is right-side added to group.'''
        def tempfunc():
            return [1, 1, 1] + self.agrouptest.tr1
        self.assertRaises(TypeError, tempfunc)

    def test_lgroup_iadd_raise(self):
        '''Raises TypeError if wrong type is modifyingly added to group.'''
        def tempfunc():
            atst = self.agrouptest.default
            atst.tr1 += [1, 1, 1]
        self.assertRaises(TypeError, tempfunc)

    def test_lsubfield_radd_raise(self):
        '''Raises TypeError if wrong type is right-side added to subfield.'''
        def tempfunc():
            return [1, 1, 1] + self.agrouptest.tr1.td1
        self.assertRaises(TypeError, tempfunc)

    def test_lsubfield_iadd_raise(self):
        '''Raises TypeError if wrong type is modifyingly added to subfield.'''
        def tempfunc():
            atst = self.agrouptest.default
            atst.tr1.td1 += [1, 1, 1]
        self.assertRaises(TypeError, tempfunc)

    def test_lroot_rmul(self):
        '''Tests repetition of a root Template with number on the right.'''
        atst = 3 * self.gentemplate
        self.assertEqual(self.root_mul, atst.render())

    def test_lroot_imul(self):
        '''Tests modifying repetition of a root Template.'''
        self.gentemplate *= 3
        self.assertEqual(self.root_mul, self.gentemplate.pipe())

    def test_lfield_mul(self):
        '''Tests the repetition of a field.'''
        atst = self.gentemplate.content * 3
        self.assertEqual(self.multest, atst.render())

    def test_lfield_rmul(self):
        '''Tests repetition of a field with the number on the right.'''
        atst = 3 * self.gentemplate.content
        self.assertEqual(self.multest, atst.render())

    def test_lfield_imul(self):
        '''Tests modifying repetition of a field.'''
        self.gentemplate.content *= 3
        self.assertEqual(self.multest, self.gentemplate.content.pipe())

    def test_lgroup_rmul(self):
        '''Tests repetition of a group with the number on the right side.'''
        atst = 3 * self.agrouptest.tr1
        self.assertEqual(self.root_group_mul, atst.render())

    def test_lgroup_imul(self):
        '''Tests modifying repetition of a group.'''
        self.agrouptest.tr1 *= 3
        self.assertEqual(self.root_group_mul, self.agrouptest.tr1.pipe())

    def test_lsubfield_rmul(self):
        '''Tests repetition of a subfield with the number on the right side.'''
        atst = 3 * self.agrouptest.tr1.td1
        self.assertEqual(self.subfield_group_mul, atst.render())

    def test_lsubfield_mul(self):
        '''Tests the repetition of a subfield.'''
        atst = self.agrouptest.tr1.td1 * 3
        self.assertEqual(self.subfield_group_mul, atst.render())

    def test_lsubfield_imul(self):
        '''Tests modifying repetition of a subfield.'''
        self.agrouptest.tr1.td1 *= 3
        self.assertEqual(self.subfield_group_mul,
            self.agrouptest.tr1.td1.pipe())

    def test_lfield_imul_rep(self):
        '''Tests restraint on a max modifying repetition of a field.'''
        def tempfunc():
            atst = self.gentemplate.default
            atst.content.max = 2
            atst.content *= 3
        self.assertRaises(TypeError, tempfunc)

    def test_lroot_imul_rep(self):
        '''Tests restraint on max modifying repetition of a root Template.'''
        def tempfunc():
            atst = self.gentemplate.default
            atst.max = 2
            atst *= 3
        self.assertRaises(TypeError, tempfunc)

    def test_lgroup_imul_rep(self):
        '''Tests restraint on max modifying repetition of a field.'''
        def tempfunc():
            atst = self.agrouptest.default
            atst.tr1.max = 2
            atst.tr1 *= 3
        self.assertRaises(TypeError, tempfunc)

    def test_lsubfield_imul_rep(self):
        '''Tests restraint on max modifying repetition of a subfield.'''
        def tempfunc():
            atst = self.agrouptest.default
            atst.tr1.td1.max = 2
            atst.tr1.td1 *= 3
        self.assertRaises(TypeError, tempfunc)

    def test_lfield_string_mod(self):
        '''Tests a modifying string substitution of a field.'''
        atst = self.modtemplate1.title % 'Test Page'
        self.assertEqual(atst.render(), self.modtest5)

    def test_lfield_string_imod(self):
        '''Tests a modifying string substitution of a field.'''
        self.modtemplate1.title %= 'Test Page'
        self.assertEqual(self.modtest5, self.modtemplate1.title.pipe())

    def test_lsubfield_string_imod(self):
        '''Tests a modifying string substitution of a subfield.'''
        self.agrouptest.tr1.td1 %= 'Test Page'
        self.assertEqual(self.groupmod, self.agrouptest.tr1.td1.pipe())

    def test_lroot_string_mod(self):
        '''Raises TypeError if string passed to a root Template.'''
        def tempfunc():
            return self.gentemplate % 'Test Page'
        self.assertRaises(TypeError, tempfunc)

    def test_lfield_mod_str_wrongtype(self):
        '''Raises TypeError if wrong type passed to a field.'''
        def tempfunc():
            return self.gentemplate.content % ['Test Page']
        self.assertRaises(TypeError, tempfunc)

    def test_lroot_group_str_mod_toofew(self):
        '''Raises TypeError if string passed to root containing only group.'''
        def tempfunc():
            return self.agrouptest % 'Test Page'
        self.assertRaises(TypeError, tempfunc)

    def test_lfield_group_str_mod_toofew(self):
        '''Raises TypeError if string passed to a group.'''
        def tempfunc():
            return self.agrouptest.tr1 % 'Test Page'
        self.assertRaises(TypeError, tempfunc)

    def test_lroot_tuple_imod(self):
        '''Tests a modifying tuple substitution of a root Template.'''
        self.gentemplate %= self.modtup
        self.assertEqual(self.modtest2, self.gentemplate.pipe())

    def test_lgroup_tuple_imod(self):
        '''Tests a modifying tuple substitution of a group.'''
        self.agrouptest.tr1 %= self.modtup
        self.assertEqual(self.groupmod2, self.agrouptest.pipe())

    def test_lroot_tuple_tuple_imod_expand(self):
        '''Tests a modifying tuple tuple substitution/expansion of root.'''
        self.modtemplate3 %= ('Title', ('Test1', 'Test2'),
            ('Test1', 'Test2'))
        self.assertEqual(self.modtest3, self.modtemplate3.pipe())

    def test_lgroup_tuple_tuple_imod_expand(self):
        '''Tests a modifying tuple tuple substitution/expansion of a group.'''
        self.agrouptest.tr1 %= (('Test1', 'Test2'), ('Test1', 'Test2'))
        self.assertEqual(self.groupmod3, self.agrouptest.pipe())

    def test_lroot_tuple_mod_wrongtype(self):
        '''Raises TypeError if wrong type in tuple is subbed into root.'''
        def tempfunc():
            return self.gentemplate % (set(['Test Page']),)
        self.assertRaises(TypeError, tempfunc)

    def test_lgroup_tuple_mod_wrongtype(self):
        '''Raises TypeError if wrong type in tuple is subbed into group.'''
        def tempfunc():
            return self.agrouptest.tr1 % (set(['Test Page']),)
        self.assertRaises(TypeError, tempfunc)

    def test_lroot_tuple_mod_toomany(self):
        '''Raises TypeError if too many items in tuple are subbed in root.'''
        def tempfunc():
            return self.gentemplate % ('Test', 'Test', 'Too Many')
        self.assertRaises(TypeError, tempfunc)

    def test_lgroup_tuple_mod_toomany(self):
        '''Raises TypeError if too many items in tuple are subbed in group.'''
        def tempfunc():
            return self.agrouptest.tr1 % ('Test', 'Test', 'Test')
        self.assertRaises(TypeError, tempfunc)

    def test_lroot_tuple_mod_toofew(self):
        '''Raises TypeError if too few items in tuple are subbed into root.'''
        def tempfunc():
            return self.gentemplate % ('Test Page')
        self.assertRaises(TypeError, tempfunc)

    def test_lgroup_tuple_mod_toofew(self):
        '''Raises TypeError if too few items in tuple are subbed into group.'''
        def tempfunc():
            return self.agrouptest.tr1 % ('Test Page')
        self.assertRaises(TypeError, tempfunc)

    def test_lroot_dict_imod(self):
        '''Tests a modifying dictionary substitution of a root Template.'''
        self.gentemplate %= self.moddict
        self.assertEqual(self.modtest2, self.gentemplate.pipe())

    def test_lgroup_dict_imod(self):
        '''Tests a modifying dict substitution of a group Template.'''
        self.agrouptest.tr1 %= self.groupdict
        self.assertEqual(self.groupmod2, self.agrouptest.pipe())

    def test_lroot_dict_tuple_imod_expand(self):
        '''Tests a modifying dictionary tuple sub/expansion of a root.'''
        self.modtemplate3 %= {'title': 'Title', 'content': ('Test1', 'Test2'),
            "content2": ('Test1', 'Test2')}
        self.assertEqual(self.modtest3, self.modtemplate3.pipe())

    def test_lgroup_dict_tuple_imod_expand(self):
        '''Tests a modifying dict tuple substitution/expansion of a group.'''
        self.agrouptest.tr1 %= {'td1': ('Test1', 'Test2'),
            'td2': ('Test1', 'Test2')}
        self.assertEqual(self.groupmod3, self.agrouptest.pipe())

    def test_lroot_dict_mod_wrongtype(self):
        '''Raises TypeError if wrong type in a dict are subbed into a root.'''
        def tempfunc():
            return self.gentemplate % {'content': set(['Test'])}
        self.assertRaises(TypeError, tempfunc)

    def test_lgroup_dict_mod_wrongtype(self):
        '''Raises TypeError if wrong type in a dict are subbed into a group.'''
        def tempfunc():
            return self.agrouptest.tr1 % {'td1': set(['Test Page'])}
        self.assertRaises(TypeError, tempfunc)

    def test_lroot_dict_mod_toomany(self):
        '''Raises TypeError if too many items in dict are subbed into root.'''
        def tempfunc():
            return self.gentemplate % {'content': 'Content Goes Here',
                'title': 'Test Page', 'test': 'Too many'}
        self.assertRaises(TypeError, tempfunc)

    def test_lgroup_dict_mod_toomany(self):
        '''Raises TypeError if too many items in dict are subbed into group.'''
        def tempfunc():
            return self.agrouptest.tr1 % {'td1': 'Content Goes Here',
                'td2': 'Test Page', 'td3': 'Too many'}
        self.assertRaises(TypeError, tempfunc)

    def test_lroot_dict_mod_toofew(self):
        '''Raises TypeError if too few items in dict are subbed into root.'''
        def tempfunc():
            return self.gentemplate % {'content': 'Content'}
        self.assertRaises(TypeError, tempfunc)

    def test_lgroup_dict_mod_toofew(self):
        '''Raises TypeError if too few items in dict are subbed into group.'''
        def tempfunc():
            return self.gentemplate % {'content': 'Content'}
        self.assertRaises(TypeError, tempfunc)

    def test_lroot_imod_mass_temp_subs(self):
        '''Tests a mass templates/substitution assignment on a root.'''
        self.rss.item %= {
            'templates': {
                'title': {'text': 'Example Title: $content'},
                'ipubdate': {'text': '$month $day, $year'},
                'link': {'text': 'http://www.example.com/rss/$id'}},
            'subs': (
                ({'text': {'content': 'First Example'}},
                {'text': {'id': '5423092'}},
                'http://www.example.com/rss/5423092',
                'Example of assigning text to a field.',
                {'text': {'month': 'June', 'day': '06', 'year': '2006'}}),
                ({'text': {'content': 'Second Example'}},
                {'text': {'id': '5423093'}},
                'http://www.example.com/rss/5423093',
                'Example of group repetition.',
                {'text': {'month': 'June', 'day': '06', 'year': '2006'}}))}
        self.assertEqual(u('''<rss version="2.0">
<channel>
<title>Example</title>
<link>http://www.example.org/</link>
<description>RSS Example</description>
<language>en-us</language>
<pubDate id="cpubdate">$month $day, $year</pubDate>
<lastBuildDate id="lastbuilddate">%(month)s %(day)s, %(year)s</lastBuildDate>
<item class="item">
<title id="title">Example Title: First Example</title>
<link id="link">http://www.example.com/rss/5423092</link>
<guid id="guid" isPermaLink="true">http://www.example.com/rss/5423092</guid>
<description id="description">Example of assigning text to a field.</description>
<pubDate id="ipubdate">June 06, 2006</pubDate>
</item>
<item class="item">
<title id="title">Example Title: Second Example</title>
<link id="link">http://www.example.com/rss/5423093</link>
<guid id="guid" isPermaLink="true">http://www.example.com/rss/5423093</guid>
<description id="description">Example of group repetition.</description>
<pubDate id="ipubdate">June 06, 2006</pubDate>
</item>
</channel>
</rss>'''), self.rss.pipe())

    def test_lroot_imod_extended_format(self):
        '''Tests an extended format substitution on a root.'''
        self.subcombotest %= {
            'grouptest': {
                'groupfield1': {
                    'template': {
                        'text': 'This %(one)s is the %(num)s column.',
                        'attrib': {'title': 'This %(one)s is column %(num)s'}
                    }, 'sub': ({
                        'text': {'one': 'first test', 'num': 'first'},
                        'attrib': {
                            'align': 'left',
                            'title': {'one': 'column', 'num': 'first'}}}, {
                        'text': 'This second test is the second column.',
                        'attrib': {
                            'align': 'right',
                            'title': {'one': 'col', 'num': 'second'}}})},
                    'groupfield2': {
                        'template': {
                            'text': 'This $test is a $fill.',
                            'attrib': {
                                'title': 'This %(one)s is next.'}},
                        'sub': ({
                            'text': 'This test is first.',
                            'attrib': {
                                'align': 'left',
                                'title': {'one': 'test'}}}, {
                            'text': {'test': 'coltest', 'fill': 'next'},
                            'attrib': {
                                 'href': {'link': 'linktest'},
                                 'title': {'other': 'Link Test'}}})}},
            'fieldtest': {
                'template': {
                    'text': 'This %(run)s is a %(test)s.',
                    'attrib': {
                        'href': 'caliban.html#%(link)s',
                        'title': 'This link goes to %(other)s'}},
                'sub': ({
                    'text': {'test': 'first test', 'run': 'template'},
                    'attrib': {
                        'href': {'link': 'test'},
                        'title': {'other': 'test'}}}, {
                    'text': {'test': 'link test', 'run': 'other template'},
                    'attrib': {
                         'href': {'link': 'linktest'},
                         'title': {'other': 'Link Test'}}})}}
        self.assertEqual(
            u('<body><a id="fieldtest" href="caliban.html#test" title="This '
        'link goes to test">This template is a first test.</a><a '
        'id="fieldtest" href="caliban.html#linktest" title="This link goes to '
        'Link Test">This other template is a link test.</a><tr '
        'class="grouptest"><td id="groupfield1" align="left" title="This '
        'column is column first">This first test is the first column.</td>'
        '<td id="groupfield1" align="right" title="This col is column '
        'second">This second test is the second column.</td><td '
        'id="groupfield2" align="left" title="This test is next.">This test '
        'is first.</td><td id="groupfield2">This coltest is a next.</td></tr>'
        '</body>'), self.subcombotest.pipe()
        )

    def test_lfield_imod_extended_format(self):
        '''Tests an extended format substitution on a field.'''
        self.subcombotest.fieldtest %= {
            'template': {
                'text': 'This %(run)s is a %(test)s.',
                'attrib': {
                    'href': 'caliban.html#%(link)s',
                    'title': 'This link goes to %(other)s'}},
            'sub': ({
                'text': {'test': 'first test', 'run': 'template'},
                'attrib': {
                    'href': {'link': 'test'},
                    'title': {'other': 'test'}}}, {
                'text': {'test': 'link test', 'run': 'other template'},
                'attrib': {
                     'href': {'link': 'linktest'},
                     'title': {'other': 'Link Test'}}})}
        self.assertEqual(
            u('<body><a id="fieldtest" href="caliban.html#test" '
            'title="This link goes to test">This template is a first '
            'test.</a><a id="fieldtest" href="caliban.html#linktest" '
            'title="This link goes to Link Test">This other template '
            'is a link test.</a><tr class="grouptest"><td '
            'id="groupfield1"/><td id="groupfield2"/></tr></body>'),
            self.subcombotest.pipe()
        )

    def test_lfield_imod_attrib_extended_format(self):
        '''Tests an extended attribute format substitution on a field.'''
        self.subcombotest.fieldtest %= {
            'template': {
                'attrib': {
                    'href': 'caliban.html#%(link)s',
                    'title': 'This link goes to %(other)s'}},
            'sub': ({
                'text': 'This is a test.',
                'attrib': {
                    'href': {'link': 'test'},
                    'title': {'other': 'test'}}}, {
                'text': 'This is another test.',
                'attrib': {
                     'href': {'link': 'linktest'},
                     'title': {'other': 'Link Test'}}})}
        self.assertEqual(
            u('<body><a id="fieldtest" href="caliban.html#test" title="This '
        'link goes to test">This is a test.</a><a id="fieldtest" '
        'href="caliban.html#linktest" title="This link goes to Link Test">'
        'This is another test.</a><tr class="grouptest"><td id="groupfield1"/>'
        '<td id="groupfield2"/></tr></body>'),
        self.subcombotest.pipe()
    )

    def test_lfield_imod_text_extended_format(self):
        '''Tests a text extended format substitution on a field.'''
        self.subcombotest.fieldtest %= {
            'template': {
                'text': 'This %(run)s is a %(test)s.'},
            'sub': ({
                'text': {'test': 'first test', 'run': 'template'}}, {
                'text': {'test': 'link test', 'run': 'other template'}})}
        self.assertEqual(
            u('<body><a id="fieldtest" href="atest">This template is '
            'a first test.</a><a id="fieldtest" href="atest">This other'
            ' template is a link test.</a><tr class="grouptest"><td id'
            '="groupfield1"/><td id="groupfield2"/></tr></body>'),
            self.subcombotest.pipe()
        )

    def test_lgroup_imod_combo_extended_format(self):
        '''Tests an extended format substitution on a group.'''
        self.subcombotest.grouptest %= {
                'groupfield1': {
                    'template': {
                        'text': 'This %(one)s is the %(num)s column.',
                        'attrib': {'title': 'This %(one)s is column %(num)s'}
                     }, 'sub': ({
                        'text': {'one': 'first test', 'num': 'first'},
                        'attrib': {
                            'align': 'left',
                            'title': {'one': 'column', 'num': 'first'}}}, {
                        'text': 'This second test is the second column.',
                        'attrib': {
                            'align': 'right',
                            'title': {'one': 'col', 'num': 'second'}}})},
                    'groupfield2': {
                        'template': {
                            'text': 'This $test is a $fill.',
                            'attrib': {
                                'title': 'This %(one)s is next.'}},
                        'sub': ({
                            'text': 'This test is first.',
                            'attrib': {
                                'align': 'left',
                                'title': {'one': 'test'}}}, {
                            'text': {'test': 'coltest', 'fill': 'next'},
                            'attrib': {
                                 'href': {'link': 'linktest'},
                                 'title': {'other': 'Link Test'}}})}}
        self.assertEqual(
            u('<body><a id="fieldtest" href="atest"/><tr class="grouptest">'
            '<td id="groupfield1" align="left" title="This column is column '
            'first">'
            'This first test is the first column.</td><td id="groupfield1" '
            'align="right" title="This col is column second">This second test'
            ' is the second column.</td><td id="groupfield2" align="left" '
            'title="This test is next.">This test is first.</td><td '
            'id="groupfield2">This coltest is a next.</td></tr></body>'),
            self.subcombotest.pipe()
        )

    def test_lfield_pow(self):
        '''Tests a expansion of a field.'''
        atst = self.powtemplate.content ** self.powtup
        self.assertEqual(self.powtest1, atst.render())

    def test_lfield_ipow(self):
        '''Tests a modifying expansion of a field.'''
        self.powtemplate.content **= self.powtup
        self.assertEqual(self.powtest1, self.powtemplate.content.pipe())

    def test_lgroup_ipow(self):
        '''Tests a modifying expansion of a group.'''
        self.agrouptest.tr1 **= self.grouptup
        self.assertEqual(self.groupow, self.agrouptest.tr1.pipe())

    def test_lfield_pow_rep(self):
        '''Raises TypeError if field expansion exceeds max repetitions.'''
        def tempfunc():
            atst = self.powtemplate.default
            atst.content.max = 1
            atst.content ** self.powtup
        self.assertRaises(TypeError, tempfunc)

    def test_lgroup_pow_rep(self):
        '''Raises TypeError if group expansion exceeds max repetitions.'''
        def tempfunc():
            atst = self.agrouptest.default
            atst.tr1.max = 1
            return atst.tr1 ** self.grouptup
        self.assertRaises(TypeError, tempfunc)

    def test_lfield_pow_wrongtype(self):
        '''Raises TypeError if wrong type passed in for field expansion.'''
        def tempfunc():
            return self.powtemplate ** 'This is a test.'
        self.assertRaises(TypeError, tempfunc)

    def test_lgroup_pow_wrongtype(self):
        '''Raises TypeError if wrong type passed in for group expansion.'''
        def tempfunc():
            return self.agrouptest.tr1 ** 'This is a test.'
        self.assertRaises(TypeError, tempfunc)

    def test_lfield_delattr_dict(self):
        '''Tests deletion of field attributes from internal dict.'''
        atst = self.btemplate.default
        del atst.content
        self.assertEqual(False, 'content' in atst._fielddict)

    def test_lgroup_delattr_dict(self):
        '''Tests deletion of group attributes from internal dict.'''
        atst = self.agrouptest.default
        del atst.tr1
        self.assertEqual(False, 'tr1' in atst._fielddict)

    def test_lsubfield_delattr_dict(self):
        '''Tests deletion of group attributes from internal dict.'''
        atst = self.agrouptest.default
        del atst.tr1.td1
        self.assertEqual(False, 'td1' in atst.tr1._fielddict)

    def test_lfield_delattr(self):
        '''Tests deletion of field attributes from internal list.'''
        atst = self.btemplate.default
        del atst.content
        self.assertEqual(False, 'content' in atst)

    def test_lgroup_delattr(self):
        '''Tests deletion of group attributes from internal list.'''
        atst = self.agrouptest.default
        del atst.tr1
        self.assertEqual(False, 'tr1' in atst)

    def test_lsubfield_delattr(self):
        '''Tests deletion of subfield attributes from internal list.'''
        atst = self.agrouptest.default
        del atst.tr1.td1
        self.assertEqual(False, 'td1' in atst.tr1)

    def test_lattribute_get(self):
        '''Tests accessing XML attribute attributes.'''
        atst = Template('<table><tr class="tr1"><td id="td1" '
        'align="left"/><td id="td2"/></tr></table>')
        self.assertEqual(atst.tr1.td1.align, 'left')

    def test_lattribute_set(self):
        '''Tests setting XML attribute attributes.'''
        atst = Template('<table><tr class="tr1"><td id="td1" '
        'align="left"/><td id="td2"/></tr></table>')
        atst.tr1.td1.align = 'right'
        self.assertEqual(atst.tr1.td1.align, 'right')

    def test_lattribute_set_dict(self):
        '''Tests setting XML attribute attributes with a dict.'''
        atst = Template('<table><tr class="tr1"><td id="td1" '
        'align="left"/><td id="td2"/></tr></table>')
        atst.tr1.td1.atemplates({'align': 'This is $dir'})
        atst.tr1.td1.align = {'dir': 'right'}
        self.assertEqual(atst.tr1.td1.align, 'This is right')

    def test_lfield_delete_attribute(self):
        '''Tests deletion of XML attribute attributes.'''
        atst = Template('<html><body><p align="left" id="content"></p>'
            '</body></html>')
        del atst.content.align
        self.assertEqual(False, hasattr(atst.content, 'align'))

    def test_lsubfield_delete_attribute(self):
        '''Tests deletion of subfield XML attribute attributes.'''
        atst = Template('<table><tr class="tr1"><td id="td1" '
        'align="left"/><td id="td2"/></tr></table>')
        del atst.tr1.td1.align
        self.assertEqual(False, hasattr(atst.tr1.td1, 'align'))

    def test_lattribute_get_key_noauto(self):
        '''Tests getting XML attributes via key w/o automagic.'''
        atst = Template('<table><tr class="tr1"><td id="td1" '
        'align="left"/><td id="td2"/></tr></table>')
        self.assertEqual(atst['tr1']['td1']['align'], 'left')

    def test_lattribute_set_key_noauto(self):
        '''Tests setting attributes via key w/o automagic.'''
        atst = Template('<table><tr class="tr1"><td id="td1" '
        'align="left"/><td id="td2"/></tr></table>', False)
        atst['tr1']['td1']['align'] = 'right'
        self.assertEqual(atst['tr1']['td1']['align'], 'right')

    def test_lattribute_set_key_noauto_dict(self):
        '''Tests setting attributes via key w/o automagic with a dict.'''
        atst = Template('<table><tr class="tr1"><td id="td1" '
        'align="left"/><td id="td2"/></tr></table>', False)
        atst['tr1']['td1'].atemplates({'align': 'This is $dir'})
        atst['tr1']['td1']['align'] = {'dir': 'right'}
        self.assertEqual(atst['tr1']['td1']['align'], 'This is right')

    def test_lfield_attribute_delete_key_noauto(self):
        '''Tests deletion of field XML attributes via key w/o automagic.'''
        atst = Template('<html><body><p align="left" id="content"></p>'
            '</body></html>', False)
        del atst['content']['align']
        self.assertEqual(False, 'align' in atst['content'])

    def test_lsubfield_attribute_delete_key_noauto(self):
        '''Tests deletion of subfield XML attributes via key on automagic.'''
        atst = Template('<table><tr class="tr1"><td id="td1" '
        'align="left"/><td id="td2"/></tr></table>', False)
        del atst['tr1']['td1']['align']
        self.assertEqual(False, 'align' in atst['tr1']['td1'])

    def test_lattribute_get_key(self):
        '''Tests getting XML attributes via key.'''
        atst = Template('<table><tr class="tr1"><td id="td1" ' \
        'align="left"/><td id="td2"/></tr></table>')
        self.assertEqual(atst.tr1.td1['align'], 'left')

    def test_lattribute_set_key(self):
        '''Tests setting XML attributes via key.'''
        atst = Template('<table><tr class="tr1"><td id="td1" '
        'align="left"/><td id="td2"/></tr></table>')
        atst.tr1.td1['align'] = 'right'
        self.assertEqual(atst.tr1.td1['align'], 'right')

    def test_lattribute_set_key_dict(self):
        '''Tests setting attributes via key with a dict.'''
        atst = Template('<table><tr class="tr1"><td id="td1" '
        'align="left"/><td id="td2"/></tr></table>')
        atst.tr1.td1.atemplates({'align': 'This is $dir'})
        atst.tr1.td1['align'] = {'dir': 'right'}
        self.assertEqual(atst.tr1.td1['align'], 'This is right')

    def test_lfield_attribute_delete_key(self):
        '''Tests deletion of field XML attributes via key.'''
        atst = Template('<html><body><p align="left" id="content"></p>'
            '</body></html>')
        del atst.content['align']
        self.assertEqual(False, hasattr(atst.content, 'align'))

    def test_lsubfield_attribute_delete_key(self):
        '''Tests deletion of subfield XML attributes via key.'''
        atst = Template('<table><tr class="tr1"><td id="td1" '
        'align="left"/><td id="td2"/></tr></table>')
        del atst.tr1.td1['align']
        self.assertEqual(False, hasattr(atst.tr1.td1, 'align'))

    def test_lattribute_len(self):
        '''Tests discovering the number of XML attributes in a field.'''
        atst = Template('<table><tr class="tr1"><td id="td1" '
            'align="left"/><td id="td2"/></tr></table>')
        self.assertEqual(len(atst.tr1.td1), 2)

    def test_lattribute_iter(self):
        '''Tests accessing XML attributes via iterator.'''
        atst = Template('<table><tr class="tr1"><td id="td1" '
            'align="left"/><td id="td2"/></tr></table>')
        test = [i for i in atst.tr1.td1]
        self.assertEqual(['id', 'align'], test)

    def test_lattribute_contains(self):
        '''Tests if an attribute is in a field or not.'''
        atst = Template('<table><tr class="tr1"><td id="td1" '
            'align="left"/><td id="td2"/></tr></table>')
        self.assertEqual(True, 'align' in atst.tr1.td1)

    def test_lattribute_contains_not(self):
        '''Tests if an attribute is in a field or not.'''
        atst = Template('<table><tr class="tr1"><td id="td1" '
            'align="left"/><td id="td2"/></tr></table>')
        self.assertEqual(False, 'align' in atst.tr1)

    def test_lupdate(self):
        '''Tests mass addition of XML attributes.'''
        test = Template('<head><link id="test"/></head>')
        test.test.update({'href': 'thing.css', 'type': 'text/css'})
        self.assertEqual(
            u('<head><link id="test" href="thing.css" type="text/css"/>'
            '</head>'),
            test.render()
        )

    def test_lupdate_wrong_type(self):
        '''Tests update to see if it throws exception with wrong type.'''
        def tempfunc():
            '''Stub'''
            test = Template('<head><link id="test"/></head>')
            test.test.update(tuple())
        self.assertRaises(TypeError, tempfunc)

    def test_lfield_purge(self):
        '''Tests if an XML attribute is purged from a field or not.'''
        final = u('<body><a/><tr class="grouptest"><td id="groupfield1"/>'
             '<td id="groupfield2"/></tr></body>')
        self.subcombotest.fieldtest.purge('href', 'id')
        self.assertEqual(final, self.subcombotest.pipe())

    def test_lsubfield_purge(self):
        '''Tests if an XML attribute is purged from a field or not.'''
        final = u('<body><a id="fieldtest" href="atest"/>'
             '<tr class="grouptest"><td/><td id="groupfield2"/></tr></body>')
        self.subcombotest.grouptest.groupfield1.purge('id')
        self.assertEqual(final, self.subcombotest.pipe())

    def test_lroot_nomarks(self):
        '''Tests deletion of marks and groupmarks of a root's children.'''
        final = u('<body><a href="atest"/><tr><td/><td/></tr></body>')
        del self.subcombotest.mark
        del self.subcombotest.groupmark
        self.assertEqual(final, self.subcombotest.pipe())

    def test_lgroup_nomarks(self):
        '''Tests deletion of marks and groupmarks of a field's children.'''
        final = u('<body><a id="fieldtest" href="atest"/><tr><td/><td/>'
            '</tr></body>')
        del self.subcombotest.grouptest.mark
        del self.subcombotest.grouptest.groupmark
        self.assertEqual(final, self.subcombotest.pipe())

    def test_lfield_nomarks(self):
        '''Tests deletion of marks of a field.'''
        final = u('<body><a href="atest"/><tr class="grouptest">'
        '<td id="groupfield1"/><td id="groupfield2"/></tr></body>')
        del self.subcombotest.fieldtest.mark
        self.assertEqual(final, self.subcombotest.pipe())

    def test_lsubfield_nomarks(self):
        '''Tests deletion of marks of a subfield.'''
        final = '<body><a id="fieldtest" href="atest"/>' \
            '<tr class="grouptest"><td/><td id="groupfield2"/></tr></body>'
        del self.subcombotest.grouptest.groupfield1.mark
        self.assertEqual(final, self.subcombotest.pipe())

    def test_latemplate_interpolation(self):
        '''Tests substituting attributes text with string interpolation.'''
        atst = self.gentemplate.default
        atst.content.atemplates({'span': '%(direction)s'})
        atst.content.update({'span': {'direction': 'left'}})
        self.assertEqual(
            atst.content.render(), '<p id="content" span="left"/>'
        )

    def test_latemplate_string_template(self):
        '''Tests substituting attributes text with string.Template.'''
        atst = self.gentemplate.default
        atst.content.atemplates({'span': '$direction'})
        atst.content.update({'span': {'direction': 'left'}})
        self.assertEqual(atst.content.render(),
            '<p id="content" span="left"/>')

    def test_latemplate_source_interpolation(self):
        '''Tests getting attribute template from inline text in the source.'''
        final = u('<body><p id="content" title="This run is a test."/></body>')
        atst = Template('<body><p id="content" title="This %(t1)s is a '
            '%(t2)s"/></body>')
        atst.content.update({'title': {'t1': 'run', 't2': 'test.'}})
        self.assertEqual(final, atst.render())

    def test_latemplate_source_string_template(self):
        '''Tests getting attribute template from inline text in the source.'''
        final = u('<body><p id="content" title="This run is a test."/></body>')
        atst = Template('<body><p id="content" title="This $t1 is a '
            '$t2"/></body>')
        atst.content.update({'title': {'t1': 'run', 't2': 'test.'}})
        self.assertEqual(final, atst.render())

    def test_latemplate_wrongtype(self):
        '''Tests substituting inline attribute text.'''
        def tempfunc():
            atst = self.gentemplate.default
            atst.content.atemplates('span')
        self.assertRaises(TypeError, tempfunc)

    def test_latemplate_nodelimiter(self):
        '''Tests substituting inline attribute text.'''
        def tempfunc():
            atst = self.gentemplate.default
            atst.content.atemplates({'span': 'direction'})
        self.assertRaises(TypeError, tempfunc)

    def test_lauto_auto_off_attribute(self):
        '''Tests if auto turns off magical attributes for attributes.'''
        atst = Template('<html><head><title id="title" style="style"/>'
            '</head><body><p id="content"/></body></html>', False)
        self.assertEqual(hasattr(atst[0], 'style'), False)

    def test_lfield_getitem(self):
        '''Tests getting field contents by index.'''
        atst = self.btemplate.default
        atst.content %= 'Test'
        self.assertEqual('Test', atst[0].text)

    def test_lgroup_getitem(self):
        '''Tests getting group contents by index.'''
        atst = self.agrouptest.default
        self.assertEqual(True, hasattr(atst[0], 'td1'))

    def test_lsubfield_getitem(self):
        '''Tests getting subfield contents by index.'''
        atst = self.agrouptest.default
        atst.tr1.td1 %= 'Test'
        self.assertEqual('Test', atst[0][0].text)

    def test_lfield_setitem(self):
        '''Tests setting field contents by index.'''
        atst = self.btemplate.default
        atst[0] %= 'Test'
        self.assertEqual('Test', atst[0].text)

    def test_lgroup_setitem(self):
        '''Tests setting group contents by index.'''
        atst = self.agrouptest.default
        atst[0].id = 'Test'
        self.assertEqual('Test', atst[0].id)

    def test_lsubfield_setitem(self):
        '''Tests setting subfield contents by index.'''
        atst = self.agrouptest.default
        atst[0][0] %= 'Test'
        self.assertEqual('Test', atst[0][0].text)

    def test_lfield_delitem(self):
        '''Tests deleting fields by index.'''
        atst = self.btemplate.default
        del atst[0]
        self.assertEqual(False, hasattr(atst, 'content'))

    def test_lgroup_delitem(self):
        '''Tests deleting groups by index.'''
        atst = self.agrouptest.default
        del atst[0]
        self.assertEqual(False, hasattr(atst, 'tr1'))

    def test_lsubfield_delitem(self):
        '''Tests deleting subfields by index.'''
        atst = self.agrouptest.default
        del atst[0][0]
        self.assertEqual(False, hasattr(atst.tr1, 'td1'))

    def test_lfield_getitem_key(self):
        '''Tests getting field contents by key.'''
        atst = self.btemplate.default
        atst.content %= 'Test'
        self.assertEqual('Test', atst['content'].text)

    def test_lgroup_getitem_key(self):
        '''Tests getting group contents by key.'''
        atst = self.agrouptest.default
        self.assertEqual(True, hasattr(atst['tr1'], 'td1'))

    def test_lsubfield_getitem_key(self):
        '''Tests getting subfield contents by key.'''
        atst = self.agrouptest.default
        atst.tr1.td1 %= 'Test'
        self.assertEqual('Test', atst['tr1']['td1'].text)

    def test_lfield_setitem_key(self):
        '''Tests setting field contents by key.'''
        atst = self.btemplate.default
        atst['content'] %= 'Test'
        self.assertEqual('Test', atst['content'].text)

    def test_lgroup_setitem_key(self):
        '''Tests setting group contents by key.'''
        atst = self.agrouptest.default
        atst['tr1'].id = 'Test'
        self.assertEqual('Test', atst['tr1'].id)

    def test_lsubfield_setitem_key(self):
        '''Tests setting subfield contents by key.'''
        atst = self.agrouptest.default
        atst['tr1']['td1'] %= 'Test'
        self.assertEqual('Test', atst['tr1']['td1'].text)

    def test_lfield_delitem_key(self):
        '''Tests deleting fields by key.'''
        atst = self.btemplate.default
        del atst['content']
        self.assertEqual(False, hasattr(atst, 'content'))

    def test_lgroup_delitem_key(self):
        '''Tests deleting groups by key.'''
        atst = self.agrouptest.default
        del atst['tr1']
        self.assertEqual(False, hasattr(atst, 'tr1'))

    def test_lgroup_field_delitem_key(self):
        '''Tests deleting subfield by key.'''
        atst = self.agrouptest.default
        del atst['tr1']['td1']
        self.assertEqual(False, hasattr(atst.tr1, 'td1'))

    def test_lroot_len(self):
        '''Tests getting number of fields in a root Template.'''
        atst = self.btemplate.default
        self.assertEqual(1, len(atst))

    def test_lgroup_len(self):
        '''Tests getting number of fields in a group.'''
        atst = self.agrouptest.default
        self.assertEqual(2, len(atst.tr1))

    def test_lcontains_true(self):
        '''Tests if a field of a certain name is in a root Template.'''
        atst = self.btemplate.default
        val = 'content' in atst
        self.assertEqual(True, val)

    def test_lcontains_false(self):
        '''Tests if a field of a certain name is not in a root Template.'''
        atst = self.btemplate.default
        val = 'content1' in atst
        self.assertEqual(False, val)

    def test_liter_false(self):
        '''Tests if an iterator finds false values.'''
        atst = self.btemplate.default
        val = False
        for i in atst:
            if i['id'] == 'content1':
                val = True
        self.assertEqual(False, val)

    def test_liter_true(self):
        '''Tests if an iterator finds true values.'''
        atst = self.btemplate.default
        val = False
        for i in atst:
            if i['id'] == 'content':
                val = True
        self.assertEqual(True, val)

    def test_liter_assign(self):
        '''Tests if an iterator assigns correctly.'''
        atst = self.btemplate.default
        for i in atst:
            i %= 'Test'
        self.assertEqual('Test', atst.content.text)

    def test_ltemplates(self):
        '''Tests batch addition of templates to a root Template.'''
        self.subcombotest.templates({
            'grouptest': {
                'groupfield1': {
                    'text': 'This %(one)s is the %(num)s column.',
                    'attrib': {'title': 'This %(one)s is column %(num)s'}},
                'groupfield2': {
                    'text': 'This $test is a $fill.',
                    'attrib': {
                        'title': 'This %(one)s is next.'}}},
            'fieldtest': {
                'text': 'This %(run)s is a %(test)s.',
                'attrib': {
                    'href': 'caliban.html#%(link)s',
                    'title': 'This link goes to %(other)s'}}})
        self.subcombotest %= {
            'grouptest': {
                'groupfield1': {
                    'sub': ({
                        'text': {'one': 'first test', 'num': 'first'},
                        'attrib': {
                            'align': 'left',
                            'title': {'one': 'column', 'num': 'first'}}}, {
                        'text': 'This second test is the second column.',
                        'attrib': {
                            'align': 'right',
                            'title': {'one': 'col', 'num': 'second'}}})},
                    'groupfield2': {
                        'sub': ({
                            'text': 'This test is first.',
                            'attrib': {
                                'align': 'left',
                                'title': {'one': 'test'}}}, {
                            'text': {'test': 'coltest', 'fill': 'next'},
                            'attrib': {
                                 'href': {'link': 'linktest'},
                                 'title': {'other': 'Link Test'}}})}},
            'fieldtest': {
                'sub': ({
                    'text': {'test': 'first test', 'run': 'template'},
                    'attrib': {
                        'href': {'link': 'test'},
                        'title': {'other': 'test'}}}, {
                    'text': {'test': 'link test', 'run': 'other template'},
                    'attrib': {
                         'href': {'link': 'linktest'},
                         'title': {'other': 'Link Test'}}})}}
        self.assertEqual(
        u('<body><a id="fieldtest" href="caliban.html#test" '
        'title="This link goes to test">This template is a first test.</a><a '
        'id="fieldtest" href="caliban.html#linktest" title="This link goes to '
        'Link Test">This other template is a link test.</a><tr '
        'class="grouptest"><td id="groupfield1" align="left" title="This '
        'column is column first">This first test is the first column.</td>'
        '<td id="groupfield1" align="right" title="This col is column '
        'second">This second test is the second column.</td><td '
        'id="groupfield2" align="left" title="This test is next.">This test '
        'is first.</td><td id="groupfield2">This coltest is a next.</td></tr>'
        '</body>'), self.subcombotest.pipe()
        )

    def test_ltemplates_mixin(self):
        '''Tests batch addition of templates to a root Template.'''
        self.subcombotest.grouptest.templates({
            'groupfield1': {
                'attrib': {'title': 'This %(one)s is column %(num)s'}},
            'groupfield2': {
                'text': 'This $test is a $fill.'}})
        self.subcombotest.grouptest %= {
                'groupfield1': {
                    'sub': ({
                        'text': 'one',
                        'attrib': {
                            'align': 'left',
                            'title': {'one': 'column', 'num': 'first'}}}, {
                        'text': 'This second test is the second column.',
                        'attrib': {
                            'align': 'right',
                            'title': {'one': 'col', 'num': 'second'}}})},
                'groupfield2': {
                    'sub': ({
                        'text': 'This test is first.'}, {
                        'text': {'test': 'coltest', 'fill': 'next'}})}}
        self.assertEqual(
            u('<body><a id="fieldtest" href="atest"/><tr class="grouptest">'
        '<td id="groupfield1" align="left" title="This column is column '
        'first">one</td><td id="groupfield1" align="right" title="This col '
        'is column second">This second test is the second column.</td><td '
        'id="groupfield2">This test is first.</td><td id="groupfield2">This '
        'coltest is a next.</td></tr></body>'),
        self.subcombotest.pipe()
        )

    def test_ltemplates_toomany(self):
        '''Raises TypeError if too many templates.'''
        def tempfunc():
            '''Stub'''
            self.subcombotest.grouptest.templates({
                'groupfield1': {
                    'attrib': {'title': 'This %(one)s is column %(num)s'}},
                'groupfield2': {
                    'text': 'This $test is a $fill.'},
                'groupfield3': {
                    'text': 'This $test is a $fill.'}})
        self.assertRaises(TypeError, tempfunc)

    def test_ltemplates_wrongtype(self):
        '''Raises TypeError if wrong type passed to templates.'''
        def tempfunc():
            '''Stub'''
            self.subcombotest.grouptest.templates([
                {'title':'This %(one)s is column %(num)s'},
                {'text':'This $test is a $fill.'},
                {'text':'This $test is a $fill.'}])
        self.assertRaises(TypeError, tempfunc)

    def test_ltext_get(self):
        '''Tests if text properties are set and fetched correctly.'''
        atst = self.btemplate.default
        atst.content.text = 'Test'
        self.assertEqual('Test', atst.content.text)

    def test_ltext_delete(self):
        '''Tests if text values are deleted correctly.'''
        atst = self.btemplate.default
        atst.content.text = 'Test'
        del atst.content.text
        self.assertEqual(None, atst.content.text)

    def test_ltext_wrongtype(self):
        '''Raises a TypeError if wrong type passed to text.'''
        def tempfunc():
            self.gentemplate.content.text = [{'place':'test'}]
        self.assertRaises(TypeError, tempfunc)

    def test_ltemplate_from_src_string_template(self):
        '''Tests a template passed in with markup with the Template source.'''
        final = '<body><p id="stuff">This run is a test.</p></body>'
        ast = Template('<body><p id="stuff">This $tst1 is a $tst2</p></body>')
        ast.stuff.text = {'tst1': 'run', 'tst2': 'test.'}
        self.assertEqual(final, ast.render())

    def test_ltemplate_from_src_interpolation(self):
        '''Tests a template passed in with markup with the Template source.'''
        ast = Template(
            '<body><p id="stuff">This %(tst1)s is a %(tst2)s</p></body>'
        )
        ast.stuff.text = {'tst1': 'run', 'tst2': 'test.'}
        self.assertEqual(
            u('<body><p id="stuff">This run is a test.</p></body>'),
            ast.render()
        )

    def test_ltemplate_nodelimiter(self):
        '''Tests setting an inline template with no delimiter'''
        def tempfunc():
            self.gentemplate.content.template = 'This is a place'
        self.assertRaises(TypeError, tempfunc)

    def test_ltemplate_wrongtype(self):
        '''Tests setting an inline template with the wrong type'''
        def tempfunc():
            self.gentemplate.content.template = ['This is a place']
        self.assertRaises(TypeError, tempfunc)

    def test_ltemplate_text_interpolation(self):
        '''Tests setting inline text with a string interpolation template.'''
        atst = self.gentemplate.default
        atst.content.template = 'This is a %(place)s'
        atst.content.text = {'place': 'test'}
        self.assertEqual(
            atst.content.render(), u('<p id="content">This is a test</p>')
        )

    def test_ltemplate_text_string_template(self):
        '''Tests setting inline text with a string.Template template.'''
        atst = self.gentemplate.default
        atst.content.template = 'This is a $place'
        atst.content.text = {'place': 'test'}
        self.assertEqual(
            atst.content.render(), u('<p id="content">This is a test</p>')
        )

    def test_lroot_max(self):
        '''Tests max assignment on a root.'''
        atst = self.gentemplate.default
        atst.max = 100
        self.assertEqual(200, atst.title.max + atst.content.max)

    def test_lgroup_max(self):
        '''Tests max assignment on a group.'''
        atst = self.agrouptest.default
        atst.max = 100
        self.assertEqual(200, atst.tr1.td1.max + atst.tr1.td2.max)

    def test_lauto_auto_off_hasattr(self):
        '''Tests if auto turns off magical attributes for elements.'''
        atst = Template(
            '<html><head><title id="title"/></head><body>'
            '<p id="content"/></body></html>',
            False
        )
        self.assertEqual(hasattr(atst, 'title'), False)

    def test_lroot_append(self):
        '''Tests appending a field to a root Template.'''
        self.atemplate.append(self.atemplate.title.default)
        self.assertEqual(
            u('<html><head><title id="title"/></head><title id="title"/>'
            '</html>'),
            self.atemplate.pipe()
        )

    def test_lroot_append_element(self):
        '''Tests appending an element to a root Template.'''
        self.atemplate.append(_Element('p'))
        self.assertEqual(
            u('<html><head><title id="title"/></head><p/></html>'),
            self.atemplate.pipe()
        )

    def test_lfield_append(self):
        '''Tests appending a root Template to a field.'''
        self.atemplate.title.append(self.atemplate.default)
        self.assertEqual(
            u('<html><head><title id="title"><html><head>'
            '<title id="title"/></head></html></title></head></html>'),
            self.atemplate.pipe()
        )

    def test_lfield_append_element(self):
        '''Tests appending an element to a field.'''
        self.atemplate.title.append(_Element('p'))
        self.assertEqual(
            u('<html><head><title id="title"><p/></title></head></html>'),
            self.atemplate.pipe()
        )

    def test_lgroup_append(self):
        '''Tests appending a root Template to a group.'''
        self.agrouptest.tr1.append(self.agrouptest.default)
        self.assertEqual(
            u('<table><tr class="tr1"><td id="td1"/><td id="td2"/>'
            '<table><tr class="tr1"><td id="td1"/><td id="td2"/></tr>'
            '</table></tr></table>'),
            self.agrouptest.pipe()
        )

    def test_lgroup_append_reset_test(self):
        '''Tests appending a root Template to a group and reseting.'''
        self.agrouptest.tr1.append(self.agrouptest.default)
        test = self.agrouptest.render()
        self.agrouptest.tr1.reset()
        self.assertEqual(
            u('<table><tr class="tr1"><td id="td1"/><td id="td2"/>'
            '<table><tr class="tr1"><td id="td1"/><td id="td2"/></tr>'
            '</table></tr></table>'),
            test
        )

    def test_lgroup_append_element(self):
        '''Tests appending an element to a group.'''
        self.agrouptest.tr1.append(_Element('p'))
        self.assertEqual(
            u('<table><tr class="tr1"><td id="td1"/><td id="td2"/>'
            '<p/></tr></table>'),
            self.agrouptest.pipe()
        )

    def test_lroot_append_wrongtype(self):
        '''Raises TypeError if appending a wrong type to a root Template.'''
        def tempfunc():
            self.atemplate.append('p')
        self.assertRaises(TypeError, tempfunc)

    def test_lgroup_append_wrongtype(self):
        '''Raises TypeError if appending a wrong type to a group.'''
        def tempfunc():
            self.agrouptest.tr1.append('p')
        self.assertRaises(TypeError, tempfunc)

    def test_lfield_append_wrongtype(self):
        '''Raises TypeError if appending the wrong type to a field.'''
        def tempfunc():
            self.atemplate.title.append('p')
        self.assertRaises(TypeError, tempfunc)

    def test_lexclude(self):
        '''Tests exclusion on a root Template.'''
        a = Template()
        a.exclude('td1', 'td2')
        a.fromstring('<tr><td id="td1"/><td id="td2"/><td id="td3"/></tr>')
        self.assertEqual(True, len(a) == 1)

    def test_lexclude_live(self):
        '''Tests exclusion on a live root Template.'''
        testfilter = Template(
            '<table><tr class="tr1"><td id="td1"/>'
            '<td id="td2"/></tr><p id="r"/></table>'
        )
        testfilter.exclude('tr1', 'r')
        self.assertEqual(True, len(testfilter) == 0)

    def test_lexclude_group(self):
        '''Tests exclusion on a group.'''
        a = Template()
        a.exclude('td1', 'td2')
        a.fromstring(
            '<table><tr class="tr1"><td id="td1"/>'
            '<td id="td2"/></tr><p id="r"/></table>'
        )
        self.assertEqual(True, len(a) == 1)

    def test_lexclude_live_group(self):
        '''Tests exclusion on a live group.'''
        testfilter = Template(
            '<table><tr class="tr1"><td id="td1"/>'
            '<td id="td2"/></tr><p id="r"/></table>'
        )
        testfilter.exclude('td1', 'td2')
        self.assertEqual(True, len(testfilter) == 1)

    def test_lexclude_wrongtype(self):
        '''Raises TypeError if wrong type included.'''
        def tempfunc():
            self.agrouptest.exclude(['eeee'])
        self.assertRaises(AttributeError, tempfunc)

    def test_lroot_include(self):
        '''Tests inclusion on a Template.'''
        testfilter = Template()
        testfilter.exclude('td1')
        testfilter.fromstring('<tr><td id="td1"/><td id="td2"/></tr>')
        testfilter.include('td1')
        self.assertEqual(True, hasattr(testfilter, 'td1'))

    def test_linclude_live(self):
        '''Tests inclusion on a live Template.'''
        testfilter = Template(
            '<table><tr class="tr1"><td id="td1"/>'
            '<td id="td2"/></tr><p id="r"/></table>'
        )
        testfilter.exclude('tr1', 'r')
        testfilter.include('tr1', 'r')
        self.assertEqual(True, len(testfilter) == 2)

    def test_linclude_group(self):
        '''Tests inclusion on a group.'''
        a = Template()
        a.exclude('td1', 'td2')
        a.fromstring('<table><tr class="tr1"><td id="td1"/>' \
            '<td id="td2"/></tr><p id="r"/></table>')
        a.include('td1', 'td2')
        self.assertEqual(True, len(a.tr1) == 2)

    def test_linclude_live_group(self):
        '''Tests inclusion on a live group.'''
        testfilter = Template('<table><tr class="tr1"><td id="td1"/>' \
            '<td id="td2"/></tr><p id="r"/></table>')
        testfilter.exclude('td1', 'td2')
        testfilter.include('td1', 'td2')
        self.assertEqual(True, len(testfilter.tr1) == 2)

    def test_linclude_wrongtype(self):
        '''Tests if inclusion raises an exception with wrong type passed.'''
        def tempfunc():
            self.agrouptest.include(['eeee'])
        self.assertRaises(TypeError, tempfunc)

    def test_lgroup_under_group_proc(self):
        '''Tests proper group within group processing.'''
        testfilter = Template(
            '<div><table class="thing">'
            '<th id="thing2"/><tr class="tr1"><td id="td1"/>'
            '<td id="td2"/></tr></table></div>'
        )
        self.assertEqual(False, hasattr(testfilter.thing, 'td1'))

    def test_lgroup_under_group_proc2(self):
        '''Tests proper group within group processing again.'''
        testfilter = Template(
            '<div><table class="thing">'
            '<th id="thing2"/><tr class="tr1"><td id="td1"/>'
            '<td id="td2"/></tr></table></div>'
        )
        self.assertEqual(True, hasattr(testfilter.thing, 'thing2'))

    def test_lno_empty_group(self):
        '''Tests if empty groups are properly pruned.'''
        testfilter = Template()
        testfilter.exclude('td1')
        testfilter.fromstring(
            '<div><table class="thing"><tr class="tr1">'
            '<td id="td1"/><td id="td2"/></tr></table></div>'
        )
        testfilter.include('td1')
        self.assertEqual(False, hasattr(testfilter, 'thing'))

    def test_lhtmltemplate_output_html4(self):
        '''Tests for correct HTML 4.0 output.'''
        self.htmlt %= ('Test Page', 'Test Content')
        self.assertEqual(
            u('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 '
            'Transitional//EN">\n<html><head><link href="test.css" '
            'type="text/css"><title id="title">Test Page</title>'
            '</head><body><p id="content">Test Content</p></body></html>'),
            self.htmlt.pipe()
        )

    def test_lhtmltemplate_output_xhtml10(self):
        '''Tests for correct XHTML 1.0 output.'''

        self.htmlt %= ('Test Page', 'Test Content')
        self.assertEqual(
            u('<?xml version="1.0" encoding="utf-8"?>\n<?xml-stylesheet '
            'href="test.css" type="text/css" ?>\n<!DOCTYPE html PUBLIC '
            '"-//W3C//DTD XHTML 1.0 Strict//EN" '
            '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n<html>'
            '<head><link href="test.css" type="text/css"/><title id="title">'
            'Test Page</title></head><body><p id="content">Test Content</p>'
            '</body></html>'),
            self.htmlt.pipe(None, 'xhtml10'),
        )

    def test_lhtmltemplate_output_xhtml11(self):
        '''Tests for correct XHTML 1.1 output.'''

        self.htmlt %= ('Test Page', 'Test Content')
        self.assertEqual(
            u('<?xml version="1.0" encoding="utf-8"?>\n<?xml-stylesheet '
            'href="test.css" type="text/css" ?>\n<!DOCTYPE html PUBLIC '
            '"-//W3C//DTD XHTML 1.1//EN" '
            '"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n<html><head>'
            '<link href="test.css" type="text/css"/><title id="title">'
            'Test Page</title></head><body><p id="content">Test Content</p>'
            '</body></html>'
            ),
            self.htmlt.pipe(None, 'xhtml11')
        )

    def test_pickle(self):

        serial = pickle.dumps(self.subcombotest)
        deserial = pickle.loads(serial)
        self.assertEqual(
            deserial.render(),
            u('<body><a href="atest" id="fieldtest"/><tr class="grouptest">'
            '<td id="groupfield1"/><td id="groupfield2"/></tr></body>')
        )

    def html_pickle(self):
        serial = pickle.dumps(self.htmlt)
        deserial = pickle.loads(serial)
        self.assertEqual(
            deserial.render(),
            u('<html><head><link href="test.css" '
            'type="text/css"><title id="title"><body><p id="content">'
            '</body></head></html>')
        )


if __name__ == '__main__':
    unittest.main()