| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 from element_generator import GenerateFieldContent | 6 from element_generator import GenerateFieldContent |
| 7 from element_generator import GenerateElements | 7 from element_generator import GenerateElements |
| 8 import unittest | 8 import unittest |
| 9 | 9 |
| 10 class ElementGeneratorTest(unittest.TestCase): | 10 class ElementGeneratorTest(unittest.TestCase): |
| 11 def testGenerateIntFieldContent(self): | 11 def testGenerateIntFieldContent(self): |
| 12 lines = []; | 12 lines = []; |
| 13 GenerateFieldContent('', {'type': 'int', 'default': 5}, None, lines) | 13 GenerateFieldContent('', {'type': 'int', 'default': 5}, None, lines, ' ', |
| 14 {}) |
| 14 self.assertEquals([' 5,'], lines) | 15 self.assertEquals([' 5,'], lines) |
| 15 lines = []; | 16 lines = []; |
| 16 GenerateFieldContent('', {'type': 'int', 'default': 5}, 12, lines) | 17 GenerateFieldContent('', {'type': 'int', 'default': 5}, 12, lines, ' ', {}) |
| 17 self.assertEquals([' 12,'], lines) | 18 self.assertEquals([' 12,'], lines) |
| 18 lines = []; | 19 lines = []; |
| 19 GenerateFieldContent('', {'type': 'int'}, -3, lines) | 20 GenerateFieldContent('', {'type': 'int'}, -3, lines, ' ', {}) |
| 20 self.assertEquals([' -3,'], lines) | 21 self.assertEquals([' -3,'], lines) |
| 21 | 22 |
| 22 def testGenerateStringFieldContent(self): | 23 def testGenerateStringFieldContent(self): |
| 23 lines = []; | 24 lines = []; |
| 24 GenerateFieldContent('', {'type': 'string', 'default': 'foo_bar'}, None, | 25 GenerateFieldContent('', {'type': 'string', 'default': 'foo_bar'}, None, |
| 25 lines) | 26 lines, ' ', {}) |
| 26 self.assertEquals([' "foo_bar",'], lines) | 27 self.assertEquals([' "foo_bar",'], lines) |
| 27 lines = []; | 28 lines = []; |
| 28 GenerateFieldContent('', {'type': 'string', 'default': 'foo'}, 'bar\n', | 29 GenerateFieldContent('', {'type': 'string', 'default': 'foo'}, 'bar\n', |
| 29 lines) | 30 lines, ' ', {}) |
| 30 self.assertEquals([' "bar\\n",'], lines) | 31 self.assertEquals([' "bar\\n",'], lines) |
| 31 lines = []; | 32 lines = []; |
| 32 GenerateFieldContent('', {'type': 'string'}, None, lines) | 33 GenerateFieldContent('', {'type': 'string'}, None, lines, ' ', {}) |
| 33 self.assertEquals([' NULL,'], lines) | 34 self.assertEquals([' NULL,'], lines) |
| 34 lines = []; | 35 lines = []; |
| 35 GenerateFieldContent('', {'type': 'string'}, 'foo', lines) | 36 GenerateFieldContent('', {'type': 'string'}, 'foo', lines, ' ', {}) |
| 36 self.assertEquals([' "foo",'], lines) | 37 self.assertEquals([' "foo",'], lines) |
| 37 | 38 |
| 38 def testGenerateString16FieldContent(self): | 39 def testGenerateString16FieldContent(self): |
| 39 lines = []; | 40 lines = []; |
| 40 GenerateFieldContent('', {'type': 'string16', | 41 GenerateFieldContent('', {'type': 'string16', |
| 41 'default': u'f\u00d8\u00d81a'}, None, lines) | 42 'default': u'f\u00d8\u00d81a'}, |
| 43 None, lines, ' ', {}) |
| 42 self.assertEquals([' L"f\\x00d8" L"\\x00d8" L"1a",'], lines) | 44 self.assertEquals([' L"f\\x00d8" L"\\x00d8" L"1a",'], lines) |
| 43 lines = []; | 45 lines = []; |
| 44 GenerateFieldContent('', {'type': 'string16', 'default': 'foo'}, | 46 GenerateFieldContent('', {'type': 'string16', 'default': 'foo'}, |
| 45 u'b\uc3a5r', lines) | 47 u'b\uc3a5r', lines, ' ', {}) |
| 46 self.assertEquals([' L"b\\xc3a5" L"r",'], lines) | 48 self.assertEquals([' L"b\\xc3a5" L"r",'], lines) |
| 47 lines = []; | 49 lines = []; |
| 48 GenerateFieldContent('', {'type': 'string16'}, None, lines) | 50 GenerateFieldContent('', {'type': 'string16'}, None, lines, ' ', {}) |
| 49 self.assertEquals([' NULL,'], lines) | 51 self.assertEquals([' NULL,'], lines) |
| 50 lines = []; | 52 lines = []; |
| 51 GenerateFieldContent('', {'type': 'string16'}, u'foo\\u1234', lines) | 53 GenerateFieldContent('', {'type': 'string16'}, u'foo\\u1234', lines, ' ', |
| 54 {}) |
| 52 self.assertEquals([' L"foo\\\\u1234",'], lines) | 55 self.assertEquals([' L"foo\\\\u1234",'], lines) |
| 53 | 56 |
| 54 def testGenerateEnumFieldContent(self): | 57 def testGenerateEnumFieldContent(self): |
| 55 lines = []; | 58 lines = []; |
| 56 GenerateFieldContent('', {'type': 'enum', 'default': 'RED'}, None, lines) | 59 GenerateFieldContent('', {'type': 'enum', 'default': 'RED'}, None, lines, |
| 60 ' ', {}) |
| 57 self.assertEquals([' RED,'], lines) | 61 self.assertEquals([' RED,'], lines) |
| 58 lines = []; | 62 lines = []; |
| 59 GenerateFieldContent('', {'type': 'enum', 'default': 'RED'}, 'BLACK', lines) | 63 GenerateFieldContent('', {'type': 'enum', 'default': 'RED'}, 'BLACK', lines, |
| 64 ' ', {}) |
| 60 self.assertEquals([' BLACK,'], lines) | 65 self.assertEquals([' BLACK,'], lines) |
| 61 lines = []; | 66 lines = []; |
| 62 GenerateFieldContent('', {'type': 'enum'}, 'BLUE', lines) | 67 GenerateFieldContent('', {'type': 'enum'}, 'BLUE', lines, ' ', {}) |
| 63 self.assertEquals([' BLUE,'], lines) | 68 self.assertEquals([' BLUE,'], lines) |
| 64 | 69 |
| 65 def testGenerateArrayFieldContent(self): | 70 def testGenerateArrayFieldContent(self): |
| 66 lines = ['STRUCT BEGINS']; | 71 lines = ['STRUCT BEGINS']; |
| 67 GenerateFieldContent('test', {'type': 'array', 'contents': {'type': 'int'}}, | 72 GenerateFieldContent('test', {'type': 'array', 'contents': {'type': 'int'}}, |
| 68 None, lines) | 73 None, lines, ' ', {}) |
| 69 self.assertEquals(['STRUCT BEGINS', ' NULL,', ' 0,'], lines) | 74 self.assertEquals(['STRUCT BEGINS', ' NULL,', ' 0,'], lines) |
| 70 lines = ['STRUCT BEGINS']; | 75 lines = ['STRUCT BEGINS']; |
| 71 GenerateFieldContent('test', {'field': 'my_array', 'type': 'array', | 76 GenerateFieldContent('test', {'field': 'my_array', 'type': 'array', |
| 72 'contents': {'type': 'int'}}, [3, 4], lines) | 77 'contents': {'type': 'int'}}, |
| 78 [3, 4], lines, ' ', {}) |
| 73 self.assertEquals('const int array_test_my_array[] = {\n' + | 79 self.assertEquals('const int array_test_my_array[] = {\n' + |
| 74 ' 3,\n' + | 80 ' 3,\n' + |
| 75 ' 4,\n' + | 81 ' 4,\n' + |
| 76 '};\n' + | 82 '};\n' + |
| 77 'STRUCT BEGINS\n' + | 83 'STRUCT BEGINS\n' + |
| 78 ' array_test_my_array,\n' + | 84 ' array_test_my_array,\n' + |
| 79 ' 2,', '\n'.join(lines)) | 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)) |
| 80 | 97 |
| 81 def testGenerateElements(self): | 98 def testGenerateElements(self): |
| 82 schema = [ | 99 schema = [ |
| 83 {'field': 'f0', 'type': 'int', 'default': 1000, 'optional': True}, | 100 {'field': 'f0', 'type': 'int', 'default': 1000, 'optional': True}, |
| 84 {'field': 'f1', 'type': 'string'}, | 101 {'field': 'f1', 'type': 'string'}, |
| 85 {'field': 'f2', 'type': 'enum', 'ctype': 'QuasiBool', 'default': 'MAYBE', | 102 {'field': 'f2', 'type': 'enum', 'ctype': 'QuasiBool', 'default': 'MAYBE', |
| 86 'optional': True}, | 103 'optional': True}, |
| 87 {'field': 'f3', 'type': 'array', 'contents': {'type': 'string16'}, | 104 {'field': 'f3', 'type': 'array', 'contents': {'type': 'string16'}, |
| 88 'optional': True} | 105 'optional': True}, |
| 106 { |
| 107 'field': 'f4', |
| 108 'type': 'struct', |
| 109 'type_name': 'InnerType', |
| 110 'fields': [ |
| 111 {'field': 'g0', 'type': 'string'} |
| 112 ], |
| 113 'optional': True |
| 114 }, |
| 115 { |
| 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'} |
| 124 ] |
| 125 }, |
| 126 'optional': True |
| 127 } |
| 89 ] | 128 ] |
| 90 description = { | 129 description = { |
| 91 'int_variables': {'a': -5, 'b': 5}, | 130 'int_variables': {'a': -5, 'b': 5}, |
| 92 'elements': { | 131 'elements': { |
| 93 'elem0': {'f0': 5, 'f1': 'foo', 'f2': 'SURE'}, | 132 'elem0': {'f0': 5, 'f1': 'foo', 'f2': 'SURE'}, |
| 94 'elem1': {'f2': 'NOWAY', 'f0': -2, 'f1': 'bar'}, | 133 'elem1': {'f2': 'NOWAY', 'f0': -2, 'f1': 'bar'}, |
| 95 'elem2': {'f1': 'foo_bar', 'f3': [u'bar', u'foo']} | 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'}]}, |
| 96 } | 137 } |
| 97 } | 138 } |
| 98 | 139 |
| 99 # Build the expected result stream based on the unpredicatble order the | 140 # Build the expected result stream based on the unpredicatble order the |
| 100 # dictionary element are listed in. | 141 # dictionary element are listed in. |
| 101 int_variable_expected = { | 142 int_variable_expected = { |
| 102 'a': 'const int a = -5;\n', | 143 'a': 'const int a = -5;\n', |
| 103 'b': 'const int b = 5;\n', | 144 'b': 'const int b = 5;\n', |
| 104 } | 145 } |
| 105 elements_expected = { | 146 elements_expected = { |
| 106 'elem0': 'const MyType elem0 = {\n' + | 147 'elem0': 'const MyType elem0 = {\n' |
| 107 ' 5,\n' + | 148 ' 5,\n' |
| 108 ' "foo",\n' + | 149 ' "foo",\n' |
| 109 ' SURE,\n' + | 150 ' SURE,\n' |
| 110 ' NULL,\n' + | 151 ' NULL,\n' |
| 152 ' 0,\n' |
| 153 ' {0},\n' |
| 154 ' NULL,\n' |
| 111 ' 0,\n' | 155 ' 0,\n' |
| 112 '};\n', | 156 '};\n', |
| 113 'elem1': 'const MyType elem1 = {\n' + | 157 'elem1': 'const MyType elem1 = {\n' |
| 114 ' -2,\n' + | 158 ' -2,\n' |
| 115 ' "bar",\n' + | 159 ' "bar",\n' |
| 116 ' NOWAY,\n' + | 160 ' NOWAY,\n' |
| 117 ' NULL,\n' + | 161 ' NULL,\n' |
| 162 ' 0,\n' |
| 163 ' {0},\n' |
| 164 ' NULL,\n' |
| 118 ' 0,\n' | 165 ' 0,\n' |
| 119 '};\n', | 166 '};\n', |
| 120 'elem2': 'const wchar_t* const array_elem2_f3[] = {\n' + | 167 'elem2': 'const wchar_t* const array_elem2_f3[] = {\n' |
| 121 ' L"bar",\n' + | 168 ' L"bar",\n' |
| 122 ' L"foo",\n' + | 169 ' L"foo",\n' |
| 123 '};\n' + | 170 '};\n' |
| 124 'const MyType elem2 = {\n' + | 171 'const MyType elem2 = {\n' |
| 125 ' 1000,\n' + | 172 ' 1000,\n' |
| 126 ' "foo_bar",\n' + | 173 ' "foo_bar",\n' |
| 127 ' MAYBE,\n' + | 174 ' MAYBE,\n' |
| 128 ' array_elem2_f3,\n' + | 175 ' array_elem2_f3,\n' |
| 129 ' 2,\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' |
| 130 '};\n' | 208 '};\n' |
| 131 } | 209 } |
| 132 expected = '' | 210 expected = '' |
| 133 for key, value in description['int_variables'].items(): | 211 for key, value in description['int_variables'].items(): |
| 134 expected += int_variable_expected[key] | 212 expected += int_variable_expected[key] |
| 135 expected += '\n' | 213 expected += '\n' |
| 136 elements = [] | 214 elements = [] |
| 137 for key, value in description['elements'].items(): | 215 for key, value in description['elements'].items(): |
| 138 elements.append(elements_expected[key]) | 216 elements.append(elements_expected[key]) |
| 139 expected += '\n'.join(elements) | 217 expected += '\n'.join(elements) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 151 'elements': { | 229 'elements': { |
| 152 'elem0': {'f0': 5}, | 230 'elem0': {'f0': 5}, |
| 153 } | 231 } |
| 154 } | 232 } |
| 155 | 233 |
| 156 self.assertRaises(RuntimeError, | 234 self.assertRaises(RuntimeError, |
| 157 lambda: GenerateElements('MyType', schema, description)) | 235 lambda: GenerateElements('MyType', schema, description)) |
| 158 | 236 |
| 159 if __name__ == '__main__': | 237 if __name__ == '__main__': |
| 160 unittest.main() | 238 unittest.main() |
| OLD | NEW |