Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: tools/json_schema_compiler/code_test.py

Issue 9114036: Code generation for extensions api (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: a fistful of rework Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # 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
5 import unittest
6 from code import Code
7
8 class CodeTest(unittest.TestCase):
9 def test_append(self):
10 c = Code()
11 c.append('line')
12 self.assertEquals(c.render(), 'line')
13
14 def test_block(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(c.render(),
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
37 def test_concat(self):
38 b = Code()
39 (b.sblock('2')
40 .append('2')
41 .eblock('2')
42 )
43 c = Code()
44 (c.sblock('1')
45 .concat(b)
46 .append('1')
47 .eblock('1')
48 )
49 self.assertEquals(c.render(),
50 '1\n'
51 ' 2\n'
52 ' 2\n'
53 ' 2\n'
54 ' 1\n'
55 '1')
56 d = Code()
57 a = Code()
58 a.concat(d)
59 self.assertEquals(a.render(), '')
60 a.concat(c)
61 self.assertEquals(a.render(),
62 '1\n'
63 ' 2\n'
64 ' 2\n'
65 ' 2\n'
66 ' 1\n'
67 '1')
68
69 def test_concat_errors(self):
70 c = Code()
71 d = Code()
72 d.append('%s')
73 self.assertRaises(TypeError, c.concat, d)
74 d = Code()
75 d.append('%(classname)s')
76 self.assertRaises(TypeError, c.concat, d)
77 d = 'line of code'
78 self.assertRaises(TypeError, c.concat, d)
79
80 def test_substitute(self):
81 c = Code()
82 c.append('%(var1)s %(var2)s %(var1)s')
83 c.substitute({'var1': 'one', 'var2': 'two'})
84 self.assertEquals(c.render(), 'one two one')
85 c.append('%(var1)s %(var2)s %(var3)s')
86 c.append('%(var2)s %(var1)s %(var3)s')
87 c.substitute({'var1': 'one', 'var2': 'two', 'var3': 'three'})
88 self.assertEquals(c.render(),
89 'one two one\n'
90 'one two three\n'
91 'two one three')
92
93 def test_substitute_errors(self):
94 # No unnamed placeholders allowed when substitute is run
95 c = Code()
96 c.append('%s %s')
97 self.assertRaises(TypeError, c.substitute, ('var1', 'one'))
98 c = Code()
99 c.append('%s %(var1)s')
100 self.assertRaises(TypeError, c.substitute, {'var1': 'one'})
101 c = Code()
102 c.append('%s %(var1)s')
103 self.assertRaises(TypeError, c.substitute, {'var1': 'one'})
104 c = Code()
105 c.append('%(var1)s')
106 self.assertRaises(KeyError, c.substitute, {'clearlynotvar1': 'one'})
107
108 def test_comment(self):
109 long_comment = ('This comment is eighty nine characters in longness, '
110 'that is, to use another word, length')
111 c = Code()
112 c.comment(long_comment)
113 self.assertEquals(c.render(),
114 '// This comment is eighty nine characters '
115 'in longness, that is, to use another\n'
116 '// word, length')
117 c = Code()
118 c.sblock('sblock')
119 c.comment(long_comment)
120 c.eblock('eblock')
121 c.comment(long_comment)
122 self.assertEquals(c.render(),
123 'sblock\n'
124 ' // This comment is eighty nine characters '
125 'in longness, that is, to use\n'
126 ' // another word, length\n'
127 'eblock\n'
128 '// This comment is eighty nine characters in '
129 'longness, that is, to use another\n'
130 '// word, length')
131 long_word = 'x' * 100
132 c = Code()
133 c.comment(long_word)
134 self.assertEquals(c.render(),
135 '// ' + 'x' * 77 + '\n'
136 '// ' + 'x' * 23)
137
138 if __name__ == '__main__':
139 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698