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

Side by Side Diff: tools/json_to_struct/element_generator_test.py

Issue 1209733002: Add the ability to code generated prepopulated static nested structs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 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
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # 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 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 from element_generator import GenerateFieldContent 6 from element_generator import GenerateFieldContent
7 from element_generator import GenerateElements 7 from element_generator import GenerateElements
8 import unittest 8 import unittest
9 9
10 class ElementGeneratorTest(unittest.TestCase): 10 class ElementGeneratorTest(unittest.TestCase):
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 ' array_test_my_array,\n' + 78 ' array_test_my_array,\n' +
79 ' 2,', '\n'.join(lines)) 79 ' 2,', '\n'.join(lines))
80 80
81 def testGenerateElements(self): 81 def testGenerateElements(self):
82 schema = [ 82 schema = [
83 {'field': 'f0', 'type': 'int', 'default': 1000, 'optional': True}, 83 {'field': 'f0', 'type': 'int', 'default': 1000, 'optional': True},
84 {'field': 'f1', 'type': 'string'}, 84 {'field': 'f1', 'type': 'string'},
85 {'field': 'f2', 'type': 'enum', 'ctype': 'QuasiBool', 'default': 'MAYBE', 85 {'field': 'f2', 'type': 'enum', 'ctype': 'QuasiBool', 'default': 'MAYBE',
86 'optional': True}, 86 'optional': True},
87 {'field': 'f3', 'type': 'array', 'contents': {'type': 'string16'}, 87 {'field': 'f3', 'type': 'array', 'contents': {'type': 'string16'},
88 'optional': True} 88 'optional': True},
89 {
90 'field': 'f4',
91 'type': 'object',
92 'type_name': 'InnerType',
93 'fields': [
94 {'field': 'g0', 'type': 'string'}
95 ],
96 'optional': True
97 },
98 {
99 'field': 'f5',
100 'type': 'array',
101 'contents': {
102 'type': 'object',
103 'type_name': 'InnerType',
104 'fields': [
105 {'field': 'a0', 'type': 'string'},
106 {'field': 'a1', 'type': 'string'}
107 ]
108 },
109 'optional': True
110 }
89 ] 111 ]
90 description = { 112 description = {
91 'int_variables': {'a': -5, 'b': 5}, 113 'int_variables': {'a': -5, 'b': 5},
92 'elements': { 114 'elements': {
93 'elem0': {'f0': 5, 'f1': 'foo', 'f2': 'SURE'}, 115 'elem0': {'f0': 5, 'f1': 'foo', 'f2': 'SURE'},
94 'elem1': {'f2': 'NOWAY', 'f0': -2, 'f1': 'bar'}, 116 'elem1': {'f2': 'NOWAY', 'f0': -2, 'f1': 'bar'},
95 'elem2': {'f1': 'foo_bar', 'f3': [u'bar', u'foo']} 117 'elem2': {'f1': 'foo_bar', 'f3': [u'bar', u'foo']},
118 'elem3': {'f1': 'foo', 'f4': {'g0': 'test'}},
119 'elem4': {'f1': 'foo', 'f5': [{'a0': 'test0', 'a1': 'test1'}]},
96 } 120 }
97 } 121 }
98 122
99 # Build the expected result stream based on the unpredicatble order the 123 # Build the expected result stream based on the unpredicatble order the
100 # dictionary element are listed in. 124 # dictionary element are listed in.
101 int_variable_expected = { 125 int_variable_expected = {
102 'a': 'const int a = -5;\n', 126 'a': 'const int a = -5;\n',
103 'b': 'const int b = 5;\n', 127 'b': 'const int b = 5;\n',
104 } 128 }
105 elements_expected = { 129 elements_expected = {
106 'elem0': 'const MyType elem0 = {\n' + 130 'elem0': 'const MyType elem0 = {\n'
107 ' 5,\n' + 131 ' 5,\n'
108 ' "foo",\n' + 132 ' "foo",\n'
109 ' SURE,\n' + 133 ' SURE,\n'
110 ' NULL,\n' + 134 ' NULL,\n'
135 ' 0,\n'
136 ' {0},\n'
137 ' NULL,\n'
111 ' 0,\n' 138 ' 0,\n'
112 '};\n', 139 '};\n',
113 'elem1': 'const MyType elem1 = {\n' + 140 'elem1': 'const MyType elem1 = {\n'
114 ' -2,\n' + 141 ' -2,\n'
115 ' "bar",\n' + 142 ' "bar",\n'
116 ' NOWAY,\n' + 143 ' NOWAY,\n'
117 ' NULL,\n' + 144 ' NULL,\n'
145 ' 0,\n'
146 ' {0},\n'
147 ' NULL,\n'
118 ' 0,\n' 148 ' 0,\n'
119 '};\n', 149 '};\n',
120 'elem2': 'const wchar_t* const array_elem2_f3[] = {\n' + 150 'elem2': 'const wchar_t* const array_elem2_f3[] = {\n'
121 ' L"bar",\n' + 151 ' L"bar",\n'
122 ' L"foo",\n' + 152 ' L"foo",\n'
123 '};\n' + 153 '};\n'
124 'const MyType elem2 = {\n' + 154 'const MyType elem2 = {\n'
125 ' 1000,\n' + 155 ' 1000,\n'
126 ' "foo_bar",\n' + 156 ' "foo_bar",\n'
127 ' MAYBE,\n' + 157 ' MAYBE,\n'
128 ' array_elem2_f3,\n' + 158 ' array_elem2_f3,\n'
129 ' 2,\n' 159 ' 2,\n'
160 ' {0},\n'
161 ' NULL,\n'
162 ' 0,\n'
163 '};\n',
164 'elem3': 'const MyType elem3 = {\n'
165 ' 1000,\n'
166 ' "foo",\n'
167 ' MAYBE,\n'
168 ' NULL,\n'
169 ' 0,\n'
170 ' {\n'
171 ' "test",\n'
172 ' },\n'
173 ' NULL,\n'
174 ' 0,\n'
175 '};\n',
176 'elem4': 'const InnerType array_elem4_f5[] = {\n'
177 ' {\n'
178 ' "test0",\n'
179 ' "test1",\n'
180 ' },\n'
181 '};\n'
182 'const MyType elem4 = {\n'
183 ' 1000,\n'
184 ' "foo",\n'
185 ' MAYBE,\n'
186 ' NULL,\n'
187 ' 0,\n'
188 ' {0},\n'
189 ' array_elem4_f5,\n'
190 ' 1,\n'
130 '};\n' 191 '};\n'
131 } 192 }
132 expected = '' 193 expected = ''
133 for key, value in description['int_variables'].items(): 194 for key, value in description['int_variables'].items():
134 expected += int_variable_expected[key] 195 expected += int_variable_expected[key]
135 expected += '\n' 196 expected += '\n'
136 elements = [] 197 elements = []
137 for key, value in description['elements'].items(): 198 for key, value in description['elements'].items():
138 elements.append(elements_expected[key]) 199 elements.append(elements_expected[key])
139 expected += '\n'.join(elements) 200 expected += '\n'.join(elements)
(...skipping 11 matching lines...) Expand all
151 'elements': { 212 'elements': {
152 'elem0': {'f0': 5}, 213 'elem0': {'f0': 5},
153 } 214 }
154 } 215 }
155 216
156 self.assertRaises(RuntimeError, 217 self.assertRaises(RuntimeError,
157 lambda: GenerateElements('MyType', schema, description)) 218 lambda: GenerateElements('MyType', schema, description))
158 219
159 if __name__ == '__main__': 220 if __name__ == '__main__':
160 unittest.main() 221 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698