1from io import StringIO
2
3from mako.pygen import adjust_whitespace
4from mako.pygen import PythonPrinter
5from mako.testing.assertions import eq_
6
7
8class GeneratePythonTest:
9    def test_generate_normal(self):
10        stream = StringIO()
11        printer = PythonPrinter(stream)
12        printer.writeline("import lala")
13        printer.writeline("for x in foo:")
14        printer.writeline("print x")
15        printer.writeline(None)
16        printer.writeline("print y")
17        assert (
18            stream.getvalue()
19            == """import lala
20for x in foo:
21    print x
22print y
23"""
24        )
25
26    def test_generate_adjusted(self):
27        block = """
28        x = 5 +6
29        if x > 7:
30            for y in range(1,5):
31                print "<td>%s</td>" % y
32"""
33        stream = StringIO()
34        printer = PythonPrinter(stream)
35        printer.write_indented_block(block)
36        printer.close()
37        # print stream.getvalue()
38        assert (
39            stream.getvalue()
40            == """
41x = 5 +6
42if x > 7:
43    for y in range(1,5):
44        print "<td>%s</td>" % y
45
46"""
47        )
48
49    def test_generate_combo(self):
50        block = """
51                x = 5 +6
52                if x > 7:
53                    for y in range(1,5):
54                        print "<td>%s</td>" % y
55                    print "hi"
56                print "there"
57                foo(lala)
58"""
59        stream = StringIO()
60        printer = PythonPrinter(stream)
61        printer.writeline("import lala")
62        printer.writeline("for x in foo:")
63        printer.writeline("print x")
64        printer.write_indented_block(block)
65        printer.writeline(None)
66        printer.writeline("print y")
67        printer.close()
68        # print "->" + stream.getvalue().replace(' ', '#') + "<-"
69        eq_(
70            stream.getvalue(),
71            """import lala
72for x in foo:
73    print x
74
75    x = 5 +6
76    if x > 7:
77        for y in range(1,5):
78            print "<td>%s</td>" % y
79        print "hi"
80    print "there"
81    foo(lala)
82
83print y
84""",
85        )
86
87    def test_multi_line(self):
88        block = """
89    if test:
90        print ''' this is a block of stuff.
91this is more stuff in the block.
92and more block.
93'''
94        do_more_stuff(g)
95"""
96        stream = StringIO()
97        printer = PythonPrinter(stream)
98        printer.write_indented_block(block)
99        printer.close()
100        # print stream.getvalue()
101        assert (
102            stream.getvalue()
103            == """
104if test:
105    print ''' this is a block of stuff.
106this is more stuff in the block.
107and more block.
108'''
109    do_more_stuff(g)
110
111"""
112        )
113
114    def test_false_unindentor(self):
115        stream = StringIO()
116        printer = PythonPrinter(stream)
117        for line in [
118            "try:",
119            "elsemyvar = 12",
120            "if True:",
121            "print 'hi'",
122            None,
123            "finally:",
124            "dosomething",
125            None,
126        ]:
127            printer.writeline(line)
128
129        assert (
130            stream.getvalue()
131            == """try:
132    elsemyvar = 12
133    if True:
134        print 'hi'
135finally:
136    dosomething
137"""
138        ), stream.getvalue()
139
140    def test_backslash_line(self):
141        block = """
142            # comment
143    if test:
144        if (lala + hoho) + \\
145(foobar + blat) == 5:
146            print "hi"
147    print "more indent"
148"""
149        stream = StringIO()
150        printer = PythonPrinter(stream)
151        printer.write_indented_block(block)
152        printer.close()
153        assert (
154            stream.getvalue()
155            == """
156            # comment
157if test:
158    if (lala + hoho) + \\
159(foobar + blat) == 5:
160        print "hi"
161print "more indent"
162
163"""
164        )
165
166
167class WhitespaceTest:
168    def test_basic(self):
169        text = """
170        for x in range(0,15):
171            print x
172        print "hi"
173        """
174        assert (
175            adjust_whitespace(text)
176            == """
177for x in range(0,15):
178    print x
179print "hi"
180"""
181        )
182
183    def test_blank_lines(self):
184        text = """
185    print "hi"  # a comment
186
187    # more comments
188
189    print g
190"""
191        assert (
192            adjust_whitespace(text)
193            == """
194print "hi"  # a comment
195
196# more comments
197
198print g
199"""
200        )
201
202    def test_open_quotes_with_pound(self):
203        text = '''
204        print """  this is text
205          # and this is text
206        # and this is too """
207'''
208        assert (
209            adjust_whitespace(text)
210            == '''
211print """  this is text
212          # and this is text
213        # and this is too """
214'''
215        )
216
217    def test_quote_with_comments(self):
218        text = """
219            print 'hi'
220            # this is a comment
221            # another comment
222            x = 7 # someone's '''comment
223            print '''
224        there
225        '''
226            # someone else's comment
227"""
228
229        assert (
230            adjust_whitespace(text)
231            == """
232print 'hi'
233# this is a comment
234# another comment
235x = 7 # someone's '''comment
236print '''
237        there
238        '''
239# someone else's comment
240"""
241        )
242
243    def test_quotes_with_pound(self):
244        text = '''
245        if True:
246            """#"""
247        elif False:
248            "bar"
249'''
250        assert (
251            adjust_whitespace(text)
252            == '''
253if True:
254    """#"""
255elif False:
256    "bar"
257'''
258        )
259
260    def test_quotes(self):
261        text = """
262        print ''' aslkjfnas kjdfn
263askdjfnaskfd fkasnf dknf sadkfjn asdkfjna sdakjn
264asdkfjnads kfajns '''
265        if x:
266            print y
267"""
268        assert (
269            adjust_whitespace(text)
270            == """
271print ''' aslkjfnas kjdfn
272askdjfnaskfd fkasnf dknf sadkfjn asdkfjna sdakjn
273asdkfjnads kfajns '''
274if x:
275    print y
276"""
277        )
278