dom/bindings/parser/tests/test_method.py

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 import WebIDL
michael@0 2
michael@0 3 def WebIDLTest(parser, harness):
michael@0 4 parser.parse("""
michael@0 5 interface TestMethods {
michael@0 6 void basic();
michael@0 7 static void basicStatic();
michael@0 8 void basicWithSimpleArgs(boolean arg1, byte arg2, unsigned long arg3);
michael@0 9 boolean basicBoolean();
michael@0 10 static boolean basicStaticBoolean();
michael@0 11 boolean basicBooleanWithSimpleArgs(boolean arg1, byte arg2, unsigned long arg3);
michael@0 12 void optionalArg(optional byte? arg1, optional sequence<byte> arg2);
michael@0 13 void variadicArg(byte?... arg1);
michael@0 14 void crazyTypes(sequence<long?[]>? arg1, boolean?[][]? arg2);
michael@0 15 object getObject();
michael@0 16 void setObject(object arg1);
michael@0 17 void setAny(any arg1);
michael@0 18 float doFloats(float arg1);
michael@0 19 };
michael@0 20 """)
michael@0 21
michael@0 22 results = parser.finish()
michael@0 23
michael@0 24 harness.ok(True, "TestMethods interface parsed without error.")
michael@0 25 harness.check(len(results), 1, "Should be one production.")
michael@0 26 iface = results[0]
michael@0 27 harness.ok(isinstance(iface, WebIDL.IDLInterface),
michael@0 28 "Should be an IDLInterface")
michael@0 29 harness.check(iface.identifier.QName(), "::TestMethods", "Interface has the right QName")
michael@0 30 harness.check(iface.identifier.name, "TestMethods", "Interface has the right name")
michael@0 31 harness.check(len(iface.members), 13, "Expect 13 members")
michael@0 32
michael@0 33 methods = iface.members
michael@0 34
michael@0 35 def checkArgument(argument, QName, name, type, optional, variadic):
michael@0 36 harness.ok(isinstance(argument, WebIDL.IDLArgument),
michael@0 37 "Should be an IDLArgument")
michael@0 38 harness.check(argument.identifier.QName(), QName, "Argument has the right QName")
michael@0 39 harness.check(argument.identifier.name, name, "Argument has the right name")
michael@0 40 harness.check(str(argument.type), type, "Argument has the right return type")
michael@0 41 harness.check(argument.optional, optional, "Argument has the right optional value")
michael@0 42 harness.check(argument.variadic, variadic, "Argument has the right variadic value")
michael@0 43
michael@0 44 def checkMethod(method, QName, name, signatures,
michael@0 45 static=False, getter=False, setter=False, creator=False,
michael@0 46 deleter=False, legacycaller=False, stringifier=False):
michael@0 47 harness.ok(isinstance(method, WebIDL.IDLMethod),
michael@0 48 "Should be an IDLMethod")
michael@0 49 harness.ok(method.isMethod(), "Method is a method")
michael@0 50 harness.ok(not method.isAttr(), "Method is not an attr")
michael@0 51 harness.ok(not method.isConst(), "Method is not a const")
michael@0 52 harness.check(method.identifier.QName(), QName, "Method has the right QName")
michael@0 53 harness.check(method.identifier.name, name, "Method has the right name")
michael@0 54 harness.check(method.isStatic(), static, "Method has the correct static value")
michael@0 55 harness.check(method.isGetter(), getter, "Method has the correct getter value")
michael@0 56 harness.check(method.isSetter(), setter, "Method has the correct setter value")
michael@0 57 harness.check(method.isCreator(), creator, "Method has the correct creator value")
michael@0 58 harness.check(method.isDeleter(), deleter, "Method has the correct deleter value")
michael@0 59 harness.check(method.isLegacycaller(), legacycaller, "Method has the correct legacycaller value")
michael@0 60 harness.check(method.isStringifier(), stringifier, "Method has the correct stringifier value")
michael@0 61 harness.check(len(method.signatures()), len(signatures), "Method has the correct number of signatures")
michael@0 62
michael@0 63 sigpairs = zip(method.signatures(), signatures)
michael@0 64 for (gotSignature, expectedSignature) in sigpairs:
michael@0 65 (gotRetType, gotArgs) = gotSignature
michael@0 66 (expectedRetType, expectedArgs) = expectedSignature
michael@0 67
michael@0 68 harness.check(str(gotRetType), expectedRetType,
michael@0 69 "Method has the expected return type.")
michael@0 70
michael@0 71 for i in range(0, len(gotArgs)):
michael@0 72 (QName, name, type, optional, variadic) = expectedArgs[i]
michael@0 73 checkArgument(gotArgs[i], QName, name, type, optional, variadic)
michael@0 74
michael@0 75 checkMethod(methods[0], "::TestMethods::basic", "basic", [("Void", [])])
michael@0 76 checkMethod(methods[1], "::TestMethods::basicStatic", "basicStatic",
michael@0 77 [("Void", [])], static=True)
michael@0 78 checkMethod(methods[2], "::TestMethods::basicWithSimpleArgs",
michael@0 79 "basicWithSimpleArgs",
michael@0 80 [("Void",
michael@0 81 [("::TestMethods::basicWithSimpleArgs::arg1", "arg1", "Boolean", False, False),
michael@0 82 ("::TestMethods::basicWithSimpleArgs::arg2", "arg2", "Byte", False, False),
michael@0 83 ("::TestMethods::basicWithSimpleArgs::arg3", "arg3", "UnsignedLong", False, False)])])
michael@0 84 checkMethod(methods[3], "::TestMethods::basicBoolean", "basicBoolean", [("Boolean", [])])
michael@0 85 checkMethod(methods[4], "::TestMethods::basicStaticBoolean", "basicStaticBoolean", [("Boolean", [])], static=True)
michael@0 86 checkMethod(methods[5], "::TestMethods::basicBooleanWithSimpleArgs",
michael@0 87 "basicBooleanWithSimpleArgs",
michael@0 88 [("Boolean",
michael@0 89 [("::TestMethods::basicBooleanWithSimpleArgs::arg1", "arg1", "Boolean", False, False),
michael@0 90 ("::TestMethods::basicBooleanWithSimpleArgs::arg2", "arg2", "Byte", False, False),
michael@0 91 ("::TestMethods::basicBooleanWithSimpleArgs::arg3", "arg3", "UnsignedLong", False, False)])])
michael@0 92 checkMethod(methods[6], "::TestMethods::optionalArg",
michael@0 93 "optionalArg",
michael@0 94 [("Void",
michael@0 95 [("::TestMethods::optionalArg::arg1", "arg1", "ByteOrNull", True, False),
michael@0 96 ("::TestMethods::optionalArg::arg2", "arg2", "ByteSequence", True, False)])])
michael@0 97 checkMethod(methods[7], "::TestMethods::variadicArg",
michael@0 98 "variadicArg",
michael@0 99 [("Void",
michael@0 100 [("::TestMethods::variadicArg::arg1", "arg1", "ByteOrNull", True, True)])])
michael@0 101 checkMethod(methods[8], "::TestMethods::crazyTypes",
michael@0 102 "crazyTypes",
michael@0 103 [("Void",
michael@0 104 [("::TestMethods::crazyTypes::arg1", "arg1", "LongOrNullArraySequenceOrNull", False, False),
michael@0 105 ("::TestMethods::crazyTypes::arg2", "arg2", "BooleanOrNullArrayArrayOrNull", False, False)])])
michael@0 106 checkMethod(methods[9], "::TestMethods::getObject",
michael@0 107 "getObject", [("Object", [])])
michael@0 108 checkMethod(methods[10], "::TestMethods::setObject",
michael@0 109 "setObject",
michael@0 110 [("Void",
michael@0 111 [("::TestMethods::setObject::arg1", "arg1", "Object", False, False)])])
michael@0 112 checkMethod(methods[11], "::TestMethods::setAny",
michael@0 113 "setAny",
michael@0 114 [("Void",
michael@0 115 [("::TestMethods::setAny::arg1", "arg1", "Any", False, False)])])
michael@0 116 checkMethod(methods[12], "::TestMethods::doFloats",
michael@0 117 "doFloats",
michael@0 118 [("Float",
michael@0 119 [("::TestMethods::doFloats::arg1", "arg1", "Float", False, False)])])
michael@0 120
michael@0 121 parser = parser.reset()
michael@0 122 threw = False
michael@0 123 try:
michael@0 124 parser.parse("""
michael@0 125 interface A {
michael@0 126 void foo(optional float bar = 1);
michael@0 127 };
michael@0 128 """)
michael@0 129 results = parser.finish()
michael@0 130 except Exception, x:
michael@0 131 threw = True
michael@0 132 harness.ok(not threw, "Should allow integer to float type corecion")
michael@0 133
michael@0 134 parser = parser.reset()
michael@0 135 threw = False
michael@0 136 try:
michael@0 137 parser.parse("""
michael@0 138 interface A {
michael@0 139 [GetterThrows] void foo();
michael@0 140 };
michael@0 141 """)
michael@0 142 results = parser.finish()
michael@0 143 except Exception, x:
michael@0 144 threw = True
michael@0 145 harness.ok(threw, "Should not allow [GetterThrows] on methods")
michael@0 146
michael@0 147 parser = parser.reset()
michael@0 148 threw = False
michael@0 149 try:
michael@0 150 parser.parse("""
michael@0 151 interface A {
michael@0 152 [SetterThrows] void foo();
michael@0 153 };
michael@0 154 """)
michael@0 155 results = parser.finish()
michael@0 156 except Exception, x:
michael@0 157 threw = True
michael@0 158 harness.ok(threw, "Should not allow [SetterThrows] on methods")
michael@0 159
michael@0 160 parser = parser.reset()
michael@0 161 threw = False
michael@0 162 try:
michael@0 163 parser.parse("""
michael@0 164 interface A {
michael@0 165 [Throw] void foo();
michael@0 166 };
michael@0 167 """)
michael@0 168 results = parser.finish()
michael@0 169 except Exception, x:
michael@0 170 threw = True
michael@0 171 harness.ok(threw, "Should spell [Throws] correctly on methods")

mercurial