Bug 1904139 - Don't re-initialize platform font list from GetDefaultFont. r=jfkthame
[gecko.git] / xpcom / idl-parser / xpidl / runtests.py
blob940f845b9b15914a6645a3d27582f5e29c61c9bf
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 json
9 import os
10 import sys
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.
15 sys.path.pop(0)
17 import unittest
19 import mozunit
21 from xpidl import header, typescript, xpidl
24 class TestParser(unittest.TestCase):
25 def setUp(self):
26 self.p = xpidl.IDLParser()
28 def testEmpty(self):
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):
46 i = self.p.parse(
47 "[scriptable, builtinclass, function, uuid(abc)] interface foo {};",
48 filename="f",
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)
65 def testMethod(self):
66 i = self.p.parse(
67 """[uuid(abc)] interface foo {
68 void bar();
69 };""",
70 filename="f",
72 self.assertTrue(isinstance(i, xpidl.IDL))
73 self.assertTrue(isinstance(i.productions[0], xpidl.Interface))
74 iface = i.productions[0]
75 m = iface.members[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):
81 i = self.p.parse(
82 """
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);
86 };""",
87 filename="f",
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]
93 m = iface.members[0]
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):
108 i = self.p.parse(
109 """[uuid(abc)] interface foo {
110 attribute long bar;
111 };""",
112 filename="f",
114 self.assertTrue(isinstance(i, xpidl.IDL))
115 self.assertTrue(isinstance(i.productions[0], xpidl.Interface))
116 iface = i.productions[0]
117 a = iface.members[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):
123 i = self.p.parse(
125 [scriptable, uuid(00000000-0000-0000-0000-000000000000)] interface nsISupports {};
126 [uuid(abc)] interface foo : nsISupports {
127 attribute long bar;
128 void getBar();
129 };""",
130 filename="f",
132 self.assertTrue(isinstance(i, xpidl.IDL))
133 i.resolve([], self.p, {})
135 class FdMock:
136 def write(self, s):
137 pass
139 try:
140 header.print_header(i, FdMock(), filename="f", relpath="f")
141 self.assertTrue(False, "Header printing failed to fail")
142 except Exception as e:
143 self.assertEqual(
144 e.args[0],
145 "Unexpected overloaded virtual method GetBar in interface foo",
148 def testNotISupports(self):
149 i = self.p.parse(
151 [uuid(abc)] interface foo {};
152 """,
153 filename="f",
155 self.assertTrue(isinstance(i, xpidl.IDL))
156 try:
157 i.resolve([], self.p, {})
158 self.assertTrue(
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):
165 i = self.p.parse(
167 [scriptable, uuid(aaa)] interface nsISupports {};
168 [scriptable, builtinclass, uuid(abc)] interface foo : nsISupports {};
169 [scriptable, uuid(def)] interface bar : foo {};
170 """,
171 filename="f",
173 self.assertTrue(isinstance(i, xpidl.IDL))
174 try:
175 i.resolve([], self.p, {})
176 self.assertTrue(
177 False, "non-builtinclasses can't inherit from builtinclasses"
179 except xpidl.IDLError as e:
180 self.assertEqual(
181 e.args[0],
182 "interface 'bar' is not builtinclass but derives from builtinclass 'foo'",
185 def testScriptableNotXPCOM(self):
186 # notxpcom method requires builtinclass on the interface
187 i = self.p.parse(
189 [scriptable, uuid(aaa)] interface nsISupports {};
190 [scriptable, uuid(abc)] interface nsIScriptableWithNotXPCOM : nsISupports {
191 [notxpcom] void method2();
193 """,
194 filename="f",
196 self.assertTrue(isinstance(i, xpidl.IDL))
197 try:
198 i.resolve([], self.p, {})
199 self.assertTrue(
200 False,
201 "Resolve should fail for non-builtinclasses with notxpcom methods",
203 except xpidl.IDLError as e:
204 self.assertEqual(
205 e.args[0],
207 "scriptable interface 'nsIScriptableWithNotXPCOM' "
208 "must be marked [builtinclass] because it contains a [notxpcom] "
209 "method 'method2'"
213 # notxpcom attribute requires builtinclass on the interface
214 i = self.p.parse(
216 interface nsISomeInterface;
217 [scriptable, uuid(aaa)] interface nsISupports {};
218 [scriptable, uuid(abc)] interface nsIScriptableWithNotXPCOM : nsISupports {
219 [notxpcom] attribute nsISomeInterface attrib;
221 """,
222 filename="f",
224 self.assertTrue(isinstance(i, xpidl.IDL))
225 try:
226 i.resolve([], self.p, {})
227 self.assertTrue(
228 False,
229 "Resolve should fail for non-builtinclasses with notxpcom attributes",
231 except xpidl.IDLError as e:
232 self.assertEqual(
233 e.args[0],
235 "scriptable interface 'nsIScriptableWithNotXPCOM' must be marked "
236 "[builtinclass] because it contains a [notxpcom] attribute 'attrib'"
240 def testUndefinedConst(self):
241 i = self.p.parse(
243 [scriptable, uuid(aaa)] interface nsISupports {};
244 [scriptable, uuid(abc)] interface foo : nsISupports {
245 const unsigned long X = Y + 1;
247 """,
248 filename="f",
250 self.assertTrue(isinstance(i, xpidl.IDL))
251 try:
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")
266 files = [
267 "xpctest_attributes.idl",
268 "xpctest_bug809674.idl",
269 "xpctest_cenums.idl",
270 "xpctest_interfaces.idl",
271 "xpctest_params.idl",
272 "xpctest_returncode.idl",
273 "xpctest_utils.idl",
276 fixtures = os.path.join(dir, "fixtures")
277 inc_dirs = [os.path.join(src, "xpcom/base")]
279 def setUp(self):
280 self.parser = xpidl.IDLParser()
282 def test_d_json(self):
283 mods = []
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")