dom/bindings/parser/tests/test_method.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/bindings/parser/tests/test_method.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,171 @@
     1.4 +import WebIDL
     1.5 +
     1.6 +def WebIDLTest(parser, harness):
     1.7 +    parser.parse("""
     1.8 +        interface TestMethods {
     1.9 +          void basic();
    1.10 +          static void basicStatic();
    1.11 +          void basicWithSimpleArgs(boolean arg1, byte arg2, unsigned long arg3);
    1.12 +          boolean basicBoolean();
    1.13 +          static boolean basicStaticBoolean();
    1.14 +          boolean basicBooleanWithSimpleArgs(boolean arg1, byte arg2, unsigned long arg3);
    1.15 +          void optionalArg(optional byte? arg1, optional sequence<byte> arg2);
    1.16 +          void variadicArg(byte?... arg1);
    1.17 +          void crazyTypes(sequence<long?[]>? arg1, boolean?[][]? arg2);
    1.18 +          object getObject();
    1.19 +          void setObject(object arg1);
    1.20 +          void setAny(any arg1);
    1.21 +          float doFloats(float arg1);
    1.22 +        };
    1.23 +    """)
    1.24 +
    1.25 +    results = parser.finish()
    1.26 +
    1.27 +    harness.ok(True, "TestMethods interface parsed without error.")
    1.28 +    harness.check(len(results), 1, "Should be one production.")
    1.29 +    iface = results[0]
    1.30 +    harness.ok(isinstance(iface, WebIDL.IDLInterface),
    1.31 +               "Should be an IDLInterface")
    1.32 +    harness.check(iface.identifier.QName(), "::TestMethods", "Interface has the right QName")
    1.33 +    harness.check(iface.identifier.name, "TestMethods", "Interface has the right name")
    1.34 +    harness.check(len(iface.members), 13, "Expect 13 members")
    1.35 +
    1.36 +    methods = iface.members
    1.37 +
    1.38 +    def checkArgument(argument, QName, name, type, optional, variadic):
    1.39 +        harness.ok(isinstance(argument, WebIDL.IDLArgument),
    1.40 +                   "Should be an IDLArgument")
    1.41 +        harness.check(argument.identifier.QName(), QName, "Argument has the right QName")
    1.42 +        harness.check(argument.identifier.name, name, "Argument has the right name")
    1.43 +        harness.check(str(argument.type), type, "Argument has the right return type")
    1.44 +        harness.check(argument.optional, optional, "Argument has the right optional value")
    1.45 +        harness.check(argument.variadic, variadic, "Argument has the right variadic value")
    1.46 +
    1.47 +    def checkMethod(method, QName, name, signatures,
    1.48 +                    static=False, getter=False, setter=False, creator=False,
    1.49 +                    deleter=False, legacycaller=False, stringifier=False):
    1.50 +        harness.ok(isinstance(method, WebIDL.IDLMethod),
    1.51 +                   "Should be an IDLMethod")
    1.52 +        harness.ok(method.isMethod(), "Method is a method")
    1.53 +        harness.ok(not method.isAttr(), "Method is not an attr")
    1.54 +        harness.ok(not method.isConst(), "Method is not a const")
    1.55 +        harness.check(method.identifier.QName(), QName, "Method has the right QName")
    1.56 +        harness.check(method.identifier.name, name, "Method has the right name")
    1.57 +        harness.check(method.isStatic(), static, "Method has the correct static value")
    1.58 +        harness.check(method.isGetter(), getter, "Method has the correct getter value")
    1.59 +        harness.check(method.isSetter(), setter, "Method has the correct setter value")
    1.60 +        harness.check(method.isCreator(), creator, "Method has the correct creator value")
    1.61 +        harness.check(method.isDeleter(), deleter, "Method has the correct deleter value")
    1.62 +        harness.check(method.isLegacycaller(), legacycaller, "Method has the correct legacycaller value")
    1.63 +        harness.check(method.isStringifier(), stringifier, "Method has the correct stringifier value")
    1.64 +        harness.check(len(method.signatures()), len(signatures), "Method has the correct number of signatures")
    1.65 +
    1.66 +        sigpairs = zip(method.signatures(), signatures)
    1.67 +        for (gotSignature, expectedSignature) in sigpairs:
    1.68 +            (gotRetType, gotArgs) = gotSignature
    1.69 +            (expectedRetType, expectedArgs) = expectedSignature
    1.70 +
    1.71 +            harness.check(str(gotRetType), expectedRetType,
    1.72 +                          "Method has the expected return type.")
    1.73 +
    1.74 +            for i in range(0, len(gotArgs)):
    1.75 +                (QName, name, type, optional, variadic) = expectedArgs[i]
    1.76 +                checkArgument(gotArgs[i], QName, name, type, optional, variadic)
    1.77 +
    1.78 +    checkMethod(methods[0], "::TestMethods::basic", "basic", [("Void", [])])
    1.79 +    checkMethod(methods[1], "::TestMethods::basicStatic", "basicStatic",
    1.80 +                [("Void", [])], static=True)
    1.81 +    checkMethod(methods[2], "::TestMethods::basicWithSimpleArgs",
    1.82 +                "basicWithSimpleArgs",
    1.83 +       [("Void",
    1.84 +        [("::TestMethods::basicWithSimpleArgs::arg1", "arg1", "Boolean", False, False),
    1.85 +         ("::TestMethods::basicWithSimpleArgs::arg2", "arg2", "Byte", False, False),
    1.86 +         ("::TestMethods::basicWithSimpleArgs::arg3", "arg3", "UnsignedLong", False, False)])])
    1.87 +    checkMethod(methods[3], "::TestMethods::basicBoolean", "basicBoolean", [("Boolean", [])])
    1.88 +    checkMethod(methods[4], "::TestMethods::basicStaticBoolean", "basicStaticBoolean", [("Boolean", [])], static=True)
    1.89 +    checkMethod(methods[5], "::TestMethods::basicBooleanWithSimpleArgs",
    1.90 +                "basicBooleanWithSimpleArgs",
    1.91 +       [("Boolean",
    1.92 +        [("::TestMethods::basicBooleanWithSimpleArgs::arg1", "arg1", "Boolean", False, False),
    1.93 +         ("::TestMethods::basicBooleanWithSimpleArgs::arg2", "arg2", "Byte", False, False),
    1.94 +         ("::TestMethods::basicBooleanWithSimpleArgs::arg3", "arg3", "UnsignedLong", False, False)])])
    1.95 +    checkMethod(methods[6], "::TestMethods::optionalArg",
    1.96 +                "optionalArg",
    1.97 +       [("Void",
    1.98 +        [("::TestMethods::optionalArg::arg1", "arg1", "ByteOrNull", True, False),
    1.99 +         ("::TestMethods::optionalArg::arg2", "arg2", "ByteSequence", True, False)])])
   1.100 +    checkMethod(methods[7], "::TestMethods::variadicArg",
   1.101 +                "variadicArg",
   1.102 +       [("Void",
   1.103 +        [("::TestMethods::variadicArg::arg1", "arg1", "ByteOrNull", True, True)])])
   1.104 +    checkMethod(methods[8], "::TestMethods::crazyTypes",
   1.105 +                "crazyTypes",
   1.106 +       [("Void",
   1.107 +        [("::TestMethods::crazyTypes::arg1", "arg1", "LongOrNullArraySequenceOrNull", False, False),
   1.108 +         ("::TestMethods::crazyTypes::arg2", "arg2", "BooleanOrNullArrayArrayOrNull", False, False)])])
   1.109 +    checkMethod(methods[9], "::TestMethods::getObject",
   1.110 +                "getObject", [("Object", [])])
   1.111 +    checkMethod(methods[10], "::TestMethods::setObject",
   1.112 +                "setObject",
   1.113 +       [("Void",
   1.114 +        [("::TestMethods::setObject::arg1", "arg1", "Object", False, False)])])
   1.115 +    checkMethod(methods[11], "::TestMethods::setAny",
   1.116 +                "setAny",
   1.117 +       [("Void",
   1.118 +        [("::TestMethods::setAny::arg1", "arg1", "Any", False, False)])])
   1.119 +    checkMethod(methods[12], "::TestMethods::doFloats",
   1.120 +                "doFloats",
   1.121 +       [("Float",
   1.122 +        [("::TestMethods::doFloats::arg1", "arg1", "Float", False, False)])])
   1.123 +
   1.124 +    parser = parser.reset()
   1.125 +    threw = False
   1.126 +    try:
   1.127 +        parser.parse("""
   1.128 +          interface A {
   1.129 +            void foo(optional float bar = 1);
   1.130 +          };
   1.131 +        """)
   1.132 +        results = parser.finish()
   1.133 +    except Exception, x:
   1.134 +        threw = True
   1.135 +    harness.ok(not threw, "Should allow integer to float type corecion")
   1.136 +
   1.137 +    parser = parser.reset()
   1.138 +    threw = False
   1.139 +    try:
   1.140 +        parser.parse("""
   1.141 +          interface A {
   1.142 +            [GetterThrows] void foo();
   1.143 +          };
   1.144 +        """)
   1.145 +        results = parser.finish()
   1.146 +    except Exception, x:
   1.147 +        threw = True
   1.148 +    harness.ok(threw, "Should not allow [GetterThrows] on methods")
   1.149 +
   1.150 +    parser = parser.reset()
   1.151 +    threw = False
   1.152 +    try:
   1.153 +        parser.parse("""
   1.154 +          interface A {
   1.155 +            [SetterThrows] void foo();
   1.156 +          };
   1.157 +        """)
   1.158 +        results = parser.finish()
   1.159 +    except Exception, x:
   1.160 +        threw = True
   1.161 +    harness.ok(threw, "Should not allow [SetterThrows] on methods")
   1.162 +
   1.163 +    parser = parser.reset()
   1.164 +    threw = False
   1.165 +    try:
   1.166 +        parser.parse("""
   1.167 +          interface A {
   1.168 +            [Throw] void foo();
   1.169 +          };
   1.170 +        """)
   1.171 +        results = parser.finish()
   1.172 +    except Exception, x:
   1.173 +        threw = True
   1.174 +    harness.ok(threw, "Should spell [Throws] correctly on methods")

mercurial