~jonas-drange/ubuntu-start-page/1252899-mobile-friendly

« back to all changes in this revision

Viewing changes to src/Mako-0.1.9/test/pygen.py

  • Committer: Matthew Nuzum
  • Date: 2008-04-18 01:58:53 UTC
  • Revision ID: matthew.nuzum@canonical.com-20080418015853-2b8rf979z2c2exxl
adding files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import unittest
 
2
 
 
3
from mako.pygen import PythonPrinter, adjust_whitespace
 
4
from StringIO import StringIO
 
5
 
 
6
class GeneratePythonTest(unittest.TestCase):
 
7
    def test_generate_normal(self):
 
8
        stream = StringIO()
 
9
        printer = PythonPrinter(stream)
 
10
        printer.writeline("import lala")
 
11
        printer.writeline("for x in foo:")
 
12
        printer.writeline("print x")
 
13
        printer.writeline(None)
 
14
        printer.writeline("print y")
 
15
        assert stream.getvalue() == \
 
16
"""import lala
 
17
for x in foo:
 
18
    print x
 
19
print y
 
20
"""
 
21
    def test_generate_adjusted(self):
 
22
        block = """
 
23
        x = 5 +6
 
24
        if x > 7:
 
25
            for y in range(1,5):
 
26
                print "<td>%s</td>" % y
 
27
"""
 
28
        stream = StringIO()
 
29
        printer = PythonPrinter(stream)
 
30
        printer.write_indented_block(block)
 
31
        printer.close()
 
32
        #print stream.getvalue()
 
33
        assert stream.getvalue() == \
 
34
"""
 
35
x = 5 +6
 
36
if x > 7:
 
37
    for y in range(1,5):
 
38
        print "<td>%s</td>" % y
 
39
 
 
40
"""
 
41
    def test_generate_combo(self):
 
42
        block = \
 
43
"""
 
44
                x = 5 +6
 
45
                if x > 7:
 
46
                    for y in range(1,5):
 
47
                        print "<td>%s</td>" % y
 
48
                    print "hi"
 
49
                print "there"
 
50
                foo(lala)
 
51
        """
 
52
        stream = StringIO()
 
53
        printer = PythonPrinter(stream)
 
54
        printer.writeline("import lala")
 
55
        printer.writeline("for x in foo:")
 
56
        printer.writeline("print x")
 
57
        printer.write_indented_block(block)
 
58
        printer.writeline(None)
 
59
        printer.writeline("print y")
 
60
        printer.close()
 
61
        #print "->" + stream.getvalue().replace(' ', '#') + "<-"
 
62
        assert stream.getvalue() == \
 
63
"""import lala
 
64
for x in foo:
 
65
    print x
 
66
 
 
67
    x = 5 +6
 
68
    if x > 7:
 
69
        for y in range(1,5):
 
70
            print "<td>%s</td>" % y
 
71
        print "hi"
 
72
    print "there"
 
73
    foo(lala)
 
74
        
 
75
print y
 
76
"""
 
77
    def test_multi_line(self):
 
78
        block = \
 
79
"""
 
80
    if test:
 
81
        print ''' this is a block of stuff.
 
82
this is more stuff in the block.
 
83
and more block.
 
84
'''
 
85
        do_more_stuff(g)
 
86
"""
 
87
        stream = StringIO()
 
88
        printer = PythonPrinter(stream)
 
89
        printer.write_indented_block(block)
 
90
        printer.close()
 
91
        #print stream.getvalue()
 
92
        assert stream.getvalue() == \
 
93
"""
 
94
if test:
 
95
    print ''' this is a block of stuff.
 
96
this is more stuff in the block.
 
97
and more block.
 
98
'''
 
99
    do_more_stuff(g)
 
100
 
 
101
"""
 
102
 
 
103
    def test_backslash_line(self):
 
104
        block = \
 
105
"""
 
106
            # comment
 
107
    if test:
 
108
        if (lala + hoho) + \\
 
109
(foobar + blat) == 5:
 
110
            print "hi"
 
111
    print "more indent"
 
112
"""
 
113
        stream = StringIO()
 
114
        printer = PythonPrinter(stream)
 
115
        printer.write_indented_block(block)
 
116
        printer.close()
 
117
        assert stream.getvalue() == \
 
118
"""
 
119
            # comment
 
120
if test:
 
121
    if (lala + hoho) + \\
 
122
(foobar + blat) == 5:
 
123
        print "hi"
 
124
print "more indent"
 
125
 
 
126
"""
 
127
 
 
128
class WhitespaceTest(unittest.TestCase):
 
129
    def test_basic(self):
 
130
        text = """
 
131
        for x in range(0,15):
 
132
            print x
 
133
        print "hi"
 
134
        """
 
135
        assert adjust_whitespace(text) == \
 
136
"""
 
137
for x in range(0,15):
 
138
    print x
 
139
print "hi"
 
140
 
 
141
"""
 
142
 
 
143
    def test_quotes(self):
 
144
        text = """
 
145
        print ''' aslkjfnas kjdfn
 
146
askdjfnaskfd fkasnf dknf sadkfjn asdkfjna sdakjn
 
147
asdkfjnads kfajns '''
 
148
        if x:
 
149
            print y
 
150
"""
 
151
        assert adjust_whitespace(text) == \
 
152
"""
 
153
print ''' aslkjfnas kjdfn
 
154
askdjfnaskfd fkasnf dknf sadkfjn asdkfjna sdakjn
 
155
asdkfjnads kfajns '''
 
156
if x:
 
157
    print y
 
158
 
 
159
"""
 
160
 
 
161
if __name__ == '__main__':
 
162
    unittest.main()