cc: Remove ref-counting from DelayBasedTimeSource.
[chromium-blink-merge.git] / tools / json_schema_compiler / code_test.py
blob90e2879340861720af6f8153c4a1dca6af765a87
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 from code import Code
7 import unittest
9 class CodeTest(unittest.TestCase):
10 def testAppend(self):
11 c = Code()
12 c.Append('line')
13 self.assertEquals('line', c.Render())
15 def testBlock(self):
16 c = Code()
17 (c.Append('line')
18 .Sblock('sblock')
19 .Append('inner')
20 .Append('moreinner')
21 .Sblock('moresblock')
22 .Append('inner')
23 .Eblock('out')
24 .Append('inner')
25 .Eblock('out')
27 self.assertEquals(
28 'line\n'
29 'sblock\n'
30 ' inner\n'
31 ' moreinner\n'
32 ' moresblock\n'
33 ' inner\n'
34 ' out\n'
35 ' inner\n'
36 'out',
37 c.Render())
39 def testConcat(self):
40 b = Code()
41 (b.Sblock('2')
42 .Append('2')
43 .Eblock('2')
45 c = Code()
46 (c.Sblock('1')
47 .Concat(b)
48 .Append('1')
49 .Eblock('1')
51 self.assertMultiLineEqual(
52 '1\n'
53 ' 2\n'
54 ' 2\n'
55 ' 2\n'
56 ' 1\n'
57 '1',
58 c.Render())
59 d = Code()
60 a = Code()
61 a.Concat(d)
62 self.assertEquals('', a.Render())
63 a.Concat(c)
64 self.assertEquals(
65 '1\n'
66 ' 2\n'
67 ' 2\n'
68 ' 2\n'
69 ' 1\n'
70 '1',
71 a.Render())
73 def testConcatErrors(self):
74 c = Code()
75 d = Code()
76 d.Append('%s')
77 self.assertRaises(TypeError, c.Concat, d)
78 d = Code()
79 d.Append('%(classname)s')
80 self.assertRaises(TypeError, c.Concat, d)
81 d = 'line of code'
82 self.assertRaises(TypeError, c.Concat, d)
84 def testSubstitute(self):
85 c = Code()
86 c.Append('%(var1)s %(var2)s %(var1)s')
87 c.Substitute({'var1': 'one', 'var2': 'two'})
88 self.assertEquals('one two one', c.Render())
89 c.Append('%(var1)s %(var2)s %(var3)s')
90 c.Append('%(var2)s %(var1)s %(var3)s')
91 c.Substitute({'var1': 'one', 'var2': 'two', 'var3': 'three'})
92 self.assertEquals(
93 'one two one\n'
94 'one two three\n'
95 'two one three',
96 c.Render())
98 def testSubstituteErrors(self):
99 # No unnamed placeholders allowed when substitute is run
100 c = Code()
101 c.Append('%s %s')
102 self.assertRaises(TypeError, c.Substitute, ('var1', 'one'))
103 c = Code()
104 c.Append('%s %(var1)s')
105 self.assertRaises(TypeError, c.Substitute, {'var1': 'one'})
106 c = Code()
107 c.Append('%s %(var1)s')
108 self.assertRaises(TypeError, c.Substitute, {'var1': 'one'})
109 c = Code()
110 c.Append('%(var1)s')
111 self.assertRaises(KeyError, c.Substitute, {'clearlynotvar1': 'one'})
113 def testIsEmpty(self):
114 c = Code()
115 self.assertTrue(c.IsEmpty())
116 c.Append('asdf')
117 self.assertFalse(c.IsEmpty())
119 def testComment(self):
120 long_comment = ('This comment is ninety one characters in longness, '
121 'that is, using a different word, length.')
122 c = Code()
123 c.Comment(long_comment)
124 self.assertEquals(
125 '// This comment is ninety one characters '
126 'in longness, that is, using a different\n'
127 '// word, length.',
128 c.Render())
129 c = Code()
130 c.Sblock('sblock')
131 c.Comment(long_comment)
132 c.Eblock('eblock')
133 c.Comment(long_comment)
134 self.assertEquals(
135 'sblock\n'
136 ' // This comment is ninety one characters '
137 'in longness, that is, using a\n'
138 ' // different word, length.\n'
139 'eblock\n'
140 '// This comment is ninety one characters in '
141 'longness, that is, using a different\n'
142 '// word, length.',
143 c.Render())
144 long_word = 'x' * 100
145 c = Code()
146 c.Comment(long_word)
147 self.assertEquals(
148 '// ' + 'x' * 77 + '\n'
149 '// ' + 'x' * 23,
150 c.Render())
151 c = Code(indent_size=2, comment_length=40)
152 c.Comment('Pretend this is a Closure Compiler style comment, which should '
153 'both wrap and indent', comment_prefix=' * ', wrap_indent=4)
154 self.assertEquals(
155 ' * Pretend this is a Closure Compiler\n'
156 ' * style comment, which should both\n'
157 ' * wrap and indent',
158 c.Render())
160 def testCommentWithSpecialCharacters(self):
161 c = Code()
162 c.Comment('20% of 80%s')
163 c.Substitute({})
164 self.assertEquals('// 20% of 80%s', c.Render())
165 d = Code()
166 d.Append('90')
167 d.Concat(c)
168 self.assertEquals('90\n'
169 '// 20% of 80%s',
170 d.Render())
172 def testLinePrefixes(self):
173 c = Code()
174 c.Sblock(line='/**', line_prefix=' * ')
175 c.Sblock('@typedef {{')
176 c.Append('foo: bar,')
177 c.Sblock('baz: {')
178 c.Append('x: y')
179 c.Eblock('}')
180 c.Eblock('}}')
181 c.Eblock(line=' */')
182 output = c.Render()
183 self.assertMultiLineEqual(
184 '/**\n'
185 ' * @typedef {{\n'
186 ' * foo: bar,\n'
187 ' * baz: {\n'
188 ' * x: y\n'
189 ' * }\n'
190 ' * }}\n'
191 ' */',
192 output)
194 def testSameLineAppendConcatComment(self):
195 c = Code()
196 c.Append('This is a line.')
197 c.Append('This too.', new_line=False)
198 d = Code()
199 d.Append('And this.')
200 c.Concat(d, new_line=False)
201 self.assertEquals('This is a line.This too.And this.', c.Render())
202 c = Code()
203 c.Append('This is a')
204 c.Comment(' spectacular 80-character line thingy ' +
205 'that fits wonderfully everywhere.',
206 comment_prefix='',
207 new_line=False)
208 self.assertEquals('This is a spectacular 80-character line thingy that ' +
209 'fits wonderfully everywhere.',
210 c.Render())
212 if __name__ == '__main__':
213 unittest.main()