OLD | NEW |
(Empty) | |
| 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. |
| 5 |
| 6 from element_generator import GenerateFieldContent |
| 7 from element_generator import GenerateElements |
| 8 import unittest |
| 9 |
| 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) |
| 21 |
| 22 def testGenerateStringFieldContent(self): |
| 23 lines = []; |
| 24 GenerateFieldContent({'type': 'string', 'default': 'foo_bar'}, None, lines) |
| 25 self.assertEquals([' "foo_bar",'], lines) |
| 26 lines = []; |
| 27 GenerateFieldContent({'type': 'string', 'default': 'foo'}, 'bar\n', lines) |
| 28 self.assertEquals([' "bar\\n",'], lines) |
| 29 lines = []; |
| 30 GenerateFieldContent({'type': 'string'}, None, lines) |
| 31 self.assertEquals([' NULL,'], lines) |
| 32 lines = []; |
| 33 GenerateFieldContent({'type': 'string'}, 'foo', lines) |
| 34 self.assertEquals([' "foo",'], lines) |
| 35 |
| 36 def testGenerateString16FieldContent(self): |
| 37 lines = []; |
| 38 GenerateFieldContent({'type': 'string16', 'default': u'f\u00d8\u00d81a'}, |
| 39 None, lines) |
| 40 self.assertEquals([' L"f\\x00d8" L"\\x00d8" L"1a",'], lines) |
| 41 lines = []; |
| 42 GenerateFieldContent({'type': 'string16', 'default': 'foo'}, u'b\uc3a5r', |
| 43 lines) |
| 44 self.assertEquals([' L"b\\xc3a5" L"r",'], lines) |
| 45 lines = []; |
| 46 GenerateFieldContent({'type': 'string16'}, None, lines) |
| 47 self.assertEquals([' NULL,'], lines) |
| 48 lines = []; |
| 49 GenerateFieldContent({'type': 'string16'}, u'foo\\u1234', lines) |
| 50 self.assertEquals([' L"foo\\\\u1234",'], lines) |
| 51 |
| 52 def testGenerateEnumFieldContent(self): |
| 53 lines = []; |
| 54 GenerateFieldContent({'type': 'enum', 'default': 'RED'}, None, lines) |
| 55 self.assertEquals([' RED,'], lines) |
| 56 lines = []; |
| 57 GenerateFieldContent({'type': 'enum', 'default': 'RED'}, 'BLACK', lines) |
| 58 self.assertEquals([' BLACK,'], lines) |
| 59 lines = []; |
| 60 GenerateFieldContent({'type': 'enum'}, 'BLUE', lines) |
| 61 self.assertEquals([' BLUE,'], lines) |
| 62 |
| 63 def testGenerateArrayFieldContent(self): |
| 64 lines = ['STRUCT BEGINS']; |
| 65 GenerateFieldContent({'type': 'array', 'contents': {'type': 'int'}}, |
| 66 None, lines) |
| 67 self.assertEquals(['STRUCT BEGINS', ' NULL,', ' 0,'], lines) |
| 68 lines = ['STRUCT BEGINS']; |
| 69 GenerateFieldContent({'field': 'my_array', 'type': 'array', |
| 70 'contents': {'type': 'int'}}, [3, 4], lines) |
| 71 self.assertEquals('const int array_my_array[] = {\n' + |
| 72 ' 3,\n' + |
| 73 ' 4,\n' + |
| 74 '};\n' + |
| 75 'STRUCT BEGINS\n' + |
| 76 ' array_my_array,\n' + |
| 77 ' 2,', '\n'.join(lines)) |
| 78 |
| 79 def testGenerateElements(self): |
| 80 schema = [ |
| 81 {'field': 'f0', 'type': 'int', 'default': 1000, 'optional': True}, |
| 82 {'field': 'f1', 'type': 'string'}, |
| 83 {'field': 'f2', 'type': 'enum', 'ctype': 'QuasiBool', 'default': 'MAYBE', |
| 84 'optional': True}, |
| 85 {'field': 'f3', 'type': 'array', 'contents': {'type': 'string16'}, |
| 86 'optional': True} |
| 87 ] |
| 88 description = { |
| 89 'int_variables': {'a': -5, 'b': 5}, |
| 90 'elements': { |
| 91 'elem0': {'f0': 5, 'f1': 'foo', 'f2': 'SURE'}, |
| 92 'elem1': {'f2': 'NOWAY', 'f0': -2, 'f1': 'bar'}, |
| 93 'elem2': {'f1': 'foo_bar', 'f3': [u'bar', u'foo']} |
| 94 } |
| 95 } |
| 96 |
| 97 # Build the expected result stream based on the unpredicatble order the |
| 98 # dictionary element are listed in. |
| 99 int_variable_expected = { |
| 100 'a': 'const int a = -5;\n', |
| 101 'b': 'const int b = 5;\n', |
| 102 } |
| 103 elements_expected = { |
| 104 'elem0': 'const MyType elem0 = {\n' + |
| 105 ' 5,\n' + |
| 106 ' "foo",\n' + |
| 107 ' SURE,\n' + |
| 108 ' NULL,\n' + |
| 109 ' 0,\n' |
| 110 '};\n', |
| 111 'elem1': 'const MyType elem1 = {\n' + |
| 112 ' -2,\n' + |
| 113 ' "bar",\n' + |
| 114 ' NOWAY,\n' + |
| 115 ' NULL,\n' + |
| 116 ' 0,\n' |
| 117 '};\n', |
| 118 'elem2': 'const wchar_t* const array_f3[] = {\n' + |
| 119 ' L"bar",\n' + |
| 120 ' L"foo",\n' + |
| 121 '};\n' + |
| 122 'const MyType elem2 = {\n' + |
| 123 ' 1000,\n' + |
| 124 ' "foo_bar",\n' + |
| 125 ' MAYBE,\n' + |
| 126 ' array_f3,\n' + |
| 127 ' 2,\n' |
| 128 '};\n' |
| 129 } |
| 130 expected = '' |
| 131 for key, value in description['int_variables'].items(): |
| 132 expected += int_variable_expected[key] |
| 133 expected += '\n' |
| 134 elements = [] |
| 135 for key, value in description['elements'].items(): |
| 136 elements.append(elements_expected[key]) |
| 137 expected += '\n'.join(elements) |
| 138 |
| 139 result = GenerateElements('MyType', schema, description) |
| 140 self.assertEquals(expected, result) |
| 141 |
| 142 def testGenerateElementsMissingMandatoryField(self): |
| 143 schema = [ |
| 144 {'field': 'f0', 'type': 'int'}, |
| 145 {'field': 'f1', 'type': 'string'}, |
| 146 ] |
| 147 description = { |
| 148 'int_variables': {'a': -5, 'b': 5}, |
| 149 'elements': { |
| 150 'elem0': {'f0': 5}, |
| 151 } |
| 152 } |
| 153 |
| 154 self.assertRaises(RuntimeError, |
| 155 lambda: GenerateElements('MyType', schema, description)) |
| 156 |
| 157 if __name__ == '__main__': |
| 158 unittest.main() |
OLD | NEW |