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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
#!/usr/bin/env python
#
# Any copyright is dedicated to the Public Domain.
# http://creativecommons.org/publicdomain/zero/1.0/
#
# Unit tests for xpidl.py
import json
import os
import sys
# Hack: the first entry in sys.path is the directory containing the script.
# This messes things up because that directory is the xpidl module, and that
# which conflicts with the xpidl submodule in the imports further below.
sys.path.pop(0)
import unittest
import mozunit
from xpidl import header, typescript, xpidl
class TestParser(unittest.TestCase):
def setUp(self):
self.p = xpidl.IDLParser()
def testEmpty(self):
i = self.p.parse("", filename="f")
self.assertTrue(isinstance(i, xpidl.IDL))
self.assertEqual([], i.productions)
def testForwardInterface(self):
i = self.p.parse("interface foo;", filename="f")
self.assertTrue(isinstance(i, xpidl.IDL))
self.assertTrue(isinstance(i.productions[0], xpidl.Forward))
self.assertEqual("foo", i.productions[0].name)
def testInterface(self):
i = self.p.parse("[uuid(abc)] interface foo {};", filename="f")
self.assertTrue(isinstance(i, xpidl.IDL))
self.assertTrue(isinstance(i.productions[0], xpidl.Interface))
self.assertEqual("foo", i.productions[0].name)
def testAttributes(self):
i = self.p.parse(
"[scriptable, builtinclass, function, uuid(abc)] interface foo {};",
filename="f",
)
self.assertTrue(isinstance(i, xpidl.IDL))
self.assertTrue(isinstance(i.productions[0], xpidl.Interface))
iface = i.productions[0]
self.assertEqual("foo", iface.name)
self.assertTrue(iface.attributes.scriptable)
self.assertTrue(iface.attributes.builtinclass)
self.assertTrue(iface.attributes.function)
i = self.p.parse("[uuid(abc)] interface foo {};", filename="f")
self.assertTrue(isinstance(i, xpidl.IDL))
self.assertTrue(isinstance(i.productions[0], xpidl.Interface))
iface = i.productions[0]
self.assertEqual("foo", iface.name)
self.assertFalse(iface.attributes.scriptable)
def testMethod(self):
i = self.p.parse(
"""[uuid(abc)] interface foo {
void bar();
};""",
filename="f",
)
self.assertTrue(isinstance(i, xpidl.IDL))
self.assertTrue(isinstance(i.productions[0], xpidl.Interface))
iface = i.productions[0]
m = iface.members[0]
self.assertTrue(isinstance(m, xpidl.Method))
self.assertEqual("bar", m.name)
self.assertEqual(xpidl.TypeId("void"), m.type)
def testMethodParams(self):
i = self.p.parse(
"""
[scriptable, uuid(aaa)] interface nsISupports {};
[uuid(abc)] interface foo : nsISupports {
long bar(in long a, in float b, [array] in long c);
};""",
filename="f",
)
i.resolve([], self.p, {})
self.assertTrue(isinstance(i, xpidl.IDL))
self.assertTrue(isinstance(i.productions[1], xpidl.Interface))
iface = i.productions[1]
m = iface.members[0]
self.assertTrue(isinstance(m, xpidl.Method))
self.assertEqual("bar", m.name)
self.assertEqual(xpidl.TypeId("long"), m.type)
self.assertEqual(3, len(m.params))
self.assertEqual(xpidl.TypeId("long"), m.params[0].type)
self.assertEqual("in", m.params[0].paramtype)
self.assertEqual(xpidl.TypeId("float"), m.params[1].type)
self.assertEqual("in", m.params[1].paramtype)
self.assertEqual(xpidl.TypeId("long"), m.params[2].type)
self.assertEqual("in", m.params[2].paramtype)
self.assertTrue(isinstance(m.params[2].realtype, xpidl.LegacyArray))
self.assertEqual("int32_t", m.params[2].realtype.type.name)
def testAttribute(self):
i = self.p.parse(
"""[uuid(abc)] interface foo {
attribute long bar;
};""",
filename="f",
)
self.assertTrue(isinstance(i, xpidl.IDL))
self.assertTrue(isinstance(i.productions[0], xpidl.Interface))
iface = i.productions[0]
a = iface.members[0]
self.assertTrue(isinstance(a, xpidl.Attribute))
self.assertEqual("bar", a.name)
self.assertEqual(xpidl.TypeId("long"), a.type)
def testOverloadedVirtual(self):
i = self.p.parse(
"""
[scriptable, uuid(00000000-0000-0000-0000-000000000000)] interface nsISupports {};
[uuid(abc)] interface foo : nsISupports {
attribute long bar;
void getBar();
};""",
filename="f",
)
self.assertTrue(isinstance(i, xpidl.IDL))
i.resolve([], self.p, {})
class FdMock:
def write(self, s):
pass
try:
header.print_header(i, FdMock(), filename="f", relpath="f")
self.assertTrue(False, "Header printing failed to fail")
except Exception as e:
self.assertEqual(
e.args[0],
"Unexpected overloaded virtual method GetBar in interface foo",
)
def testNotISupports(self):
i = self.p.parse(
"""
[uuid(abc)] interface foo {};
""",
filename="f",
)
self.assertTrue(isinstance(i, xpidl.IDL))
try:
i.resolve([], self.p, {})
self.assertTrue(
False, "Must check that interfaces inherit from nsISupports"
)
except xpidl.IDLError as e:
self.assertEqual(e.args[0], "Interface 'foo' must inherit from nsISupports")
def testBuiltinClassParent(self):
i = self.p.parse(
"""
[scriptable, uuid(aaa)] interface nsISupports {};
[scriptable, builtinclass, uuid(abc)] interface foo : nsISupports {};
[scriptable, uuid(def)] interface bar : foo {};
""",
filename="f",
)
self.assertTrue(isinstance(i, xpidl.IDL))
try:
i.resolve([], self.p, {})
self.assertTrue(
False, "non-builtinclasses can't inherit from builtinclasses"
)
except xpidl.IDLError as e:
self.assertEqual(
e.args[0],
"interface 'bar' is not builtinclass but derives from builtinclass 'foo'",
)
def testScriptableNotXPCOM(self):
# notxpcom method requires builtinclass on the interface
i = self.p.parse(
"""
[scriptable, uuid(aaa)] interface nsISupports {};
[scriptable, uuid(abc)] interface nsIScriptableWithNotXPCOM : nsISupports {
[notxpcom] void method2();
};
""",
filename="f",
)
self.assertTrue(isinstance(i, xpidl.IDL))
try:
i.resolve([], self.p, {})
self.assertTrue(
False,
"Resolve should fail for non-builtinclasses with notxpcom methods",
)
except xpidl.IDLError as e:
self.assertEqual(
e.args[0],
(
"scriptable interface 'nsIScriptableWithNotXPCOM' "
"must be marked [builtinclass] because it contains a [notxpcom] "
"method 'method2'"
),
)
# notxpcom attribute requires builtinclass on the interface
i = self.p.parse(
"""
interface nsISomeInterface;
[scriptable, uuid(aaa)] interface nsISupports {};
[scriptable, uuid(abc)] interface nsIScriptableWithNotXPCOM : nsISupports {
[notxpcom] attribute nsISomeInterface attrib;
};
""",
filename="f",
)
self.assertTrue(isinstance(i, xpidl.IDL))
try:
i.resolve([], self.p, {})
self.assertTrue(
False,
"Resolve should fail for non-builtinclasses with notxpcom attributes",
)
except xpidl.IDLError as e:
self.assertEqual(
e.args[0],
(
"scriptable interface 'nsIScriptableWithNotXPCOM' must be marked "
"[builtinclass] because it contains a [notxpcom] attribute 'attrib'"
),
)
def testUndefinedConst(self):
i = self.p.parse(
"""
[scriptable, uuid(aaa)] interface nsISupports {};
[scriptable, uuid(abc)] interface foo : nsISupports {
const unsigned long X = Y + 1;
};
""",
filename="f",
)
self.assertTrue(isinstance(i, xpidl.IDL))
try:
i.resolve([], self.p, {})
self.assertTrue(False, "Must detect undefined symbols")
except xpidl.IDLError as e:
self.assertEqual(e.args[0], ("cannot find symbol 'Y'"))
class TestTypescript(unittest.TestCase):
"""A few basic smoke tests for typescript generation."""
dir = os.path.dirname(__file__)
src = os.path.join(dir, "..", "..", "..")
# We use the xpctest.xpt *.idl files from:
tests_dir = os.path.join(src, "js/xpconnect/tests/idl")
files = [
"xpctest_attributes.idl",
"xpctest_bug809674.idl",
"xpctest_cenums.idl",
"xpctest_interfaces.idl",
"xpctest_params.idl",
"xpctest_returncode.idl",
"xpctest_utils.idl",
]
fixtures = os.path.join(dir, "fixtures")
inc_dirs = [os.path.join(src, "xpcom/base")]
def setUp(self):
self.parser = xpidl.IDLParser()
def test_d_json(self):
mods = []
for file in self.files:
path = os.path.join(self.tests_dir, file)
idl = self.parser.parse(open(path).read(), path)
idl.resolve(self.inc_dirs, self.parser, {})
mods.append(typescript.ts_source(idl))
result = json.dumps(mods, indent=2, sort_keys=True)
expected = open(os.path.join(self.fixtures, "xpctest.d.json")).read()
self.assertEqual(result, expected, "types data json does not match")
if __name__ == "__main__":
mozunit.main(runwith="unittest")
|