dom/bindings/parser/tests/test_toJSON.py
author Boris Zbarsky <bzbarsky@mit.edu>
Fri, 01 Jun 2018 12:17:10 -0400
changeset 420886 916e5914d84acd420f61b5dd48027ddce9465e5d
parent 418765 23c967ee86114b1b0cfa87b44e32950af9c804f5
child 430813 b5a9f0882836c16bf2fbc7f2eab60f9f143f65f2
permissions -rw-r--r--
Bug 1465602 part 1. Enforce that the default toJSON can only return 'object'. r=qdot The spec says: The return type of the default toJSON operation must be object.

def WebIDLTest(parser, harness):
    threw = False
    try:
        parser.parse(
            """
            interface Test {
              object toJSON();
            };
            """)
        results = parser.finish()
    except:
        threw = True
    harness.ok(not threw, "Should allow a toJSON method.")

    parser = parser.reset()
    threw = False
    try:
        parser.parse(
            """
            interface Test {
              object toJSON(object arg);
              object toJSON(long arg);
            };
            """)
        results = parser.finish()
    except:
        threw = True
    harness.ok(threw, "Should not allow overloads of a toJSON method.")

    parser = parser.reset()
    threw = False
    try:
        parser.parse(
            """
            interface Test {
              object toJSON(object arg);
            };
            """)
        results = parser.finish()
    except:
        threw = True
    harness.ok(threw, "Should not allow a toJSON method with arguments.")

    parser = parser.reset()
    threw = False
    try:
        parser.parse(
            """
            interface Test {
              long toJSON();
            };
            """)
        results = parser.finish()
    except:
        threw = True
    harness.ok(not threw, "Should allow a toJSON method with 'long' as return type.")

    parser = parser.reset()
    threw = False
    try:
        parser.parse(
            """
            interface Test {
              [Default] object toJSON();
            };
            """)
        results = parser.finish()
    except:
        threw = True
    harness.ok(not threw, "Should allow a default toJSON method with 'object' as return type.")

    parser = parser.reset()
    threw = False
    try:
        parser.parse(
            """
            interface Test {
              [Default] long toJSON();
            };
            """)
        results = parser.finish()
    except:
        threw = True
    harness.ok(threw, "Should not allow a default toJSON method with non-'object' as return type.")

    parser = parser.reset()
    threw = False
    try:
        parser.parse(
            """
            interface Test {
              any toJSON();
            };
            """)
        results = parser.finish()
    except:
        threw = True
    harness.ok(threw, "Should not allow a toJSON method with a non-JSON return type.")

    # We should probably write some tests here about what types are
    # considered JSON types.  Bug 1462537.