|
1 import WebIDL |
|
2 |
|
3 def WebIDLTest(parser, harness): |
|
4 parser.parse(""" |
|
5 interface TestMethods { |
|
6 void basic(); |
|
7 static void basicStatic(); |
|
8 void basicWithSimpleArgs(boolean arg1, byte arg2, unsigned long arg3); |
|
9 boolean basicBoolean(); |
|
10 static boolean basicStaticBoolean(); |
|
11 boolean basicBooleanWithSimpleArgs(boolean arg1, byte arg2, unsigned long arg3); |
|
12 void optionalArg(optional byte? arg1, optional sequence<byte> arg2); |
|
13 void variadicArg(byte?... arg1); |
|
14 void crazyTypes(sequence<long?[]>? arg1, boolean?[][]? arg2); |
|
15 object getObject(); |
|
16 void setObject(object arg1); |
|
17 void setAny(any arg1); |
|
18 float doFloats(float arg1); |
|
19 }; |
|
20 """) |
|
21 |
|
22 results = parser.finish() |
|
23 |
|
24 harness.ok(True, "TestMethods interface parsed without error.") |
|
25 harness.check(len(results), 1, "Should be one production.") |
|
26 iface = results[0] |
|
27 harness.ok(isinstance(iface, WebIDL.IDLInterface), |
|
28 "Should be an IDLInterface") |
|
29 harness.check(iface.identifier.QName(), "::TestMethods", "Interface has the right QName") |
|
30 harness.check(iface.identifier.name, "TestMethods", "Interface has the right name") |
|
31 harness.check(len(iface.members), 13, "Expect 13 members") |
|
32 |
|
33 methods = iface.members |
|
34 |
|
35 def checkArgument(argument, QName, name, type, optional, variadic): |
|
36 harness.ok(isinstance(argument, WebIDL.IDLArgument), |
|
37 "Should be an IDLArgument") |
|
38 harness.check(argument.identifier.QName(), QName, "Argument has the right QName") |
|
39 harness.check(argument.identifier.name, name, "Argument has the right name") |
|
40 harness.check(str(argument.type), type, "Argument has the right return type") |
|
41 harness.check(argument.optional, optional, "Argument has the right optional value") |
|
42 harness.check(argument.variadic, variadic, "Argument has the right variadic value") |
|
43 |
|
44 def checkMethod(method, QName, name, signatures, |
|
45 static=False, getter=False, setter=False, creator=False, |
|
46 deleter=False, legacycaller=False, stringifier=False): |
|
47 harness.ok(isinstance(method, WebIDL.IDLMethod), |
|
48 "Should be an IDLMethod") |
|
49 harness.ok(method.isMethod(), "Method is a method") |
|
50 harness.ok(not method.isAttr(), "Method is not an attr") |
|
51 harness.ok(not method.isConst(), "Method is not a const") |
|
52 harness.check(method.identifier.QName(), QName, "Method has the right QName") |
|
53 harness.check(method.identifier.name, name, "Method has the right name") |
|
54 harness.check(method.isStatic(), static, "Method has the correct static value") |
|
55 harness.check(method.isGetter(), getter, "Method has the correct getter value") |
|
56 harness.check(method.isSetter(), setter, "Method has the correct setter value") |
|
57 harness.check(method.isCreator(), creator, "Method has the correct creator value") |
|
58 harness.check(method.isDeleter(), deleter, "Method has the correct deleter value") |
|
59 harness.check(method.isLegacycaller(), legacycaller, "Method has the correct legacycaller value") |
|
60 harness.check(method.isStringifier(), stringifier, "Method has the correct stringifier value") |
|
61 harness.check(len(method.signatures()), len(signatures), "Method has the correct number of signatures") |
|
62 |
|
63 sigpairs = zip(method.signatures(), signatures) |
|
64 for (gotSignature, expectedSignature) in sigpairs: |
|
65 (gotRetType, gotArgs) = gotSignature |
|
66 (expectedRetType, expectedArgs) = expectedSignature |
|
67 |
|
68 harness.check(str(gotRetType), expectedRetType, |
|
69 "Method has the expected return type.") |
|
70 |
|
71 for i in range(0, len(gotArgs)): |
|
72 (QName, name, type, optional, variadic) = expectedArgs[i] |
|
73 checkArgument(gotArgs[i], QName, name, type, optional, variadic) |
|
74 |
|
75 checkMethod(methods[0], "::TestMethods::basic", "basic", [("Void", [])]) |
|
76 checkMethod(methods[1], "::TestMethods::basicStatic", "basicStatic", |
|
77 [("Void", [])], static=True) |
|
78 checkMethod(methods[2], "::TestMethods::basicWithSimpleArgs", |
|
79 "basicWithSimpleArgs", |
|
80 [("Void", |
|
81 [("::TestMethods::basicWithSimpleArgs::arg1", "arg1", "Boolean", False, False), |
|
82 ("::TestMethods::basicWithSimpleArgs::arg2", "arg2", "Byte", False, False), |
|
83 ("::TestMethods::basicWithSimpleArgs::arg3", "arg3", "UnsignedLong", False, False)])]) |
|
84 checkMethod(methods[3], "::TestMethods::basicBoolean", "basicBoolean", [("Boolean", [])]) |
|
85 checkMethod(methods[4], "::TestMethods::basicStaticBoolean", "basicStaticBoolean", [("Boolean", [])], static=True) |
|
86 checkMethod(methods[5], "::TestMethods::basicBooleanWithSimpleArgs", |
|
87 "basicBooleanWithSimpleArgs", |
|
88 [("Boolean", |
|
89 [("::TestMethods::basicBooleanWithSimpleArgs::arg1", "arg1", "Boolean", False, False), |
|
90 ("::TestMethods::basicBooleanWithSimpleArgs::arg2", "arg2", "Byte", False, False), |
|
91 ("::TestMethods::basicBooleanWithSimpleArgs::arg3", "arg3", "UnsignedLong", False, False)])]) |
|
92 checkMethod(methods[6], "::TestMethods::optionalArg", |
|
93 "optionalArg", |
|
94 [("Void", |
|
95 [("::TestMethods::optionalArg::arg1", "arg1", "ByteOrNull", True, False), |
|
96 ("::TestMethods::optionalArg::arg2", "arg2", "ByteSequence", True, False)])]) |
|
97 checkMethod(methods[7], "::TestMethods::variadicArg", |
|
98 "variadicArg", |
|
99 [("Void", |
|
100 [("::TestMethods::variadicArg::arg1", "arg1", "ByteOrNull", True, True)])]) |
|
101 checkMethod(methods[8], "::TestMethods::crazyTypes", |
|
102 "crazyTypes", |
|
103 [("Void", |
|
104 [("::TestMethods::crazyTypes::arg1", "arg1", "LongOrNullArraySequenceOrNull", False, False), |
|
105 ("::TestMethods::crazyTypes::arg2", "arg2", "BooleanOrNullArrayArrayOrNull", False, False)])]) |
|
106 checkMethod(methods[9], "::TestMethods::getObject", |
|
107 "getObject", [("Object", [])]) |
|
108 checkMethod(methods[10], "::TestMethods::setObject", |
|
109 "setObject", |
|
110 [("Void", |
|
111 [("::TestMethods::setObject::arg1", "arg1", "Object", False, False)])]) |
|
112 checkMethod(methods[11], "::TestMethods::setAny", |
|
113 "setAny", |
|
114 [("Void", |
|
115 [("::TestMethods::setAny::arg1", "arg1", "Any", False, False)])]) |
|
116 checkMethod(methods[12], "::TestMethods::doFloats", |
|
117 "doFloats", |
|
118 [("Float", |
|
119 [("::TestMethods::doFloats::arg1", "arg1", "Float", False, False)])]) |
|
120 |
|
121 parser = parser.reset() |
|
122 threw = False |
|
123 try: |
|
124 parser.parse(""" |
|
125 interface A { |
|
126 void foo(optional float bar = 1); |
|
127 }; |
|
128 """) |
|
129 results = parser.finish() |
|
130 except Exception, x: |
|
131 threw = True |
|
132 harness.ok(not threw, "Should allow integer to float type corecion") |
|
133 |
|
134 parser = parser.reset() |
|
135 threw = False |
|
136 try: |
|
137 parser.parse(""" |
|
138 interface A { |
|
139 [GetterThrows] void foo(); |
|
140 }; |
|
141 """) |
|
142 results = parser.finish() |
|
143 except Exception, x: |
|
144 threw = True |
|
145 harness.ok(threw, "Should not allow [GetterThrows] on methods") |
|
146 |
|
147 parser = parser.reset() |
|
148 threw = False |
|
149 try: |
|
150 parser.parse(""" |
|
151 interface A { |
|
152 [SetterThrows] void foo(); |
|
153 }; |
|
154 """) |
|
155 results = parser.finish() |
|
156 except Exception, x: |
|
157 threw = True |
|
158 harness.ok(threw, "Should not allow [SetterThrows] on methods") |
|
159 |
|
160 parser = parser.reset() |
|
161 threw = False |
|
162 try: |
|
163 parser.parse(""" |
|
164 interface A { |
|
165 [Throw] void foo(); |
|
166 }; |
|
167 """) |
|
168 results = parser.finish() |
|
169 except Exception, x: |
|
170 threw = True |
|
171 harness.ok(threw, "Should spell [Throws] correctly on methods") |