blob: 6d54ecc47a4dbec9ee14bb2c2cc5e8c3e49169ba [file] [log] [blame]
[email protected]8577ed9862012-03-28 20:45:381#!/usr/bin/env python
[email protected]15f08dd2012-01-27 07:29:482# 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.
5
6from code import Code
7import unittest
8
9class CodeTest(unittest.TestCase):
10 def testAppend(self):
11 c = Code()
12 c.Append('line')
13 self.assertEquals('line', c.Render())
14
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')
26 )
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())
38
39 def testConcat(self):
40 b = Code()
41 (b.Sblock('2')
42 .Append('2')
43 .Eblock('2')
44 )
45 c = Code()
46 (c.Sblock('1')
47 .Concat(b)
48 .Append('1')
49 .Eblock('1')
50 )
rdevlin.cronin9ead75e2015-03-30 17:36:1751 self.assertMultiLineEqual(
[email protected]15f08dd2012-01-27 07:29:4852 '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())
72
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)
83
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())
97
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'})
112
113 def testIsEmpty(self):
114 c = Code()
115 self.assertTrue(c.IsEmpty())
116 c.Append('asdf')
117 self.assertFalse(c.IsEmpty())
118
119 def testComment(self):
rdevlin.cronin8ea16e12015-03-27 00:13:13120 long_comment = ('This comment is ninety one characters in longness, '
121 'that is, using a different word, length.')
[email protected]15f08dd2012-01-27 07:29:48122 c = Code()
123 c.Comment(long_comment)
124 self.assertEquals(
rdevlin.cronin8ea16e12015-03-27 00:13:13125 '// This comment is ninety one characters '
126 'in longness, that is, using a different\n'
127 '// word, length.',
[email protected]15f08dd2012-01-27 07:29:48128 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'
rdevlin.cronin8ea16e12015-03-27 00:13:13136 ' // This comment is ninety one characters '
137 'in longness, that is, using a\n'
138 ' // different word, length.\n'
[email protected]15f08dd2012-01-27 07:29:48139 'eblock\n'
rdevlin.cronin8ea16e12015-03-27 00:13:13140 '// This comment is ninety one characters in '
141 'longness, that is, using a different\n'
142 '// word, length.',
[email protected]15f08dd2012-01-27 07:29:48143 c.Render())
Mike Frysingerece1fa92020-06-03 19:40:49144 # Words that cannot be broken up are left as too long.
[email protected]15f08dd2012-01-27 07:29:48145 long_word = 'x' * 100
146 c = Code()
Mike Frysingerece1fa92020-06-03 19:40:49147 c.Comment('xxx')
[email protected]15f08dd2012-01-27 07:29:48148 c.Comment(long_word)
Mike Frysingerece1fa92020-06-03 19:40:49149 c.Comment('xxx')
[email protected]15f08dd2012-01-27 07:29:48150 self.assertEquals(
Mike Frysingerece1fa92020-06-03 19:40:49151 '// xxx\n'
152 '// ' + 'x' * 100 + '\n'
153 '// xxx',
[email protected]15f08dd2012-01-27 07:29:48154 c.Render())
rdevlin.cronin8ea16e12015-03-27 00:13:13155 c = Code(indent_size=2, comment_length=40)
156 c.Comment('Pretend this is a Closure Compiler style comment, which should '
157 'both wrap and indent', comment_prefix=' * ', wrap_indent=4)
158 self.assertEquals(
159 ' * Pretend this is a Closure Compiler\n'
160 ' * style comment, which should both\n'
161 ' * wrap and indent',
162 c.Render())
[email protected]15f08dd2012-01-27 07:29:48163
[email protected]feba21e2012-03-02 15:05:27164 def testCommentWithSpecialCharacters(self):
165 c = Code()
166 c.Comment('20% of 80%s')
167 c.Substitute({})
168 self.assertEquals('// 20% of 80%s', c.Render())
169 d = Code()
170 d.Append('90')
171 d.Concat(c)
172 self.assertEquals('90\n'
173 '// 20% of 80%s',
174 d.Render())
175
rdevlin.cronin9ead75e2015-03-30 17:36:17176 def testLinePrefixes(self):
177 c = Code()
178 c.Sblock(line='/**', line_prefix=' * ')
179 c.Sblock('@typedef {{')
180 c.Append('foo: bar,')
181 c.Sblock('baz: {')
182 c.Append('x: y')
183 c.Eblock('}')
184 c.Eblock('}}')
185 c.Eblock(line=' */')
186 output = c.Render()
187 self.assertMultiLineEqual(
188 '/**\n'
189 ' * @typedef {{\n'
190 ' * foo: bar,\n'
191 ' * baz: {\n'
192 ' * x: y\n'
193 ' * }\n'
194 ' * }}\n'
195 ' */',
196 output)
197
rdevlin.cronin684111162015-04-07 17:20:59198 def testSameLineAppendConcatComment(self):
rdevlin.cronin9ead75e2015-03-30 17:36:17199 c = Code()
200 c.Append('This is a line.')
201 c.Append('This too.', new_line=False)
202 d = Code()
203 d.Append('And this.')
204 c.Concat(d, new_line=False)
205 self.assertEquals('This is a line.This too.And this.', c.Render())
rdevlin.cronin684111162015-04-07 17:20:59206 c = Code()
207 c.Append('This is a')
208 c.Comment(' spectacular 80-character line thingy ' +
209 'that fits wonderfully everywhere.',
210 comment_prefix='',
211 new_line=False)
212 self.assertEquals('This is a spectacular 80-character line thingy that ' +
213 'fits wonderfully everywhere.',
214 c.Render())
rdevlin.cronin9ead75e2015-03-30 17:36:17215
[email protected]15f08dd2012-01-27 07:29:48216if __name__ == '__main__':
217 unittest.main()