michael@0: import WebIDL michael@0: michael@0: def WebIDLTest(parser, harness): michael@0: parser.parse(""" michael@0: interface TestMethods { michael@0: void basic(); michael@0: static void basicStatic(); michael@0: void basicWithSimpleArgs(boolean arg1, byte arg2, unsigned long arg3); michael@0: boolean basicBoolean(); michael@0: static boolean basicStaticBoolean(); michael@0: boolean basicBooleanWithSimpleArgs(boolean arg1, byte arg2, unsigned long arg3); michael@0: void optionalArg(optional byte? arg1, optional sequence arg2); michael@0: void variadicArg(byte?... arg1); michael@0: void crazyTypes(sequence? arg1, boolean?[][]? arg2); michael@0: object getObject(); michael@0: void setObject(object arg1); michael@0: void setAny(any arg1); michael@0: float doFloats(float arg1); michael@0: }; michael@0: """) michael@0: michael@0: results = parser.finish() michael@0: michael@0: harness.ok(True, "TestMethods interface parsed without error.") michael@0: harness.check(len(results), 1, "Should be one production.") michael@0: iface = results[0] michael@0: harness.ok(isinstance(iface, WebIDL.IDLInterface), michael@0: "Should be an IDLInterface") michael@0: harness.check(iface.identifier.QName(), "::TestMethods", "Interface has the right QName") michael@0: harness.check(iface.identifier.name, "TestMethods", "Interface has the right name") michael@0: harness.check(len(iface.members), 13, "Expect 13 members") michael@0: michael@0: methods = iface.members michael@0: michael@0: def checkArgument(argument, QName, name, type, optional, variadic): michael@0: harness.ok(isinstance(argument, WebIDL.IDLArgument), michael@0: "Should be an IDLArgument") michael@0: harness.check(argument.identifier.QName(), QName, "Argument has the right QName") michael@0: harness.check(argument.identifier.name, name, "Argument has the right name") michael@0: harness.check(str(argument.type), type, "Argument has the right return type") michael@0: harness.check(argument.optional, optional, "Argument has the right optional value") michael@0: harness.check(argument.variadic, variadic, "Argument has the right variadic value") michael@0: michael@0: def checkMethod(method, QName, name, signatures, michael@0: static=False, getter=False, setter=False, creator=False, michael@0: deleter=False, legacycaller=False, stringifier=False): michael@0: harness.ok(isinstance(method, WebIDL.IDLMethod), michael@0: "Should be an IDLMethod") michael@0: harness.ok(method.isMethod(), "Method is a method") michael@0: harness.ok(not method.isAttr(), "Method is not an attr") michael@0: harness.ok(not method.isConst(), "Method is not a const") michael@0: harness.check(method.identifier.QName(), QName, "Method has the right QName") michael@0: harness.check(method.identifier.name, name, "Method has the right name") michael@0: harness.check(method.isStatic(), static, "Method has the correct static value") michael@0: harness.check(method.isGetter(), getter, "Method has the correct getter value") michael@0: harness.check(method.isSetter(), setter, "Method has the correct setter value") michael@0: harness.check(method.isCreator(), creator, "Method has the correct creator value") michael@0: harness.check(method.isDeleter(), deleter, "Method has the correct deleter value") michael@0: harness.check(method.isLegacycaller(), legacycaller, "Method has the correct legacycaller value") michael@0: harness.check(method.isStringifier(), stringifier, "Method has the correct stringifier value") michael@0: harness.check(len(method.signatures()), len(signatures), "Method has the correct number of signatures") michael@0: michael@0: sigpairs = zip(method.signatures(), signatures) michael@0: for (gotSignature, expectedSignature) in sigpairs: michael@0: (gotRetType, gotArgs) = gotSignature michael@0: (expectedRetType, expectedArgs) = expectedSignature michael@0: michael@0: harness.check(str(gotRetType), expectedRetType, michael@0: "Method has the expected return type.") michael@0: michael@0: for i in range(0, len(gotArgs)): michael@0: (QName, name, type, optional, variadic) = expectedArgs[i] michael@0: checkArgument(gotArgs[i], QName, name, type, optional, variadic) michael@0: michael@0: checkMethod(methods[0], "::TestMethods::basic", "basic", [("Void", [])]) michael@0: checkMethod(methods[1], "::TestMethods::basicStatic", "basicStatic", michael@0: [("Void", [])], static=True) michael@0: checkMethod(methods[2], "::TestMethods::basicWithSimpleArgs", michael@0: "basicWithSimpleArgs", michael@0: [("Void", michael@0: [("::TestMethods::basicWithSimpleArgs::arg1", "arg1", "Boolean", False, False), michael@0: ("::TestMethods::basicWithSimpleArgs::arg2", "arg2", "Byte", False, False), michael@0: ("::TestMethods::basicWithSimpleArgs::arg3", "arg3", "UnsignedLong", False, False)])]) michael@0: checkMethod(methods[3], "::TestMethods::basicBoolean", "basicBoolean", [("Boolean", [])]) michael@0: checkMethod(methods[4], "::TestMethods::basicStaticBoolean", "basicStaticBoolean", [("Boolean", [])], static=True) michael@0: checkMethod(methods[5], "::TestMethods::basicBooleanWithSimpleArgs", michael@0: "basicBooleanWithSimpleArgs", michael@0: [("Boolean", michael@0: [("::TestMethods::basicBooleanWithSimpleArgs::arg1", "arg1", "Boolean", False, False), michael@0: ("::TestMethods::basicBooleanWithSimpleArgs::arg2", "arg2", "Byte", False, False), michael@0: ("::TestMethods::basicBooleanWithSimpleArgs::arg3", "arg3", "UnsignedLong", False, False)])]) michael@0: checkMethod(methods[6], "::TestMethods::optionalArg", michael@0: "optionalArg", michael@0: [("Void", michael@0: [("::TestMethods::optionalArg::arg1", "arg1", "ByteOrNull", True, False), michael@0: ("::TestMethods::optionalArg::arg2", "arg2", "ByteSequence", True, False)])]) michael@0: checkMethod(methods[7], "::TestMethods::variadicArg", michael@0: "variadicArg", michael@0: [("Void", michael@0: [("::TestMethods::variadicArg::arg1", "arg1", "ByteOrNull", True, True)])]) michael@0: checkMethod(methods[8], "::TestMethods::crazyTypes", michael@0: "crazyTypes", michael@0: [("Void", michael@0: [("::TestMethods::crazyTypes::arg1", "arg1", "LongOrNullArraySequenceOrNull", False, False), michael@0: ("::TestMethods::crazyTypes::arg2", "arg2", "BooleanOrNullArrayArrayOrNull", False, False)])]) michael@0: checkMethod(methods[9], "::TestMethods::getObject", michael@0: "getObject", [("Object", [])]) michael@0: checkMethod(methods[10], "::TestMethods::setObject", michael@0: "setObject", michael@0: [("Void", michael@0: [("::TestMethods::setObject::arg1", "arg1", "Object", False, False)])]) michael@0: checkMethod(methods[11], "::TestMethods::setAny", michael@0: "setAny", michael@0: [("Void", michael@0: [("::TestMethods::setAny::arg1", "arg1", "Any", False, False)])]) michael@0: checkMethod(methods[12], "::TestMethods::doFloats", michael@0: "doFloats", michael@0: [("Float", michael@0: [("::TestMethods::doFloats::arg1", "arg1", "Float", False, False)])]) michael@0: michael@0: parser = parser.reset() michael@0: threw = False michael@0: try: michael@0: parser.parse(""" michael@0: interface A { michael@0: void foo(optional float bar = 1); michael@0: }; michael@0: """) michael@0: results = parser.finish() michael@0: except Exception, x: michael@0: threw = True michael@0: harness.ok(not threw, "Should allow integer to float type corecion") michael@0: michael@0: parser = parser.reset() michael@0: threw = False michael@0: try: michael@0: parser.parse(""" michael@0: interface A { michael@0: [GetterThrows] void foo(); michael@0: }; michael@0: """) michael@0: results = parser.finish() michael@0: except Exception, x: michael@0: threw = True michael@0: harness.ok(threw, "Should not allow [GetterThrows] on methods") michael@0: michael@0: parser = parser.reset() michael@0: threw = False michael@0: try: michael@0: parser.parse(""" michael@0: interface A { michael@0: [SetterThrows] void foo(); michael@0: }; michael@0: """) michael@0: results = parser.finish() michael@0: except Exception, x: michael@0: threw = True michael@0: harness.ok(threw, "Should not allow [SetterThrows] on methods") michael@0: michael@0: parser = parser.reset() michael@0: threw = False michael@0: try: michael@0: parser.parse(""" michael@0: interface A { michael@0: [Throw] void foo(); michael@0: }; michael@0: """) michael@0: results = parser.finish() michael@0: except Exception, x: michael@0: threw = True michael@0: harness.ok(threw, "Should spell [Throws] correctly on methods")