diff options
Diffstat (limited to 'mojo/public/tools/bindings/pylib/mojom_tests')
17 files changed, 0 insertions, 2485 deletions
diff --git a/mojo/public/tools/bindings/pylib/mojom_tests/__init__.py b/mojo/public/tools/bindings/pylib/mojom_tests/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/mojo/public/tools/bindings/pylib/mojom_tests/__init__.py +++ /dev/null diff --git a/mojo/public/tools/bindings/pylib/mojom_tests/fileutil_unittest.py b/mojo/public/tools/bindings/pylib/mojom_tests/fileutil_unittest.py deleted file mode 100644 index d56faad..0000000 --- a/mojo/public/tools/bindings/pylib/mojom_tests/fileutil_unittest.py +++ /dev/null @@ -1,55 +0,0 @@ -# Copyright 2015 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. - -import imp -import os.path -import shutil -import sys -import tempfile -import unittest - -def _GetDirAbove(dirname): - """Returns the directory "above" this file containing |dirname| (which must - also be "above" this file).""" - path = os.path.abspath(__file__) - while True: - path, tail = os.path.split(path) - assert tail - if tail == dirname: - return path - -try: - imp.find_module("mojom") -except ImportError: - sys.path.append(os.path.join(_GetDirAbove("pylib"), "pylib")) -from mojom import fileutil - - -class FileUtilTest(unittest.TestCase): - - def testEnsureDirectoryExists(self): - """Test that EnsureDirectoryExists fuctions correctly.""" - - temp_dir = tempfile.mkdtemp() - try: - self.assertTrue(os.path.exists(temp_dir)) - - # Directory does not exist, yet. - full = os.path.join(temp_dir, "foo", "bar") - self.assertFalse(os.path.exists(full)) - - # Create the directory. - fileutil.EnsureDirectoryExists(full) - self.assertTrue(os.path.exists(full)) - - # Trying to create it again does not cause an error. - fileutil.EnsureDirectoryExists(full) - self.assertTrue(os.path.exists(full)) - - # Bypass check for directory existence to tickle error handling that - # occurs in response to a race. - fileutil.EnsureDirectoryExists(full, always_try_to_create=True) - self.assertTrue(os.path.exists(full)) - finally: - shutil.rmtree(temp_dir) diff --git a/mojo/public/tools/bindings/pylib/mojom_tests/generate/__init__.py b/mojo/public/tools/bindings/pylib/mojom_tests/generate/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/mojo/public/tools/bindings/pylib/mojom_tests/generate/__init__.py +++ /dev/null diff --git a/mojo/public/tools/bindings/pylib/mojom_tests/generate/data_unittest.py b/mojo/public/tools/bindings/pylib/mojom_tests/generate/data_unittest.py deleted file mode 100644 index 70c92a3..0000000 --- a/mojo/public/tools/bindings/pylib/mojom_tests/generate/data_unittest.py +++ /dev/null @@ -1,156 +0,0 @@ -# Copyright 2014 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. - -import imp -import os.path -import sys -import unittest - -def _GetDirAbove(dirname): - """Returns the directory "above" this file containing |dirname| (which must - also be "above" this file).""" - path = os.path.abspath(__file__) - while True: - path, tail = os.path.split(path) - assert tail - if tail == dirname: - return path - -try: - imp.find_module("mojom") -except ImportError: - sys.path.append(os.path.join(_GetDirAbove("pylib"), "pylib")) -from mojom.generate import data -from mojom.generate import module as mojom - - -class DataTest(unittest.TestCase): - - def testStructDataConversion(self): - """Tests that a struct can be converted from data.""" - module = mojom.Module('test_module', 'test_namespace') - struct_data = { - 'name': 'SomeStruct', - 'enums': [], - 'constants': [], - 'fields': [ - {'name': 'field1', 'kind': 'i32'}, - {'name': 'field2', 'kind': 'i32', 'ordinal': 10}, - {'name': 'field3', 'kind': 'i32', 'default': 15}]} - - struct = data.StructFromData(module, struct_data) - struct.fields = map(lambda field: - data.StructFieldFromData(module, field, struct), struct.fields_data) - self.assertEquals(struct_data, data.StructToData(struct)) - - def testUnionDataConversion(self): - """Tests that a union can be converted from data.""" - module = mojom.Module('test_module', 'test_namespace') - union_data = { - 'name': 'SomeUnion', - 'fields': [ - {'name': 'field1', 'kind': 'i32'}, - {'name': 'field2', 'kind': 'i32', 'ordinal': 10}]} - - union = data.UnionFromData(module, union_data) - union.fields = map(lambda field: - data.UnionFieldFromData(module, field, union), union.fields_data) - self.assertEquals(union_data, data.UnionToData(union)) - - def testImportFromDataNoMissingImports(self): - """Tests that unions, structs, interfaces and enums are imported.""" - module = mojom.Module('test_module', 'test_namespace') - imported_module = mojom.Module('import_module', 'import_namespace') - #TODO(azani): Init values in module.py. - #TODO(azani): Test that values are imported. - imported_module.values = {} - imported_data = {'module' : imported_module} - - - struct = mojom.Struct('TestStruct', module=module) - imported_module.kinds[struct.spec] = struct - - union = mojom.Union('TestUnion', module=module) - imported_module.kinds[union.spec] = union - - interface = mojom.Interface('TestInterface', module=module) - imported_module.kinds[interface.spec] = interface - - enum = mojom.Enum('TestEnum', module=module) - imported_module.kinds[enum.spec] = enum - - data.ImportFromData(module, imported_data) - - # Test that the kind was imported. - self.assertIn(struct.spec, module.kinds) - self.assertEquals(struct.name, module.kinds[struct.spec].name) - - self.assertIn(union.spec, module.kinds) - self.assertEquals(union.name, module.kinds[union.spec].name) - - self.assertIn(interface.spec, module.kinds) - self.assertEquals(interface.name, module.kinds[interface.spec].name) - - self.assertIn(enum.spec, module.kinds) - self.assertEquals(enum.name, module.kinds[enum.spec].name) - - # Test that the imported kind is a copy and not the original. - self.assertIsNot(struct, module.kinds[struct.spec]) - self.assertIsNot(union, module.kinds[union.spec]) - self.assertIsNot(interface, module.kinds[interface.spec]) - self.assertIsNot(enum, module.kinds[enum.spec]) - - def testImportFromDataNoExtraneousImports(self): - """Tests that arrays, maps and interface requests are not imported.""" - module = mojom.Module('test_module', 'test_namespace') - imported_module = mojom.Module('import_module', 'import_namespace') - #TODO(azani): Init values in module.py. - imported_module.values = {} - imported_data = {'module' : imported_module} - - array = mojom.Array(mojom.INT16, length=20) - imported_module.kinds[array.spec] = array - - map_kind = mojom.Map(mojom.INT16, mojom.INT16) - imported_module.kinds[map_kind.spec] = map_kind - - interface = mojom.Interface('TestInterface', module=module) - imported_module.kinds[interface.spec] = interface - - interface_req = mojom.InterfaceRequest(interface) - imported_module.kinds[interface_req.spec] = interface_req - - data.ImportFromData(module, imported_data) - - self.assertNotIn(array.spec, module.kinds) - self.assertNotIn(map_kind.spec, module.kinds) - self.assertNotIn(interface_req.spec, module.kinds) - - def testNonInterfaceAsInterfaceRequest(self): - """Tests that a non-interface cannot be used for interface requests.""" - module = mojom.Module('test_module', 'test_namespace') - interface = mojom.Interface('TestInterface', module=module) - method_dict = { - 'name': 'Foo', - 'parameters': [{'name': 'foo', 'kind': 'r:i32'}], - } - with self.assertRaises(Exception) as e: - data.MethodFromData(module, method_dict, interface) - self.assertEquals(e.exception.__str__(), - 'Interface request requires \'i32\' to be an interface.') - - def testNonInterfaceAsAssociatedInterface(self): - """Tests that a non-interface type cannot be used for associated - interfaces.""" - module = mojom.Module('test_module', 'test_namespace') - interface = mojom.Interface('TestInterface', module=module) - method_dict = { - 'name': 'Foo', - 'parameters': [{'name': 'foo', 'kind': 'asso:i32'}], - } - with self.assertRaises(Exception) as e: - data.MethodFromData(module, method_dict, interface) - self.assertEquals( - e.exception.__str__(), - 'Associated interface requires \'i32\' to be an interface.') diff --git a/mojo/public/tools/bindings/pylib/mojom_tests/generate/generator_unittest.py b/mojo/public/tools/bindings/pylib/mojom_tests/generate/generator_unittest.py deleted file mode 100644 index a684773..0000000 --- a/mojo/public/tools/bindings/pylib/mojom_tests/generate/generator_unittest.py +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright 2014 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. - -import imp -import os.path -import sys -import unittest - -def _GetDirAbove(dirname): - """Returns the directory "above" this file containing |dirname| (which must - also be "above" this file).""" - path = os.path.abspath(__file__) - while True: - path, tail = os.path.split(path) - assert tail - if tail == dirname: - return path - -try: - imp.find_module("mojom") -except ImportError: - sys.path.append(os.path.join(_GetDirAbove("pylib"), "pylib")) -from mojom.generate import generator - - -class StringManipulationTest(unittest.TestCase): - """generator contains some string utilities, this tests only those.""" - - def testUnderToCamel(self): - """Tests UnderToCamel which converts underscore_separated to CamelCase.""" - self.assertEquals("CamelCase", generator.UnderToCamel("camel_case")) - self.assertEquals("CamelCase", generator.UnderToCamel("CAMEL_CASE")) - -if __name__ == "__main__": - unittest.main() - diff --git a/mojo/public/tools/bindings/pylib/mojom_tests/generate/module_unittest.py b/mojo/public/tools/bindings/pylib/mojom_tests/generate/module_unittest.py deleted file mode 100644 index 75b7cd5..0000000 --- a/mojo/public/tools/bindings/pylib/mojom_tests/generate/module_unittest.py +++ /dev/null @@ -1,48 +0,0 @@ -# Copyright 2014 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. - -import imp -import os.path -import sys -import unittest - -def _GetDirAbove(dirname): - """Returns the directory "above" this file containing |dirname| (which must - also be "above" this file).""" - path = os.path.abspath(__file__) - while True: - path, tail = os.path.split(path) - assert tail - if tail == dirname: - return path - -try: - imp.find_module("mojom") -except ImportError: - sys.path.append(os.path.join(_GetDirAbove("pylib"), "pylib")) -from mojom.generate import module as mojom - - -class ModuleTest(unittest.TestCase): - - def testNonInterfaceAsInterfaceRequest(self): - """Tests that a non-interface cannot be used for interface requests.""" - module = mojom.Module('test_module', 'test_namespace') - struct = mojom.Struct('TestStruct', module=module) - with self.assertRaises(Exception) as e: - mojom.InterfaceRequest(struct) - self.assertEquals( - e.exception.__str__(), - 'Interface request requires \'x:TestStruct\' to be an interface.') - - def testNonInterfaceAsAssociatedInterface(self): - """Tests that a non-interface type cannot be used for associated interfaces. - """ - module = mojom.Module('test_module', 'test_namespace') - struct = mojom.Struct('TestStruct', module=module) - with self.assertRaises(Exception) as e: - mojom.AssociatedInterface(struct) - self.assertEquals( - e.exception.__str__(), - 'Associated interface requires \'x:TestStruct\' to be an interface.') diff --git a/mojo/public/tools/bindings/pylib/mojom_tests/generate/pack_unittest.py b/mojo/public/tools/bindings/pylib/mojom_tests/generate/pack_unittest.py deleted file mode 100644 index 75f6d51..0000000 --- a/mojo/public/tools/bindings/pylib/mojom_tests/generate/pack_unittest.py +++ /dev/null @@ -1,136 +0,0 @@ -# Copyright 2015 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. - -import imp -import os.path -import sys -import unittest - - -def _GetDirAbove(dirname): - """Returns the directory "above" this file containing |dirname| (which must - also be "above" this file).""" - path = os.path.abspath(__file__) - while True: - path, tail = os.path.split(path) - assert tail - if tail == dirname: - return path - - -try: - imp.find_module("mojom") -except ImportError: - sys.path.append(os.path.join(_GetDirAbove("pylib"), "pylib")) -from mojom.generate import pack -from mojom.generate import module as mojom - - -# TODO(yzshen): Move tests in pack_tests.py here. -class PackTest(unittest.TestCase): - def _CheckPackSequence(self, kinds, fields, offsets): - """Checks the pack order and offsets of a sequence of mojom.Kinds. - - Args: - kinds: A sequence of mojom.Kinds that specify the fields that are to be - created. - fields: The expected order of the resulting fields, with the integer "1" - first. - offsets: The expected order of offsets, with the integer "0" first. - """ - struct = mojom.Struct('test') - index = 1 - for kind in kinds: - struct.AddField('%d' % index, kind) - index += 1 - ps = pack.PackedStruct(struct) - num_fields = len(ps.packed_fields) - self.assertEquals(len(kinds), num_fields) - for i in xrange(num_fields): - self.assertEquals('%d' % fields[i], ps.packed_fields[i].field.name) - self.assertEquals(offsets[i], ps.packed_fields[i].offset) - - def testMinVersion(self): - """Tests that |min_version| is properly set for packed fields.""" - struct = mojom.Struct('test') - struct.AddField('field_2', mojom.BOOL, 2) - struct.AddField('field_0', mojom.INT32, 0) - struct.AddField('field_1', mojom.INT64, 1) - ps = pack.PackedStruct(struct) - - self.assertEquals('field_0', ps.packed_fields[0].field.name) - self.assertEquals('field_2', ps.packed_fields[1].field.name) - self.assertEquals('field_1', ps.packed_fields[2].field.name) - - self.assertEquals(0, ps.packed_fields[0].min_version) - self.assertEquals(0, ps.packed_fields[1].min_version) - self.assertEquals(0, ps.packed_fields[2].min_version) - - struct.fields[0].attributes = {'MinVersion': 1} - ps = pack.PackedStruct(struct) - - self.assertEquals(0, ps.packed_fields[0].min_version) - self.assertEquals(1, ps.packed_fields[1].min_version) - self.assertEquals(0, ps.packed_fields[2].min_version) - - def testGetVersionInfoEmptyStruct(self): - """Tests that pack.GetVersionInfo() never returns an empty list, even for - empty structs. - """ - struct = mojom.Struct('test') - ps = pack.PackedStruct(struct) - - versions = pack.GetVersionInfo(ps) - self.assertEquals(1, len(versions)) - self.assertEquals(0, versions[0].version) - self.assertEquals(0, versions[0].num_fields) - self.assertEquals(8, versions[0].num_bytes) - - def testGetVersionInfoComplexOrder(self): - """Tests pack.GetVersionInfo() using a struct whose definition order, - ordinal order and pack order for fields are all different. - """ - struct = mojom.Struct('test') - struct.AddField('field_3', mojom.BOOL, ordinal=3, - attributes={'MinVersion': 3}) - struct.AddField('field_0', mojom.INT32, ordinal=0) - struct.AddField('field_1', mojom.INT64, ordinal=1, - attributes={'MinVersion': 2}) - struct.AddField('field_2', mojom.INT64, ordinal=2, - attributes={'MinVersion': 3}) - ps = pack.PackedStruct(struct) - - versions = pack.GetVersionInfo(ps) - self.assertEquals(3, len(versions)) - - self.assertEquals(0, versions[0].version) - self.assertEquals(1, versions[0].num_fields) - self.assertEquals(16, versions[0].num_bytes) - - self.assertEquals(2, versions[1].version) - self.assertEquals(2, versions[1].num_fields) - self.assertEquals(24, versions[1].num_bytes) - - self.assertEquals(3, versions[2].version) - self.assertEquals(4, versions[2].num_fields) - self.assertEquals(32, versions[2].num_bytes) - - def testInterfaceAlignment(self): - """Tests that interfaces are aligned on 4-byte boundaries, although the size - of an interface is 8 bytes. - """ - kinds = (mojom.INT32, mojom.Interface('test_interface')) - fields = (1, 2) - offsets = (0, 4) - self._CheckPackSequence(kinds, fields, offsets) - - def testAssociatedInterfaceAlignment(self): - """Tests that associated interfaces are aligned on 4-byte boundaries, - although the size of an associated interface is 8 bytes. - """ - kinds = (mojom.INT32, - mojom.AssociatedInterface(mojom.Interface('test_interface'))) - fields = (1, 2) - offsets = (0, 4) - self._CheckPackSequence(kinds, fields, offsets) diff --git a/mojo/public/tools/bindings/pylib/mojom_tests/parse/__init__.py b/mojo/public/tools/bindings/pylib/mojom_tests/parse/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/mojo/public/tools/bindings/pylib/mojom_tests/parse/__init__.py +++ /dev/null diff --git a/mojo/public/tools/bindings/pylib/mojom_tests/parse/ast_unittest.py b/mojo/public/tools/bindings/pylib/mojom_tests/parse/ast_unittest.py deleted file mode 100644 index dd28cdd..0000000 --- a/mojo/public/tools/bindings/pylib/mojom_tests/parse/ast_unittest.py +++ /dev/null @@ -1,135 +0,0 @@ -# Copyright 2014 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. - -import imp -import os.path -import sys -import unittest - -def _GetDirAbove(dirname): - """Returns the directory "above" this file containing |dirname| (which must - also be "above" this file).""" - path = os.path.abspath(__file__) - while True: - path, tail = os.path.split(path) - assert tail - if tail == dirname: - return path - -try: - imp.find_module("mojom") -except ImportError: - sys.path.append(os.path.join(_GetDirAbove("pylib"), "pylib")) -import mojom.parse.ast as ast - - -class _TestNode(ast.NodeBase): - """Node type for tests.""" - - def __init__(self, value, **kwargs): - super(_TestNode, self).__init__(**kwargs) - self.value = value - - def __eq__(self, other): - return super(_TestNode, self).__eq__(other) and self.value == other.value - - -class _TestNodeList(ast.NodeListBase): - """Node list type for tests.""" - - _list_item_type = _TestNode - - -class ASTTest(unittest.TestCase): - """Tests various AST classes.""" - - def testNodeBase(self): - # Test |__eq__()|; this is only used for testing, where we want to do - # comparison by value and ignore filenames/line numbers (for convenience). - node1 = ast.NodeBase(filename="hello.mojom", lineno=123) - node2 = ast.NodeBase() - self.assertEquals(node1, node2) - self.assertEquals(node2, node1) - - # Check that |__ne__()| just defers to |__eq__()| properly. - self.assertFalse(node1 != node2) - self.assertFalse(node2 != node1) - - # Check that |filename| and |lineno| are set properly (and are None by - # default). - self.assertEquals(node1.filename, "hello.mojom") - self.assertEquals(node1.lineno, 123) - self.assertIsNone(node2.filename) - self.assertIsNone(node2.lineno) - - # |NodeBase|'s |__eq__()| should compare types (and a subclass's |__eq__()| - # should first defer to its superclass's). - node3 = _TestNode(123) - self.assertNotEqual(node1, node3) - self.assertNotEqual(node3, node1) - # Also test |__eq__()| directly. - self.assertFalse(node1 == node3) - self.assertFalse(node3 == node1) - - node4 = _TestNode(123, filename="world.mojom", lineno=123) - self.assertEquals(node4, node3) - node5 = _TestNode(456) - self.assertNotEquals(node5, node4) - - def testNodeListBase(self): - node1 = _TestNode(1, filename="foo.mojom", lineno=1) - # Equal to, but not the same as, |node1|: - node1b = _TestNode(1, filename="foo.mojom", lineno=1) - node2 = _TestNode(2, filename="foo.mojom", lineno=2) - - nodelist1 = _TestNodeList() # Contains: (empty). - self.assertEquals(nodelist1, nodelist1) - self.assertEquals(nodelist1.items, []) - self.assertIsNone(nodelist1.filename) - self.assertIsNone(nodelist1.lineno) - - nodelist2 = _TestNodeList(node1) # Contains: 1. - self.assertEquals(nodelist2, nodelist2) - self.assertEquals(nodelist2.items, [node1]) - self.assertNotEqual(nodelist2, nodelist1) - self.assertEquals(nodelist2.filename, "foo.mojom") - self.assertEquals(nodelist2.lineno, 1) - - nodelist3 = _TestNodeList([node2]) # Contains: 2. - self.assertEquals(nodelist3.items, [node2]) - self.assertNotEqual(nodelist3, nodelist1) - self.assertNotEqual(nodelist3, nodelist2) - self.assertEquals(nodelist3.filename, "foo.mojom") - self.assertEquals(nodelist3.lineno, 2) - - nodelist1.Append(node1b) # Contains: 1. - self.assertEquals(nodelist1.items, [node1]) - self.assertEquals(nodelist1, nodelist2) - self.assertNotEqual(nodelist1, nodelist3) - self.assertEquals(nodelist1.filename, "foo.mojom") - self.assertEquals(nodelist1.lineno, 1) - - nodelist1.Append(node2) # Contains: 1, 2. - self.assertEquals(nodelist1.items, [node1, node2]) - self.assertNotEqual(nodelist1, nodelist2) - self.assertNotEqual(nodelist1, nodelist3) - self.assertEquals(nodelist1.lineno, 1) - - nodelist2.Append(node2) # Contains: 1, 2. - self.assertEquals(nodelist2.items, [node1, node2]) - self.assertEquals(nodelist2, nodelist1) - self.assertNotEqual(nodelist2, nodelist3) - self.assertEquals(nodelist2.lineno, 1) - - nodelist3.Insert(node1) # Contains: 1, 2. - self.assertEquals(nodelist3.items, [node1, node2]) - self.assertEquals(nodelist3, nodelist1) - self.assertEquals(nodelist3, nodelist2) - self.assertEquals(nodelist3.lineno, 1) - - # Test iteration: - i = 1 - for item in nodelist1: - self.assertEquals(item.value, i) - i += 1 diff --git a/mojo/public/tools/bindings/pylib/mojom_tests/parse/lexer_unittest.py b/mojo/public/tools/bindings/pylib/mojom_tests/parse/lexer_unittest.py deleted file mode 100644 index 6822cbc..0000000 --- a/mojo/public/tools/bindings/pylib/mojom_tests/parse/lexer_unittest.py +++ /dev/null @@ -1,192 +0,0 @@ -# Copyright 2014 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. - -import imp -import os.path -import sys -import unittest - -def _GetDirAbove(dirname): - """Returns the directory "above" this file containing |dirname| (which must - also be "above" this file).""" - path = os.path.abspath(__file__) - while True: - path, tail = os.path.split(path) - assert tail - if tail == dirname: - return path - -try: - imp.find_module("ply") -except ImportError: - sys.path.append(os.path.join(_GetDirAbove("mojo"), "third_party")) -from ply import lex - -try: - imp.find_module("mojom") -except ImportError: - sys.path.append(os.path.join(_GetDirAbove("pylib"), "pylib")) -import mojom.parse.lexer - - -# This (monkey-patching LexToken to make comparison value-based) is evil, but -# we'll do it anyway. (I'm pretty sure ply's lexer never cares about comparing -# for object identity.) -def _LexTokenEq(self, other): - return self.type == other.type and self.value == other.value and \ - self.lineno == other.lineno and self.lexpos == other.lexpos -setattr(lex.LexToken, '__eq__', _LexTokenEq) - - -def _MakeLexToken(token_type, value, lineno=1, lexpos=0): - """Makes a LexToken with the given parameters. (Note that lineno is 1-based, - but lexpos is 0-based.)""" - rv = lex.LexToken() - rv.type, rv.value, rv.lineno, rv.lexpos = token_type, value, lineno, lexpos - return rv - - -def _MakeLexTokenForKeyword(keyword, **kwargs): - """Makes a LexToken for the given keyword.""" - return _MakeLexToken(keyword.upper(), keyword.lower(), **kwargs) - - -class LexerTest(unittest.TestCase): - """Tests |mojom.parse.lexer.Lexer|.""" - - def __init__(self, *args, **kwargs): - unittest.TestCase.__init__(self, *args, **kwargs) - # Clone all lexer instances from this one, since making a lexer is slow. - self._zygote_lexer = lex.lex(mojom.parse.lexer.Lexer("my_file.mojom")) - - def testValidKeywords(self): - """Tests valid keywords.""" - self.assertEquals(self._SingleTokenForInput("handle"), - _MakeLexTokenForKeyword("handle")) - self.assertEquals(self._SingleTokenForInput("import"), - _MakeLexTokenForKeyword("import")) - self.assertEquals(self._SingleTokenForInput("module"), - _MakeLexTokenForKeyword("module")) - self.assertEquals(self._SingleTokenForInput("struct"), - _MakeLexTokenForKeyword("struct")) - self.assertEquals(self._SingleTokenForInput("union"), - _MakeLexTokenForKeyword("union")) - self.assertEquals(self._SingleTokenForInput("interface"), - _MakeLexTokenForKeyword("interface")) - self.assertEquals(self._SingleTokenForInput("enum"), - _MakeLexTokenForKeyword("enum")) - self.assertEquals(self._SingleTokenForInput("const"), - _MakeLexTokenForKeyword("const")) - self.assertEquals(self._SingleTokenForInput("true"), - _MakeLexTokenForKeyword("true")) - self.assertEquals(self._SingleTokenForInput("false"), - _MakeLexTokenForKeyword("false")) - self.assertEquals(self._SingleTokenForInput("default"), - _MakeLexTokenForKeyword("default")) - self.assertEquals(self._SingleTokenForInput("array"), - _MakeLexTokenForKeyword("array")) - self.assertEquals(self._SingleTokenForInput("map"), - _MakeLexTokenForKeyword("map")) - self.assertEquals(self._SingleTokenForInput("associated"), - _MakeLexTokenForKeyword("associated")) - - def testValidIdentifiers(self): - """Tests identifiers.""" - self.assertEquals(self._SingleTokenForInput("abcd"), - _MakeLexToken("NAME", "abcd")) - self.assertEquals(self._SingleTokenForInput("AbC_d012_"), - _MakeLexToken("NAME", "AbC_d012_")) - self.assertEquals(self._SingleTokenForInput("_0123"), - _MakeLexToken("NAME", "_0123")) - - def testInvalidIdentifiers(self): - with self.assertRaisesRegexp( - mojom.parse.lexer.LexError, - r"^my_file\.mojom:1: Error: Illegal character '\$'$"): - self._TokensForInput("$abc") - with self.assertRaisesRegexp( - mojom.parse.lexer.LexError, - r"^my_file\.mojom:1: Error: Illegal character '\$'$"): - self._TokensForInput("a$bc") - - def testDecimalIntegerConstants(self): - self.assertEquals(self._SingleTokenForInput("0"), - _MakeLexToken("INT_CONST_DEC", "0")) - self.assertEquals(self._SingleTokenForInput("1"), - _MakeLexToken("INT_CONST_DEC", "1")) - self.assertEquals(self._SingleTokenForInput("123"), - _MakeLexToken("INT_CONST_DEC", "123")) - self.assertEquals(self._SingleTokenForInput("10"), - _MakeLexToken("INT_CONST_DEC", "10")) - - def testValidTokens(self): - """Tests valid tokens (which aren't tested elsewhere).""" - # Keywords tested in |testValidKeywords|. - # NAME tested in |testValidIdentifiers|. - self.assertEquals(self._SingleTokenForInput("@123"), - _MakeLexToken("ORDINAL", "@123")) - self.assertEquals(self._SingleTokenForInput("456"), - _MakeLexToken("INT_CONST_DEC", "456")) - self.assertEquals(self._SingleTokenForInput("0x01aB2eF3"), - _MakeLexToken("INT_CONST_HEX", "0x01aB2eF3")) - self.assertEquals(self._SingleTokenForInput("123.456"), - _MakeLexToken("FLOAT_CONST", "123.456")) - self.assertEquals(self._SingleTokenForInput("\"hello\""), - _MakeLexToken("STRING_LITERAL", "\"hello\"")) - self.assertEquals(self._SingleTokenForInput("+"), - _MakeLexToken("PLUS", "+")) - self.assertEquals(self._SingleTokenForInput("-"), - _MakeLexToken("MINUS", "-")) - self.assertEquals(self._SingleTokenForInput("&"), - _MakeLexToken("AMP", "&")) - self.assertEquals(self._SingleTokenForInput("?"), - _MakeLexToken("QSTN", "?")) - self.assertEquals(self._SingleTokenForInput("="), - _MakeLexToken("EQUALS", "=")) - self.assertEquals(self._SingleTokenForInput("=>"), - _MakeLexToken("RESPONSE", "=>")) - self.assertEquals(self._SingleTokenForInput("("), - _MakeLexToken("LPAREN", "(")) - self.assertEquals(self._SingleTokenForInput(")"), - _MakeLexToken("RPAREN", ")")) - self.assertEquals(self._SingleTokenForInput("["), - _MakeLexToken("LBRACKET", "[")) - self.assertEquals(self._SingleTokenForInput("]"), - _MakeLexToken("RBRACKET", "]")) - self.assertEquals(self._SingleTokenForInput("{"), - _MakeLexToken("LBRACE", "{")) - self.assertEquals(self._SingleTokenForInput("}"), - _MakeLexToken("RBRACE", "}")) - self.assertEquals(self._SingleTokenForInput("<"), - _MakeLexToken("LANGLE", "<")) - self.assertEquals(self._SingleTokenForInput(">"), - _MakeLexToken("RANGLE", ">")) - self.assertEquals(self._SingleTokenForInput(";"), - _MakeLexToken("SEMI", ";")) - self.assertEquals(self._SingleTokenForInput(","), - _MakeLexToken("COMMA", ",")) - self.assertEquals(self._SingleTokenForInput("."), - _MakeLexToken("DOT", ".")) - - def _TokensForInput(self, input_string): - """Gets a list of tokens for the given input string.""" - lexer = self._zygote_lexer.clone() - lexer.input(input_string) - rv = [] - while True: - tok = lexer.token() - if not tok: - return rv - rv.append(tok) - - def _SingleTokenForInput(self, input_string): - """Gets the single token for the given input string. (Raises an exception if - the input string does not result in exactly one token.)""" - toks = self._TokensForInput(input_string) - assert len(toks) == 1 - return toks[0] - - -if __name__ == "__main__": - unittest.main() diff --git a/mojo/public/tools/bindings/pylib/mojom_tests/parse/parser_unittest.py b/mojo/public/tools/bindings/pylib/mojom_tests/parse/parser_unittest.py deleted file mode 100644 index 3f4ca87..0000000 --- a/mojo/public/tools/bindings/pylib/mojom_tests/parse/parser_unittest.py +++ /dev/null @@ -1,1497 +0,0 @@ -# Copyright 2014 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. - -import imp -import os.path -import sys -import unittest - -def _GetDirAbove(dirname): - """Returns the directory "above" this file containing |dirname| (which must - also be "above" this file).""" - path = os.path.abspath(__file__) - while True: - path, tail = os.path.split(path) - assert tail - if tail == dirname: - return path - -try: - imp.find_module("mojom") -except ImportError: - sys.path.append(os.path.join(_GetDirAbove("pylib"), "pylib")) -import mojom.parse.ast as ast -import mojom.parse.lexer as lexer -import mojom.parse.parser as parser - - -class ParserTest(unittest.TestCase): - """Tests |parser.Parse()|.""" - - def testTrivialValidSource(self): - """Tests a trivial, but valid, .mojom source.""" - - source = """\ - // This is a comment. - - module my_module; - """ - expected = ast.Mojom( - ast.Module(('IDENTIFIER', 'my_module'), None), - ast.ImportList(), - []) - self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) - - def testSourceWithCrLfs(self): - """Tests a .mojom source with CR-LFs instead of LFs.""" - - source = "// This is a comment.\r\n\r\nmodule my_module;\r\n" - expected = ast.Mojom( - ast.Module(('IDENTIFIER', 'my_module'), None), - ast.ImportList(), - []) - self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) - - def testUnexpectedEOF(self): - """Tests a "truncated" .mojom source.""" - - source = """\ - // This is a comment. - - module my_module - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom: Error: Unexpected end of file$"): - parser.Parse(source, "my_file.mojom") - - def testCommentLineNumbers(self): - """Tests that line numbers are correctly tracked when comments are - present.""" - - source1 = """\ - // Isolated C++-style comments. - - // Foo. - asdf1 - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:4: Error: Unexpected 'asdf1':\n *asdf1$"): - parser.Parse(source1, "my_file.mojom") - - source2 = """\ - // Consecutive C++-style comments. - // Foo. - // Bar. - - struct Yada { // Baz. - // Quux. - int32 x; - }; - - asdf2 - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:10: Error: Unexpected 'asdf2':\n *asdf2$"): - parser.Parse(source2, "my_file.mojom") - - source3 = """\ - /* Single-line C-style comments. */ - /* Foobar. */ - - /* Baz. */ - asdf3 - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:5: Error: Unexpected 'asdf3':\n *asdf3$"): - parser.Parse(source3, "my_file.mojom") - - source4 = """\ - /* Multi-line C-style comments. - */ - /* - Foo. - Bar. - */ - - /* Baz - Quux. */ - asdf4 - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:10: Error: Unexpected 'asdf4':\n *asdf4$"): - parser.Parse(source4, "my_file.mojom") - - - def testSimpleStruct(self): - """Tests a simple .mojom source that just defines a struct.""" - - source = """\ - module my_module; - - struct MyStruct { - int32 a; - double b; - }; - """ - expected = ast.Mojom( - ast.Module(('IDENTIFIER', 'my_module'), None), - ast.ImportList(), - [ast.Struct( - 'MyStruct', - None, - ast.StructBody( - [ast.StructField('a', None, None, 'int32', None), - ast.StructField('b', None, None, 'double', None)]))]) - self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) - - def testSimpleStructWithoutModule(self): - """Tests a simple struct without an explict module statement.""" - - source = """\ - struct MyStruct { - int32 a; - double b; - }; - """ - expected = ast.Mojom( - None, - ast.ImportList(), - [ast.Struct( - 'MyStruct', - None, - ast.StructBody( - [ast.StructField('a', None, None, 'int32', None), - ast.StructField('b', None, None, 'double', None)]))]) - self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) - - def testValidStructDefinitions(self): - """Tests all types of definitions that can occur in a struct.""" - - source = """\ - struct MyStruct { - enum MyEnum { VALUE }; - const double kMyConst = 1.23; - int32 a; - SomeOtherStruct b; // Invalidity detected at another stage. - }; - """ - expected = ast.Mojom( - None, - ast.ImportList(), - [ast.Struct( - 'MyStruct', - None, - ast.StructBody( - [ast.Enum('MyEnum', - None, - ast.EnumValueList( - ast.EnumValue('VALUE', None, None))), - ast.Const('kMyConst', 'double', '1.23'), - ast.StructField('a', None, None, 'int32', None), - ast.StructField('b', None, None, 'SomeOtherStruct', None)]))]) - self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) - - def testInvalidStructDefinitions(self): - """Tests that definitions that aren't allowed in a struct are correctly - detected.""" - - source1 = """\ - struct MyStruct { - MyMethod(int32 a); - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected '\(':\n" - r" *MyMethod\(int32 a\);$"): - parser.Parse(source1, "my_file.mojom") - - source2 = """\ - struct MyStruct { - struct MyInnerStruct { - int32 a; - }; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected 'struct':\n" - r" *struct MyInnerStruct {$"): - parser.Parse(source2, "my_file.mojom") - - source3 = """\ - struct MyStruct { - interface MyInterface { - MyMethod(int32 a); - }; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected 'interface':\n" - r" *interface MyInterface {$"): - parser.Parse(source3, "my_file.mojom") - - def testMissingModuleName(self): - """Tests an (invalid) .mojom with a missing module name.""" - - source1 = """\ - // Missing module name. - module ; - struct MyStruct { - int32 a; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected ';':\n *module ;$"): - parser.Parse(source1, "my_file.mojom") - - # Another similar case, but make sure that line-number tracking/reporting - # is correct. - source2 = """\ - module - // This line intentionally left unblank. - - struct MyStruct { - int32 a; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:4: Error: Unexpected 'struct':\n" - r" *struct MyStruct {$"): - parser.Parse(source2, "my_file.mojom") - - def testMultipleModuleStatements(self): - """Tests an (invalid) .mojom with multiple module statements.""" - - source = """\ - module foo; - module bar; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Multiple \"module\" statements not " - r"allowed:\n *module bar;$"): - parser.Parse(source, "my_file.mojom") - - def testModuleStatementAfterImport(self): - """Tests an (invalid) .mojom with a module statement after an import.""" - - source = """\ - import "foo.mojom"; - module foo; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: \"module\" statements must precede imports " - r"and definitions:\n *module foo;$"): - parser.Parse(source, "my_file.mojom") - - def testModuleStatementAfterDefinition(self): - """Tests an (invalid) .mojom with a module statement after a definition.""" - - source = """\ - struct MyStruct { - int32 a; - }; - module foo; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:4: Error: \"module\" statements must precede imports " - r"and definitions:\n *module foo;$"): - parser.Parse(source, "my_file.mojom") - - def testImportStatementAfterDefinition(self): - """Tests an (invalid) .mojom with an import statement after a definition.""" - - source = """\ - struct MyStruct { - int32 a; - }; - import "foo.mojom"; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:4: Error: \"import\" statements must precede " - r"definitions:\n *import \"foo.mojom\";$"): - parser.Parse(source, "my_file.mojom") - - def testEnums(self): - """Tests that enum statements are correctly parsed.""" - - source = """\ - module my_module; - enum MyEnum1 { VALUE1, VALUE2 }; // No trailing comma. - enum MyEnum2 { - VALUE1 = -1, - VALUE2 = 0, - VALUE3 = + 987, // Check that space is allowed. - VALUE4 = 0xAF12, - VALUE5 = -0x09bcd, - VALUE6 = VALUE5, - VALUE7, // Leave trailing comma. - }; - """ - expected = ast.Mojom( - ast.Module(('IDENTIFIER', 'my_module'), None), - ast.ImportList(), - [ast.Enum( - 'MyEnum1', - None, - ast.EnumValueList([ast.EnumValue('VALUE1', None, None), - ast.EnumValue('VALUE2', None, None)])), - ast.Enum( - 'MyEnum2', - None, - ast.EnumValueList([ast.EnumValue('VALUE1', None, '-1'), - ast.EnumValue('VALUE2', None, '0'), - ast.EnumValue('VALUE3', None, '+987'), - ast.EnumValue('VALUE4', None, '0xAF12'), - ast.EnumValue('VALUE5', None, '-0x09bcd'), - ast.EnumValue('VALUE6', None, ('IDENTIFIER', - 'VALUE5')), - ast.EnumValue('VALUE7', None, None)]))]) - self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) - - def testInvalidEnumInitializers(self): - """Tests that invalid enum initializers are correctly detected.""" - - # No values. - source1 = """\ - enum MyEnum { - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected '}':\n" - r" *};$"): - parser.Parse(source1, "my_file.mojom") - - # Floating point value. - source2 = "enum MyEnum { VALUE = 0.123 };" - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:1: Error: Unexpected '0\.123':\n" - r"enum MyEnum { VALUE = 0\.123 };$"): - parser.Parse(source2, "my_file.mojom") - - # Boolean value. - source2 = "enum MyEnum { VALUE = true };" - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:1: Error: Unexpected 'true':\n" - r"enum MyEnum { VALUE = true };$"): - parser.Parse(source2, "my_file.mojom") - - def testConsts(self): - """Tests some constants and struct members initialized with them.""" - - source = """\ - module my_module; - - struct MyStruct { - const int8 kNumber = -1; - int8 number@0 = kNumber; - }; - """ - expected = ast.Mojom( - ast.Module(('IDENTIFIER', 'my_module'), None), - ast.ImportList(), - [ast.Struct( - 'MyStruct', None, - ast.StructBody( - [ast.Const('kNumber', 'int8', '-1'), - ast.StructField('number', None, ast.Ordinal(0), 'int8', - ('IDENTIFIER', 'kNumber'))]))]) - self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) - - def testNoConditionals(self): - """Tests that ?: is not allowed.""" - - source = """\ - module my_module; - - enum MyEnum { - MY_ENUM_1 = 1 ? 2 : 3 - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:4: Error: Unexpected '\?':\n" - r" *MY_ENUM_1 = 1 \? 2 : 3$"): - parser.Parse(source, "my_file.mojom") - - def testSimpleOrdinals(self): - """Tests that (valid) ordinal values are scanned correctly.""" - - source = """\ - module my_module; - - // This isn't actually valid .mojom, but the problem (missing ordinals) - // should be handled at a different level. - struct MyStruct { - int32 a0@0; - int32 a1@1; - int32 a2@2; - int32 a9@9; - int32 a10 @10; - int32 a11 @11; - int32 a29 @29; - int32 a1234567890 @1234567890; - }; - """ - expected = ast.Mojom( - ast.Module(('IDENTIFIER', 'my_module'), None), - ast.ImportList(), - [ast.Struct( - 'MyStruct', - None, - ast.StructBody( - [ast.StructField('a0', None, ast.Ordinal(0), 'int32', None), - ast.StructField('a1', None, ast.Ordinal(1), 'int32', None), - ast.StructField('a2', None, ast.Ordinal(2), 'int32', None), - ast.StructField('a9', None, ast.Ordinal(9), 'int32', None), - ast.StructField('a10', None, ast.Ordinal(10), 'int32', None), - ast.StructField('a11', None, ast.Ordinal(11), 'int32', None), - ast.StructField('a29', None, ast.Ordinal(29), 'int32', None), - ast.StructField('a1234567890', None, ast.Ordinal(1234567890), - 'int32', None)]))]) - self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) - - def testInvalidOrdinals(self): - """Tests that (lexically) invalid ordinals are correctly detected.""" - - source1 = """\ - module my_module; - - struct MyStruct { - int32 a_missing@; - }; - """ - with self.assertRaisesRegexp( - lexer.LexError, - r"^my_file\.mojom:4: Error: Missing ordinal value$"): - parser.Parse(source1, "my_file.mojom") - - source2 = """\ - module my_module; - - struct MyStruct { - int32 a_octal@01; - }; - """ - with self.assertRaisesRegexp( - lexer.LexError, - r"^my_file\.mojom:4: Error: " - r"Octal and hexadecimal ordinal values not allowed$"): - parser.Parse(source2, "my_file.mojom") - - source3 = """\ - module my_module; struct MyStruct { int32 a_invalid_octal@08; }; - """ - with self.assertRaisesRegexp( - lexer.LexError, - r"^my_file\.mojom:1: Error: " - r"Octal and hexadecimal ordinal values not allowed$"): - parser.Parse(source3, "my_file.mojom") - - source4 = "module my_module; struct MyStruct { int32 a_hex@0x1aB9; };" - with self.assertRaisesRegexp( - lexer.LexError, - r"^my_file\.mojom:1: Error: " - r"Octal and hexadecimal ordinal values not allowed$"): - parser.Parse(source4, "my_file.mojom") - - source5 = "module my_module; struct MyStruct { int32 a_hex@0X0; };" - with self.assertRaisesRegexp( - lexer.LexError, - r"^my_file\.mojom:1: Error: " - r"Octal and hexadecimal ordinal values not allowed$"): - parser.Parse(source5, "my_file.mojom") - - source6 = """\ - struct MyStruct { - int32 a_too_big@999999999999; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: " - r"Ordinal value 999999999999 too large:\n" - r" *int32 a_too_big@999999999999;$"): - parser.Parse(source6, "my_file.mojom") - - def testNestedNamespace(self): - """Tests that "nested" namespaces work.""" - - source = """\ - module my.mod; - - struct MyStruct { - int32 a; - }; - """ - expected = ast.Mojom( - ast.Module(('IDENTIFIER', 'my.mod'), None), - ast.ImportList(), - [ast.Struct( - 'MyStruct', - None, - ast.StructBody(ast.StructField('a', None, None, 'int32', None)))]) - self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) - - def testValidHandleTypes(self): - """Tests (valid) handle types.""" - - source = """\ - struct MyStruct { - handle a; - handle<data_pipe_consumer> b; - handle <data_pipe_producer> c; - handle < message_pipe > d; - handle - < shared_buffer - > e; - }; - """ - expected = ast.Mojom( - None, - ast.ImportList(), - [ast.Struct( - 'MyStruct', - None, - ast.StructBody( - [ast.StructField('a', None, None, 'handle', None), - ast.StructField('b', None, None, 'handle<data_pipe_consumer>', - None), - ast.StructField('c', None, None, 'handle<data_pipe_producer>', - None), - ast.StructField('d', None, None, 'handle<message_pipe>', None), - ast.StructField('e', None, None, 'handle<shared_buffer>', - None)]))]) - self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) - - def testInvalidHandleType(self): - """Tests an invalid (unknown) handle type.""" - - source = """\ - struct MyStruct { - handle<wtf_is_this> foo; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: " - r"Invalid handle type 'wtf_is_this':\n" - r" *handle<wtf_is_this> foo;$"): - parser.Parse(source, "my_file.mojom") - - def testValidDefaultValues(self): - """Tests default values that are valid (to the parser).""" - - source = """\ - struct MyStruct { - int16 a0 = 0; - uint16 a1 = 0x0; - uint16 a2 = 0x00; - uint16 a3 = 0x01; - uint16 a4 = 0xcd; - int32 a5 = 12345; - int64 a6 = -12345; - int64 a7 = +12345; - uint32 a8 = 0x12cd3; - uint32 a9 = -0x12cD3; - uint32 a10 = +0x12CD3; - bool a11 = true; - bool a12 = false; - float a13 = 1.2345; - float a14 = -1.2345; - float a15 = +1.2345; - float a16 = 123.; - float a17 = .123; - double a18 = 1.23E10; - double a19 = 1.E-10; - double a20 = .5E+10; - double a21 = -1.23E10; - double a22 = +.123E10; - }; - """ - expected = ast.Mojom( - None, - ast.ImportList(), - [ast.Struct( - 'MyStruct', - None, - ast.StructBody( - [ast.StructField('a0', None, None, 'int16', '0'), - ast.StructField('a1', None, None, 'uint16', '0x0'), - ast.StructField('a2', None, None, 'uint16', '0x00'), - ast.StructField('a3', None, None, 'uint16', '0x01'), - ast.StructField('a4', None, None, 'uint16', '0xcd'), - ast.StructField('a5' , None, None, 'int32', '12345'), - ast.StructField('a6', None, None, 'int64', '-12345'), - ast.StructField('a7', None, None, 'int64', '+12345'), - ast.StructField('a8', None, None, 'uint32', '0x12cd3'), - ast.StructField('a9', None, None, 'uint32', '-0x12cD3'), - ast.StructField('a10', None, None, 'uint32', '+0x12CD3'), - ast.StructField('a11', None, None, 'bool', 'true'), - ast.StructField('a12', None, None, 'bool', 'false'), - ast.StructField('a13', None, None, 'float', '1.2345'), - ast.StructField('a14', None, None, 'float', '-1.2345'), - ast.StructField('a15', None, None, 'float', '+1.2345'), - ast.StructField('a16', None, None, 'float', '123.'), - ast.StructField('a17', None, None, 'float', '.123'), - ast.StructField('a18', None, None, 'double', '1.23E10'), - ast.StructField('a19', None, None, 'double', '1.E-10'), - ast.StructField('a20', None, None, 'double', '.5E+10'), - ast.StructField('a21', None, None, 'double', '-1.23E10'), - ast.StructField('a22', None, None, 'double', '+.123E10')]))]) - self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) - - def testValidFixedSizeArray(self): - """Tests parsing a fixed size array.""" - - source = """\ - struct MyStruct { - array<int32> normal_array; - array<int32, 1> fixed_size_array_one_entry; - array<int32, 10> fixed_size_array_ten_entries; - array<array<array<int32, 1>>, 2> nested_arrays; - }; - """ - expected = ast.Mojom( - None, - ast.ImportList(), - [ast.Struct( - 'MyStruct', - None, - ast.StructBody( - [ast.StructField('normal_array', None, None, 'int32[]', None), - ast.StructField('fixed_size_array_one_entry', None, None, - 'int32[1]', None), - ast.StructField('fixed_size_array_ten_entries', None, None, - 'int32[10]', None), - ast.StructField('nested_arrays', None, None, - 'int32[1][][2]', None)]))]) - self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) - - def testValidNestedArray(self): - """Tests parsing a nested array.""" - - source = "struct MyStruct { array<array<int32>> nested_array; };" - expected = ast.Mojom( - None, - ast.ImportList(), - [ast.Struct( - 'MyStruct', - None, - ast.StructBody( - ast.StructField('nested_array', None, None, 'int32[][]', - None)))]) - self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) - - def testInvalidFixedArraySize(self): - """Tests that invalid fixed array bounds are correctly detected.""" - - source1 = """\ - struct MyStruct { - array<int32, 0> zero_size_array; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Fixed array size 0 invalid:\n" - r" *array<int32, 0> zero_size_array;$"): - parser.Parse(source1, "my_file.mojom") - - source2 = """\ - struct MyStruct { - array<int32, 999999999999> too_big_array; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Fixed array size 999999999999 invalid:\n" - r" *array<int32, 999999999999> too_big_array;$"): - parser.Parse(source2, "my_file.mojom") - - source3 = """\ - struct MyStruct { - array<int32, abcdefg> not_a_number; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected 'abcdefg':\n" - r" *array<int32, abcdefg> not_a_number;"): - parser.Parse(source3, "my_file.mojom") - - def testValidAssociativeArrays(self): - """Tests that we can parse valid associative array structures.""" - - source1 = "struct MyStruct { map<string, uint8> data; };" - expected1 = ast.Mojom( - None, - ast.ImportList(), - [ast.Struct( - 'MyStruct', - None, - ast.StructBody( - [ast.StructField('data', None, None, 'uint8{string}', None)]))]) - self.assertEquals(parser.Parse(source1, "my_file.mojom"), expected1) - - source2 = "interface MyInterface { MyMethod(map<string, uint8> a); };" - expected2 = ast.Mojom( - None, - ast.ImportList(), - [ast.Interface( - 'MyInterface', - None, - ast.InterfaceBody( - ast.Method( - 'MyMethod', - None, - None, - ast.ParameterList( - ast.Parameter('a', None, None, 'uint8{string}')), - None)))]) - self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2) - - source3 = "struct MyStruct { map<string, array<uint8>> data; };" - expected3 = ast.Mojom( - None, - ast.ImportList(), - [ast.Struct( - 'MyStruct', - None, - ast.StructBody( - [ast.StructField('data', None, None, 'uint8[]{string}', - None)]))]) - self.assertEquals(parser.Parse(source3, "my_file.mojom"), expected3) - - def testValidMethod(self): - """Tests parsing method declarations.""" - - source1 = "interface MyInterface { MyMethod(int32 a); };" - expected1 = ast.Mojom( - None, - ast.ImportList(), - [ast.Interface( - 'MyInterface', - None, - ast.InterfaceBody( - ast.Method( - 'MyMethod', - None, - None, - ast.ParameterList(ast.Parameter('a', None, None, 'int32')), - None)))]) - self.assertEquals(parser.Parse(source1, "my_file.mojom"), expected1) - - source2 = """\ - interface MyInterface { - MyMethod1@0(int32 a@0, int64 b@1); - MyMethod2@1() => (); - }; - """ - expected2 = ast.Mojom( - None, - ast.ImportList(), - [ast.Interface( - 'MyInterface', - None, - ast.InterfaceBody( - [ast.Method( - 'MyMethod1', - None, - ast.Ordinal(0), - ast.ParameterList([ast.Parameter('a', None, ast.Ordinal(0), - 'int32'), - ast.Parameter('b', None, ast.Ordinal(1), - 'int64')]), - None), - ast.Method( - 'MyMethod2', - None, - ast.Ordinal(1), - ast.ParameterList(), - ast.ParameterList())]))]) - self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2) - - source3 = """\ - interface MyInterface { - MyMethod(string a) => (int32 a, bool b); - }; - """ - expected3 = ast.Mojom( - None, - ast.ImportList(), - [ast.Interface( - 'MyInterface', - None, - ast.InterfaceBody( - ast.Method( - 'MyMethod', - None, - None, - ast.ParameterList(ast.Parameter('a', None, None, 'string')), - ast.ParameterList([ast.Parameter('a', None, None, 'int32'), - ast.Parameter('b', None, None, - 'bool')]))))]) - self.assertEquals(parser.Parse(source3, "my_file.mojom"), expected3) - - def testInvalidMethods(self): - """Tests that invalid method declarations are correctly detected.""" - - # No trailing commas. - source1 = """\ - interface MyInterface { - MyMethod(string a,); - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected '\)':\n" - r" *MyMethod\(string a,\);$"): - parser.Parse(source1, "my_file.mojom") - - # No leading commas. - source2 = """\ - interface MyInterface { - MyMethod(, string a); - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected ',':\n" - r" *MyMethod\(, string a\);$"): - parser.Parse(source2, "my_file.mojom") - - def testValidInterfaceDefinitions(self): - """Tests all types of definitions that can occur in an interface.""" - - source = """\ - interface MyInterface { - enum MyEnum { VALUE }; - const int32 kMyConst = 123; - MyMethod(int32 x) => (MyEnum y); - }; - """ - expected = ast.Mojom( - None, - ast.ImportList(), - [ast.Interface( - 'MyInterface', - None, - ast.InterfaceBody( - [ast.Enum('MyEnum', - None, - ast.EnumValueList( - ast.EnumValue('VALUE', None, None))), - ast.Const('kMyConst', 'int32', '123'), - ast.Method( - 'MyMethod', - None, - None, - ast.ParameterList(ast.Parameter('x', None, None, 'int32')), - ast.ParameterList(ast.Parameter('y', None, None, - 'MyEnum')))]))]) - self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) - - def testInvalidInterfaceDefinitions(self): - """Tests that definitions that aren't allowed in an interface are correctly - detected.""" - - source1 = """\ - interface MyInterface { - struct MyStruct { - int32 a; - }; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected 'struct':\n" - r" *struct MyStruct {$"): - parser.Parse(source1, "my_file.mojom") - - source2 = """\ - interface MyInterface { - interface MyInnerInterface { - MyMethod(int32 x); - }; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected 'interface':\n" - r" *interface MyInnerInterface {$"): - parser.Parse(source2, "my_file.mojom") - - source3 = """\ - interface MyInterface { - int32 my_field; - }; - """ - # The parser thinks that "int32" is a plausible name for a method, so it's - # "my_field" that gives it away. - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected 'my_field':\n" - r" *int32 my_field;$"): - parser.Parse(source3, "my_file.mojom") - - def testValidAttributes(self): - """Tests parsing attributes (and attribute lists).""" - - # Note: We use structs because they have (optional) attribute lists. - - # Empty attribute list. - source1 = "[] struct MyStruct {};" - expected1 = ast.Mojom( - None, - ast.ImportList(), - [ast.Struct('MyStruct', ast.AttributeList(), ast.StructBody())]) - self.assertEquals(parser.Parse(source1, "my_file.mojom"), expected1) - - # One-element attribute list, with name value. - source2 = "[MyAttribute=MyName] struct MyStruct {};" - expected2 = ast.Mojom( - None, - ast.ImportList(), - [ast.Struct( - 'MyStruct', - ast.AttributeList(ast.Attribute("MyAttribute", "MyName")), - ast.StructBody())]) - self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2) - - # Two-element attribute list, with one string value and one integer value. - source3 = "[MyAttribute1 = \"hello\", MyAttribute2 = 5] struct MyStruct {};" - expected3 = ast.Mojom( - None, - ast.ImportList(), - [ast.Struct( - 'MyStruct', - ast.AttributeList([ast.Attribute("MyAttribute1", "hello"), - ast.Attribute("MyAttribute2", 5)]), - ast.StructBody())]) - self.assertEquals(parser.Parse(source3, "my_file.mojom"), expected3) - - # Various places that attribute list is allowed. - source4 = """\ - [Attr0=0] module my_module; - - [Attr1=1] struct MyStruct { - [Attr2=2] int32 a; - }; - [Attr3=3] union MyUnion { - [Attr4=4] int32 a; - }; - [Attr5=5] enum MyEnum { - [Attr6=6] a - }; - [Attr7=7] interface MyInterface { - [Attr8=8] MyMethod([Attr9=9] int32 a) => ([Attr10=10] bool b); - }; - """ - expected4 = ast.Mojom( - ast.Module(('IDENTIFIER', 'my_module'), - ast.AttributeList([ast.Attribute("Attr0", 0)])), - ast.ImportList(), - [ast.Struct( - 'MyStruct', - ast.AttributeList(ast.Attribute("Attr1", 1)), - ast.StructBody( - ast.StructField( - 'a', ast.AttributeList([ast.Attribute("Attr2", 2)]), - None, 'int32', None))), - ast.Union( - 'MyUnion', - ast.AttributeList(ast.Attribute("Attr3", 3)), - ast.UnionBody( - ast.UnionField( - 'a', ast.AttributeList([ast.Attribute("Attr4", 4)]), None, - 'int32'))), - ast.Enum( - 'MyEnum', - ast.AttributeList(ast.Attribute("Attr5", 5)), - ast.EnumValueList( - ast.EnumValue( - 'VALUE', ast.AttributeList([ast.Attribute("Attr6", 6)]), - None))), - ast.Interface( - 'MyInterface', - ast.AttributeList(ast.Attribute("Attr7", 7)), - ast.InterfaceBody( - ast.Method( - 'MyMethod', - ast.AttributeList(ast.Attribute("Attr8", 8)), - None, - ast.ParameterList( - ast.Parameter( - 'a', ast.AttributeList([ast.Attribute("Attr9", 9)]), - None, 'int32')), - ast.ParameterList( - ast.Parameter( - 'b', - ast.AttributeList([ast.Attribute("Attr10", 10)]), - None, 'bool')))))]) - self.assertEquals(parser.Parse(source4, "my_file.mojom"), expected4) - - # TODO(vtl): Boolean attributes don't work yet. (In fact, we just |eval()| - # literal (non-name) values, which is extremely dubious.) - - def testInvalidAttributes(self): - """Tests that invalid attributes and attribute lists are correctly - detected.""" - - # Trailing commas not allowed. - source1 = "[MyAttribute=MyName,] struct MyStruct {};" - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:1: Error: Unexpected '\]':\n" - r"\[MyAttribute=MyName,\] struct MyStruct {};$"): - parser.Parse(source1, "my_file.mojom") - - # Missing value. - source2 = "[MyAttribute=] struct MyStruct {};" - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:1: Error: Unexpected '\]':\n" - r"\[MyAttribute=\] struct MyStruct {};$"): - parser.Parse(source2, "my_file.mojom") - - # Missing key. - source3 = "[=MyName] struct MyStruct {};" - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:1: Error: Unexpected '=':\n" - r"\[=MyName\] struct MyStruct {};$"): - parser.Parse(source3, "my_file.mojom") - - def testValidImports(self): - """Tests parsing import statements.""" - - # One import (no module statement). - source1 = "import \"somedir/my.mojom\";" - expected1 = ast.Mojom( - None, - ast.ImportList(ast.Import("somedir/my.mojom")), - []) - self.assertEquals(parser.Parse(source1, "my_file.mojom"), expected1) - - # Two imports (no module statement). - source2 = """\ - import "somedir/my1.mojom"; - import "somedir/my2.mojom"; - """ - expected2 = ast.Mojom( - None, - ast.ImportList([ast.Import("somedir/my1.mojom"), - ast.Import("somedir/my2.mojom")]), - []) - self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2) - - # Imports with module statement. - source3 = """\ - module my_module; - import "somedir/my1.mojom"; - import "somedir/my2.mojom"; - """ - expected3 = ast.Mojom( - ast.Module(('IDENTIFIER', 'my_module'), None), - ast.ImportList([ast.Import("somedir/my1.mojom"), - ast.Import("somedir/my2.mojom")]), - []) - self.assertEquals(parser.Parse(source3, "my_file.mojom"), expected3) - - def testInvalidImports(self): - """Tests that invalid import statements are correctly detected.""" - - source1 = """\ - // Make the error occur on line 2. - import invalid - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected 'invalid':\n" - r" *import invalid$"): - parser.Parse(source1, "my_file.mojom") - - source2 = """\ - import // Missing string. - struct MyStruct { - int32 a; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected 'struct':\n" - r" *struct MyStruct {$"): - parser.Parse(source2, "my_file.mojom") - - source3 = """\ - import "foo.mojom" // Missing semicolon. - struct MyStruct { - int32 a; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected 'struct':\n" - r" *struct MyStruct {$"): - parser.Parse(source3, "my_file.mojom") - - def testValidNullableTypes(self): - """Tests parsing nullable types.""" - - source = """\ - struct MyStruct { - int32? a; // This is actually invalid, but handled at a different - // level. - string? b; - array<int32> ? c; - array<string ? > ? d; - array<array<int32>?>? e; - array<int32, 1>? f; - array<string?, 1>? g; - some_struct? h; - handle? i; - handle<data_pipe_consumer>? j; - handle<data_pipe_producer>? k; - handle<message_pipe>? l; - handle<shared_buffer>? m; - some_interface&? n; - }; - """ - expected = ast.Mojom( - None, - ast.ImportList(), - [ast.Struct( - 'MyStruct', - None, - ast.StructBody( - [ast.StructField('a', None, None,'int32?', None), - ast.StructField('b', None, None,'string?', None), - ast.StructField('c', None, None,'int32[]?', None), - ast.StructField('d', None, None,'string?[]?', None), - ast.StructField('e', None, None,'int32[]?[]?', None), - ast.StructField('f', None, None,'int32[1]?', None), - ast.StructField('g', None, None,'string?[1]?', None), - ast.StructField('h', None, None,'some_struct?', None), - ast.StructField('i', None, None,'handle?', None), - ast.StructField('j', None, None,'handle<data_pipe_consumer>?', - None), - ast.StructField('k', None, None,'handle<data_pipe_producer>?', - None), - ast.StructField('l', None, None,'handle<message_pipe>?', None), - ast.StructField('m', None, None,'handle<shared_buffer>?', - None), - ast.StructField('n', None, None,'some_interface&?', None)]))]) - self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) - - def testInvalidNullableTypes(self): - """Tests that invalid nullable types are correctly detected.""" - source1 = """\ - struct MyStruct { - string?? a; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected '\?':\n" - r" *string\?\? a;$"): - parser.Parse(source1, "my_file.mojom") - - source2 = """\ - struct MyStruct { - handle?<data_pipe_consumer> a; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected '<':\n" - r" *handle\?<data_pipe_consumer> a;$"): - parser.Parse(source2, "my_file.mojom") - - source3 = """\ - struct MyStruct { - some_interface?& a; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected '&':\n" - r" *some_interface\?& a;$"): - parser.Parse(source3, "my_file.mojom") - - def testSimpleUnion(self): - """Tests a simple .mojom source that just defines a union.""" - source = """\ - module my_module; - - union MyUnion { - int32 a; - double b; - }; - """ - expected = ast.Mojom( - ast.Module(('IDENTIFIER', 'my_module'), None), - ast.ImportList(), - [ast.Union( - 'MyUnion', - None, - ast.UnionBody([ - ast.UnionField('a', None, None, 'int32'), - ast.UnionField('b', None, None, 'double') - ]))]) - actual = parser.Parse(source, "my_file.mojom") - self.assertEquals(actual, expected) - - def testUnionWithOrdinals(self): - """Test that ordinals are assigned to fields.""" - source = """\ - module my_module; - - union MyUnion { - int32 a @10; - double b @30; - }; - """ - expected = ast.Mojom( - ast.Module(('IDENTIFIER', 'my_module'), None), - ast.ImportList(), - [ast.Union( - 'MyUnion', - None, - ast.UnionBody([ - ast.UnionField('a', None, ast.Ordinal(10), 'int32'), - ast.UnionField('b', None, ast.Ordinal(30), 'double') - ]))]) - actual = parser.Parse(source, "my_file.mojom") - self.assertEquals(actual, expected) - - def testUnionWithStructMembers(self): - """Test that struct members are accepted.""" - source = """\ - module my_module; - - union MyUnion { - SomeStruct s; - }; - """ - expected = ast.Mojom( - ast.Module(('IDENTIFIER', 'my_module'), None), - ast.ImportList(), - [ast.Union( - 'MyUnion', - None, - ast.UnionBody([ - ast.UnionField('s', None, None, 'SomeStruct') - ]))]) - actual = parser.Parse(source, "my_file.mojom") - self.assertEquals(actual, expected) - - def testUnionWithArrayMember(self): - """Test that array members are accepted.""" - source = """\ - module my_module; - - union MyUnion { - array<int32> a; - }; - """ - expected = ast.Mojom( - ast.Module(('IDENTIFIER', 'my_module'), None), - ast.ImportList(), - [ast.Union( - 'MyUnion', - None, - ast.UnionBody([ - ast.UnionField('a', None, None, 'int32[]') - ]))]) - actual = parser.Parse(source, "my_file.mojom") - self.assertEquals(actual, expected) - - def testUnionWithMapMember(self): - """Test that map members are accepted.""" - source = """\ - module my_module; - - union MyUnion { - map<int32, string> m; - }; - """ - expected = ast.Mojom( - ast.Module(('IDENTIFIER', 'my_module'), None), - ast.ImportList(), - [ast.Union( - 'MyUnion', - None, - ast.UnionBody([ - ast.UnionField('m', None, None, 'string{int32}') - ]))]) - actual = parser.Parse(source, "my_file.mojom") - self.assertEquals(actual, expected) - - def testUnionDisallowNestedStruct(self): - """Tests that structs cannot be nested in unions.""" - source = """\ - module my_module; - - union MyUnion { - struct MyStruct { - int32 a; - }; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:4: Error: Unexpected 'struct':\n" - r" *struct MyStruct {$"): - parser.Parse(source, "my_file.mojom") - - def testUnionDisallowNestedInterfaces(self): - """Tests that interfaces cannot be nested in unions.""" - source = """\ - module my_module; - - union MyUnion { - interface MyInterface { - MyMethod(int32 a); - }; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:4: Error: Unexpected 'interface':\n" - r" *interface MyInterface {$"): - parser.Parse(source, "my_file.mojom") - - def testUnionDisallowNestedUnion(self): - """Tests that unions cannot be nested in unions.""" - source = """\ - module my_module; - - union MyUnion { - union MyOtherUnion { - int32 a; - }; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:4: Error: Unexpected 'union':\n" - r" *union MyOtherUnion {$"): - parser.Parse(source, "my_file.mojom") - - def testUnionDisallowNestedEnum(self): - """Tests that enums cannot be nested in unions.""" - source = """\ - module my_module; - - union MyUnion { - enum MyEnum { - A, - }; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:4: Error: Unexpected 'enum':\n" - r" *enum MyEnum {$"): - parser.Parse(source, "my_file.mojom") - - def testValidAssociatedKinds(self): - """Tests parsing associated interfaces and requests.""" - source1 = """\ - struct MyStruct { - associated MyInterface a; - associated MyInterface& b; - associated MyInterface? c; - associated MyInterface&? d; - }; - """ - expected1 = ast.Mojom( - None, - ast.ImportList(), - [ast.Struct( - 'MyStruct', - None, - ast.StructBody( - [ast.StructField('a', None, None,'asso<MyInterface>', None), - ast.StructField('b', None, None,'asso<MyInterface&>', None), - ast.StructField('c', None, None,'asso<MyInterface>?', None), - ast.StructField('d', None, None,'asso<MyInterface&>?', - None)]))]) - self.assertEquals(parser.Parse(source1, "my_file.mojom"), expected1) - - source2 = """\ - interface MyInterface { - MyMethod(associated A a) =>(associated B& b); - };""" - expected2 = ast.Mojom( - None, - ast.ImportList(), - [ast.Interface( - 'MyInterface', - None, - ast.InterfaceBody( - ast.Method( - 'MyMethod', - None, - None, - ast.ParameterList( - ast.Parameter('a', None, None, 'asso<A>')), - ast.ParameterList( - ast.Parameter('b', None, None, 'asso<B&>')))))]) - self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2) - - def testInvalidAssociatedKinds(self): - """Tests that invalid associated interfaces and requests are correctly - detected.""" - source1 = """\ - struct MyStruct { - associated associated SomeInterface a; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected 'associated':\n" - r" *associated associated SomeInterface a;$"): - parser.Parse(source1, "my_file.mojom") - - source2 = """\ - struct MyStruct { - associated handle a; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected 'handle':\n" - r" *associated handle a;$"): - parser.Parse(source2, "my_file.mojom") - - source3 = """\ - struct MyStruct { - associated? MyInterface& a; - }; - """ - with self.assertRaisesRegexp( - parser.ParseError, - r"^my_file\.mojom:2: Error: Unexpected '\?':\n" - r" *associated\? MyInterface& a;$"): - parser.Parse(source3, "my_file.mojom") - - -if __name__ == "__main__": - unittest.main() diff --git a/mojo/public/tools/bindings/pylib/mojom_tests/parse/run_parser.py b/mojo/public/tools/bindings/pylib/mojom_tests/parse/run_parser.py deleted file mode 100755 index b160de6..0000000 --- a/mojo/public/tools/bindings/pylib/mojom_tests/parse/run_parser.py +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env python -# Copyright 2014 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. - -"""Simple testing utility to just run the mojom parser.""" - - -import os.path -import sys - -sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), - os.path.pardir, os.path.pardir)) - -from mojom.parse.parser import Parse, ParseError - - -def main(argv): - if len(argv) < 2: - print "usage: %s filename" % argv[0] - return 0 - - for filename in argv[1:]: - with open(filename) as f: - print "%s:" % filename - try: - print Parse(f.read(), filename) - except ParseError, e: - print e - return 1 - - return 0 - - -if __name__ == '__main__': - sys.exit(main(sys.argv)) diff --git a/mojo/public/tools/bindings/pylib/mojom_tests/parse/run_translate.py b/mojo/public/tools/bindings/pylib/mojom_tests/parse/run_translate.py deleted file mode 100755 index 899d40e..0000000 --- a/mojo/public/tools/bindings/pylib/mojom_tests/parse/run_translate.py +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env python -# Copyright 2014 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. - -"""Simple testing utility to just run the mojom translate stage.""" - - -import os.path -import sys - -sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), - os.path.pardir, os.path.pardir)) - -from mojom.parse.parser import Parse -from mojom.parse.translate import Translate - - -def main(argv): - if len(argv) < 2: - print "usage: %s filename" % sys.argv[0] - return 1 - - for filename in argv[1:]: - with open(filename) as f: - print "%s:" % filename - print Translate(Parse(f.read(), filename), - os.path.splitext(os.path.basename(filename))[0]) - - return 0 - - -if __name__ == '__main__': - sys.exit(main(sys.argv)) diff --git a/mojo/public/tools/bindings/pylib/mojom_tests/parse/translate_unittest.py b/mojo/public/tools/bindings/pylib/mojom_tests/parse/translate_unittest.py deleted file mode 100644 index 2520332..0000000 --- a/mojo/public/tools/bindings/pylib/mojom_tests/parse/translate_unittest.py +++ /dev/null @@ -1,80 +0,0 @@ -# Copyright 2014 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. - -import imp -import os.path -import sys -import unittest - -def _GetDirAbove(dirname): - """Returns the directory "above" this file containing |dirname| (which must - also be "above" this file).""" - path = os.path.abspath(__file__) - while True: - path, tail = os.path.split(path) - assert tail - if tail == dirname: - return path - -try: - imp.find_module("mojom") -except ImportError: - sys.path.append(os.path.join(_GetDirAbove("pylib"), "pylib")) -from mojom.parse import ast -from mojom.parse import translate - - -class TranslateTest(unittest.TestCase): - """Tests |parser.Parse()|.""" - - def testSimpleArray(self): - """Tests a simple int32[].""" - # pylint: disable=W0212 - self.assertEquals(translate._MapKind("int32[]"), "a:i32") - - def testAssociativeArray(self): - """Tests a simple uint8{string}.""" - # pylint: disable=W0212 - self.assertEquals(translate._MapKind("uint8{string}"), "m[s][u8]") - - def testLeftToRightAssociativeArray(self): - """Makes sure that parsing is done from right to left on the internal kinds - in the presence of an associative array.""" - # pylint: disable=W0212 - self.assertEquals(translate._MapKind("uint8[]{string}"), "m[s][a:u8]") - - def testTranslateSimpleUnions(self): - """Makes sure that a simple union is translated correctly.""" - tree = ast.Mojom( - None, - ast.ImportList(), - [ast.Union("SomeUnion", None, ast.UnionBody( - [ast.UnionField("a", None, None, "int32"), - ast.UnionField("b", None, None, "string")]))]) - expected = [{ - "name": "SomeUnion", - "fields": [{"kind": "i32", "name": "a"}, - {"kind": "s", "name": "b"}]}] - actual = translate.Translate(tree, "mojom_tree") - self.assertEquals(actual["unions"], expected) - - def testMapTreeForTypeRaisesWithDuplicate(self): - """Verifies _MapTreeForType() raises when passed two values with the same - name.""" - methods = [ast.Method('dup', None, None, ast.ParameterList(), None), - ast.Method('dup', None, None, ast.ParameterList(), None)] - self.assertRaises(Exception, translate._MapTreeForType, - (lambda x: x, methods, '', 'scope')) - - def testAssociatedKinds(self): - """Tests type spec translation of associated interfaces and requests.""" - # pylint: disable=W0212 - self.assertEquals(translate._MapKind("asso<SomeInterface>?"), - "?asso:x:SomeInterface") - self.assertEquals(translate._MapKind("asso<SomeInterface&>?"), - "?asso:r:x:SomeInterface") - - -if __name__ == "__main__": - unittest.main() diff --git a/mojo/public/tools/bindings/pylib/mojom_tests/support/__init__.py b/mojo/public/tools/bindings/pylib/mojom_tests/support/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/mojo/public/tools/bindings/pylib/mojom_tests/support/__init__.py +++ /dev/null diff --git a/mojo/public/tools/bindings/pylib/mojom_tests/support/find_files.py b/mojo/public/tools/bindings/pylib/mojom_tests/support/find_files.py deleted file mode 100644 index 2a4b17b..0000000 --- a/mojo/public/tools/bindings/pylib/mojom_tests/support/find_files.py +++ /dev/null @@ -1,32 +0,0 @@ -# Copyright 2014 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. - -import fnmatch -from os import walk -from os.path import join -import sys - - -def FindFiles(top, pattern, **kwargs): - """Finds files under |top| matching the glob pattern |pattern|, returning a - list of paths.""" - matches = [] - for dirpath, _, filenames in walk(top, **kwargs): - for filename in fnmatch.filter(filenames, pattern): - matches.append(join(dirpath, filename)) - return matches - - -def main(argv): - if len(argv) != 3: - print "usage: %s path pattern" % argv[0] - return 1 - - for filename in FindFiles(argv[1], argv[2]): - print filename - return 0 - - -if __name__ == '__main__': - sys.exit(main(sys.argv)) diff --git a/mojo/public/tools/bindings/pylib/mojom_tests/support/run_bindings_generator.py b/mojo/public/tools/bindings/pylib/mojom_tests/support/run_bindings_generator.py deleted file mode 100644 index 20ef461..0000000 --- a/mojo/public/tools/bindings/pylib/mojom_tests/support/run_bindings_generator.py +++ /dev/null @@ -1,47 +0,0 @@ -# Copyright 2014 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. - -import os.path -from subprocess import check_call -import sys - - -def RunBindingsGenerator(out_dir, root_dir, mojom_file, extra_flags=None): - out_dir = os.path.abspath(out_dir) - root_dir = os.path.abspath(root_dir) - mojom_file = os.path.abspath(mojom_file) - - # The mojom file should be under the root directory somewhere. - assert mojom_file.startswith(root_dir) - mojom_reldir = os.path.dirname(os.path.relpath(mojom_file, root_dir)) - - # TODO(vtl): Abstract out the "main" functions, so that we can just import - # the bindings generator (which would be more portable and easier to use in - # tests). - this_dir = os.path.dirname(os.path.abspath(__file__)) - # We're in src/mojo/public/tools/bindings/pylib/mojom_tests/support; - # mojom_bindings_generator.py is in .../bindings. - bindings_generator = os.path.join(this_dir, os.pardir, os.pardir, os.pardir, - "mojom_bindings_generator.py") - - args = ["python", bindings_generator, - "-o", os.path.join(out_dir, mojom_reldir)] - if extra_flags: - args.extend(extra_flags) - args.append(mojom_file) - - check_call(args) - - -def main(argv): - if len(argv) < 4: - print "usage: %s out_dir root_dir mojom_file [extra_flags]" % argv[0] - return 1 - - RunBindingsGenerator(argv[1], argv[2], argv[3], extra_flags=argv[4:]) - return 0 - - -if __name__ == '__main__': - sys.exit(main(sys.argv)) |