summaryrefslogtreecommitdiffstats
path: root/tools/json_to_struct/element_generator_test.py
diff options
context:
space:
mode:
authorbeaudoin@chromium.org <beaudoin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-15 00:37:34 +0000
committerbeaudoin@chromium.org <beaudoin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-15 00:37:34 +0000
commite02d8e288c3f4be8220c21b3936e484892e39095 (patch)
tree65ec2f5c688fe0570fe607f0a2ea78f1adaa9ada /tools/json_to_struct/element_generator_test.py
parent63931bd97ba41089edb259b906e0869b3de96c70 (diff)
downloadchromium_src-e02d8e288c3f4be8220c21b3936e484892e39095.zip
chromium_src-e02d8e288c3f4be8220c21b3936e484892e39095.tar.gz
chromium_src-e02d8e288c3f4be8220c21b3936e484892e39095.tar.bz2
Moving prepopulated search engines to a JSON file.
This CL also includes the python script required to convert the JSON file to a .cc/.h pair. The generated .cc/.h are not generated by the build process and must be committed to the repository. BUG=159990 Review URL: https://chromiumcodereview.appspot.com/11377049 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@167793 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/json_to_struct/element_generator_test.py')
-rwxr-xr-xtools/json_to_struct/element_generator_test.py158
1 files changed, 158 insertions, 0 deletions
diff --git a/tools/json_to_struct/element_generator_test.py b/tools/json_to_struct/element_generator_test.py
new file mode 100755
index 0000000..484e83f
--- /dev/null
+++ b/tools/json_to_struct/element_generator_test.py
@@ -0,0 +1,158 @@
+#!/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({'type': 'array', 'contents': {'type': 'int'}},
+ None, lines)
+ self.assertEquals(['STRUCT BEGINS', ' NULL,', ' 0,'], lines)
+ lines = ['STRUCT BEGINS'];
+ GenerateFieldContent({'field': 'my_array', 'type': 'array',
+ 'contents': {'type': 'int'}}, [3, 4], lines)
+ self.assertEquals('const int array_my_array[] = {\n' +
+ ' 3,\n' +
+ ' 4,\n' +
+ '};\n' +
+ 'STRUCT BEGINS\n' +
+ ' array_my_array,\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}
+ ]
+ 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']}
+ }
+ }
+
+ # 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'
+ '};\n',
+ 'elem1': 'const MyType elem1 = {\n' +
+ ' -2,\n' +
+ ' "bar",\n' +
+ ' NOWAY,\n' +
+ ' NULL,\n' +
+ ' 0,\n'
+ '};\n',
+ 'elem2': 'const wchar_t* const array_f3[] = {\n' +
+ ' L"bar",\n' +
+ ' L"foo",\n' +
+ '};\n' +
+ 'const MyType elem2 = {\n' +
+ ' 1000,\n' +
+ ' "foo_bar",\n' +
+ ' MAYBE,\n' +
+ ' array_f3,\n' +
+ ' 2,\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()