tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_enum.py (3193B)


      1 import WebIDL
      2 
      3 
      4 def WebIDLTest(parser, harness):
      5    parser.parse(
      6        """
      7        enum TestEnum {
      8          "",
      9          "foo",
     10          "bar"
     11        };
     12 
     13        interface TestEnumInterface {
     14          TestEnum doFoo(boolean arg);
     15          readonly attribute TestEnum foo;
     16        };
     17    """
     18    )
     19 
     20    results = parser.finish()
     21 
     22    harness.ok(True, "TestEnumInterfaces interface parsed without error.")
     23    harness.check(len(results), 2, "Should be one production")
     24    harness.ok(isinstance(results[0], WebIDL.IDLEnum), "Should be an IDLEnum")
     25    harness.ok(isinstance(results[1], WebIDL.IDLInterface), "Should be an IDLInterface")
     26 
     27    enum = results[0]
     28    harness.check(enum.identifier.QName(), "::TestEnum", "Enum has the right QName")
     29    harness.check(enum.identifier.name, "TestEnum", "Enum has the right name")
     30    harness.check(enum.values(), ["", "foo", "bar"], "Enum has the right values")
     31 
     32    iface = results[1]
     33 
     34    harness.check(
     35        iface.identifier.QName(), "::TestEnumInterface", "Interface has the right QName"
     36    )
     37    harness.check(
     38        iface.identifier.name, "TestEnumInterface", "Interface has the right name"
     39    )
     40    harness.check(iface.parent, None, "Interface has no parent")
     41 
     42    members = iface.members
     43    harness.check(len(members), 2, "Should be one production")
     44    harness.ok(isinstance(members[0], WebIDL.IDLMethod), "Should be an IDLMethod")
     45    method = members[0]
     46    harness.check(
     47        method.identifier.QName(),
     48        "::TestEnumInterface::doFoo",
     49        "Method has correct QName",
     50    )
     51    harness.check(method.identifier.name, "doFoo", "Method has correct name")
     52 
     53    signatures = method.signatures()
     54    harness.check(len(signatures), 1, "Expect one signature")
     55 
     56    (returnType, arguments) = signatures[0]
     57    harness.check(
     58        str(returnType), "TestEnum (Wrapper)", "Method type is the correct name"
     59    )
     60    harness.check(len(arguments), 1, "Method has the right number of arguments")
     61    arg = arguments[0]
     62    harness.ok(isinstance(arg, WebIDL.IDLArgument), "Should be an IDLArgument")
     63    harness.check(str(arg.type), "Boolean", "Argument has the right type")
     64 
     65    attr = members[1]
     66    harness.check(
     67        attr.identifier.QName(), "::TestEnumInterface::foo", "Attr has correct QName"
     68    )
     69    harness.check(attr.identifier.name, "foo", "Attr has correct name")
     70 
     71    harness.check(str(attr.type), "TestEnum (Wrapper)", "Attr type is the correct name")
     72 
     73    # Now reset our parser
     74    parser = parser.reset()
     75    threw = False
     76    try:
     77        parser.parse(
     78            """
     79          enum Enum {
     80            "a",
     81            "b",
     82            "c"
     83          };
     84          interface TestInterface {
     85            undefined foo(optional Enum e = "d");
     86          };
     87        """
     88        )
     89        parser.finish()
     90    except WebIDL.WebIDLError:
     91        threw = True
     92 
     93    harness.ok(threw, "Should not allow a bogus default value for an enum")
     94 
     95    # Now reset our parser
     96    parser = parser.reset()
     97    parser.parse(
     98        """
     99      enum Enum {
    100        "a",
    101        "b",
    102        "c",
    103      };
    104    """
    105    )
    106    results = parser.finish()
    107    harness.check(len(results), 1, "Should allow trailing comma in enum")