Bug 1915045 Ensure decode tasks are scheduled on BufferingState::Enter() r=media...
[gecko.git] / dom / bindings / parser / tests / test_toJSON.py
blobec81080becae40ec12b0560fdda4f965390cf1b8
1 import WebIDL
4 def WebIDLTest(parser, harness):
5 threw = False
6 try:
7 parser.parse(
8 """
9 interface Test {
10 object toJSON();
12 """
14 parser.finish()
15 except WebIDL.WebIDLError:
16 threw = True
17 harness.ok(not threw, "Should allow a toJSON method.")
19 parser = parser.reset()
20 threw = False
21 try:
22 parser.parse(
23 """
24 interface Test {
25 object toJSON(object arg);
26 object toJSON(long arg);
28 """
30 parser.finish()
31 except WebIDL.WebIDLError:
32 threw = True
33 harness.ok(threw, "Should not allow overloads of a toJSON method.")
35 parser = parser.reset()
36 threw = False
37 try:
38 parser.parse(
39 """
40 interface Test {
41 object toJSON(object arg);
43 """
45 parser.finish()
46 except WebIDL.WebIDLError:
47 threw = True
48 harness.ok(threw, "Should not allow a toJSON method with arguments.")
50 parser = parser.reset()
51 threw = False
52 try:
53 parser.parse(
54 """
55 interface Test {
56 long toJSON();
58 """
60 parser.finish()
61 except WebIDL.WebIDLError:
62 threw = True
63 harness.ok(not threw, "Should allow a toJSON method with 'long' as return type.")
65 parser = parser.reset()
66 threw = False
67 try:
68 parser.parse(
69 """
70 interface Test {
71 [Default] object toJSON();
73 """
75 parser.finish()
76 except WebIDL.WebIDLError:
77 threw = True
78 harness.ok(
79 not threw, "Should allow a default toJSON method with 'object' as return type."
82 parser = parser.reset()
83 threw = False
84 try:
85 parser.parse(
86 """
87 interface Test {
88 [Default] long toJSON();
90 """
92 parser.finish()
93 except WebIDL.WebIDLError:
94 threw = True
95 harness.ok(
96 threw,
97 "Should not allow a default toJSON method with non-'object' as return type.",
100 JsonTypes = [
101 "byte",
102 "octet",
103 "short",
104 "unsigned short",
105 "long",
106 "unsigned long",
107 "long long",
108 "unsigned long long",
109 "float",
110 "unrestricted float",
111 "double",
112 "unrestricted double",
113 "boolean",
114 "DOMString",
115 "ByteString",
116 "UTF8String",
117 "USVString",
118 "Enum",
119 "InterfaceWithToJSON",
120 "object",
123 nonJsonTypes = [
124 "InterfaceWithoutToJSON",
125 "any",
126 "Int8Array",
127 "Int16Array",
128 "Int32Array",
129 "Uint8Array",
130 "Uint16Array",
131 "Uint32Array",
132 "Uint8ClampedArray",
133 "Float32Array",
134 "Float64Array",
135 "ArrayBuffer",
138 def doTest(testIDL, shouldThrow, description):
139 p = parser.reset()
140 threw = False
141 try:
142 p.parse(
143 testIDL
144 + """
145 enum Enum { "a", "b", "c" };
146 interface InterfaceWithToJSON { long toJSON(); };
147 interface InterfaceWithoutToJSON {};
150 p.finish()
151 except Exception as x:
152 threw = True
153 harness.ok(x.message == "toJSON method has non-JSON return type", x)
154 harness.check(threw, shouldThrow, description)
156 for type in JsonTypes:
157 doTest(
158 "interface Test { %s toJSON(); };" % type,
159 False,
160 "%s should be a JSON type" % type,
163 doTest(
164 "interface Test { sequence<%s> toJSON(); };" % type,
165 False,
166 "sequence<%s> should be a JSON type" % type,
169 doTest(
170 "dictionary Foo { %s foo; }; " "interface Test { Foo toJSON(); }; " % type,
171 False,
172 "dictionary containing only JSON type (%s) should be a JSON type" % type,
175 doTest(
176 "dictionary Foo { %s foo; }; dictionary Bar : Foo { }; "
177 "interface Test { Bar toJSON(); }; " % type,
178 False,
179 "dictionary whose ancestors only contain JSON types should be a JSON type",
182 doTest(
183 "dictionary Foo { any foo; }; dictionary Bar : Foo { %s bar; };"
184 "interface Test { Bar toJSON(); };" % type,
185 True,
186 "dictionary whose ancestors contain non-JSON types should not be a JSON type",
189 doTest(
190 "interface Test { record<DOMString, %s> toJSON(); };" % type,
191 False,
192 "record<DOMString, %s> should be a JSON type" % type,
195 doTest(
196 "interface Test { record<ByteString, %s> toJSON(); };" % type,
197 False,
198 "record<ByteString, %s> should be a JSON type" % type,
201 doTest(
202 "interface Test { record<UTF8String, %s> toJSON(); };" % type,
203 False,
204 "record<UTF8String, %s> should be a JSON type" % type,
207 doTest(
208 "interface Test { record<USVString, %s> toJSON(); };" % type,
209 False,
210 "record<USVString, %s> should be a JSON type" % type,
213 otherUnionType = "Foo" if type != "object" else "long"
214 doTest(
215 "interface Foo { object toJSON(); };"
216 "interface Test { (%s or %s) toJSON(); };" % (otherUnionType, type),
217 False,
218 "union containing only JSON types (%s or %s) should be a JSON type"
219 % (otherUnionType, type),
222 doTest(
223 "interface test { %s? toJSON(); };" % type,
224 False,
225 "Nullable type (%s) should be a JSON type" % type,
228 doTest(
229 "interface Foo : InterfaceWithoutToJSON { %s toJSON(); };"
230 "interface Test { Foo toJSON(); };" % type,
231 False,
232 "interface with toJSON should be a JSON type",
235 doTest(
236 "interface Foo : InterfaceWithToJSON { };" "interface Test { Foo toJSON(); };",
237 False,
238 "inherited interface with toJSON should be a JSON type",
241 for type in nonJsonTypes:
242 doTest(
243 "interface Test { %s toJSON(); };" % type,
244 True,
245 "%s should not be a JSON type" % type,
248 doTest(
249 "interface Test { sequence<%s> toJSON(); };" % type,
250 True,
251 "sequence<%s> should not be a JSON type" % type,
254 doTest(
255 "dictionary Foo { %s foo; }; " "interface Test { Foo toJSON(); }; " % type,
256 True,
257 "Dictionary containing a non-JSON type (%s) should not be a JSON type"
258 % type,
261 doTest(
262 "dictionary Foo { %s foo; }; dictionary Bar : Foo { }; "
263 "interface Test { Bar toJSON(); }; " % type,
264 True,
265 "dictionary whose ancestors only contain non-JSON types should not be a JSON type",
268 doTest(
269 "interface Test { record<DOMString, %s> toJSON(); };" % type,
270 True,
271 "record<DOMString, %s> should not be a JSON type" % type,
274 doTest(
275 "interface Test { record<ByteString, %s> toJSON(); };" % type,
276 True,
277 "record<ByteString, %s> should not be a JSON type" % type,
280 doTest(
281 "interface Test { record<USVString, %s> toJSON(); };" % type,
282 True,
283 "record<USVString, %s> should not be a JSON type" % type,
286 if type != "any":
287 doTest(
288 "interface Foo { object toJSON(); }; "
289 "interface Test { (Foo or %s) toJSON(); };" % type,
290 True,
291 "union containing a non-JSON type (%s) should not be a JSON type"
292 % type,
295 doTest(
296 "interface test { %s? toJSON(); };" % type,
297 True,
298 "Nullable type (%s) should not be a JSON type" % type,
301 doTest(
302 "dictionary Foo { long foo; any bar; };" "interface Test { Foo toJSON(); };",
303 True,
304 "dictionary containing a non-JSON type should not be a JSON type",
307 doTest(
308 "interface Foo : InterfaceWithoutToJSON { }; "
309 "interface Test { Foo toJSON(); };",
310 True,
311 "interface without toJSON should not be a JSON type",