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
10 class ElementGeneratorTest(unittest
.TestCase
):
11 def testGenerateIntFieldContent(self
):
13 GenerateFieldContent('', {'type': 'int', 'default': 5}, None, lines
, ' ',
15 self
.assertEquals([' 5,'], lines
)
17 GenerateFieldContent('', {'type': 'int', 'default': 5}, 12, lines
, ' ', {})
18 self
.assertEquals([' 12,'], lines
)
20 GenerateFieldContent('', {'type': 'int'}, -3, lines
, ' ', {})
21 self
.assertEquals([' -3,'], lines
)
23 def testGenerateStringFieldContent(self
):
25 GenerateFieldContent('', {'type': 'string', 'default': 'foo_bar'}, None,
27 self
.assertEquals([' "foo_bar",'], lines
)
29 GenerateFieldContent('', {'type': 'string', 'default': 'foo'}, 'bar\n',
31 self
.assertEquals([' "bar\\n",'], lines
)
33 GenerateFieldContent('', {'type': 'string'}, None, lines
, ' ', {})
34 self
.assertEquals([' NULL,'], lines
)
36 GenerateFieldContent('', {'type': 'string'}, 'foo', lines
, ' ', {})
37 self
.assertEquals([' "foo",'], lines
)
39 def testGenerateString16FieldContent(self
):
41 GenerateFieldContent('', {'type': 'string16',
42 'default': u
'f\u00d8\u00d81a'},
44 self
.assertEquals([' L"f\\x00d8" L"\\x00d8" L"1a",'], lines
)
46 GenerateFieldContent('', {'type': 'string16', 'default': 'foo'},
47 u
'b\uc3a5r', lines
, ' ', {})
48 self
.assertEquals([' L"b\\xc3a5" L"r",'], lines
)
50 GenerateFieldContent('', {'type': 'string16'}, None, lines
, ' ', {})
51 self
.assertEquals([' NULL,'], lines
)
53 GenerateFieldContent('', {'type': 'string16'}, u
'foo\\u1234', lines
, ' ',
55 self
.assertEquals([' L"foo\\\\u1234",'], lines
)
57 def testGenerateEnumFieldContent(self
):
59 GenerateFieldContent('', {'type': 'enum', 'default': 'RED'}, None, lines
,
61 self
.assertEquals([' RED,'], lines
)
63 GenerateFieldContent('', {'type': 'enum', 'default': 'RED'}, 'BLACK', lines
,
65 self
.assertEquals([' BLACK,'], 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'}},
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' +
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' +
95 ' array_test_my_array_1,\n' +
96 ' 2,', '\n'.join(lines
))
98 def testGenerateElements(self
):
100 {'field': 'f0', 'type': 'int', 'default': 1000, 'optional': True},
101 {'field': 'f1', 'type': 'string'},
102 {'field': 'f2', 'type': 'enum', 'ctype': 'QuasiBool', 'default': 'MAYBE',
104 {'field': 'f3', 'type': 'array', 'contents': {'type': 'string16'},
109 'type_name': 'InnerType',
111 {'field': 'g0', 'type': 'string'}
120 'type_name': 'InnerType',
122 {'field': 'a0', 'type': 'string'},
123 {'field': 'a1', 'type': 'string'}
130 'int_variables': {'a': -5, 'b': 5},
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'
157 'elem1': 'const MyType elem1 = {\n'
167 'elem2': 'const wchar_t* const array_elem2_f3[] = {\n'
171 'const MyType elem2 = {\n'
181 'elem3': 'const MyType elem3 = {\n'
193 'elem4': 'const InnerType array_elem4_f5[] = {\n'
199 'const MyType elem4 = {\n'
211 for key
, value
in description
['int_variables'].items():
212 expected
+= int_variable_expected
[key
]
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
):
224 {'field': 'f0', 'type': 'int'},
225 {'field': 'f1', 'type': 'string'},
228 'int_variables': {'a': -5, 'b': 5},
234 self
.assertRaises(RuntimeError,
235 lambda: GenerateElements('MyType', schema
, description
))
237 if __name__
== '__main__':