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
)
14 self
.assertEquals([' 5,'], lines
)
16 GenerateFieldContent('', {'type': 'int', 'default': 5}, 12, lines
)
17 self
.assertEquals([' 12,'], lines
)
19 GenerateFieldContent('', {'type': 'int'}, -3, lines
)
20 self
.assertEquals([' -3,'], lines
)
22 def testGenerateStringFieldContent(self
):
24 GenerateFieldContent('', {'type': 'string', 'default': 'foo_bar'}, None,
26 self
.assertEquals([' "foo_bar",'], lines
)
28 GenerateFieldContent('', {'type': 'string', 'default': 'foo'}, 'bar\n',
30 self
.assertEquals([' "bar\\n",'], lines
)
32 GenerateFieldContent('', {'type': 'string'}, None, lines
)
33 self
.assertEquals([' NULL,'], lines
)
35 GenerateFieldContent('', {'type': 'string'}, 'foo', lines
)
36 self
.assertEquals([' "foo",'], lines
)
38 def testGenerateString16FieldContent(self
):
40 GenerateFieldContent('', {'type': 'string16',
41 'default': u
'f\u00d8\u00d81a'}, None, lines
)
42 self
.assertEquals([' L"f\\x00d8" L"\\x00d8" L"1a",'], lines
)
44 GenerateFieldContent('', {'type': 'string16', 'default': 'foo'},
46 self
.assertEquals([' L"b\\xc3a5" L"r",'], lines
)
48 GenerateFieldContent('', {'type': 'string16'}, None, lines
)
49 self
.assertEquals([' NULL,'], lines
)
51 GenerateFieldContent('', {'type': 'string16'}, u
'foo\\u1234', lines
)
52 self
.assertEquals([' L"foo\\\\u1234",'], lines
)
54 def testGenerateEnumFieldContent(self
):
56 GenerateFieldContent('', {'type': 'enum', 'default': 'RED'}, None, lines
)
57 self
.assertEquals([' RED,'], lines
)
59 GenerateFieldContent('', {'type': 'enum', 'default': 'RED'}, 'BLACK', lines
)
60 self
.assertEquals([' BLACK,'], lines
)
62 GenerateFieldContent('', {'type': 'enum'}, 'BLUE', lines
)
63 self
.assertEquals([' BLUE,'], lines
)
65 def testGenerateArrayFieldContent(self
):
66 lines
= ['STRUCT BEGINS'];
67 GenerateFieldContent('test', {'type': 'array', 'contents': {'type': 'int'}},
69 self
.assertEquals(['STRUCT BEGINS', ' NULL,', ' 0,'], lines
)
70 lines
= ['STRUCT BEGINS'];
71 GenerateFieldContent('test', {'field': 'my_array', 'type': 'array',
72 'contents': {'type': 'int'}}, [3, 4], lines
)
73 self
.assertEquals('const int array_test_my_array[] = {\n' +
78 ' array_test_my_array,\n' +
79 ' 2,', '\n'.join(lines
))
81 def testGenerateElements(self
):
83 {'field': 'f0', 'type': 'int', 'default': 1000, 'optional': True},
84 {'field': 'f1', 'type': 'string'},
85 {'field': 'f2', 'type': 'enum', 'ctype': 'QuasiBool', 'default': 'MAYBE',
87 {'field': 'f3', 'type': 'array', 'contents': {'type': 'string16'},
91 'int_variables': {'a': -5, 'b': 5},
93 'elem0': {'f0': 5, 'f1': 'foo', 'f2': 'SURE'},
94 'elem1': {'f2': 'NOWAY', 'f0': -2, 'f1': 'bar'},
95 'elem2': {'f1': 'foo_bar', 'f3': [u
'bar', u
'foo']}
99 # Build the expected result stream based on the unpredicatble order the
100 # dictionary element are listed in.
101 int_variable_expected
= {
102 'a': 'const int a = -5;\n',
103 'b': 'const int b = 5;\n',
105 elements_expected
= {
106 'elem0': 'const MyType elem0 = {\n' +
113 'elem1': 'const MyType elem1 = {\n' +
120 'elem2': 'const wchar_t* const array_elem2_f3[] = {\n' +
124 'const MyType elem2 = {\n' +
128 ' array_elem2_f3,\n' +
133 for key
, value
in description
['int_variables'].items():
134 expected
+= int_variable_expected
[key
]
137 for key
, value
in description
['elements'].items():
138 elements
.append(elements_expected
[key
])
139 expected
+= '\n'.join(elements
)
141 result
= GenerateElements('MyType', schema
, description
)
142 self
.assertEquals(expected
, result
)
144 def testGenerateElementsMissingMandatoryField(self
):
146 {'field': 'f0', 'type': 'int'},
147 {'field': 'f1', 'type': 'string'},
150 'int_variables': {'a': -5, 'b': 5},
156 self
.assertRaises(RuntimeError,
157 lambda: GenerateElements('MyType', schema
, description
))
159 if __name__
== '__main__':