Source

web2py-template / test_new_template.py

Full commit
  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
# coding: utf-8

import sys
argv = sys.argv
sys.argv = [argv[0]] #it is necessary to overwrite sys.argv because I want to
# get argv in this program. If you pass some arguments unittest will read and
# fail to start in most cases
import unittest

DEBUG = False

def do_it(self):
    if DEBUG:
        print '***** Input: *****'
        print self.input_
        print '***** Expected output: *****'
        print self.expected_output
        print '***** Returned output: *****'
        parsed_template = parse_template(self.input_)
        print parsed_template
        print '--------------------------------------'
    else:
        parsed_template = parse_template(self.input_)
    self.assertEquals(self.expected_output, parsed_template)


class TestParsePythonTemplateTag(unittest.TestCase):
    do_it = do_it

    def test_parse_none_should_return_empty_string(self):
        self.input_ = '''{{}}'''
        self.expected_output = ''
        self.do_it()
    
    
    def test_try_to_use_HTML_escaped_brackest_should_not_parse_as_python_code(self):
        self.input_ = '&#123&#123a=b&#125&#125'
        self.expected_output = 'response.write("&#123&#123a=b&#125&#125", escape=False)'
        self.do_it()


    def test_parse_plain_text_should_return_one_response_write_with_the_text(self):
        self.input_ = 'just plain text'
        self.expected_output = 'response.write("%s", escape=False)' % \
                               self.input_
        self.do_it()


    def test_parse_plain_text_and_one_python_command_as_response_write_with_single_quote(self):
        self.input_ = "some plain text {{='hi'}}"
        self.expected_output = '''response.write("some plain text ", escape=False)
response.write('hi')'''
        self.do_it()


    def test_parse_plain_text_and_one_python_command_as_response_write_with_double_quote(self):
        self.input_ = 'some plain text {{="hi"}}'
        self.expected_output = '''response.write("some plain text ", escape=False)
response.write("hi")'''
        self.do_it()


    def test_parse_plain_text_and_one_python_command_in_the_middle_as_response_write_with_single_quote(self):
        self.input_ = 'some plain text {{="hi"}} bla bla bla'
        self.expected_output = '''response.write("some plain text ", escape=False)
response.write("hi")
response.write(" bla bla bla", escape=False)'''
        self.do_it()


    def test_using_equal_symbol_inside_template_tag_with_single_and_double_quotes_should_return_the_same_quote_used(self):
        self.input_ = '''some plain text {{="hi"}} bla bla bla {{='hello'}}'''
        self.expected_output = '''response.write("some plain text ", escape=False)
response.write("hi")
response.write(" bla bla bla ", escape=False)
response.write('hello')'''
        self.do_it()


    def test_using_equal_symbol_inside_template_tag_with_triple_single_quotes_should_return_response_write_with_triple_quotes(self):
        self.input_ = """some plain text {{='''hi'''}} bla bla bla {{='hello'}}"""
        self.expected_output = """response.write("some plain text ", escape=False)
response.write('''hi''')
response.write(" bla bla bla ", escape=False)
response.write('hello')"""
        self.do_it()


    def test_using_equal_symbol_inside_template_tag_with_triple_double_quotes_should_return_response_write_with_triple_quotes(self):
        self.input_ = '''some plain text {{="""hi"""}} bla bla bla {{='hello'}}'''
        self.expected_output = '''response.write("some plain text ", escape=False)
response.write("""hi""")
response.write(" bla bla bla ", escape=False)
response.write('hello')'''
        self.do_it()


    def test_do_not_response_write_HTML_when_close_and_open_template_tags_are_together(self):
        self.input_ = """some plain text {{='''hi'''}}{{='hello'}}"""
        self.expected_output = """response.write("some plain text ", escape=False)
response.write('''hi''')
response.write('hello')"""
        self.do_it()


    def test_using_some_python_expression_after_equal_alias_should_response_write_this_python_expression_without_quotes(self):
        self.input_ = '''some plain text {{='hi'}}{{=python expression}}'''
        self.expected_output = '''response.write("some plain text ", escape=False)
response.write('hi')
response.write(python expression)'''
        self.do_it()


    def test_using_python_code_inside_template_tag_should_return_this_python_code_without_modifications(self):
        self.input_ = '''some plain text {{='hi'}}{{python code}}'''
        self.expected_output = '''response.write("some plain text ", escape=False)
response.write('hi')
python code'''
        self.do_it()


    def test_using_mix_of_python_string_and_other_python_code_after_equal_inside_template_tag_should_response_write_the_entire_python_expression_without_modification(self):
        self.input_ = '''some plain text {{='hi'}}{{='a%s' % bla}}'''
        self.expected_output = '''response.write("some plain text ", escape=False)
response.write('hi')
response.write('a%s' % bla)'''
        self.do_it()


    def test_putting_template_open_tags_inside_single_quote_strings_on_python_code_should_be_threated_as_strings_and_not_opening_template_tags(self):
        self.input_ = '''some plain text {{='h{{i'}}{{python code}}'''
        self.expected_output = '''response.write("some plain text ", escape=False)
response.write('h{{i')
python code'''
        self.do_it()


    def test_putting_template_close_tags_inside_single_quote_strings_on_python_code_should_be_threated_as_strings_and_not_closing_template_tags(self):
        self.input_ = '''some plain text {{='h}}i'}}{{python code}}'''
        self.expected_output = '''response.write("some plain text ", escape=False)
response.write('h}}i')
python code'''
        self.do_it()


    def test_putting_template_close_tags_inside_double_quote_strings_on_python_code_should_be_threated_as_strings_and_not_closing_template_tags(self):
        self.input_ = '''some plain text {{="h}}i"}}{{python code}}'''
        self.expected_output = '''response.write("some plain text ", escape=False)
response.write("h}}i")
python code'''
        self.do_it()


    def test_putting_one_close_bracket_inside_single_quote_strings_on_python_code_should_be_threated_as_string_and_not_part_of_a_template_tag(self):
        self.input_ = '''some plain text {{='h}i'}}{{python code}}'''
        self.expected_output = '''response.write("some plain text ", escape=False)
response.write('h}i')
python code'''
        self.do_it()


    def test_putting_one_open_bracket_inside_single_quote_strings_on_python_code_should_be_threated_as_string_and_not_part_of_a_template_tag(self):
        self.input_ = '''some plain text {{='h{i'}}{{python code}}'''
        self.expected_output = '''response.write("some plain text ", escape=False)
response.write('h{i')
python code'''
        self.do_it()


    def test_putting_a_lot_of_open_brackets_inside_single_quote_strings_on_python_code_should_be_threated_as_string_and_not_part_of_a_template_tag(self):
        self.input_ = '''some plain text {{='h{{{i'}}{{python code}}'''
        self.expected_output = '''response.write("some plain text ", escape=False)
response.write('h{{{i')
python code'''
        self.do_it()


    def test_putting_a_pair_number_of_open_brackets_inside_single_quote_strings_on_python_code_should_be_threated_as_string_and_not_part_of_a_template_tag(self):
        self.input_ = '''some plain text {{='h{{{{i'}}{{python code}}'''
        self.expected_output = '''response.write("some plain text ", escape=False)
response.write('h{{{{i')
python code'''
        self.do_it()


    def test_putting_close_template_tag_inside_triple_double_quote_string_on_python_code_should_be_threated_as_string_and_not_part_of_a_template_tag(self):
        self.input_ = '''some plain text {{="""h}}i"""}}{{python code}}'''
        self.expected_output = '''response.write("some plain text ", escape=False)
response.write("""h}}i""")
python code'''
        self.do_it()


    def test_using_close_template_tag_after_single_quote_inside_double_quote_string_on_python_code_should_not_be_threated_as_closing_template_tag(self):
        """This test avoids the bug on counting ' and " as string open/close.
        If the interpreter does not verify what open quote was used, it'll close
        the string on the first ' (after ") and causes a bug.
        """
        self.input_ = '''{{="'h}}i"}}'''
        self.expected_output = '''response.write("'h}}i")'''
        self.do_it()


    def test_using_other_quotes_on_double_quote_string_on_python_code_and_close_template_tag_inside_string_should_not_be_threated_as_closing_template_ta(self):
        self.input_ = '''{{="'h}}'i"}}'''
        self.expected_output = '''response.write("'h}}'i")'''
        self.do_it()


    def test_160_render_only_one_python_command_as_response_write12(self):
        self.input_ = """some plain text {{='''hi'''}}{{python code
more python code}}"""
        self.expected_output = """response.write("some plain text ", escape=False)
response.write('''hi''')
python code
more python code"""
        self.do_it()


    def test_python_multine_code_and_using_equal_alias_among_other_lines_of_python_code_should_return_response_write_for_equal_alias_expression_and_the_rest_of_python_code_untouched(self):
        self.input_ = """some plain text {{='''hi'''}}{{python code
more python code
='testing web2py'}}"""
        self.expected_output = """response.write("some plain text ", escape=False)
response.write('''hi''')
python code
more python code
response.write('testing web2py')"""
        self.do_it()


    def test_using_formatters_in_strings_on_python_code_should_response_write_it_without_modification(self):
        self.input_ = """some plain text {{='''hi'''}}{{='testing web2py'
='it is cool: %s' % a}}"""
        self.expected_output = """response.write("some plain text ", escape=False)
response.write('''hi''')
response.write('testing web2py')
response.write('it is cool: %s' % a)"""
        self.do_it()


    def test_using_multiline_strings_on_equal_alias_should_response_write_the_string_withtou_modification(self):
        self.input_ = """some plain text {{='''hi'''}}{{='''testing web2py
it is cool!'''}}"""
        self.expected_output = """response.write("some plain text ", escape=False)
response.write('''hi''')
response.write('''testing web2py
it is cool!''')"""
        self.do_it()


    def test_using_equal_alias_calling_other_multiline_expression_should_response_write_the_entire_expression_without_modification(self):
        self.input_ = """some plain text {{='''hi'''}}
{{=some_function('''testing web2py
it is cool!''')}}"""
        self.expected_output = """response.write("some plain text ", escape=False)
response.write('''hi''')
response.write("\\n", escape=False)
response.write(some_function('''testing web2py
it is cool!'''))"""
        self.do_it()


    def test_parse_a_code_with_just_one_comment_should_return_empty_string(self):
        self.input_ = '''{{ #bla bla bla}}'''
        self.expected_output = ''
        self.do_it()


    def test_python_comments_used_inside_template_tags_should_not_be_returned(self):
        self.input_ = """some plain text {{='hi' # some comment
='hello'}}"""
        self.expected_output = """response.write("some plain text ", escape=False)
response.write('hi' )
response.write('hello')"""
        self.do_it()


    def test_python_comments_used_inside_template_tags_should_not_be_returned_unless_the_end_of_template_tag(self):
        self.input_ = """some plain text {{='hi' # some comment }}
hello"""
        self.expected_output = """response.write("some plain text ", escape=False)
response.write('hi' )
response.write("\\nhello", escape=False)"""
        self.do_it()
        

    def test_do_not_return_blank_lines_if_comments_on_the_beginning(self):
        self.input_ = """some plain text {{='hi'
#bla bla bla
='hello'}}{{
#some comment
a = 2
}}"""
        self.expected_output = """response.write("some plain text ", escape=False)
response.write('hi')
response.write('hello')
a = 2"""
        self.do_it()


    def test_create_a_lot_of_dictionaries(self):
        self.input_ = """A{{a = {'a': 1}}}B{{=a['a']}}"""
        self.expected_output = """response.write("A", escape=False)
a = {'a': 1}
response.write("B", escape=False)
response.write(a['a'])"""
        self.do_it()


    def test_dict_symbols_on_HTML_with_space_in_the_midlle(self):
        self.input_ = '{ { bla bla bla } }'
        self.expected_output = 'response.write("{ { bla bla bla } }", escape=False)'
        self.do_it()


    def test_dict_symbols_on_HTML_with_other_character_in_the_midlle(self):
        self.input_ = '{a{ bla bla bla }a}'
        self.expected_output = 'response.write("{a{ bla bla bla }a}", escape=False)'
        self.do_it()


    def test_dict_symbols_on_HTML_with_comment_symbol_in_the_midlle(self):
        self.input_ = '{#{ bla bla bla }#}'
        self.expected_output = 'response.write("{#{ bla bla bla }#}", escape=False)'
        self.do_it()


    def test_dict_symbols_on_HTML_with_blank_line_in_the_midlle(self):
        self.input_ = '''{
{ bla bla bla }
}'''
        self.expected_output = '''response.write("{\\n{ bla bla bla }\\n}", escape=False)'''
        self.do_it()


    def test_create_wrong_python_code_with_dictionary_symbol(self):
        self.input_ = """A{{ a = {} } }}B"""
        self.expected_output = """response.write("A", escape=False)
a = {} }
response.write("B", escape=False)"""
        self.do_it()


    def test_create_a_string_with_spaces(self):
        self.input_ = """A{{a = '''   
bla bla bla'''
}}B"""
        self.expected_output = """response.write("A", escape=False)
a = '''   
bla bla bla'''
response.write("B", escape=False)"""
        self.do_it()


    def test_create_a_lot_of_dictionaries_in_python_code(self):
        self.input_ = 'A{{{{{}}}}}B'
        self.expected_output = """response.write("A", escape=False)
{{{}}}
response.write("B", escape=False)"""
        self.do_it()


    def test_python_code_in_the_last_line_dont_need_to_close_brackets(self):
        self.input_ = '''A{{
a = 1
b = 2'''
        self.expected_output = """response.write("A", escape=False)
a = 1
b = 2"""
        self.do_it()


    def test_create_a_lot_of_dictionaries_in_python_code_with_unmatching_brackets(self):
        self.input_ = 'A{{ {{{}} }}B{{=a}}'
        self.expected_output = """response.write("A", escape=False)
{{{}} }}B{{=a}}"""
        self.do_it()


    def test_backslash_on_python_strings(self):
        self.input_ = '''{{
a = 'some \\
multiline \\
string'
}}'''
        self.expected_output = """a = 'some \\
multiline \\
string'"""
        self.do_it()


    def test_backslash_and_quote_on_python_strings(self):
        self.input_ = """{{
a = 'some \\' }}\\
string'
b = '\\\\\\'}}'
}}"""
        self.expected_output = """a = 'some \\' }}\\
string'
b = '\\\\\\'}}'"""
        self.do_it()


    def test_backslash_state_with_multiple_backslashes(self):
        self.input_ = '''{{a = '\\\\testing'}}'bla'''
        self.expected_output = '''a = '\\\\testing'
response.write("'bla", escape=False)'''
        self.do_it()


    def test_multiline_on_equal_at_python_code_with_backslash(self):
        self.input_ = '''{{=a + \\
b}}'''
        self.expected_output = '''response.write(a + \\
b)'''
        self.do_it()


    def test_multiline_on_strings_in_equal_at_python_code(self):
        self.input_ = '''{{=a + """bla bla bla
foo bar"""}}'''
        self.expected_output = '''response.write(a + """bla bla bla
foo bar""")'''
        self.do_it()


    def test_multiline_on_equal_at_python_code_with_parenthesis_directly(self):
        self.input_ = '''{{=("foo"
"bar")}}'''
        self.expected_output = '''response.write(("foo"
"bar"))'''
        self.do_it()


    def test_multiline_on_equal_at_python_code_with_parenthesis(self):
        self.input_ = '''{{=a + ("foo"
"bar")}}'''
        self.expected_output = '''response.write(a + ("foo"
"bar"))'''
        self.do_it()


    def test_multiline_on_equal_at_python_code_with_brackets(self):
        self.input_ = '''{{=str({"foo":
"bar"})}}'''
        self.expected_output = '''response.write(str({"foo":
"bar"}))'''
        self.do_it()


    def test_multiline_on_python_code_with_indentation(self):
        self.input_ = '''{{if something:
="foo" + \\
"bar"
pass}}a'''
        self.expected_output = '''if something:
    response.write("foo" + \\
"bar")
response.write("a", escape=False)'''
        self.do_it()


    def test_multiline_on_python_code_with_indentation_and_parenthesis(self):
        self.input_ = '''{{if something:
="foo" + ("bar"
"baz")
pass}}a'''
        self.expected_output = '''if something:
    response.write("foo" + ("bar"
"baz"))
response.write("a", escape=False)'''
        self.do_it()


    def test_multiline_on_equal_at_python_code_with_a_lot_of_parenthesis_and_brackets(self):
        self.input_ = '''{{=a + "foo" + str([1, 2, 3,
4, 5, 6]) + {'a': 123,
'b': 456}.__str__() + ("foo"
"bar")}}'''
        self.expected_output = '''response.write(a + "foo" + str([1, 2, 3,
4, 5, 6]) + {'a': 123,
'b': 456}.__str__() + ("foo"
"bar"))'''
        self.do_it()

