|
1 #!/usr/bin/env python |
|
2 # |
|
3 # Any copyright is dedicated to the Public Domain. |
|
4 # http://creativecommons.org/publicdomain/zero/1.0/ |
|
5 # |
|
6 # Unit tests for xpidl.py |
|
7 |
|
8 import mozunit |
|
9 import unittest |
|
10 import xpidl |
|
11 |
|
12 class TestParser(unittest.TestCase): |
|
13 def setUp(self): |
|
14 self.p = xpidl.IDLParser() |
|
15 |
|
16 def testEmpty(self): |
|
17 i = self.p.parse("", filename='f') |
|
18 self.assertTrue(isinstance(i, xpidl.IDL)) |
|
19 self.assertEqual([], i.productions) |
|
20 |
|
21 def testForwardInterface(self): |
|
22 i = self.p.parse("interface foo;", filename='f') |
|
23 self.assertTrue(isinstance(i, xpidl.IDL)) |
|
24 self.assertTrue(isinstance(i.productions[0], xpidl.Forward)) |
|
25 self.assertEqual("foo", i.productions[0].name) |
|
26 |
|
27 def testInterface(self): |
|
28 i = self.p.parse("[uuid(abc)] interface foo {};", filename='f') |
|
29 self.assertTrue(isinstance(i, xpidl.IDL)) |
|
30 self.assertTrue(isinstance(i.productions[0], xpidl.Interface)) |
|
31 self.assertEqual("foo", i.productions[0].name) |
|
32 |
|
33 def testAttributes(self): |
|
34 i = self.p.parse("[scriptable, builtinclass, function, deprecated, uuid(abc)] interface foo {};", filename='f') |
|
35 self.assertTrue(isinstance(i, xpidl.IDL)) |
|
36 self.assertTrue(isinstance(i.productions[0], xpidl.Interface)) |
|
37 iface = i.productions[0] |
|
38 self.assertEqual("foo", iface.name) |
|
39 self.assertTrue(iface.attributes.scriptable) |
|
40 self.assertTrue(iface.attributes.builtinclass) |
|
41 self.assertTrue(iface.attributes.function) |
|
42 self.assertTrue(iface.attributes.deprecated) |
|
43 |
|
44 i = self.p.parse("[noscript, uuid(abc)] interface foo {};", filename='f') |
|
45 self.assertTrue(isinstance(i, xpidl.IDL)) |
|
46 self.assertTrue(isinstance(i.productions[0], xpidl.Interface)) |
|
47 iface = i.productions[0] |
|
48 self.assertEqual("foo", iface.name) |
|
49 self.assertTrue(iface.attributes.noscript) |
|
50 |
|
51 def testMethod(self): |
|
52 i = self.p.parse("""[uuid(abc)] interface foo { |
|
53 void bar(); |
|
54 };""", filename='f') |
|
55 self.assertTrue(isinstance(i, xpidl.IDL)) |
|
56 self.assertTrue(isinstance(i.productions[0], xpidl.Interface)) |
|
57 iface = i.productions[0] |
|
58 m = iface.members[0] |
|
59 self.assertTrue(isinstance(m, xpidl.Method)) |
|
60 self.assertEqual("bar", m.name) |
|
61 self.assertEqual("void", m.type) |
|
62 |
|
63 def testMethodParams(self): |
|
64 i = self.p.parse("""[uuid(abc)] interface foo { |
|
65 long bar(in long a, in float b, [array] in long c); |
|
66 };""", filename='f') |
|
67 i.resolve([], self.p) |
|
68 self.assertTrue(isinstance(i, xpidl.IDL)) |
|
69 self.assertTrue(isinstance(i.productions[0], xpidl.Interface)) |
|
70 iface = i.productions[0] |
|
71 m = iface.members[0] |
|
72 self.assertTrue(isinstance(m, xpidl.Method)) |
|
73 self.assertEqual("bar", m.name) |
|
74 self.assertEqual("long", m.type) |
|
75 self.assertEqual(3, len(m.params)) |
|
76 self.assertEqual("long", m.params[0].type) |
|
77 self.assertEqual("in", m.params[0].paramtype) |
|
78 self.assertEqual("float", m.params[1].type) |
|
79 self.assertEqual("in", m.params[1].paramtype) |
|
80 self.assertEqual("long", m.params[2].type) |
|
81 self.assertEqual("in", m.params[2].paramtype) |
|
82 self.assertTrue(isinstance(m.params[2].realtype, xpidl.Array)) |
|
83 self.assertEqual("long", m.params[2].realtype.type.name) |
|
84 |
|
85 def testAttribute(self): |
|
86 i = self.p.parse("""[uuid(abc)] interface foo { |
|
87 attribute long bar; |
|
88 };""", filename='f') |
|
89 self.assertTrue(isinstance(i, xpidl.IDL)) |
|
90 self.assertTrue(isinstance(i.productions[0], xpidl.Interface)) |
|
91 iface = i.productions[0] |
|
92 a = iface.members[0] |
|
93 self.assertTrue(isinstance(a, xpidl.Attribute)) |
|
94 self.assertEqual("bar", a.name) |
|
95 self.assertEqual("long", a.type) |
|
96 |
|
97 if __name__ == '__main__': |
|
98 mozunit.main() |