blob: c6ca33f5b688242fd6419896387770c701fb0a02 [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 )
51 self.assertEquals(
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())
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())
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())
rdevlin.cronin8ea16e12015-03-27 00:13:13151 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())
[email protected]15f08dd2012-01-27 07:29:48159
[email protected]feba21e2012-03-02 15:05:27160 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())
171
[email protected]15f08dd2012-01-27 07:29:48172if __name__ == '__main__':
173 unittest.main()