Bug 1359269 - Part 11: Add a bunch of parser tests; r=bzbarsky
authorManish Goregaokar <manishearth@gmail.com>
Sat, 02 Mar 2019 04:22:12 +0000
changeset 462099 4166cae81546
parent 462098 759c00e21197
child 462100 a6fd8376d3ec
child 462158 82134a2f7b89
push id79452
push usermgoregaokar@mozilla.com
push dateSat, 02 Mar 2019 04:24:43 +0000
treeherderautoland@4166cae81546 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbzbarsky
bugs1359269
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1359269 - Part 11: Add a bunch of parser tests; r=bzbarsky Depends on D20061 Differential Revision: https://phabricator.services.mozilla.com/D20992
dom/bindings/parser/tests/test_attributes_on_types.py
dom/bindings/parser/tests/test_extended_attributes.py
new file mode 100644
--- /dev/null
+++ b/dom/bindings/parser/tests/test_attributes_on_types.py
@@ -0,0 +1,238 @@
+# Import the WebIDL module, so we can do isinstance checks and whatnot
+import WebIDL
+
+def WebIDLTest(parser, harness):
+    # Basic functionality
+    threw = False
+    try:
+        parser.parse("""
+            typedef [EnforceRange] long Foo;
+            typedef [Clamp] long Bar;
+            typedef [TreatNullAs=EmptyString] DOMString Baz;
+            dictionary A {
+                required [EnforceRange] long a;
+                required [Clamp] long b;
+                [ChromeOnly, EnforceRange] long c;
+                Foo d;
+            };
+            interface B {
+                attribute Foo typedefFoo;
+                attribute [EnforceRange] long foo;
+                attribute [Clamp] long bar;
+                attribute [TreatNullAs=EmptyString] DOMString baz;
+                void method([EnforceRange] long foo, [Clamp] long bar,
+                            [TreatNullAs=EmptyString] DOMString baz);
+                void method2(optional [EnforceRange] long foo, optional [Clamp] long bar,
+                             optional [TreatNullAs=EmptyString] DOMString baz);
+            };
+            interface Setlike {
+                setlike<[Clamp] long>;
+            };
+            interface Maplike {
+                maplike<[Clamp] long, [EnforceRange] long>;
+            };
+            interface Iterable {
+                iterable<[Clamp] long, [EnforceRange] long>;
+            };
+        """)
+        results = parser.finish()
+    except:
+        threw = True
+
+    harness.ok(not threw, "Should not have thrown on parsing normal")
+    if not threw:
+        harness.check(results[0].innerType.enforceRange, True, "Foo is [EnforceRange]")
+        harness.check(results[1].innerType.clamp, True, "Bar is [Clamp]")
+        harness.check(results[2].innerType.treatNullAsEmpty, True, "Baz is [TreatNullAs=EmptyString]")
+        A = results[3]
+        harness.check(A.members[0].type.enforceRange, True, "A.a is [EnforceRange]")
+        harness.check(A.members[1].type.clamp, True, "A.b is [Clamp]")
+        harness.check(A.members[2].type.enforceRange, True, "A.c is [EnforceRange]")
+        harness.check(A.members[3].type.enforceRange, True, "A.d is [EnforceRange]")
+        B = results[4]
+        harness.check(B.members[0].type.enforceRange, True, "B.typedefFoo is [EnforceRange]")
+        harness.check(B.members[1].type.enforceRange, True, "B.foo is [EnforceRange]")
+        harness.check(B.members[2].type.clamp, True, "B.bar is [Clamp]")
+        harness.check(B.members[3].type.treatNullAsEmpty, True, "B.baz is [TreatNullAs=EmptyString]")
+        method = B.members[4].signatures()[0][1]
+        harness.check(method[0].type.enforceRange, True, "foo argument of method is [EnforceRange]")
+        harness.check(method[1].type.clamp, True, "bar argument of method is [Clamp]")
+        harness.check(method[2].type.treatNullAsEmpty, True, "baz argument of method is [TreatNullAs=EmptyString]")
+        method2 = B.members[5].signatures()[0][1]
+        harness.check(method[0].type.enforceRange, True, "foo argument of method2 is [EnforceRange]")
+        harness.check(method[1].type.clamp, True, "bar argument of method2 is [Clamp]")
+        harness.check(method[2].type.treatNullAsEmpty, True, "baz argument of method2 is [TreatNullAs=EmptyString]")
+
+    ATTRIBUTES = [("[Clamp]", "long"), ("[EnforceRange]", "long"), ("[TreatNullAs=EmptyString]", "DOMString")]
+    TEMPLATES = [
+        ("required dictionary members", """
+            dictionary Foo {
+                %s required %s foo;
+            };
+        """),
+        ("optional arguments", """
+            interface Foo {
+                void foo(%s optional %s foo);
+            };
+        """),
+        ("typedefs", """
+            %s typedef %s foo;
+        """),
+        ("attributes", """
+            interface Foo {
+            %s attribute %s foo;
+            };
+        """),
+        ("readonly attributes", """
+            interface Foo {
+                readonly attribute %s %s foo;
+            };
+        """),
+        ("readonly unresolved attributes", """
+            interface Foo {
+              readonly attribute Bar baz;
+            };
+            typedef %s %s Bar;
+        """)              
+    ];
+
+    for (name, template) in TEMPLATES:
+        parser = parser.reset()
+        threw = False
+        try:
+            parser.parse(template % ("", "long"))
+            parser.finish()
+        except:
+            threw = True
+        harness.ok(not threw, "Template for %s parses without attributes" % name)
+        for (attribute, type) in ATTRIBUTES:
+            parser = parser.reset()
+            threw = False
+            try:
+                parser.parse(template % (attribute, type))
+                parser.finish()
+            except:
+                threw = True
+            harness.ok(threw,
+                       "Should not allow %s on %s" % (attribute, name))
+
+    parser = parser.reset()
+    threw = False
+    try:
+        parser.parse("""
+            typedef [Clamp, EnforceRange] long Foo;
+        """)
+        parser.finish()
+    except:
+        threw = True
+
+    harness.ok(threw, "Should not allow mixing [Clamp] and [EnforceRange]")
+
+    parser = parser.reset()
+    threw = False
+    try:
+        parser.parse("""
+            typedef [EnforceRange, Clamp] long Foo;
+        """)
+        parser.finish()
+    except:
+        threw = True
+
+    harness.ok(threw, "Should not allow mixing [Clamp] and [EnforceRange]")
+
+
+    parser = parser.reset()
+    threw = False
+    try:
+        parser.parse("""
+            typedef [Clamp] long Foo;
+            typedef [EnforceRange] Foo bar;
+        """)
+        parser.finish()
+    except:
+        threw = True
+
+    harness.ok(threw, "Should not allow mixing [Clamp] and [EnforceRange] via typedefs")
+
+    parser = parser.reset()
+    threw = False
+    try:
+        parser.parse("""
+            typedef [EnforceRange] long Foo;
+            typedef [Clamp] Foo bar;
+        """)
+        parser.finish()
+    except:
+        threw = True
+
+    harness.ok(threw, "Should not allow mixing [Clamp] and [EnforceRange] via typedefs")
+
+    parser = parser.reset()
+    threw = False
+    try:
+        parser.parse("""
+            typedef [Clamp] DOMString Foo;
+        """)
+        parser.finish()
+    except:
+        threw = True
+
+    harness.ok(threw, "Should not allow [Clamp] on DOMString")
+
+
+    parser = parser.reset()
+    threw = False
+    try:
+        parser.parse("""
+            typedef [EnforceRange] DOMString Foo;
+        """)
+        parser.finish()
+    except:
+        threw = True
+
+    harness.ok(threw, "Should not allow [EnforceRange] on DOMString")
+
+
+    parser = parser.reset()
+    threw = False
+    try:
+        parser.parse("""
+            typedef [TreatNullAs=EmptyString] long Foo;
+        """)
+        parser.finish()
+    except:
+        threw = True
+
+    harness.ok(threw, "Should not allow [TreatNullAs] on long")
+
+    parser = parser.reset()
+    threw = False
+    try:
+        parser.parse("""
+            interface Foo {
+               void foo([Clamp] Bar arg);
+            };
+            typedef long Bar;
+        """)
+        results = parser.finish()
+    except:
+        threw = True
+    harness.ok(not threw, "Should allow type attributes on unresolved types")
+    harness.check(results[0].members[0].signatures()[0][1][0].type.clamp, True,
+                  "Unresolved types with type attributes should correctly resolve with attributes")
+
+    parser = parser.reset()
+    threw = False
+    try:
+        parser.parse("""
+            interface Foo {
+               void foo(Bar arg);
+            };
+            typedef [Clamp] long Bar;
+        """)
+        results = parser.finish()
+    except:
+        threw = True
+    harness.ok(not threw, "Should allow type attributes on typedefs")
+    harness.check(results[0].members[0].signatures()[0][1][0].type.clamp, True,
+                  "Unresolved types that resolve to typedefs with attributes should correctly resolve with attributes")
--- a/dom/bindings/parser/tests/test_extended_attributes.py
+++ b/dom/bindings/parser/tests/test_extended_attributes.py
@@ -51,19 +51,19 @@ def WebIDLTest(parser, harness):
           void testClamp([Clamp] long foo);
           void testNotClamp(long foo);
         };
     """)
 
     results = parser.finish()
     # Pull out the first argument out of the arglist of the first (and
     # only) signature.
-    harness.ok(results[0].members[0].signatures()[0][1][0].clamp,
+    harness.ok(results[0].members[0].signatures()[0][1][0].type.clamp,
                "Should be clamped")
-    harness.ok(not results[0].members[1].signatures()[0][1][0].clamp,
+    harness.ok(not results[0].members[1].signatures()[0][1][0].type.clamp,
                "Should not be clamped")
 
     parser = parser.reset()
     threw = False
     try:
         parser.parse("""
             interface TestClamp2 {
               void testClamp([Clamp=something] long foo);
@@ -81,19 +81,19 @@ def WebIDLTest(parser, harness):
           void testEnforceRange([EnforceRange] long foo);
           void testNotEnforceRange(long foo);
         };
     """)
 
     results = parser.finish()
     # Pull out the first argument out of the arglist of the first (and
     # only) signature.
-    harness.ok(results[0].members[0].signatures()[0][1][0].enforceRange,
+    harness.ok(results[0].members[0].signatures()[0][1][0].type.enforceRange,
                "Should be enforceRange")
-    harness.ok(not results[0].members[1].signatures()[0][1][0].enforceRange,
+    harness.ok(not results[0].members[1].signatures()[0][1][0].type.enforceRange,
                "Should not be enforceRange")
 
     parser = parser.reset()
     threw = False
     try:
         parser.parse("""
             interface TestEnforceRange2 {
               void testEnforceRange([EnforceRange=something] long foo);