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

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: updated branch, utilized Result, small fixes 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