test_toJSON.py (8680B)
1 import WebIDL 2 3 4 def WebIDLTest(parser, harness): 5 threw = False 6 try: 7 parser.parse( 8 """ 9 interface Test { 10 object toJSON(); 11 }; 12 """ 13 ) 14 parser.finish() 15 except WebIDL.WebIDLError: 16 threw = True 17 harness.ok(not threw, "Should allow a toJSON method.") 18 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); 27 }; 28 """ 29 ) 30 parser.finish() 31 except WebIDL.WebIDLError: 32 threw = True 33 harness.ok(threw, "Should not allow overloads of a toJSON method.") 34 35 parser = parser.reset() 36 threw = False 37 try: 38 parser.parse( 39 """ 40 interface Test { 41 object toJSON(object arg); 42 }; 43 """ 44 ) 45 parser.finish() 46 except WebIDL.WebIDLError: 47 threw = True 48 harness.ok(threw, "Should not allow a toJSON method with arguments.") 49 50 parser = parser.reset() 51 threw = False 52 try: 53 parser.parse( 54 """ 55 interface Test { 56 long toJSON(); 57 }; 58 """ 59 ) 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.") 64 65 parser = parser.reset() 66 threw = False 67 try: 68 parser.parse( 69 """ 70 interface Test { 71 [Default] object toJSON(); 72 }; 73 """ 74 ) 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." 80 ) 81 82 parser = parser.reset() 83 threw = False 84 try: 85 parser.parse( 86 """ 87 interface Test { 88 [Default] long toJSON(); 89 }; 90 """ 91 ) 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.", 98 ) 99 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", 121 ] 122 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", 136 ] 137 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 {}; 148 """ 149 ) 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) 155 156 for type in JsonTypes: 157 doTest( 158 "interface Test { %s toJSON(); };" % type, 159 False, 160 "%s should be a JSON type" % type, 161 ) 162 163 doTest( 164 "interface Test { sequence<%s> toJSON(); };" % type, 165 False, 166 "sequence<%s> should be a JSON type" % type, 167 ) 168 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, 173 ) 174 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", 180 ) 181 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", 187 ) 188 189 doTest( 190 "interface Test { record<DOMString, %s> toJSON(); };" % type, 191 False, 192 "record<DOMString, %s> should be a JSON type" % type, 193 ) 194 195 doTest( 196 "interface Test { record<ByteString, %s> toJSON(); };" % type, 197 False, 198 "record<ByteString, %s> should be a JSON type" % type, 199 ) 200 201 doTest( 202 "interface Test { record<UTF8String, %s> toJSON(); };" % type, 203 False, 204 "record<UTF8String, %s> should be a JSON type" % type, 205 ) 206 207 doTest( 208 "interface Test { record<USVString, %s> toJSON(); };" % type, 209 False, 210 "record<USVString, %s> should be a JSON type" % type, 211 ) 212 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), 220 ) 221 222 doTest( 223 "interface test { %s? toJSON(); };" % type, 224 False, 225 "Nullable type (%s) should be a JSON type" % type, 226 ) 227 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", 233 ) 234 235 doTest( 236 "interface Foo : InterfaceWithToJSON { };interface Test { Foo toJSON(); };", 237 False, 238 "inherited interface with toJSON should be a JSON type", 239 ) 240 241 for type in nonJsonTypes: 242 doTest( 243 "interface Test { %s toJSON(); };" % type, 244 True, 245 "%s should not be a JSON type" % type, 246 ) 247 248 doTest( 249 "interface Test { sequence<%s> toJSON(); };" % type, 250 True, 251 "sequence<%s> should not be a JSON type" % type, 252 ) 253 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, 259 ) 260 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", 266 ) 267 268 doTest( 269 "interface Test { record<DOMString, %s> toJSON(); };" % type, 270 True, 271 "record<DOMString, %s> should not be a JSON type" % type, 272 ) 273 274 doTest( 275 "interface Test { record<ByteString, %s> toJSON(); };" % type, 276 True, 277 "record<ByteString, %s> should not be a JSON type" % type, 278 ) 279 280 doTest( 281 "interface Test { record<USVString, %s> toJSON(); };" % type, 282 True, 283 "record<USVString, %s> should not be a JSON type" % type, 284 ) 285 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, 293 ) 294 295 doTest( 296 "interface test { %s? toJSON(); };" % type, 297 True, 298 "Nullable type (%s) should not be a JSON type" % type, 299 ) 300 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", 305 ) 306 307 doTest( 308 "interface Foo : InterfaceWithoutToJSON { }; interface Test { Foo toJSON(); };", 309 True, 310 "interface without toJSON should not be a JSON type", 311 )