1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/bindings/parser/tests/test_attr.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,341 @@ 1.4 +import WebIDL 1.5 + 1.6 +def WebIDLTest(parser, harness): 1.7 + testData = [("::TestAttr%s::b", "b", "Byte%s", False), 1.8 + ("::TestAttr%s::rb", "rb", "Byte%s", True), 1.9 + ("::TestAttr%s::o", "o", "Octet%s", False), 1.10 + ("::TestAttr%s::ro", "ro", "Octet%s", True), 1.11 + ("::TestAttr%s::s", "s", "Short%s", False), 1.12 + ("::TestAttr%s::rs", "rs", "Short%s", True), 1.13 + ("::TestAttr%s::us", "us", "UnsignedShort%s", False), 1.14 + ("::TestAttr%s::rus", "rus", "UnsignedShort%s", True), 1.15 + ("::TestAttr%s::l", "l", "Long%s", False), 1.16 + ("::TestAttr%s::rl", "rl", "Long%s", True), 1.17 + ("::TestAttr%s::ul", "ul", "UnsignedLong%s", False), 1.18 + ("::TestAttr%s::rul", "rul", "UnsignedLong%s", True), 1.19 + ("::TestAttr%s::ll", "ll", "LongLong%s", False), 1.20 + ("::TestAttr%s::rll", "rll", "LongLong%s", True), 1.21 + ("::TestAttr%s::ull", "ull", "UnsignedLongLong%s", False), 1.22 + ("::TestAttr%s::rull", "rull", "UnsignedLongLong%s", True), 1.23 + ("::TestAttr%s::str", "str", "String%s", False), 1.24 + ("::TestAttr%s::rstr", "rstr", "String%s", True), 1.25 + ("::TestAttr%s::obj", "obj", "Object%s", False), 1.26 + ("::TestAttr%s::robj", "robj", "Object%s", True), 1.27 + ("::TestAttr%s::object", "object", "Object%s", False), 1.28 + ("::TestAttr%s::f", "f", "Float%s", False), 1.29 + ("::TestAttr%s::rf", "rf", "Float%s", True)] 1.30 + 1.31 + parser.parse(""" 1.32 + interface TestAttr { 1.33 + attribute byte b; 1.34 + readonly attribute byte rb; 1.35 + attribute octet o; 1.36 + readonly attribute octet ro; 1.37 + attribute short s; 1.38 + readonly attribute short rs; 1.39 + attribute unsigned short us; 1.40 + readonly attribute unsigned short rus; 1.41 + attribute long l; 1.42 + readonly attribute long rl; 1.43 + attribute unsigned long ul; 1.44 + readonly attribute unsigned long rul; 1.45 + attribute long long ll; 1.46 + readonly attribute long long rll; 1.47 + attribute unsigned long long ull; 1.48 + readonly attribute unsigned long long rull; 1.49 + attribute DOMString str; 1.50 + readonly attribute DOMString rstr; 1.51 + attribute object obj; 1.52 + readonly attribute object robj; 1.53 + attribute object _object; 1.54 + attribute float f; 1.55 + readonly attribute float rf; 1.56 + }; 1.57 + 1.58 + interface TestAttrNullable { 1.59 + attribute byte? b; 1.60 + readonly attribute byte? rb; 1.61 + attribute octet? o; 1.62 + readonly attribute octet? ro; 1.63 + attribute short? s; 1.64 + readonly attribute short? rs; 1.65 + attribute unsigned short? us; 1.66 + readonly attribute unsigned short? rus; 1.67 + attribute long? l; 1.68 + readonly attribute long? rl; 1.69 + attribute unsigned long? ul; 1.70 + readonly attribute unsigned long? rul; 1.71 + attribute long long? ll; 1.72 + readonly attribute long long? rll; 1.73 + attribute unsigned long long? ull; 1.74 + readonly attribute unsigned long long? rull; 1.75 + attribute DOMString? str; 1.76 + readonly attribute DOMString? rstr; 1.77 + attribute object? obj; 1.78 + readonly attribute object? robj; 1.79 + attribute object? _object; 1.80 + attribute float? f; 1.81 + readonly attribute float? rf; 1.82 + }; 1.83 + 1.84 + interface TestAttrArray { 1.85 + attribute byte[] b; 1.86 + readonly attribute byte[] rb; 1.87 + attribute octet[] o; 1.88 + readonly attribute octet[] ro; 1.89 + attribute short[] s; 1.90 + readonly attribute short[] rs; 1.91 + attribute unsigned short[] us; 1.92 + readonly attribute unsigned short[] rus; 1.93 + attribute long[] l; 1.94 + readonly attribute long[] rl; 1.95 + attribute unsigned long[] ul; 1.96 + readonly attribute unsigned long[] rul; 1.97 + attribute long long[] ll; 1.98 + readonly attribute long long[] rll; 1.99 + attribute unsigned long long[] ull; 1.100 + readonly attribute unsigned long long[] rull; 1.101 + attribute DOMString[] str; 1.102 + readonly attribute DOMString[] rstr; 1.103 + attribute object[] obj; 1.104 + readonly attribute object[] robj; 1.105 + attribute object[] _object; 1.106 + attribute float[] f; 1.107 + readonly attribute float[] rf; 1.108 + }; 1.109 + 1.110 + interface TestAttrNullableArray { 1.111 + attribute byte[]? b; 1.112 + readonly attribute byte[]? rb; 1.113 + attribute octet[]? o; 1.114 + readonly attribute octet[]? ro; 1.115 + attribute short[]? s; 1.116 + readonly attribute short[]? rs; 1.117 + attribute unsigned short[]? us; 1.118 + readonly attribute unsigned short[]? rus; 1.119 + attribute long[]? l; 1.120 + readonly attribute long[]? rl; 1.121 + attribute unsigned long[]? ul; 1.122 + readonly attribute unsigned long[]? rul; 1.123 + attribute long long[]? ll; 1.124 + readonly attribute long long[]? rll; 1.125 + attribute unsigned long long[]? ull; 1.126 + readonly attribute unsigned long long[]? rull; 1.127 + attribute DOMString[]? str; 1.128 + readonly attribute DOMString[]? rstr; 1.129 + attribute object[]? obj; 1.130 + readonly attribute object[]? robj; 1.131 + attribute object[]? _object; 1.132 + attribute float[]? f; 1.133 + readonly attribute float[]? rf; 1.134 + }; 1.135 + 1.136 + interface TestAttrArrayOfNullableTypes { 1.137 + attribute byte?[] b; 1.138 + readonly attribute byte?[] rb; 1.139 + attribute octet?[] o; 1.140 + readonly attribute octet?[] ro; 1.141 + attribute short?[] s; 1.142 + readonly attribute short?[] rs; 1.143 + attribute unsigned short?[] us; 1.144 + readonly attribute unsigned short?[] rus; 1.145 + attribute long?[] l; 1.146 + readonly attribute long?[] rl; 1.147 + attribute unsigned long?[] ul; 1.148 + readonly attribute unsigned long?[] rul; 1.149 + attribute long long?[] ll; 1.150 + readonly attribute long long?[] rll; 1.151 + attribute unsigned long long?[] ull; 1.152 + readonly attribute unsigned long long?[] rull; 1.153 + attribute DOMString?[] str; 1.154 + readonly attribute DOMString?[] rstr; 1.155 + attribute object?[] obj; 1.156 + readonly attribute object?[] robj; 1.157 + attribute object?[] _object; 1.158 + attribute float?[] f; 1.159 + readonly attribute float?[] rf; 1.160 + }; 1.161 + 1.162 + interface TestAttrNullableArrayOfNullableTypes { 1.163 + attribute byte?[]? b; 1.164 + readonly attribute byte?[]? rb; 1.165 + attribute octet?[]? o; 1.166 + readonly attribute octet?[]? ro; 1.167 + attribute short?[]? s; 1.168 + readonly attribute short?[]? rs; 1.169 + attribute unsigned short?[]? us; 1.170 + readonly attribute unsigned short?[]? rus; 1.171 + attribute long?[]? l; 1.172 + readonly attribute long?[]? rl; 1.173 + attribute unsigned long?[]? ul; 1.174 + readonly attribute unsigned long?[]? rul; 1.175 + attribute long long?[]? ll; 1.176 + readonly attribute long long?[]? rll; 1.177 + attribute unsigned long long?[]? ull; 1.178 + readonly attribute unsigned long long?[]? rull; 1.179 + attribute DOMString?[]? str; 1.180 + readonly attribute DOMString?[]? rstr; 1.181 + attribute object?[]? obj; 1.182 + readonly attribute object?[]? robj; 1.183 + attribute object?[]? _object; 1.184 + attribute float?[]? f; 1.185 + readonly attribute float?[]? rf; 1.186 + }; 1.187 + """) 1.188 + 1.189 + results = parser.finish() 1.190 + 1.191 + def checkAttr(attr, QName, name, type, readonly): 1.192 + harness.ok(isinstance(attr, WebIDL.IDLAttribute), 1.193 + "Should be an IDLAttribute") 1.194 + harness.ok(attr.isAttr(), "Attr is an Attr") 1.195 + harness.ok(not attr.isMethod(), "Attr is not an method") 1.196 + harness.ok(not attr.isConst(), "Attr is not a const") 1.197 + harness.check(attr.identifier.QName(), QName, "Attr has the right QName") 1.198 + harness.check(attr.identifier.name, name, "Attr has the right name") 1.199 + harness.check(str(attr.type), type, "Attr has the right type") 1.200 + harness.check(attr.readonly, readonly, "Attr's readonly state is correct") 1.201 + 1.202 + harness.ok(True, "TestAttr interface parsed without error.") 1.203 + harness.check(len(results), 6, "Should be six productions.") 1.204 + iface = results[0] 1.205 + harness.ok(isinstance(iface, WebIDL.IDLInterface), 1.206 + "Should be an IDLInterface") 1.207 + harness.check(iface.identifier.QName(), "::TestAttr", "Interface has the right QName") 1.208 + harness.check(iface.identifier.name, "TestAttr", "Interface has the right name") 1.209 + harness.check(len(iface.members), len(testData), "Expect %s members" % len(testData)) 1.210 + 1.211 + attrs = iface.members 1.212 + 1.213 + for i in range(len(attrs)): 1.214 + data = testData[i] 1.215 + attr = attrs[i] 1.216 + (QName, name, type, readonly) = data 1.217 + checkAttr(attr, QName % "", name, type % "", readonly) 1.218 + 1.219 + iface = results[1] 1.220 + harness.ok(isinstance(iface, WebIDL.IDLInterface), 1.221 + "Should be an IDLInterface") 1.222 + harness.check(iface.identifier.QName(), "::TestAttrNullable", "Interface has the right QName") 1.223 + harness.check(iface.identifier.name, "TestAttrNullable", "Interface has the right name") 1.224 + harness.check(len(iface.members), len(testData), "Expect %s members" % len(testData)) 1.225 + 1.226 + attrs = iface.members 1.227 + 1.228 + for i in range(len(attrs)): 1.229 + data = testData[i] 1.230 + attr = attrs[i] 1.231 + (QName, name, type, readonly) = data 1.232 + checkAttr(attr, QName % "Nullable", name, type % "OrNull", readonly) 1.233 + 1.234 + iface = results[2] 1.235 + harness.ok(isinstance(iface, WebIDL.IDLInterface), 1.236 + "Should be an IDLInterface") 1.237 + harness.check(iface.identifier.QName(), "::TestAttrArray", "Interface has the right QName") 1.238 + harness.check(iface.identifier.name, "TestAttrArray", "Interface has the right name") 1.239 + harness.check(len(iface.members), len(testData), "Expect %s members" % len(testData)) 1.240 + 1.241 + attrs = iface.members 1.242 + 1.243 + for i in range(len(attrs)): 1.244 + data = testData[i] 1.245 + attr = attrs[i] 1.246 + (QName, name, type, readonly) = data 1.247 + checkAttr(attr, QName % "Array", name, type % "Array", readonly) 1.248 + 1.249 + iface = results[3] 1.250 + harness.ok(isinstance(iface, WebIDL.IDLInterface), 1.251 + "Should be an IDLInterface") 1.252 + harness.check(iface.identifier.QName(), "::TestAttrNullableArray", "Interface has the right QName") 1.253 + harness.check(iface.identifier.name, "TestAttrNullableArray", "Interface has the right name") 1.254 + harness.check(len(iface.members), len(testData), "Expect %s members" % len(testData)) 1.255 + 1.256 + attrs = iface.members 1.257 + 1.258 + for i in range(len(attrs)): 1.259 + data = testData[i] 1.260 + attr = attrs[i] 1.261 + (QName, name, type, readonly) = data 1.262 + checkAttr(attr, QName % "NullableArray", name, type % "ArrayOrNull", readonly) 1.263 + 1.264 + iface = results[4] 1.265 + harness.ok(isinstance(iface, WebIDL.IDLInterface), 1.266 + "Should be an IDLInterface") 1.267 + harness.check(iface.identifier.QName(), "::TestAttrArrayOfNullableTypes", "Interface has the right QName") 1.268 + harness.check(iface.identifier.name, "TestAttrArrayOfNullableTypes", "Interface has the right name") 1.269 + harness.check(len(iface.members), len(testData), "Expect %s members" % len(testData)) 1.270 + 1.271 + attrs = iface.members 1.272 + 1.273 + for i in range(len(attrs)): 1.274 + data = testData[i] 1.275 + attr = attrs[i] 1.276 + (QName, name, type, readonly) = data 1.277 + checkAttr(attr, QName % "ArrayOfNullableTypes", name, type % "OrNullArray", readonly) 1.278 + 1.279 + iface = results[5] 1.280 + harness.ok(isinstance(iface, WebIDL.IDLInterface), 1.281 + "Should be an IDLInterface") 1.282 + harness.check(iface.identifier.QName(), "::TestAttrNullableArrayOfNullableTypes", "Interface has the right QName") 1.283 + harness.check(iface.identifier.name, "TestAttrNullableArrayOfNullableTypes", "Interface has the right name") 1.284 + harness.check(len(iface.members), len(testData), "Expect %s members" % len(testData)) 1.285 + 1.286 + attrs = iface.members 1.287 + 1.288 + for i in range(len(attrs)): 1.289 + data = testData[i] 1.290 + attr = attrs[i] 1.291 + (QName, name, type, readonly) = data 1.292 + checkAttr(attr, QName % "NullableArrayOfNullableTypes", name, type % "OrNullArrayOrNull", readonly) 1.293 + 1.294 + parser = parser.reset() 1.295 + threw = False 1.296 + try: 1.297 + parser.parse(""" 1.298 + interface A { 1.299 + [SetterThrows] readonly attribute boolean foo; 1.300 + }; 1.301 + """) 1.302 + results = parser.finish() 1.303 + except Exception, x: 1.304 + threw = True 1.305 + harness.ok(threw, "Should not allow [SetterThrows] on readonly attributes") 1.306 + 1.307 + parser = parser.reset() 1.308 + threw = False 1.309 + try: 1.310 + parser.parse(""" 1.311 + interface A { 1.312 + [Throw] readonly attribute boolean foo; 1.313 + }; 1.314 + """) 1.315 + results = parser.finish() 1.316 + except Exception, x: 1.317 + threw = True 1.318 + harness.ok(threw, "Should spell [Throws] correctly") 1.319 + 1.320 + parser = parser.reset() 1.321 + threw = False 1.322 + try: 1.323 + parser.parse(""" 1.324 + interface A { 1.325 + [SameObject] readonly attribute boolean foo; 1.326 + }; 1.327 + """) 1.328 + results = parser.finish() 1.329 + except Exception, x: 1.330 + threw = True 1.331 + harness.ok(threw, "Should not allow [SameObject] on attributes not of interface type") 1.332 + 1.333 + parser = parser.reset() 1.334 + threw = False 1.335 + try: 1.336 + parser.parse(""" 1.337 + interface A { 1.338 + [SameObject] readonly attribute A foo; 1.339 + }; 1.340 + """) 1.341 + results = parser.finish() 1.342 + except Exception, x: 1.343 + threw = True 1.344 + harness.ok(not threw, "Should allow [SameObject] on attributes of interface type")