3 # Any copyright is dedicated to the Public Domain.
4 # http://creativecommons.org/publicdomain/zero/1.0/
6 # Unit tests for xpidl.py
12 # Hack: the first entry in sys.path is the directory containing the script.
13 # This messes things up because that directory is the xpidl module, and that
14 # which conflicts with the xpidl submodule in the imports further below.
21 from xpidl
import header
, typescript
, xpidl
24 class TestParser(unittest
.TestCase
):
26 self
.p
= xpidl
.IDLParser()
29 i
= self
.p
.parse("", filename
="f")
30 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
31 self
.assertEqual([], i
.productions
)
33 def testForwardInterface(self
):
34 i
= self
.p
.parse("interface foo;", filename
="f")
35 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
36 self
.assertTrue(isinstance(i
.productions
[0], xpidl
.Forward
))
37 self
.assertEqual("foo", i
.productions
[0].name
)
39 def testInterface(self
):
40 i
= self
.p
.parse("[uuid(abc)] interface foo {};", filename
="f")
41 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
42 self
.assertTrue(isinstance(i
.productions
[0], xpidl
.Interface
))
43 self
.assertEqual("foo", i
.productions
[0].name
)
45 def testAttributes(self
):
47 "[scriptable, builtinclass, function, uuid(abc)] interface foo {};",
50 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
51 self
.assertTrue(isinstance(i
.productions
[0], xpidl
.Interface
))
52 iface
= i
.productions
[0]
53 self
.assertEqual("foo", iface
.name
)
54 self
.assertTrue(iface
.attributes
.scriptable
)
55 self
.assertTrue(iface
.attributes
.builtinclass
)
56 self
.assertTrue(iface
.attributes
.function
)
58 i
= self
.p
.parse("[uuid(abc)] interface foo {};", filename
="f")
59 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
60 self
.assertTrue(isinstance(i
.productions
[0], xpidl
.Interface
))
61 iface
= i
.productions
[0]
62 self
.assertEqual("foo", iface
.name
)
63 self
.assertFalse(iface
.attributes
.scriptable
)
67 """[uuid(abc)] interface foo {
72 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
73 self
.assertTrue(isinstance(i
.productions
[0], xpidl
.Interface
))
74 iface
= i
.productions
[0]
76 self
.assertTrue(isinstance(m
, xpidl
.Method
))
77 self
.assertEqual("bar", m
.name
)
78 self
.assertEqual(xpidl
.TypeId("void"), m
.type)
80 def testMethodParams(self
):
83 [scriptable, uuid(aaa)] interface nsISupports {};
84 [uuid(abc)] interface foo : nsISupports {
85 long bar(in long a, in float b, [array] in long c);
89 i
.resolve([], self
.p
, {})
90 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
91 self
.assertTrue(isinstance(i
.productions
[1], xpidl
.Interface
))
92 iface
= i
.productions
[1]
94 self
.assertTrue(isinstance(m
, xpidl
.Method
))
95 self
.assertEqual("bar", m
.name
)
96 self
.assertEqual(xpidl
.TypeId("long"), m
.type)
97 self
.assertEqual(3, len(m
.params
))
98 self
.assertEqual(xpidl
.TypeId("long"), m
.params
[0].type)
99 self
.assertEqual("in", m
.params
[0].paramtype
)
100 self
.assertEqual(xpidl
.TypeId("float"), m
.params
[1].type)
101 self
.assertEqual("in", m
.params
[1].paramtype
)
102 self
.assertEqual(xpidl
.TypeId("long"), m
.params
[2].type)
103 self
.assertEqual("in", m
.params
[2].paramtype
)
104 self
.assertTrue(isinstance(m
.params
[2].realtype
, xpidl
.LegacyArray
))
105 self
.assertEqual("int32_t", m
.params
[2].realtype
.type.name
)
107 def testAttribute(self
):
109 """[uuid(abc)] interface foo {
114 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
115 self
.assertTrue(isinstance(i
.productions
[0], xpidl
.Interface
))
116 iface
= i
.productions
[0]
118 self
.assertTrue(isinstance(a
, xpidl
.Attribute
))
119 self
.assertEqual("bar", a
.name
)
120 self
.assertEqual(xpidl
.TypeId("long"), a
.type)
122 def testOverloadedVirtual(self
):
125 [scriptable, uuid(00000000-0000-0000-0000-000000000000)] interface nsISupports {};
126 [uuid(abc)] interface foo : nsISupports {
132 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
133 i
.resolve([], self
.p
, {})
140 header
.print_header(i
, FdMock(), filename
="f", relpath
="f")
141 self
.assertTrue(False, "Header printing failed to fail")
142 except Exception as e
:
145 "Unexpected overloaded virtual method GetBar in interface foo",
148 def testNotISupports(self
):
151 [uuid(abc)] interface foo {};
155 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
157 i
.resolve([], self
.p
, {})
159 False, "Must check that interfaces inherit from nsISupports"
161 except xpidl
.IDLError
as e
:
162 self
.assertEqual(e
.args
[0], "Interface 'foo' must inherit from nsISupports")
164 def testBuiltinClassParent(self
):
167 [scriptable, uuid(aaa)] interface nsISupports {};
168 [scriptable, builtinclass, uuid(abc)] interface foo : nsISupports {};
169 [scriptable, uuid(def)] interface bar : foo {};
173 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
175 i
.resolve([], self
.p
, {})
177 False, "non-builtinclasses can't inherit from builtinclasses"
179 except xpidl
.IDLError
as e
:
182 "interface 'bar' is not builtinclass but derives from builtinclass 'foo'",
185 def testScriptableNotXPCOM(self
):
186 # notxpcom method requires builtinclass on the interface
189 [scriptable, uuid(aaa)] interface nsISupports {};
190 [scriptable, uuid(abc)] interface nsIScriptableWithNotXPCOM : nsISupports {
191 [notxpcom] void method2();
196 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
198 i
.resolve([], self
.p
, {})
201 "Resolve should fail for non-builtinclasses with notxpcom methods",
203 except xpidl
.IDLError
as e
:
207 "scriptable interface 'nsIScriptableWithNotXPCOM' "
208 "must be marked [builtinclass] because it contains a [notxpcom] "
213 # notxpcom attribute requires builtinclass on the interface
216 interface nsISomeInterface;
217 [scriptable, uuid(aaa)] interface nsISupports {};
218 [scriptable, uuid(abc)] interface nsIScriptableWithNotXPCOM : nsISupports {
219 [notxpcom] attribute nsISomeInterface attrib;
224 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
226 i
.resolve([], self
.p
, {})
229 "Resolve should fail for non-builtinclasses with notxpcom attributes",
231 except xpidl
.IDLError
as e
:
235 "scriptable interface 'nsIScriptableWithNotXPCOM' must be marked "
236 "[builtinclass] because it contains a [notxpcom] attribute 'attrib'"
240 def testUndefinedConst(self
):
243 [scriptable, uuid(aaa)] interface nsISupports {};
244 [scriptable, uuid(abc)] interface foo : nsISupports {
245 const unsigned long X = Y + 1;
250 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
252 i
.resolve([], self
.p
, {})
253 self
.assertTrue(False, "Must detect undefined symbols")
254 except xpidl
.IDLError
as e
:
255 self
.assertEqual(e
.args
[0], ("cannot find symbol 'Y'"))
258 class TestTypescript(unittest
.TestCase
):
259 """A few basic smoke tests for typescript generation."""
261 dir = os
.path
.dirname(__file__
)
262 src
= os
.path
.join(dir, "..", "..", "..")
264 # We use the xpctest.xpt *.idl files from:
265 tests_dir
= os
.path
.join(src
, "js/xpconnect/tests/idl")
267 "xpctest_attributes.idl",
268 "xpctest_bug809674.idl",
269 "xpctest_cenums.idl",
270 "xpctest_interfaces.idl",
271 "xpctest_params.idl",
272 "xpctest_returncode.idl",
276 fixtures
= os
.path
.join(dir, "fixtures")
277 inc_dirs
= [os
.path
.join(src
, "xpcom/base")]
280 self
.parser
= xpidl
.IDLParser()
282 def test_d_json(self
):
284 for file in self
.files
:
285 path
= os
.path
.join(self
.tests_dir
, file)
286 idl
= self
.parser
.parse(open(path
).read(), path
)
287 idl
.resolve(self
.inc_dirs
, self
.parser
, {})
288 mods
.append(typescript
.ts_source(idl
))
290 result
= json
.dumps(mods
, indent
=2, sort_keys
=True)
291 expected
= open(os
.path
.join(self
.fixtures
, "xpctest.d.json")).read()
292 self
.assertEqual(result
, expected
, "types data json does not match")
295 if __name__
== "__main__":
296 mozunit
.main(runwith
="unittest")