dom/bindings/parser/tests/test_constructor.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/bindings/parser/tests/test_constructor.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,109 @@
     1.4 +import WebIDL
     1.5 +
     1.6 +def WebIDLTest(parser, harness):
     1.7 +    def checkArgument(argument, QName, name, type, optional, variadic):
     1.8 +        harness.ok(isinstance(argument, WebIDL.IDLArgument),
     1.9 +                   "Should be an IDLArgument")
    1.10 +        harness.check(argument.identifier.QName(), QName, "Argument has the right QName")
    1.11 +        harness.check(argument.identifier.name, name, "Argument has the right name")
    1.12 +        harness.check(str(argument.type), type, "Argument has the right return type")
    1.13 +        harness.check(argument.optional, optional, "Argument has the right optional value")
    1.14 +        harness.check(argument.variadic, variadic, "Argument has the right variadic value")
    1.15 +
    1.16 +    def checkMethod(method, QName, name, signatures,
    1.17 +                    static=True, getter=False, setter=False, creator=False,
    1.18 +                    deleter=False, legacycaller=False, stringifier=False,
    1.19 +                    chromeOnly=False):
    1.20 +        harness.ok(isinstance(method, WebIDL.IDLMethod),
    1.21 +                   "Should be an IDLMethod")
    1.22 +        harness.ok(method.isMethod(), "Method is a method")
    1.23 +        harness.ok(not method.isAttr(), "Method is not an attr")
    1.24 +        harness.ok(not method.isConst(), "Method is not a const")
    1.25 +        harness.check(method.identifier.QName(), QName, "Method has the right QName")
    1.26 +        harness.check(method.identifier.name, name, "Method has the right name")
    1.27 +        harness.check(method.isStatic(), static, "Method has the correct static value")
    1.28 +        harness.check(method.isGetter(), getter, "Method has the correct getter value")
    1.29 +        harness.check(method.isSetter(), setter, "Method has the correct setter value")
    1.30 +        harness.check(method.isCreator(), creator, "Method has the correct creator value")
    1.31 +        harness.check(method.isDeleter(), deleter, "Method has the correct deleter value")
    1.32 +        harness.check(method.isLegacycaller(), legacycaller, "Method has the correct legacycaller value")
    1.33 +        harness.check(method.isStringifier(), stringifier, "Method has the correct stringifier value")
    1.34 +        harness.check(method.getExtendedAttribute("ChromeOnly") is not None, chromeOnly, "Method has the correct value for ChromeOnly")
    1.35 +        harness.check(len(method.signatures()), len(signatures), "Method has the correct number of signatures")
    1.36 +
    1.37 +        sigpairs = zip(method.signatures(), signatures)
    1.38 +        for (gotSignature, expectedSignature) in sigpairs:
    1.39 +            (gotRetType, gotArgs) = gotSignature
    1.40 +            (expectedRetType, expectedArgs) = expectedSignature
    1.41 +
    1.42 +            harness.check(str(gotRetType), expectedRetType,
    1.43 +                          "Method has the expected return type.")
    1.44 +
    1.45 +            for i in range(0, len(gotArgs)):
    1.46 +                (QName, name, type, optional, variadic) = expectedArgs[i]
    1.47 +                checkArgument(gotArgs[i], QName, name, type, optional, variadic)
    1.48 +
    1.49 +    parser.parse("""
    1.50 +        [Constructor]
    1.51 +        interface TestConstructorNoArgs {
    1.52 +        };
    1.53 +
    1.54 +        [Constructor(DOMString name)]
    1.55 +        interface TestConstructorWithArgs {
    1.56 +        };
    1.57 +
    1.58 +        [Constructor(object foo), Constructor(boolean bar)]
    1.59 +        interface TestConstructorOverloads {
    1.60 +        };
    1.61 +    """)
    1.62 +    results = parser.finish()
    1.63 +    harness.check(len(results), 3, "Should be three productions")
    1.64 +    harness.ok(isinstance(results[0], WebIDL.IDLInterface),
    1.65 +               "Should be an IDLInterface")
    1.66 +    harness.ok(isinstance(results[1], WebIDL.IDLInterface),
    1.67 +               "Should be an IDLInterface")
    1.68 +    harness.ok(isinstance(results[2], WebIDL.IDLInterface),
    1.69 +               "Should be an IDLInterface")
    1.70 +
    1.71 +    checkMethod(results[0].ctor(), "::TestConstructorNoArgs::constructor",
    1.72 +                "constructor", [("TestConstructorNoArgs (Wrapper)", [])])
    1.73 +    checkMethod(results[1].ctor(), "::TestConstructorWithArgs::constructor",
    1.74 +                "constructor",
    1.75 +                [("TestConstructorWithArgs (Wrapper)",
    1.76 +                 [("::TestConstructorWithArgs::constructor::name", "name", "String", False, False)])])
    1.77 +    checkMethod(results[2].ctor(), "::TestConstructorOverloads::constructor",
    1.78 +                "constructor",
    1.79 +                [("TestConstructorOverloads (Wrapper)",
    1.80 +                 [("::TestConstructorOverloads::constructor::foo", "foo", "Object", False, False)]),
    1.81 +                 ("TestConstructorOverloads (Wrapper)",
    1.82 +                 [("::TestConstructorOverloads::constructor::bar", "bar", "Boolean", False, False)])])
    1.83 +
    1.84 +    parser = parser.reset()
    1.85 +    parser.parse("""
    1.86 +        [ChromeConstructor()]
    1.87 +        interface TestChromeConstructor {
    1.88 +        };
    1.89 +    """)
    1.90 +    results = parser.finish()
    1.91 +    harness.check(len(results), 1, "Should be one production")
    1.92 +    harness.ok(isinstance(results[0], WebIDL.IDLInterface),
    1.93 +               "Should be an IDLInterface")
    1.94 +
    1.95 +    checkMethod(results[0].ctor(), "::TestChromeConstructor::constructor",
    1.96 +                "constructor", [("TestChromeConstructor (Wrapper)", [])],
    1.97 +                chromeOnly=True)
    1.98 +
    1.99 +    parser = parser.reset()
   1.100 +    threw = False
   1.101 +    try:
   1.102 +        parser.parse("""
   1.103 +        [Constructor(),
   1.104 +         ChromeConstructor(DOMString a)]
   1.105 +        interface TestChromeConstructor {
   1.106 +        };
   1.107 +        """)
   1.108 +        results = parser.finish()
   1.109 +    except:
   1.110 +        threw = True
   1.111 +
   1.112 +    harness.ok(threw, "Can't have both a Constructor and a ChromeConstructor")

mercurial