Bug 1867190 - Add prefs for PHC probablities r=glandium
[gecko.git] / xpcom / idl-parser / xpidl / runtests.py
blob2dd269dfd98006201b14c868e00275dca0e25df5
1 #!/usr/bin/env python
3 # Any copyright is dedicated to the Public Domain.
4 # http://creativecommons.org/publicdomain/zero/1.0/
6 # Unit tests for xpidl.py
8 import sys
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.
13 sys.path.pop(0)
15 import unittest
17 import mozunit
19 from xpidl import header, xpidl
22 class TestParser(unittest.TestCase):
23 def setUp(self):
24 self.p = xpidl.IDLParser()
26 def testEmpty(self):
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):
44 i = self.p.parse(
45 "[scriptable, builtinclass, function, uuid(abc)] interface foo {};",
46 filename="f",
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)
63 def testMethod(self):
64 i = self.p.parse(
65 """[uuid(abc)] interface foo {
66 void bar();
67 };""",
68 filename="f",
70 self.assertTrue(isinstance(i, xpidl.IDL))
71 self.assertTrue(isinstance(i.productions[0], xpidl.Interface))
72 iface = i.productions[0]
73 m = iface.members[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):
79 i = self.p.parse(
80 """
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);
84 };""",
85 filename="f",
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]
91 m = iface.members[0]
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):
106 i = self.p.parse(
107 """[uuid(abc)] interface foo {
108 attribute long bar;
109 };""",
110 filename="f",
112 self.assertTrue(isinstance(i, xpidl.IDL))
113 self.assertTrue(isinstance(i.productions[0], xpidl.Interface))
114 iface = i.productions[0]
115 a = iface.members[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):
121 i = self.p.parse(
123 [scriptable, uuid(00000000-0000-0000-0000-000000000000)] interface nsISupports {};
124 [uuid(abc)] interface foo : nsISupports {
125 attribute long bar;
126 void getBar();
127 };""",
128 filename="f",
130 self.assertTrue(isinstance(i, xpidl.IDL))
131 i.resolve([], self.p, {})
133 class FdMock:
134 def write(self, s):
135 pass
137 try:
138 header.print_header(i, FdMock(), filename="f", relpath="f")
139 self.assertTrue(False, "Header printing failed to fail")
140 except Exception as e:
141 self.assertEqual(
142 e.args[0],
143 "Unexpected overloaded virtual method GetBar in interface foo",
146 def testNotISupports(self):
147 i = self.p.parse(
149 [uuid(abc)] interface foo {};
150 """,
151 filename="f",
153 self.assertTrue(isinstance(i, xpidl.IDL))
154 try:
155 i.resolve([], self.p, {})
156 self.assertTrue(
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):
163 i = self.p.parse(
165 [scriptable, uuid(aaa)] interface nsISupports {};
166 [scriptable, builtinclass, uuid(abc)] interface foo : nsISupports {};
167 [scriptable, uuid(def)] interface bar : foo {};
168 """,
169 filename="f",
171 self.assertTrue(isinstance(i, xpidl.IDL))
172 try:
173 i.resolve([], self.p, {})
174 self.assertTrue(
175 False, "non-builtinclasses can't inherit from builtinclasses"
177 except xpidl.IDLError as e:
178 self.assertEqual(
179 e.args[0],
180 "interface 'bar' is not builtinclass but derives from builtinclass 'foo'",
183 def testScriptableNotXPCOM(self):
184 # notxpcom method requires builtinclass on the interface
185 i = self.p.parse(
187 [scriptable, uuid(aaa)] interface nsISupports {};
188 [scriptable, uuid(abc)] interface nsIScriptableWithNotXPCOM : nsISupports {
189 [notxpcom] void method2();
191 """,
192 filename="f",
194 self.assertTrue(isinstance(i, xpidl.IDL))
195 try:
196 i.resolve([], self.p, {})
197 self.assertTrue(
198 False,
199 "Resolve should fail for non-builtinclasses with notxpcom methods",
201 except xpidl.IDLError as e:
202 self.assertEqual(
203 e.args[0],
205 "scriptable interface 'nsIScriptableWithNotXPCOM' "
206 "must be marked [builtinclass] because it contains a [notxpcom] "
207 "method 'method2'"
211 # notxpcom attribute requires builtinclass on the interface
212 i = self.p.parse(
214 interface nsISomeInterface;
215 [scriptable, uuid(aaa)] interface nsISupports {};
216 [scriptable, uuid(abc)] interface nsIScriptableWithNotXPCOM : nsISupports {
217 [notxpcom] attribute nsISomeInterface attrib;
219 """,
220 filename="f",
222 self.assertTrue(isinstance(i, xpidl.IDL))
223 try:
224 i.resolve([], self.p, {})
225 self.assertTrue(
226 False,
227 "Resolve should fail for non-builtinclasses with notxpcom attributes",
229 except xpidl.IDLError as e:
230 self.assertEqual(
231 e.args[0],
233 "scriptable interface 'nsIScriptableWithNotXPCOM' must be marked "
234 "[builtinclass] because it contains a [notxpcom] attribute 'attrib'"
238 def testUndefinedConst(self):
239 i = self.p.parse(
241 [scriptable, uuid(aaa)] interface nsISupports {};
242 [scriptable, uuid(abc)] interface foo : nsISupports {
243 const unsigned long X = Y + 1;
245 """,
246 filename="f",
248 self.assertTrue(isinstance(i, xpidl.IDL))
249 try:
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")