| OLD | NEW |
| (Empty) | |
| 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 'attributes': [], |
| 36 'enums': [], |
| 37 'constants': [], |
| 38 'fields': [ |
| 39 {'name': 'field1', 'kind': 'i32'}, |
| 40 {'name': 'field2', 'kind': 'i32', 'ordinal': 10}, |
| 41 {'name': 'field3', 'kind': 'i32', 'default': 15}]} |
| 42 |
| 43 struct = data.StructFromData(module, struct_data) |
| 44 del struct_data['attributes'] |
| 45 del struct_data['enums'] |
| 46 del struct_data['constants'] |
| 47 struct.fields = map(lambda field: |
| 48 data.FieldFromData(module, field, struct), struct.fields_data) |
| 49 self.assertEquals(struct_data, data.StructToData(struct)) |
| 50 |
| 51 def testUnionDataConversion(self): |
| 52 """Tests that a union can be converted from data.""" |
| 53 module = mojom.Module('test_module', 'test_namespace') |
| 54 union_data = { |
| 55 'name': 'SomeUnion', |
| 56 'fields': [ |
| 57 {'name': 'field1', 'kind': 'i32'}, |
| 58 {'name': 'field2', 'kind': 'i32', 'ordinal': 10}]} |
| 59 |
| 60 union = data.UnionFromData(module, union_data) |
| 61 union.fields = map(lambda field: |
| 62 data.FieldFromData(module, field, union), union.fields_data) |
| 63 self.assertEquals(union_data, data.UnionToData(union)) |
| 64 |
| 65 def testImportFromDataNoMissingImports(self): |
| 66 """Tests that unions, structs, interfaces and enums are imported.""" |
| 67 module = mojom.Module('test_module', 'test_namespace') |
| 68 imported_module = mojom.Module('import_module', 'import_namespace') |
| 69 #TODO(azani): Init values in module.py. |
| 70 #TODO(azani): Test that values are imported. |
| 71 imported_module.values = {} |
| 72 imported_data = {'module' : imported_module} |
| 73 |
| 74 |
| 75 struct = mojom.Struct('TestStruct', module=module) |
| 76 imported_module.kinds[struct.spec] = struct |
| 77 |
| 78 union = mojom.Union('TestUnion', module=module) |
| 79 imported_module.kinds[union.spec] = union |
| 80 |
| 81 interface = mojom.Interface('TestInterface', module=module) |
| 82 imported_module.kinds[interface.spec] = interface |
| 83 |
| 84 enum = mojom.Enum('TestEnum', module=module) |
| 85 imported_module.kinds[enum.spec] = enum |
| 86 |
| 87 data.ImportFromData(module, imported_data) |
| 88 |
| 89 # Test that the kind was imported. |
| 90 self.assertIn(struct.spec, module.kinds) |
| 91 self.assertEquals(struct.name, module.kinds[struct.spec].name) |
| 92 |
| 93 self.assertIn(union.spec, module.kinds) |
| 94 self.assertEquals(union.name, module.kinds[union.spec].name) |
| 95 |
| 96 self.assertIn(interface.spec, module.kinds) |
| 97 self.assertEquals(interface.name, module.kinds[interface.spec].name) |
| 98 |
| 99 self.assertIn(enum.spec, module.kinds) |
| 100 self.assertEquals(enum.name, module.kinds[enum.spec].name) |
| 101 |
| 102 # Test that the imported kind is a copy and not the original. |
| 103 self.assertIsNot(struct, module.kinds[struct.spec]) |
| 104 self.assertIsNot(union, module.kinds[union.spec]) |
| 105 self.assertIsNot(interface, module.kinds[interface.spec]) |
| 106 self.assertIsNot(enum, module.kinds[enum.spec]) |
| 107 |
| 108 def testImportFromDataNoExtraneousImports(self): |
| 109 """Tests that arrays, maps and interface requests are not imported.""" |
| 110 module = mojom.Module('test_module', 'test_namespace') |
| 111 imported_module = mojom.Module('import_module', 'import_namespace') |
| 112 #TODO(azani): Init values in module.py. |
| 113 imported_module.values = {} |
| 114 imported_data = {'module' : imported_module} |
| 115 |
| 116 array = mojom.Array(mojom.INT16, length=20) |
| 117 imported_module.kinds[array.spec] = array |
| 118 |
| 119 map_kind = mojom.Map(mojom.INT16, mojom.INT16) |
| 120 imported_module.kinds[map_kind.spec] = map_kind |
| 121 |
| 122 interface_req = mojom.InterfaceRequest(mojom.INT16) |
| 123 imported_module.kinds[interface_req.spec] = interface_req |
| 124 |
| 125 data.ImportFromData(module, imported_data) |
| 126 |
| 127 self.assertNotIn(array.spec, module.kinds) |
| 128 self.assertNotIn(map_kind.spec, module.kinds) |
| 129 self.assertNotIn(interface_req.spec, module.kinds) |
| OLD | NEW |