summaryrefslogtreecommitdiffstats
path: root/tools/json_to_struct/element_generator_test.py
blob: 373338ebed1d6f85f7f76b07b5740a68f63bb3e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from element_generator import GenerateFieldContent
from element_generator import GenerateElements
import unittest

class ElementGeneratorTest(unittest.TestCase):
  def testGenerateIntFieldContent(self):
    lines = [];
    GenerateFieldContent('', {'type': 'int', 'default': 5}, None, lines, '  ',
                         {})
    self.assertEquals(['  5,'], lines)
    lines = [];
    GenerateFieldContent('', {'type': 'int', 'default': 5}, 12, lines, '  ', {})
    self.assertEquals(['  12,'], lines)
    lines = [];
    GenerateFieldContent('', {'type': 'int'}, -3, lines, '  ', {})
    self.assertEquals(['  -3,'], lines)

  def testGenerateStringFieldContent(self):
    lines = [];
    GenerateFieldContent('', {'type': 'string', 'default': 'foo_bar'}, None,
                         lines, '  ', {})
    self.assertEquals(['  "foo_bar",'], lines)
    lines = [];
    GenerateFieldContent('', {'type': 'string', 'default': 'foo'}, 'bar\n',
                         lines, '  ', {})
    self.assertEquals(['  "bar\\n",'], lines)
    lines = [];
    GenerateFieldContent('', {'type': 'string'}, None, lines, '  ', {})
    self.assertEquals(['  NULL,'], lines)
    lines = [];
    GenerateFieldContent('', {'type': 'string'}, 'foo', lines, '  ', {})
    self.assertEquals(['  "foo",'], lines)

  def testGenerateString16FieldContent(self):
    lines = [];
    GenerateFieldContent('', {'type': 'string16',
                              'default': u'f\u00d8\u00d81a'},
                         None, lines, '  ', {})
    self.assertEquals(['  L"f\\x00d8" L"\\x00d8" L"1a",'], lines)
    lines = [];
    GenerateFieldContent('', {'type': 'string16', 'default': 'foo'},
                         u'b\uc3a5r', lines, '  ', {})
    self.assertEquals(['  L"b\\xc3a5" L"r",'], lines)
    lines = [];
    GenerateFieldContent('', {'type': 'string16'}, None, lines, '  ', {})
    self.assertEquals(['  NULL,'], lines)
    lines = [];
    GenerateFieldContent('', {'type': 'string16'}, u'foo\\u1234', lines, '  ',
                         {})
    self.assertEquals(['  L"foo\\\\u1234",'], lines)

  def testGenerateEnumFieldContent(self):
    lines = [];
    GenerateFieldContent('', {'type': 'enum', 'default': 'RED'}, None, lines,
                         '  ', {})
    self.assertEquals(['  RED,'], lines)
    lines = [];
    GenerateFieldContent('', {'type': 'enum', 'default': 'RED'}, 'BLACK', lines,
                         '  ', {})
    self.assertEquals(['  BLACK,'], lines)
    lines = [];
    GenerateFieldContent('', {'type': 'enum'}, 'BLUE', lines, '  ', {})
    self.assertEquals(['  BLUE,'], lines)

  def testGenerateArrayFieldContent(self):
    lines = ['STRUCT BEGINS'];
    GenerateFieldContent('test', {'type': 'array', 'contents': {'type': 'int'}},
                         None, lines, '  ', {})
    self.assertEquals(['STRUCT BEGINS', '  NULL,', '  0,'], lines)
    lines = ['STRUCT BEGINS'];
    GenerateFieldContent('test', {'field': 'my_array', 'type': 'array',
                                  'contents': {'type': 'int'}},
                         [3, 4], lines, '  ', {})
    self.assertEquals('const int array_test_my_array[] = {\n' +
      '  3,\n' +
      '  4,\n' +
      '};\n' +
      'STRUCT BEGINS\n' +
      '  array_test_my_array,\n' +
      '  2,', '\n'.join(lines))
    lines = ['STRUCT BEGINS'];
    GenerateFieldContent('test', {'field': 'my_array', 'type': 'array',
                                  'contents': {'type': 'int'}},
                         [3, 4], lines, '  ', {'array_test_my_array': 1})
    self.assertEquals('const int array_test_my_array_1[] = {\n' +
      '  3,\n' +
      '  4,\n' +
      '};\n' +
      'STRUCT BEGINS\n' +
      '  array_test_my_array_1,\n' +
      '  2,', '\n'.join(lines))

  def testGenerateElements(self):
    schema = [
      {'field': 'f0', 'type': 'int', 'default': 1000, 'optional': True},
      {'field': 'f1', 'type': 'string'},
      {'field': 'f2', 'type': 'enum', 'ctype': 'QuasiBool', 'default': 'MAYBE',
       'optional': True},
      {'field': 'f3', 'type': 'array', 'contents': {'type': 'string16'},
       'optional': True},
      {
        'field': 'f4',
        'type': 'struct',
        'type_name': 'InnerType',
        'fields': [
          {'field': 'g0', 'type': 'string'}
        ],
        'optional': True
      },
      {
        'field': 'f5',
        'type': 'array',
        'contents': {
          'type': 'struct',
          'type_name': 'InnerType',
          'fields': [
            {'field': 'a0', 'type': 'string'},
            {'field': 'a1', 'type': 'string'}
          ]
        },
        'optional': True
      }
    ]
    description = {
      'int_variables': {'a': -5, 'b': 5},
      'elements': {
        'elem0': {'f0': 5, 'f1': 'foo', 'f2': 'SURE'},
        'elem1': {'f2': 'NOWAY', 'f0': -2, 'f1': 'bar'},
        'elem2': {'f1': 'foo_bar', 'f3': [u'bar', u'foo']},
        'elem3': {'f1': 'foo', 'f4': {'g0': 'test'}},
        'elem4': {'f1': 'foo', 'f5': [{'a0': 'test0', 'a1': 'test1'}]},
      }
    }

    # Build the expected result stream based on the unpredicatble order the
    # dictionary element are listed in.
    int_variable_expected = {
      'a': 'const int a = -5;\n',
      'b': 'const int b = 5;\n',
    }
    elements_expected = {
      'elem0': 'const MyType elem0 = {\n'
               '  5,\n'
               '  "foo",\n'
               '  SURE,\n'
               '  NULL,\n'
               '  0,\n'
               '  {0},\n'
               '  NULL,\n'
               '  0,\n'
               '};\n',
      'elem1': 'const MyType elem1 = {\n'
               '  -2,\n'
               '  "bar",\n'
               '  NOWAY,\n'
               '  NULL,\n'
               '  0,\n'
               '  {0},\n'
               '  NULL,\n'
               '  0,\n'
               '};\n',
      'elem2': 'const wchar_t* const array_elem2_f3[] = {\n'
               '  L"bar",\n'
               '  L"foo",\n'
               '};\n'
               'const MyType elem2 = {\n'
               '  1000,\n'
               '  "foo_bar",\n'
               '  MAYBE,\n'
               '  array_elem2_f3,\n'
               '  2,\n'
               '  {0},\n'
               '  NULL,\n'
               '  0,\n'
               '};\n',
      'elem3': 'const MyType elem3 = {\n'
               '  1000,\n'
               '  "foo",\n'
               '  MAYBE,\n'
               '  NULL,\n'
               '  0,\n'
               '  {\n'
               '    "test",\n'
               '  },\n'
               '  NULL,\n'
               '  0,\n'
               '};\n',
      'elem4': 'const InnerType array_elem4_f5[] = {\n'
               '  {\n'
               '    "test0",\n'
               '    "test1",\n'
               '  },\n'
               '};\n'
               'const MyType elem4 = {\n'
               '  1000,\n'
               '  "foo",\n'
               '  MAYBE,\n'
               '  NULL,\n'
               '  0,\n'
               '  {0},\n'
               '  array_elem4_f5,\n'
               '  1,\n'
               '};\n'
    }
    expected = ''
    for key, value in description['int_variables'].items():
      expected += int_variable_expected[key]
    expected += '\n'
    elements = []
    for key, value in description['elements'].items():
      elements.append(elements_expected[key])
    expected += '\n'.join(elements)

    result = GenerateElements('MyType', schema, description)
    self.assertEquals(expected, result)

  def testGenerateElementsMissingMandatoryField(self):
    schema = [
      {'field': 'f0', 'type': 'int'},
      {'field': 'f1', 'type': 'string'},
    ]
    description = {
      'int_variables': {'a': -5, 'b': 5},
      'elements': {
        'elem0': {'f0': 5},
      }
    }

    self.assertRaises(RuntimeError,
      lambda: GenerateElements('MyType', schema, description))

if __name__ == '__main__':
  unittest.main()