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

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: switched namespaces, various 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 from code import Code
6 import unittest
7
8 class 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 testComment(self):
113 long_comment = ('This comment is eighty nine characters in longness, '
114 'that is, to use another word, length')
115 c = Code()
116 c.comment(long_comment)
117 self.assertEquals(
118 '// This comment is eighty nine characters '
119 'in longness, that is, to use another\n'
120 '// word, length',
121 c.render())
122 c = Code()
123 c.sblock('sblock')
124 c.comment(long_comment)
125 c.eblock('eblock')
126 c.comment(long_comment)
127 self.assertEquals(
128 'sblock\n'
129 ' // This comment is eighty nine characters '
130 'in longness, that is, to use\n'
131 ' // another word, length\n'
132 'eblock\n'
133 '// This comment is eighty nine characters in '
134 'longness, that is, to use another\n'
135 '// word, length',
136 c.render())
137 long_word = 'x' * 100
138 c = Code()
139 c.comment(long_word)
140 self.assertEquals(
141 '// ' + 'x' * 77 + '\n'
142 '// ' + 'x' * 23,
143 c.render())
144
145 if __name__ == '__main__':
146 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698