class TestHTML(unittest.TestCase):

    do_it = do_it

    def test_single_quote_outside_python_code(self):
        self.input_ = """'{{a = 1}}"""
        self.expected_output = """response.write("'", escape=False)
a = 1"""
        self.do_it()


    def test_double_quote_outside_python_code(self):
        self.input_ = '''"{{a = 1}}'''
        self.expected_output = """response.write("\\\"", escape=False)
a = 1"""
        self.do_it()

    def test_new_line_on_HTML_should_be_backslashed_on_response_write(self):
        self.input_ = """
"""
        self.expected_output = """response.write("\\n", escape=False)"""
        self.do_it()


    def test_all_kind_of_quotes_outside_python_code(self):
        self.input_ = """'''\"\"\"\"'''{{lot_of_quotes = True}}'''\"\"\"\"'''\"\"\"
\"\"\"'''\"\"\""""
        self.expected_output = """response.write("'''\\"\\"\\"\\"'''", escape=False)
lot_of_quotes = True
response.write("'''\\"\\"\\"\\"'''\\"\\"\\"\\n\\"\\"\\"'''\\"\\"\\"", escape=False)"""
        self.do_it()


class TestIndentation(unittest.TestCase):
    do_it = do_it

    def test_if_in_python_code_should_indent_next_lines(self):
        self.input_ = '''{{if something:
some_code
other_code}}'''
        self.expected_output = '''if something:
    some_code
    other_code'''
        self.do_it()


    def test_if_in_python_code_should_indent_next_lines_and_pass_should_unindent(self):
        self.input_ = '''{{if something:
some_code
other_code
pass
code_outside_if}}'''
        self.expected_output = '''if something:
    some_code
    other_code
code_outside_if'''
        self.do_it()


    def test_if_and_else_in_python_code_should_indent_only_block_lines(self):
        self.input_ = '''{{if something:
some_code
other_code
else:
code_on_else
other_code_on_else
pass
code_outside}}'''
        self.expected_output = '''if something:
    some_code
    other_code
else:
    code_on_else
    other_code_on_else
code_outside'''
        self.do_it()


    def test_if_elif_else_for_else_try_except_with_in_python_code_should_indent_only_block_lines(self):
        self.input_ = '''{{if something:
some_code
other_code
elif other_condition:
elif_code
other_elif_code
else:
code_on_else
other_code_on_else
pass
code_outside
for a in b:
code_on_for
other_code
pass
some_code_outside
for c in d:
code_on_second_for
other_code_on_second_for
else:
code_on_else
other_code_on_else
pass
other_code_outside
try:
something
code_to_try
except:
code_on_the_exception
blablabla
pass
code_outside
try:
other_try_code
more_one_line
except SomeException, e:
lets_solve_this_exception
more_code
except:
code_to_general_exception
goes_here
pass
more_code_outside_all_blocks}}'''
        self.expected_output = '''if something:
    some_code
    other_code
elif other_condition:
    elif_code
    other_elif_code
else:
    code_on_else
    other_code_on_else
code_outside
for a in b:
    code_on_for
    other_code
some_code_outside
for c in d:
    code_on_second_for
    other_code_on_second_for
else:
    code_on_else
    other_code_on_else
other_code_outside
try:
    something
    code_to_try
except:
    code_on_the_exception
    blablabla
code_outside
try:
    other_try_code
    more_one_line
except SomeException, e:
    lets_solve_this_exception
    more_code
except:
    code_to_general_exception
    goes_here
more_code_outside_all_blocks'''
        self.do_it()


    def test_a_lot_of_indentation_levels(self):
        self.input_ = '''{{if a:
for b in c:
try:
if something:
do_nothing
pass
pass
pass
pass}}'''
        self.expected_output = '''if a:
    for b in c:
        try:
            if something:
                do_nothing'''
        self.do_it()


    def test_indentation_on_different_code_blocks(self):
        self.input_ = '''{{if my_string == '}}':}}
bla bla
{{elif other_string == 'extend':}}
other HTML
{{pass}}'''
        self.expected_output = '''if my_string == '}}':
    response.write("\\nbla bla\\n", escape=False)
elif other_string == 'extend':
    response.write("\\nother HTML\\n", escape=False)'''
        self.do_it()


    def test_a_lot_of_indentation_levels_on_different_code_blocks(self):
        self.input_ = '''{{if my_string == '}}':}}
bla bla
{{if other_thing:
a = 2}}
foo
{{for a in b:}}
bar
{{pass
else:
a = 2}}
baz
{{pass
elif other_string == 'extend':}}
other HTML
{{pass}}'''
        self.expected_output = '''if my_string == '}}':
    response.write("\\nbla bla\\n", escape=False)
    if other_thing:
        a = 2
        response.write("\\nfoo\\n", escape=False)
        for a in b:
            response.write("\\nbar\\n", escape=False)
    else:
        a = 2
        response.write("\\nbaz\\n", escape=False)
elif other_string == 'extend':
    response.write("\\nother HTML\\n", escape=False)'''
        self.do_it()


    def test_pass_should_unindent_even_on_different_template_tags(self):
        self.input_ = '''{{if something:}}
bla bla bla
{{pass}}
some code
{{if a:}}
b
{{pass}}'''
        self.expected_output = '''if something:
    response.write("\\nbla bla bla\\n", escape=False)
response.write("\\nsome code\\n", escape=False)
if a:
    response.write("\\nb\\n", escape=False)'''
        self.do_it()


    def test_unindent_with_break_keyword(self):
        self.input_ = '''{{for a in b:
break}} test'''
        self.expected_output = '''for a in b:
    break
response.write(" test", escape=False)'''
        self.do_it()


    def test_unindent_with_break_keyword_in_second_level_indentation(self):
        self.input_ = '''{{for a in b:
if a == 10:
break
pass}} test'''
        self.expected_output = '''for a in b:
    if a == 10:
        break
response.write(" test", escape=False)'''
        self.do_it()


    def test_unindent_with_continue_keyword_in_second_level_indentation(self):
        self.input_ = '''{{for a in b:
if a == 10:
continue
pass}} test'''
        self.expected_output = '''for a in b:
    if a == 10:
        continue
response.write(" test", escape=False)'''
        self.do_it()


    def test_return_with_continue_keyword_in_second_level_indentation(self):
        self.input_ = '''{{def some_function(arg):
if arg == 10:
return True
pass}} test'''
        self.expected_output = '''def some_function(arg):
    if arg == 10:
        return True
response.write(" test", escape=False)'''
        self.do_it()


    def test_some_unindentation_words_and_python_code_with_spaces(self):
        self.input_ = '''{{def some_function(arg):
if arg == 10:
  return True
 pass
  something}} test'''
        self.expected_output = '''def some_function(arg):
    if arg == 10:
        return True
something
response.write(" test", escape=False)'''
        self.do_it()

if __name__ == '__main__':
    if len(argv) == 1:
        import new_template
        parse_template = new_template.parse_template
        if DEBUG:
            print '********** Using new template engine'
    else:
        import template
        parse_template = template.parse
        if DEBUG:
            print '********** Using *old* template engine'


    unittest.main()
    write_responses.close()