3 # Any copyright is dedicated to the Public Domain.
4 # http://creativecommons.org/publicdomain/zero/1.0/
6 # Unit tests for xpidl.py
10 # Hack: the first entry in sys.path is the directory containing the script.
11 # This messes things up because that directory is the xpidl module, and that
12 # which conflicts with the xpidl submodule in the imports further below.
19 from xpidl
import header
, xpidl
22 class TestParser(unittest
.TestCase
):
24 self
.p
= xpidl
.IDLParser()
27 i
= self
.p
.parse("", filename
="f")
28 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
29 self
.assertEqual([], i
.productions
)
31 def testForwardInterface(self
):
32 i
= self
.p
.parse("interface foo;", filename
="f")
33 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
34 self
.assertTrue(isinstance(i
.productions
[0], xpidl
.Forward
))
35 self
.assertEqual("foo", i
.productions
[0].name
)
37 def testInterface(self
):
38 i
= self
.p
.parse("[uuid(abc)] interface foo {};", filename
="f")
39 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
40 self
.assertTrue(isinstance(i
.productions
[0], xpidl
.Interface
))
41 self
.assertEqual("foo", i
.productions
[0].name
)
43 def testAttributes(self
):
45 "[scriptable, builtinclass, function, uuid(abc)] interface foo {};",
48 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
49 self
.assertTrue(isinstance(i
.productions
[0], xpidl
.Interface
))
50 iface
= i
.productions
[0]
51 self
.assertEqual("foo", iface
.name
)
52 self
.assertTrue(iface
.attributes
.scriptable
)
53 self
.assertTrue(iface
.attributes
.builtinclass
)
54 self
.assertTrue(iface
.attributes
.function
)
56 i
= self
.p
.parse("[uuid(abc)] interface foo {};", filename
="f")
57 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
58 self
.assertTrue(isinstance(i
.productions
[0], xpidl
.Interface
))
59 iface
= i
.productions
[0]
60 self
.assertEqual("foo", iface
.name
)
61 self
.assertFalse(iface
.attributes
.scriptable
)
65 """[uuid(abc)] interface foo {
70 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
71 self
.assertTrue(isinstance(i
.productions
[0], xpidl
.Interface
))
72 iface
= i
.productions
[0]
74 self
.assertTrue(isinstance(m
, xpidl
.Method
))
75 self
.assertEqual("bar", m
.name
)
76 self
.assertEqual(xpidl
.TypeId("void"), m
.type)
78 def testMethodParams(self
):
81 [scriptable, uuid(aaa)] interface nsISupports {};
82 [uuid(abc)] interface foo : nsISupports {
83 long bar(in long a, in float b, [array] in long c);
87 i
.resolve([], self
.p
, {})
88 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
89 self
.assertTrue(isinstance(i
.productions
[1], xpidl
.Interface
))
90 iface
= i
.productions
[1]
92 self
.assertTrue(isinstance(m
, xpidl
.Method
))
93 self
.assertEqual("bar", m
.name
)
94 self
.assertEqual(xpidl
.TypeId("long"), m
.type)
95 self
.assertEqual(3, len(m
.params
))
96 self
.assertEqual(xpidl
.TypeId("long"), m
.params
[0].type)
97 self
.assertEqual("in", m
.params
[0].paramtype
)
98 self
.assertEqual(xpidl
.TypeId("float"), m
.params
[1].type)
99 self
.assertEqual("in", m
.params
[1].paramtype
)
100 self
.assertEqual(xpidl
.TypeId("long"), m
.params
[2].type)
101 self
.assertEqual("in", m
.params
[2].paramtype
)
102 self
.assertTrue(isinstance(m
.params
[2].realtype
, xpidl
.LegacyArray
))
103 self
.assertEqual("long", m
.params
[2].realtype
.type.name
)
105 def testAttribute(self
):
107 """[uuid(abc)] interface foo {
112 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
113 self
.assertTrue(isinstance(i
.productions
[0], xpidl
.Interface
))
114 iface
= i
.productions
[0]
116 self
.assertTrue(isinstance(a
, xpidl
.Attribute
))
117 self
.assertEqual("bar", a
.name
)
118 self
.assertEqual(xpidl
.TypeId("long"), a
.type)
120 def testOverloadedVirtual(self
):
123 [scriptable, uuid(00000000-0000-0000-0000-000000000000)] interface nsISupports {};
124 [uuid(abc)] interface foo : nsISupports {
130 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
131 i
.resolve([], self
.p
, {})
138 header
.print_header(i
, FdMock(), filename
="f", relpath
="f")
139 self
.assertTrue(False, "Header printing failed to fail")
140 except Exception as e
:
143 "Unexpected overloaded virtual method GetBar in interface foo",
146 def testNotISupports(self
):
149 [uuid(abc)] interface foo {};
153 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
155 i
.resolve([], self
.p
, {})
157 False, "Must check that interfaces inherit from nsISupports"
159 except xpidl
.IDLError
as e
:
160 self
.assertEqual(e
.args
[0], "Interface 'foo' must inherit from nsISupports")
162 def testBuiltinClassParent(self
):
165 [scriptable, uuid(aaa)] interface nsISupports {};
166 [scriptable, builtinclass, uuid(abc)] interface foo : nsISupports {};
167 [scriptable, uuid(def)] interface bar : foo {};
171 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
173 i
.resolve([], self
.p
, {})
175 False, "non-builtinclasses can't inherit from builtinclasses"
177 except xpidl
.IDLError
as e
:
180 "interface 'bar' is not builtinclass but derives from builtinclass 'foo'",
183 def testScriptableNotXPCOM(self
):
184 # notxpcom method requires builtinclass on the interface
187 [scriptable, uuid(aaa)] interface nsISupports {};
188 [scriptable, uuid(abc)] interface nsIScriptableWithNotXPCOM : nsISupports {
189 [notxpcom] void method2();
194 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
196 i
.resolve([], self
.p
, {})
199 "Resolve should fail for non-builtinclasses with notxpcom methods",
201 except xpidl
.IDLError
as e
:
205 "scriptable interface 'nsIScriptableWithNotXPCOM' "
206 "must be marked [builtinclass] because it contains a [notxpcom] "
211 # notxpcom attribute requires builtinclass on the interface
214 interface nsISomeInterface;
215 [scriptable, uuid(aaa)] interface nsISupports {};
216 [scriptable, uuid(abc)] interface nsIScriptableWithNotXPCOM : nsISupports {
217 [notxpcom] attribute nsISomeInterface attrib;
222 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
224 i
.resolve([], self
.p
, {})
227 "Resolve should fail for non-builtinclasses with notxpcom attributes",
229 except xpidl
.IDLError
as e
:
233 "scriptable interface 'nsIScriptableWithNotXPCOM' must be marked "
234 "[builtinclass] because it contains a [notxpcom] attribute 'attrib'"
238 def testUndefinedConst(self
):
241 [scriptable, uuid(aaa)] interface nsISupports {};
242 [scriptable, uuid(abc)] interface foo : nsISupports {
243 const unsigned long X = Y + 1;
248 self
.assertTrue(isinstance(i
, xpidl
.IDL
))
250 i
.resolve([], self
.p
, {})
251 self
.assertTrue(False, "Must detect undefined symbols")
252 except xpidl
.IDLError
as e
:
253 self
.assertEqual(e
.args
[0], ("cannot find symbol 'Y'"))
256 if __name__
== "__main__":
257 mozunit
.main(runwith
="unittest")