blob: 8431d9540d9e2eb42e29275dfceb71aa2c6f0278 [file] [log] [blame]
[email protected]15f08dd2012-01-27 07:29:481# Copyright (c) 2012 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5from code import Code
6import unittest
7
8class CodeTest(unittest.TestCase):
9 def testAppend(self):
10 c = Code()
11 c.Append('line')
12 self.assertEquals('line', c.Render())
13
14 def testBlock(self):
15 c = Code()
16 (c.Append('line')
17 .Sblock('sblock')
18 .Append('inner')
19 .Append('moreinner')
20 .Sblock('moresblock')
21 .Append('inner')
22 .Eblock('out')
23 .Append('inner')
24 .Eblock('out')
25 )
26 self.assertEquals(
27 'line\n'
28 'sblock\n'
29 ' inner\n'
30 ' moreinner\n'
31 ' moresblock\n'
32 ' inner\n'
33 ' out\n'
34 ' inner\n'
35 'out',
36 c.Render())
37
38 def testConcat(self):
39 b = Code()
40 (b.Sblock('2')
41 .Append('2')
42 .Eblock('2')
43 )
44 c = Code()
45 (c.Sblock('1')
46 .Concat(b)
47 .Append('1')
48 .Eblock('1')
49 )
50 self.assertEquals(
51 '1\n'
52 ' 2\n'
53 ' 2\n'
54 ' 2\n'
55 ' 1\n'
56 '1',
57 c.Render())
58 d = Code()
59 a = Code()
60 a.Concat(d)
61 self.assertEquals('', a.Render())
62 a.Concat(c)
63 self.assertEquals(
64 '1\n'
65 ' 2\n'
66 ' 2\n'
67 ' 2\n'
68 ' 1\n'
69 '1',
70 a.Render())
71
72 def testConcatErrors(self):
73 c = Code()
74 d = Code()
75 d.Append('%s')
76 self.assertRaises(TypeError, c.Concat, d)
77 d = Code()
78 d.Append('%(classname)s')
79 self.assertRaises(TypeError, c.Concat, d)
80 d = 'line of code'
81 self.assertRaises(TypeError, c.Concat, d)
82
83 def testSubstitute(self):
84 c = Code()
85 c.Append('%(var1)s %(var2)s %(var1)s')
86 c.Substitute({'var1': 'one', 'var2': 'two'})
87 self.assertEquals('one two one', c.Render())
88 c.Append('%(var1)s %(var2)s %(var3)s')
89 c.Append('%(var2)s %(var1)s %(var3)s')
90 c.Substitute({'var1': 'one', 'var2': 'two', 'var3': 'three'})
91 self.assertEquals(
92 'one two one\n'
93 'one two three\n'
94 'two one three',
95 c.Render())
96
97 def testSubstituteErrors(self):
98 # No unnamed placeholders allowed when substitute is run
99 c = Code()
100 c.Append('%s %s')
101 self.assertRaises(TypeError, c.Substitute, ('var1', 'one'))
102 c = Code()
103 c.Append('%s %(var1)s')
104 self.assertRaises(TypeError, c.Substitute, {'var1': 'one'})
105 c = Code()
106 c.Append('%s %(var1)s')
107 self.assertRaises(TypeError, c.Substitute, {'var1': 'one'})
108 c = Code()
109 c.Append('%(var1)s')
110 self.assertRaises(KeyError, c.Substitute, {'clearlynotvar1': 'one'})
111
112 def testIsEmpty(self):
113 c = Code()
114 self.assertTrue(c.IsEmpty())
115 c.Append('asdf')
116 self.assertFalse(c.IsEmpty())
117
118 def testComment(self):
119 long_comment = ('This comment is eighty nine characters in longness, '
120 'that is, to use another word, length')
121 c = Code()
122 c.Comment(long_comment)
123 self.assertEquals(
124 '// This comment is eighty nine characters '
125 'in longness, that is, to use another\n'
126 '// word, length',
127 c.Render())
128 c = Code()
129 c.Sblock('sblock')
130 c.Comment(long_comment)
131 c.Eblock('eblock')
132 c.Comment(long_comment)
133 self.assertEquals(
134 'sblock\n'
135 ' // This comment is eighty nine characters '
136 'in longness, that is, to use\n'
137 ' // another word, length\n'
138 'eblock\n'
139 '// This comment is eighty nine characters in '
140 'longness, that is, to use another\n'
141 '// word, length',
142 c.Render())
143 long_word = 'x' * 100
144 c = Code()
145 c.Comment(long_word)
146 self.assertEquals(
147 '// ' + 'x' * 77 + '\n'
148 '// ' + 'x' * 23,
149 c.Render())
150
151if __name__ == '__main__':
152 unittest.main()