1 # Copyright 2014 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 import imp 6 import os.path 7 import sys 8 import unittest 9 10 def _GetDirAbove(dirname): 11 """Returns the directory "above" this file containing |dirname| (which must 12 also be "above" this file).""" 13 path = os.path.abspath(__file__) 14 while True: 15 path, tail = os.path.split(path) 16 assert tail 17 if tail == dirname: 18 return path 19 20 try: 21 imp.find_module("mojom") 22 except ImportError: 23 sys.path.append(os.path.join(_GetDirAbove("pylib"), "pylib")) 24 from mojom.generate import data 25 from mojom.generate import module as mojom 26 27 28 class DataTest(unittest.TestCase): 29 30 def testStructDataConversion(self): 31 """Tests that a struct can be converted from data.""" 32 module = mojom.Module('test_module', 'test_namespace') 33 struct_data = { 34 'name': 'SomeStruct', 35 'enums': [], 36 'constants': [], 37 'fields': [ 38 {'name': 'field1', 'kind': 'i32'}, 39 {'name': 'field2', 'kind': 'i32', 'ordinal': 10}, 40 {'name': 'field3', 'kind': 'i32', 'default': 15}]} 41 42 struct = data.StructFromData(module, struct_data) 43 struct.fields = map(lambda field: 44 data.StructFieldFromData(module, field, struct), struct.fields_data) 45 self.assertEquals(struct_data, data.StructToData(struct)) 46 47 def testUnionDataConversion(self): 48 """Tests that a union can be converted from data.""" 49 module = mojom.Module('test_module', 'test_namespace') 50 union_data = { 51 'name': 'SomeUnion', 52 'fields': [ 53 {'name': 'field1', 'kind': 'i32'}, 54 {'name': 'field2', 'kind': 'i32', 'ordinal': 10}]} 55 56 union = data.UnionFromData(module, union_data) 57 union.fields = map(lambda field: 58 data.UnionFieldFromData(module, field, union), union.fields_data) 59 self.assertEquals(union_data, data.UnionToData(union)) 60 61 def testImportFromDataNoMissingImports(self): 62 """Tests that unions, structs, interfaces and enums are imported.""" 63 module = mojom.Module('test_module', 'test_namespace') 64 imported_module = mojom.Module('import_module', 'import_namespace') 65 #TODO(azani): Init values in module.py. 66 #TODO(azani): Test that values are imported. 67 imported_module.values = {} 68 imported_data = {'module' : imported_module} 69 70 71 struct = mojom.Struct('TestStruct', module=module) 72 imported_module.kinds[struct.spec] = struct 73 74 union = mojom.Union('TestUnion', module=module) 75 imported_module.kinds[union.spec] = union 76 77 interface = mojom.Interface('TestInterface', module=module) 78 imported_module.kinds[interface.spec] = interface 79 80 enum = mojom.Enum('TestEnum', module=module) 81 imported_module.kinds[enum.spec] = enum 82 83 data.ImportFromData(module, imported_data) 84 85 # Test that the kind was imported. 86 self.assertIn(struct.spec, module.kinds) 87 self.assertEquals(struct.name, module.kinds[struct.spec].name) 88 89 self.assertIn(union.spec, module.kinds) 90 self.assertEquals(union.name, module.kinds[union.spec].name) 91 92 self.assertIn(interface.spec, module.kinds) 93 self.assertEquals(interface.name, module.kinds[interface.spec].name) 94 95 self.assertIn(enum.spec, module.kinds) 96 self.assertEquals(enum.name, module.kinds[enum.spec].name) 97 98 # Test that the imported kind is a copy and not the original. 99 self.assertIsNot(struct, module.kinds[struct.spec]) 100 self.assertIsNot(union, module.kinds[union.spec]) 101 self.assertIsNot(interface, module.kinds[interface.spec]) 102 self.assertIsNot(enum, module.kinds[enum.spec]) 103 104 def testImportFromDataNoExtraneousImports(self): 105 """Tests that arrays, maps and interface requests are not imported.""" 106 module = mojom.Module('test_module', 'test_namespace') 107 imported_module = mojom.Module('import_module', 'import_namespace') 108 #TODO(azani): Init values in module.py. 109 imported_module.values = {} 110 imported_data = {'module' : imported_module} 111 112 array = mojom.Array(mojom.INT16, length=20) 113 imported_module.kinds[array.spec] = array 114 115 map_kind = mojom.Map(mojom.INT16, mojom.INT16) 116 imported_module.kinds[map_kind.spec] = map_kind 117 118 interface = mojom.Interface('TestInterface', module=module) 119 imported_module.kinds[interface.spec] = interface 120 121 interface_req = mojom.InterfaceRequest(interface) 122 imported_module.kinds[interface_req.spec] = interface_req 123 124 data.ImportFromData(module, imported_data) 125 126 self.assertNotIn(array.spec, module.kinds) 127 self.assertNotIn(map_kind.spec, module.kinds) 128 self.assertNotIn(interface_req.spec, module.kinds) 129 130 def testNonInterfaceAsInterfaceRequest(self): 131 """Tests that a non-interface cannot be used for interface requests.""" 132 module = mojom.Module('test_module', 'test_namespace') 133 interface = mojom.Interface('TestInterface', module=module) 134 method_dict = { 135 'name': 'Foo', 136 'parameters': [{'name': 'foo', 'kind': 'r:i32'}], 137 } 138 with self.assertRaises(Exception) as e: 139 data.MethodFromData(module, method_dict, interface) 140 self.assertEquals(e.exception.__str__(), 141 'Interface request requires \'i32\' to be an interface.') 142 143 def testNonInterfaceAsAssociatedInterface(self): 144 """Tests that a non-interface type cannot be used for associated 145 interfaces.""" 146 module = mojom.Module('test_module', 'test_namespace') 147 interface = mojom.Interface('TestInterface', module=module) 148 method_dict = { 149 'name': 'Foo', 150 'parameters': [{'name': 'foo', 'kind': 'asso:i32'}], 151 } 152 with self.assertRaises(Exception) as e: 153 data.MethodFromData(module, method_dict, interface) 154 self.assertEquals( 155 e.exception.__str__(), 156 'Associated interface requires \'i32\' to be an interface.') 157