1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
import WebIDL
def WebIDLTest(parser, harness):
threw = False
try:
parser.parse(
"""
interface Test {
object toJSON();
};
"""
)
parser.finish()
except WebIDL.WebIDLError:
threw = True
harness.ok(not threw, "Should allow a toJSON method.")
parser = parser.reset()
threw = False
try:
parser.parse(
"""
interface Test {
object toJSON(object arg);
object toJSON(long arg);
};
"""
)
parser.finish()
except WebIDL.WebIDLError:
threw = True
harness.ok(threw, "Should not allow overloads of a toJSON method.")
parser = parser.reset()
threw = False
try:
parser.parse(
"""
interface Test {
object toJSON(object arg);
};
"""
)
parser.finish()
except WebIDL.WebIDLError:
threw = True
harness.ok(threw, "Should not allow a toJSON method with arguments.")
parser = parser.reset()
threw = False
try:
parser.parse(
"""
interface Test {
long toJSON();
};
"""
)
parser.finish()
except WebIDL.WebIDLError:
threw = True
harness.ok(not threw, "Should allow a toJSON method with 'long' as return type.")
parser = parser.reset()
threw = False
try:
parser.parse(
"""
interface Test {
[Default] object toJSON();
};
"""
)
parser.finish()
except WebIDL.WebIDLError:
threw = True
harness.ok(
not threw, "Should allow a default toJSON method with 'object' as return type."
)
parser = parser.reset()
threw = False
try:
parser.parse(
"""
interface Test {
[Default] long toJSON();
};
"""
)
parser.finish()
except WebIDL.WebIDLError:
threw = True
harness.ok(
threw,
"Should not allow a default toJSON method with non-'object' as return type.",
)
JsonTypes = [
"byte",
"octet",
"short",
"unsigned short",
"long",
"unsigned long",
"long long",
"unsigned long long",
"float",
"unrestricted float",
"double",
"unrestricted double",
"boolean",
"DOMString",
"ByteString",
"UTF8String",
"USVString",
"Enum",
"InterfaceWithToJSON",
"object",
]
nonJsonTypes = [
"InterfaceWithoutToJSON",
"any",
"Int8Array",
"Int16Array",
"Int32Array",
"Uint8Array",
"Uint16Array",
"Uint32Array",
"Uint8ClampedArray",
"Float32Array",
"Float64Array",
"ArrayBuffer",
]
def doTest(testIDL, shouldThrow, description):
p = parser.reset()
threw = False
try:
p.parse(
testIDL
+ """
enum Enum { "a", "b", "c" };
interface InterfaceWithToJSON { long toJSON(); };
interface InterfaceWithoutToJSON {};
"""
)
p.finish()
except Exception as x:
threw = True
harness.ok(x.message == "toJSON method has non-JSON return type", x)
harness.check(threw, shouldThrow, description)
for type in JsonTypes:
doTest(
"interface Test { %s toJSON(); };" % type,
False,
"%s should be a JSON type" % type,
)
doTest(
"interface Test { sequence<%s> toJSON(); };" % type,
False,
"sequence<%s> should be a JSON type" % type,
)
doTest(
"dictionary Foo { %s foo; }; " "interface Test { Foo toJSON(); }; " % type,
False,
"dictionary containing only JSON type (%s) should be a JSON type" % type,
)
doTest(
"dictionary Foo { %s foo; }; dictionary Bar : Foo { }; "
"interface Test { Bar toJSON(); }; " % type,
False,
"dictionary whose ancestors only contain JSON types should be a JSON type",
)
doTest(
"dictionary Foo { any foo; }; dictionary Bar : Foo { %s bar; };"
"interface Test { Bar toJSON(); };" % type,
True,
"dictionary whose ancestors contain non-JSON types should not be a JSON type",
)
doTest(
"interface Test { record<DOMString, %s> toJSON(); };" % type,
False,
"record<DOMString, %s> should be a JSON type" % type,
)
doTest(
"interface Test { record<ByteString, %s> toJSON(); };" % type,
False,
"record<ByteString, %s> should be a JSON type" % type,
)
doTest(
"interface Test { record<UTF8String, %s> toJSON(); };" % type,
False,
"record<UTF8String, %s> should be a JSON type" % type,
)
doTest(
"interface Test { record<USVString, %s> toJSON(); };" % type,
False,
"record<USVString, %s> should be a JSON type" % type,
)
otherUnionType = "Foo" if type != "object" else "long"
doTest(
"interface Foo { object toJSON(); };"
"interface Test { (%s or %s) toJSON(); };" % (otherUnionType, type),
False,
"union containing only JSON types (%s or %s) should be a JSON type"
% (otherUnionType, type),
)
doTest(
"interface test { %s? toJSON(); };" % type,
False,
"Nullable type (%s) should be a JSON type" % type,
)
doTest(
"interface Foo : InterfaceWithoutToJSON { %s toJSON(); };"
"interface Test { Foo toJSON(); };" % type,
False,
"interface with toJSON should be a JSON type",
)
doTest(
"interface Foo : InterfaceWithToJSON { };" "interface Test { Foo toJSON(); };",
False,
"inherited interface with toJSON should be a JSON type",
)
for type in nonJsonTypes:
doTest(
"interface Test { %s toJSON(); };" % type,
True,
"%s should not be a JSON type" % type,
)
doTest(
"interface Test { sequence<%s> toJSON(); };" % type,
True,
"sequence<%s> should not be a JSON type" % type,
)
doTest(
"dictionary Foo { %s foo; }; " "interface Test { Foo toJSON(); }; " % type,
True,
"Dictionary containing a non-JSON type (%s) should not be a JSON type"
% type,
)
doTest(
"dictionary Foo { %s foo; }; dictionary Bar : Foo { }; "
"interface Test { Bar toJSON(); }; " % type,
True,
"dictionary whose ancestors only contain non-JSON types should not be a JSON type",
)
doTest(
"interface Test { record<DOMString, %s> toJSON(); };" % type,
True,
"record<DOMString, %s> should not be a JSON type" % type,
)
doTest(
"interface Test { record<ByteString, %s> toJSON(); };" % type,
True,
"record<ByteString, %s> should not be a JSON type" % type,
)
doTest(
"interface Test { record<USVString, %s> toJSON(); };" % type,
True,
"record<USVString, %s> should not be a JSON type" % type,
)
if type != "any":
doTest(
"interface Foo { object toJSON(); }; "
"interface Test { (Foo or %s) toJSON(); };" % type,
True,
"union containing a non-JSON type (%s) should not be a JSON type"
% type,
)
doTest(
"interface test { %s? toJSON(); };" % type,
True,
"Nullable type (%s) should not be a JSON type" % type,
)
doTest(
"dictionary Foo { long foo; any bar; };" "interface Test { Foo toJSON(); };",
True,
"dictionary containing a non-JSON type should not be a JSON type",
)
doTest(
"interface Foo : InterfaceWithoutToJSON { }; "
"interface Test { Foo toJSON(); };",
True,
"interface without toJSON should not be a JSON type",
)
|