Add ICU message format support
[chromium-blink-merge.git] / tools / json_to_struct / element_generator_test.py
blob373338ebed1d6f85f7f76b07b5740a68f63bb3e8
1 #!/usr/bin/env python
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
4 # found in the LICENSE file.
6 from element_generator import GenerateFieldContent
7 from element_generator import GenerateElements
8 import unittest
10 class ElementGeneratorTest(unittest.TestCase):
11 def testGenerateIntFieldContent(self):
12 lines = [];
13 GenerateFieldContent('', {'type': 'int', 'default': 5}, None, lines, ' ',
14 {})
15 self.assertEquals([' 5,'], lines)
16 lines = [];
17 GenerateFieldContent('', {'type': 'int', 'default': 5}, 12, lines, ' ', {})
18 self.assertEquals([' 12,'], lines)
19 lines = [];
20 GenerateFieldContent('', {'type': 'int'}, -3, lines, ' ', {})
21 self.assertEquals([' -3,'], lines)
23 def testGenerateStringFieldContent(self):
24 lines = [];
25 GenerateFieldContent('', {'type': 'string', 'default': 'foo_bar'}, None,
26 lines, ' ', {})
27 self.assertEquals([' "foo_bar",'], lines)
28 lines = [];
29 GenerateFieldContent('', {'type': 'string', 'default': 'foo'}, 'bar\n',
30 lines, ' ', {})
31 self.assertEquals([' "bar\\n",'], lines)
32 lines = [];
33 GenerateFieldContent('', {'type': 'string'}, None, lines, ' ', {})
34 self.assertEquals([' NULL,'], lines)
35 lines = [];
36 GenerateFieldContent('', {'type': 'string'}, 'foo', lines, ' ', {})
37 self.assertEquals([' "foo",'], lines)
39 def testGenerateString16FieldContent(self):
40 lines = [];
41 GenerateFieldContent('', {'type': 'string16',
42 'default': u'f\u00d8\u00d81a'},
43 None, lines, ' ', {})
44 self.assertEquals([' L"f\\x00d8" L"\\x00d8" L"1a",'], lines)
45 lines = [];
46 GenerateFieldContent('', {'type': 'string16', 'default': 'foo'},
47 u'b\uc3a5r', lines, ' ', {})
48 self.assertEquals([' L"b\\xc3a5" L"r",'], lines)
49 lines = [];
50 GenerateFieldContent('', {'type': 'string16'}, None, lines, ' ', {})
51 self.assertEquals([' NULL,'], lines)
52 lines = [];
53 GenerateFieldContent('', {'type': 'string16'}, u'foo\\u1234', lines, ' ',
54 {})
55 self.assertEquals([' L"foo\\\\u1234",'], lines)
57 def testGenerateEnumFieldContent(self):
58 lines = [];
59 GenerateFieldContent('', {'type': 'enum', 'default': 'RED'}, None, lines,
60 ' ', {})
61 self.assertEquals([' RED,'], lines)
62 lines = [];
63 GenerateFieldContent('', {'type': 'enum', 'default': 'RED'}, 'BLACK', lines,
64 ' ', {})
65 self.assertEquals([' BLACK,'], lines)
66 lines = [];
67 GenerateFieldContent('', {'type': 'enum'}, 'BLUE', lines, ' ', {})
68 self.assertEquals([' BLUE,'], lines)
70 def testGenerateArrayFieldContent(self):
71 lines = ['STRUCT BEGINS'];
72 GenerateFieldContent('test', {'type': 'array', 'contents': {'type': 'int'}},
73 None, lines, ' ', {})
74 self.assertEquals(['STRUCT BEGINS', ' NULL,', ' 0,'], lines)
75 lines = ['STRUCT BEGINS'];
76 GenerateFieldContent('test', {'field': 'my_array', 'type': 'array',
77 'contents': {'type': 'int'}},
78 [3, 4], lines, ' ', {})
79 self.assertEquals('const int array_test_my_array[] = {\n' +
80 ' 3,\n' +
81 ' 4,\n' +
82 '};\n' +
83 'STRUCT BEGINS\n' +
84 ' array_test_my_array,\n' +
85 ' 2,', '\n'.join(lines))
86 lines = ['STRUCT BEGINS'];
87 GenerateFieldContent('test', {'field': 'my_array', 'type': 'array',
88 'contents': {'type': 'int'}},
89 [3, 4], lines, ' ', {'array_test_my_array': 1})
90 self.assertEquals('const int array_test_my_array_1[] = {\n' +
91 ' 3,\n' +
92 ' 4,\n' +
93 '};\n' +
94 'STRUCT BEGINS\n' +
95 ' array_test_my_array_1,\n' +
96 ' 2,', '\n'.join(lines))
98 def testGenerateElements(self):
99 schema = [
100 {'field': 'f0', 'type': 'int', 'default': 1000, 'optional': True},
101 {'field': 'f1', 'type': 'string'},
102 {'field': 'f2', 'type': 'enum', 'ctype': 'QuasiBool', 'default': 'MAYBE',
103 'optional': True},
104 {'field': 'f3', 'type': 'array', 'contents': {'type': 'string16'},
105 'optional': True},
107 'field': 'f4',
108 'type': 'struct',
109 'type_name': 'InnerType',
110 'fields': [
111 {'field': 'g0', 'type': 'string'}
113 'optional': True
116 'field': 'f5',
117 'type': 'array',
118 'contents': {
119 'type': 'struct',
120 'type_name': 'InnerType',
121 'fields': [
122 {'field': 'a0', 'type': 'string'},
123 {'field': 'a1', 'type': 'string'}
126 'optional': True
129 description = {
130 'int_variables': {'a': -5, 'b': 5},
131 'elements': {
132 'elem0': {'f0': 5, 'f1': 'foo', 'f2': 'SURE'},
133 'elem1': {'f2': 'NOWAY', 'f0': -2, 'f1': 'bar'},
134 'elem2': {'f1': 'foo_bar', 'f3': [u'bar', u'foo']},
135 'elem3': {'f1': 'foo', 'f4': {'g0': 'test'}},
136 'elem4': {'f1': 'foo', 'f5': [{'a0': 'test0', 'a1': 'test1'}]},
140 # Build the expected result stream based on the unpredicatble order the
141 # dictionary element are listed in.
142 int_variable_expected = {
143 'a': 'const int a = -5;\n',
144 'b': 'const int b = 5;\n',
146 elements_expected = {
147 'elem0': 'const MyType elem0 = {\n'
148 ' 5,\n'
149 ' "foo",\n'
150 ' SURE,\n'
151 ' NULL,\n'
152 ' 0,\n'
153 ' {0},\n'
154 ' NULL,\n'
155 ' 0,\n'
156 '};\n',
157 'elem1': 'const MyType elem1 = {\n'
158 ' -2,\n'
159 ' "bar",\n'
160 ' NOWAY,\n'
161 ' NULL,\n'
162 ' 0,\n'
163 ' {0},\n'
164 ' NULL,\n'
165 ' 0,\n'
166 '};\n',
167 'elem2': 'const wchar_t* const array_elem2_f3[] = {\n'
168 ' L"bar",\n'
169 ' L"foo",\n'
170 '};\n'
171 'const MyType elem2 = {\n'
172 ' 1000,\n'
173 ' "foo_bar",\n'
174 ' MAYBE,\n'
175 ' array_elem2_f3,\n'
176 ' 2,\n'
177 ' {0},\n'
178 ' NULL,\n'
179 ' 0,\n'
180 '};\n',
181 'elem3': 'const MyType elem3 = {\n'
182 ' 1000,\n'
183 ' "foo",\n'
184 ' MAYBE,\n'
185 ' NULL,\n'
186 ' 0,\n'
187 ' {\n'
188 ' "test",\n'
189 ' },\n'
190 ' NULL,\n'
191 ' 0,\n'
192 '};\n',
193 'elem4': 'const InnerType array_elem4_f5[] = {\n'
194 ' {\n'
195 ' "test0",\n'
196 ' "test1",\n'
197 ' },\n'
198 '};\n'
199 'const MyType elem4 = {\n'
200 ' 1000,\n'
201 ' "foo",\n'
202 ' MAYBE,\n'
203 ' NULL,\n'
204 ' 0,\n'
205 ' {0},\n'
206 ' array_elem4_f5,\n'
207 ' 1,\n'
208 '};\n'
210 expected = ''
211 for key, value in description['int_variables'].items():
212 expected += int_variable_expected[key]
213 expected += '\n'
214 elements = []
215 for key, value in description['elements'].items():
216 elements.append(elements_expected[key])
217 expected += '\n'.join(elements)
219 result = GenerateElements('MyType', schema, description)
220 self.assertEquals(expected, result)
222 def testGenerateElementsMissingMandatoryField(self):
223 schema = [
224 {'field': 'f0', 'type': 'int'},
225 {'field': 'f1', 'type': 'string'},
227 description = {
228 'int_variables': {'a': -5, 'b': 5},
229 'elements': {
230 'elem0': {'f0': 5},
234 self.assertRaises(RuntimeError,
235 lambda: GenerateElements('MyType', schema, description))
237 if __name__ == '__main__':
238 unittest.main()