Enable InitCrashKeys() in Android Breakpad.
[chromium-blink-merge.git] / tools / json_to_struct / element_generator_test.py
blob67459a1d8713f142ebbc8850d2978ed279e09bae
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 self.assertEquals([' 5,'], lines)
15 lines = [];
16 GenerateFieldContent('', {'type': 'int', 'default': 5}, 12, lines)
17 self.assertEquals([' 12,'], lines)
18 lines = [];
19 GenerateFieldContent('', {'type': 'int'}, -3, lines)
20 self.assertEquals([' -3,'], lines)
22 def testGenerateStringFieldContent(self):
23 lines = [];
24 GenerateFieldContent('', {'type': 'string', 'default': 'foo_bar'}, None,
25 lines)
26 self.assertEquals([' "foo_bar",'], lines)
27 lines = [];
28 GenerateFieldContent('', {'type': 'string', 'default': 'foo'}, 'bar\n',
29 lines)
30 self.assertEquals([' "bar\\n",'], lines)
31 lines = [];
32 GenerateFieldContent('', {'type': 'string'}, None, lines)
33 self.assertEquals([' NULL,'], lines)
34 lines = [];
35 GenerateFieldContent('', {'type': 'string'}, 'foo', lines)
36 self.assertEquals([' "foo",'], lines)
38 def testGenerateString16FieldContent(self):
39 lines = [];
40 GenerateFieldContent('', {'type': 'string16',
41 'default': u'f\u00d8\u00d81a'}, None, lines)
42 self.assertEquals([' L"f\\x00d8" L"\\x00d8" L"1a",'], lines)
43 lines = [];
44 GenerateFieldContent('', {'type': 'string16', 'default': 'foo'},
45 u'b\uc3a5r', lines)
46 self.assertEquals([' L"b\\xc3a5" L"r",'], lines)
47 lines = [];
48 GenerateFieldContent('', {'type': 'string16'}, None, lines)
49 self.assertEquals([' NULL,'], lines)
50 lines = [];
51 GenerateFieldContent('', {'type': 'string16'}, u'foo\\u1234', lines)
52 self.assertEquals([' L"foo\\\\u1234",'], lines)
54 def testGenerateEnumFieldContent(self):
55 lines = [];
56 GenerateFieldContent('', {'type': 'enum', 'default': 'RED'}, None, lines)
57 self.assertEquals([' RED,'], lines)
58 lines = [];
59 GenerateFieldContent('', {'type': 'enum', 'default': 'RED'}, 'BLACK', lines)
60 self.assertEquals([' BLACK,'], lines)
61 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'}},
68 None, lines)
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' +
74 ' 3,\n' +
75 ' 4,\n' +
76 '};\n' +
77 'STRUCT BEGINS\n' +
78 ' array_test_my_array,\n' +
79 ' 2,', '\n'.join(lines))
81 def testGenerateElements(self):
82 schema = [
83 {'field': 'f0', 'type': 'int', 'default': 1000, 'optional': True},
84 {'field': 'f1', 'type': 'string'},
85 {'field': 'f2', 'type': 'enum', 'ctype': 'QuasiBool', 'default': 'MAYBE',
86 'optional': True},
87 {'field': 'f3', 'type': 'array', 'contents': {'type': 'string16'},
88 'optional': True}
90 description = {
91 'int_variables': {'a': -5, 'b': 5},
92 'elements': {
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' +
107 ' 5,\n' +
108 ' "foo",\n' +
109 ' SURE,\n' +
110 ' NULL,\n' +
111 ' 0,\n'
112 '};\n',
113 'elem1': 'const MyType elem1 = {\n' +
114 ' -2,\n' +
115 ' "bar",\n' +
116 ' NOWAY,\n' +
117 ' NULL,\n' +
118 ' 0,\n'
119 '};\n',
120 'elem2': 'const wchar_t* const array_elem2_f3[] = {\n' +
121 ' L"bar",\n' +
122 ' L"foo",\n' +
123 '};\n' +
124 'const MyType elem2 = {\n' +
125 ' 1000,\n' +
126 ' "foo_bar",\n' +
127 ' MAYBE,\n' +
128 ' array_elem2_f3,\n' +
129 ' 2,\n'
130 '};\n'
132 expected = ''
133 for key, value in description['int_variables'].items():
134 expected += int_variable_expected[key]
135 expected += '\n'
136 elements = []
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):
145 schema = [
146 {'field': 'f0', 'type': 'int'},
147 {'field': 'f1', 'type': 'string'},
149 description = {
150 'int_variables': {'a': -5, 'b': 5},
151 'elements': {
152 'elem0': {'f0': 5},
156 self.assertRaises(RuntimeError,
157 lambda: GenerateElements('MyType', schema, description))
159 if __name__ == '__main__':
160 unittest.main()