/
data_unittest.py
156 lines (126 loc) · 5.71 KB
/
data_unittest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# 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 importlib
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:
importlib.util.find_spec("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.')