Source

webstring / webstring / tests / test_text.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
# -*- coding: utf-8 -*-
'''Unit tests for webstring'''

import unittest
try:
    import cPickle as pickle
except ImportError:
    import pickle

from webstring import TextTemplate


class Testwebstring(unittest.TestCase):

    '''Test class for webstring text template'''

    # Add tests
    groupend = (
        '<tr class="tr1"><td id="td1"></td><td id="td2"></td></tr>'
        '<tr class="tr2"><td id="td1"></td><td id="td2"></td></tr>'
    )
    altgroupend = (
        '<tr class="tr2"><td id="td1"></td><td id="td2"></td>'
        '</tr><tr class="tr1"><td id="td1"></td><td id="td2"></td></tr>'
    )
    subgroupend = '<td id="td1" /><td id="td1" />'
    addtest = (
        '<html><head><title id="title"></title></head></html><html><body>'
        '<p id="content"></p></body></html>'
    )
    altaddtest = (
        '<html><body><p id="content" /></body><head>'
        '<title id="title" /></head></html>'
    )
    field_add = '<p id="content" /><title id="title" />'
    field_group_add = (
        '<title id="title" /><tr class="tr1"><td id="td1"></td>'
        '<td id="td2"></td></tr>'
    )
    root_group_add = (
        '<html><head><title id="title"></title></head></html>'
        '<tr class="tr1"><td id="td1"></td><td id="td2"></td></tr>'
    )
    root_element_add = '<html><head><title id="title" /></head><p /></html>'
    field_element_add = '<title id="title" /><p />'
    group_element_add = (
        '<tr class="tr1"><td id="td1" /><td id="td2" /></tr><p />'
    )
    subfield_element_add = '<td id="td1" /><p />'
    # Mul tests
    multest = '<p id="content" /><p id="content" /><p id="content" />'
    root_mul = (
        '<html><head><title id="title"></title></head><html><body>'
        '<p id="content"></p></body></html></html><html><head><title '
        'id="title"></title></head><html><body><p id="content"></p></body>'
        '</html></html><html><head><title id="title"></title></head><html>'
        '<body><p id="content"></p></body></html></html>'
    )
    root_group_mul = (
        '<tr class="tr1"><td id="td1"></td><td id="td2"></td>'
        '</tr><tr class="tr1"><td id="td1"></td><td id="td2"></td></tr>'
        '<tr class="tr1"><td id="td1"></td><td id="td2"></td></tr>'
    )
    subfield_group_mul = '<td id="td1" /><td id="td1" /><td id="td1" />'
    # Mod tests
    modtest1 = (
        '<html><head><title id="title">Test Page</title>'
        '</head><body /></html>'
    )
    modtest2 = (
        '<html><head><title id="title">Test Page</title></head><html><body>'
        '<p id="content">Content Goes Here</p></body></html></html>'
    )
    modtest3 = (
        '<html><head><title id="title">Title</title></head><body><p '
        'id="content">Test1Test2</p><p id="content2">Test1Test2</p></body>'
        '</html>'
    )
    modtest5 = '<title id="title">Test Page</title>'
    groupmod = '<td id="td1">Test Page</td>'
    groupmod2 = (
        '<table><tr class="tr1"><td id="td1">Test Page</td>'
        '<td id="td2">Content Goes Here</td></tr></table>'
    )
    groupmod3 = (
        '<table><tr class="tr1"><td id="td1">Test1Test2</td>'
        '<td id="td2">Test1Test2</td></tr></table>'
    )
    # Pow tets
    powtest1 = '<p id="content">Test1</p><p id="content">Test2</p>'
    groupow = (
        '<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 = TextTemplate(
        '<html><head><title id="title">$title$</title></head>'
        '<body /></html>'
    )
    modtemplate3 = TextTemplate(
        '<html><head><title id="title">$title$</title></head><body><p '
        'id="content">$content$</p><p id="content2">$content2$</p></body>'
        '</html>'
    )
    gentemplate = TextTemplate(
        '<html><head><title id="title">$title$</title></head><html><body>'
        '<p id="content">$content$</p></body></html></html>'
    )
    atemplate = TextTemplate(
        '<html><head><title id="title">$title$</title></head></html>'
    )
    btemplate = TextTemplate(
        '<html><body><p id="content">$content$</p></body></html>'
    )
    powtemplate = TextTemplate(
        '<html><head /><body><p id="content">$content$</p></body></html>'
    )
    agrouptest = TextTemplate(
        '<table>$$tr1<tr class="tr1"><td id="td1">$td1$'
        '</td><td id="td2">$td2$</td></tr>$$</table>'
    )
    bgrouptest = TextTemplate(
        '<table>$$tr2<tr class="tr2"><td id="td1">$td1$'
        '</td><td id="td2">$td2$</td></tr>$$</table>'
    )
    subcombotest = TextTemplate(
        '<body><a id="fieldtest" href="atest">'
        '$fieldtest$</a>$$grouptest<tr class="grouptest"><td id="groupfield1">'
        '$groupfield1$</td><td id="groupfield2">$groupfield2$</td></tr>$$'
        '</body>'
    )
    # Data structures
    powdict = {'content1': 'Test1', 'content2': 'Test2'}
    powtup = ('<p id="content">Test1</p>', '<p id="content">Test2</p>')
    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_root_add(self):
        '''Tests addition of two root Templates.'''
        atst = self.atemplate + self.btemplate
        self.assertEqual(self.addtest, atst.render())

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

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

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

    def test_field_radd(self):
        '''Tests the right-side addition of two fields.'''
        self.atemplate.title %= '<title id="title" />'
        self.btemplate.content %= '<p id="content" />'
        atst = self.btemplate.content + self.atemplate.title
        self.assertEqual(self.field_add, atst.render())

    def test_field_iadd(self):
        '''Tests the modifying addition of two fields.'''
        self.atemplate.title %= '<title id="title" />'
        self.btemplate.content += self.atemplate.title
        self.assertEqual(self.field_add, self.btemplate.content.pipe())

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

    def test_group_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_group_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_subfield_add(self):
        '''Tests the addition of two subfields.'''
        self.bgrouptest.tr2.td1 %= '<td id="td1" />'
        self.agrouptest.tr1.td1 %= '<td id="td1" />'
        atst = self.agrouptest.tr1.td1 + self.bgrouptest.tr2.td1
        self.assertEqual(self.subgroupend, atst.render())

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

    def test_subfield_iadd(self):
        '''Tests the modifying addition of two subfields.'''
        self.agrouptest.tr1.td1 %= '<td id="td1" />'
        self.bgrouptest.tr2.td1 %= '<td id="td1" />'
        self.agrouptest.tr1.td1 += self.bgrouptest.tr2.td1
        self.assertEqual(self.subgroupend, self.agrouptest.tr1.td1.pipe())

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

    def test_field_group_iadd(self):
        '''Tests the modifying addition of a field and group.'''
        self.atemplate.title %= '<title id="title" />'
        self.atemplate.title += self.agrouptest.tr1
        self.assertEqual(self.field_group_add,
            self.atemplate.title.pipe())

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

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

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

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

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

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

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

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

    def test_field_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_field_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_group_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_group_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_subfield_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_subfield_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_root_rmul(self):
        '''Tests repetition of a root TextTemplate with number on the right.'''
        atst = 3 * self.gentemplate
        self.assertEqual(self.root_mul, atst.render())

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

    def test_field_mul(self):
        '''Tests the repetition of a field.'''
        self.gentemplate.content %= '<p id="content" />'
        atst = self.gentemplate.content * 3
        self.assertEqual(self.multest, atst.render())

    def test_field_rmul(self):
        '''Tests repetition of a field with the number on the right.'''
        self.gentemplate.content %= '<p id="content" />'
        atst = 3 * self.gentemplate.content
        self.assertEqual(self.multest, atst.render())

    def test_field_imul(self):
        '''Tests modifying repetition of a field.'''
        self.gentemplate.content %= '<p id="content" />'
        self.gentemplate.content *= 3
        self.assertEqual(self.multest, self.gentemplate.content.pipe())

    def test_group_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_group_imul(self):
        '''Tests modifying repetition of a group.'''
        self.agrouptest.tr1 *= 3
        self.assertEqual(self.root_group_mul, self.agrouptest.tr1.pipe())

    def test_subfield_rmul(self):
        '''Tests repetition of a subfield with the number on the right side.'''
        self.agrouptest.tr1.td1 %= '<td id="td1" />'
        atst = 3 * self.agrouptest.tr1.td1
        self.assertEqual(self.subfield_group_mul, atst.render())

    def test_subfield_mul(self):
        '''Tests the repetition of a subfield.'''
        self.agrouptest.tr1.td1 %= '<td id="td1" />'
        atst = self.agrouptest.tr1.td1 * 3
        self.assertEqual(self.subfield_group_mul, atst.render())

    def test_subfield_imul(self):
        '''Tests modifying repetition of a subfield.'''
        self.agrouptest.tr1.td1 %= '<td id="td1" />'
        self.agrouptest.tr1.td1 *= 3
        self.assertEqual(self.subfield_group_mul,
            self.agrouptest.tr1.td1.pipe())

    def test_field_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_root_imul_rep(self):
        '''
        Tests restraint on max modifying repetition of a root TextTemplate.
        '''
        def tempfunc():
            atst = self.gentemplate.default
            atst.max = 2
            atst *= 3
        self.assertRaises(TypeError, tempfunc)

    def test_group_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_subfield_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_field_string_mod(self):
        '''Tests a modifying string substitution of a field.'''
        atst = self.modtemplate1.title % '<title id="title">Test Page</title>'
        self.assertEqual(atst.render(), self.modtest5)

    def test_field_string_imod(self):
        '''Tests a modifying string substitution of a field.'''
        self.modtemplate1.title %= '<title id="title">Test Page</title>'
        self.assertEqual(self.modtest5, self.modtemplate1.title.pipe())

    def test_subfield_string_imod(self):
        '''Tests a modifying string substitution of a subfield.'''
        self.agrouptest.tr1.td1 %= '<td id="td1">Test Page</td>'
        self.assertEqual(self.groupmod, self.agrouptest.tr1.td1.pipe())

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

    def test_field_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_root_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_field_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_root_tuple_imod(self):
        '''Tests a modifying tuple substitution of a root TextTemplate.'''
        self.gentemplate %= self.modtup
        self.assertEqual(self.modtest2, self.gentemplate.pipe())

    def test_group_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_root_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_group_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_root_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_group_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_root_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_group_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_root_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_group_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_root_dict_imod(self):
        '''Tests a modifying dictionary substitution of a root TextTemplate.'''
        self.gentemplate %= self.moddict
        self.assertEqual(self.modtest2, self.gentemplate.pipe())

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

    def test_root_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_group_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_root_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_group_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_root_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_group_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_root_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_group_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_field_pow(self):
        '''Tests a expansion of a field.'''
        atst = self.powtemplate.content ** self.powtup
        self.assertEqual(self.powtest1, atst.render())

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

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

    def test_field_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_group_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_field_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_group_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_field_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_group_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_subfield_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_field_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_group_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_subfield_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_field_getitem(self):
        '''Tests getting field contents by index.'''
        atst = self.btemplate.default
        atst.content %= 'Test'
        self.assertEqual('Test', atst[0].text)

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

    def test_subfield_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_field_setitem(self):
        '''Tests setting field contents by index.'''
        atst = self.btemplate.default
        atst[0] %= 'Test'
        self.assertEqual('Test', atst[0].text)

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

    def test_subfield_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_field_delitem(self):
        '''Tests deleting fields by index.'''
        atst = self.btemplate.default
        del atst[0]
        self.assertEqual(False, hasattr(atst, 'content'))

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

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

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

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

    def test_group_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_root_len(self):
        '''Tests getting number of fields in a root TextTemplate.'''
        atst = self.btemplate.default
        self.assertEqual(1, len(atst))

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

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

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

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

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

    def test_iter_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_root_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_group_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_auto_auto_off_hasattr(self):
        '''Tests if auto turns off magical attributes for elements.'''
        atst = TextTemplate(
            '<html><head><title id="title">$title</title></head><body>'
            '<p id="content">$content</p></body></html>',
            False
        )
        self.assertEqual(hasattr(atst, 'title'), False)

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

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

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

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

    def test_root_append_wrongtype(self):
        '''
        Raises TypeError if appending a wrong type to a root TextTemplate.
        '''
        def tempfunc():
            self.atemplate.append(['p'])
        self.assertRaises(TypeError, tempfunc)

    def test_group_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_field_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_exclude(self):
        '''Tests exclusion on a root TextTemplate.'''
        a = TextTemplate()
        a.exclude('td1', 'td2')
        a.fromstring(
            '<tr><td id="td1">$td1$</td><td id="td2">$td2$</td>'
            '<td id="td3">$td3$</td></tr>'
        )
        self.assertEqual(True, len(a) == 1)

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

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

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

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

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

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

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

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

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

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

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

    def test_delattr_root_template(self):
        e = TextTemplate('$test$ is a $test2$')
        del e.test
        self.assertEqual(e._template, u' is a %s')

    def test_delattr_group_template(self):
        e = TextTemplate('$$test a$$is a $test2$')
        del e.test
        self.assertEqual(e._template, u'is a %s')


if __name__ == '__main__':
    unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.