tor-browser

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

test_attr.py (6879B)


      1 import WebIDL
      2 
      3 
      4 def WebIDLTest(parser, harness):
      5    testData = [
      6        ("::TestAttr%s::b", "b", "Byte%s", False),
      7        ("::TestAttr%s::rb", "rb", "Byte%s", True),
      8        ("::TestAttr%s::o", "o", "Octet%s", False),
      9        ("::TestAttr%s::ro", "ro", "Octet%s", True),
     10        ("::TestAttr%s::s", "s", "Short%s", False),
     11        ("::TestAttr%s::rs", "rs", "Short%s", True),
     12        ("::TestAttr%s::us", "us", "UnsignedShort%s", False),
     13        ("::TestAttr%s::rus", "rus", "UnsignedShort%s", True),
     14        ("::TestAttr%s::l", "l", "Long%s", False),
     15        ("::TestAttr%s::rl", "rl", "Long%s", True),
     16        ("::TestAttr%s::ul", "ul", "UnsignedLong%s", False),
     17        ("::TestAttr%s::rul", "rul", "UnsignedLong%s", True),
     18        ("::TestAttr%s::ll", "ll", "LongLong%s", False),
     19        ("::TestAttr%s::rll", "rll", "LongLong%s", True),
     20        ("::TestAttr%s::ull", "ull", "UnsignedLongLong%s", False),
     21        ("::TestAttr%s::rull", "rull", "UnsignedLongLong%s", True),
     22        ("::TestAttr%s::str", "str", "String%s", False),
     23        ("::TestAttr%s::rstr", "rstr", "String%s", True),
     24        ("::TestAttr%s::obj", "obj", "Object%s", False),
     25        ("::TestAttr%s::robj", "robj", "Object%s", True),
     26        ("::TestAttr%s::object", "object", "Object%s", False),
     27        ("::TestAttr%s::f", "f", "Float%s", False),
     28        ("::TestAttr%s::rf", "rf", "Float%s", True),
     29    ]
     30 
     31    parser.parse(
     32        """
     33        interface TestAttr {
     34          attribute byte b;
     35          readonly attribute byte rb;
     36          attribute octet o;
     37          readonly attribute octet ro;
     38          attribute short s;
     39          readonly attribute short rs;
     40          attribute unsigned short us;
     41          readonly attribute unsigned short rus;
     42          attribute long l;
     43          readonly attribute long rl;
     44          attribute unsigned long ul;
     45          readonly attribute unsigned long rul;
     46          attribute long long ll;
     47          readonly attribute long long rll;
     48          attribute unsigned long long ull;
     49          readonly attribute unsigned long long rull;
     50          attribute DOMString str;
     51          readonly attribute DOMString rstr;
     52          attribute object obj;
     53          readonly attribute object robj;
     54          attribute object _object;
     55          attribute float f;
     56          readonly attribute float rf;
     57        };
     58 
     59        interface TestAttrNullable {
     60          attribute byte? b;
     61          readonly attribute byte? rb;
     62          attribute octet? o;
     63          readonly attribute octet? ro;
     64          attribute short? s;
     65          readonly attribute short? rs;
     66          attribute unsigned short? us;
     67          readonly attribute unsigned short? rus;
     68          attribute long? l;
     69          readonly attribute long? rl;
     70          attribute unsigned long? ul;
     71          readonly attribute unsigned long? rul;
     72          attribute long long? ll;
     73          readonly attribute long long? rll;
     74          attribute unsigned long long? ull;
     75          readonly attribute unsigned long long? rull;
     76          attribute DOMString? str;
     77          readonly attribute DOMString? rstr;
     78          attribute object? obj;
     79          readonly attribute object? robj;
     80          attribute object? _object;
     81          attribute float? f;
     82          readonly attribute float? rf;
     83        };
     84    """
     85    )
     86 
     87    results = parser.finish()
     88 
     89    def checkAttr(attr, QName, name, type, readonly):
     90        harness.ok(isinstance(attr, WebIDL.IDLAttribute), "Should be an IDLAttribute")
     91        harness.ok(attr.isAttr(), "Attr is an Attr")
     92        harness.ok(not attr.isMethod(), "Attr is not an method")
     93        harness.ok(not attr.isConst(), "Attr is not a const")
     94        harness.check(attr.identifier.QName(), QName, "Attr has the right QName")
     95        harness.check(attr.identifier.name, name, "Attr has the right name")
     96        harness.check(str(attr.type), type, "Attr has the right type")
     97        harness.check(attr.readonly, readonly, "Attr's readonly state is correct")
     98 
     99    harness.ok(True, "TestAttr interface parsed without error.")
    100    harness.check(len(results), 2, "Should be two productions.")
    101    iface = results[0]
    102    harness.ok(isinstance(iface, WebIDL.IDLInterface), "Should be an IDLInterface")
    103    harness.check(
    104        iface.identifier.QName(), "::TestAttr", "Interface has the right QName"
    105    )
    106    harness.check(iface.identifier.name, "TestAttr", "Interface has the right name")
    107    harness.check(
    108        len(iface.members), len(testData), "Expect %s members" % len(testData)
    109    )
    110 
    111    attrs = iface.members
    112 
    113    for i in range(len(attrs)):
    114        data = testData[i]
    115        attr = attrs[i]
    116        (QName, name, type, readonly) = data
    117        checkAttr(attr, QName % "", name, type % "", readonly)
    118 
    119    iface = results[1]
    120    harness.ok(isinstance(iface, WebIDL.IDLInterface), "Should be an IDLInterface")
    121    harness.check(
    122        iface.identifier.QName(), "::TestAttrNullable", "Interface has the right QName"
    123    )
    124    harness.check(
    125        iface.identifier.name, "TestAttrNullable", "Interface has the right name"
    126    )
    127    harness.check(
    128        len(iface.members), len(testData), "Expect %s members" % len(testData)
    129    )
    130 
    131    attrs = iface.members
    132 
    133    for i in range(len(attrs)):
    134        data = testData[i]
    135        attr = attrs[i]
    136        (QName, name, type, readonly) = data
    137        checkAttr(attr, QName % "Nullable", name, type % "OrNull", readonly)
    138 
    139    parser = parser.reset()
    140    threw = False
    141    try:
    142        parser.parse(
    143            """
    144          interface A {
    145            [SetterThrows] readonly attribute boolean foo;
    146          };
    147        """
    148        )
    149        results = parser.finish()
    150    except WebIDL.WebIDLError:
    151        threw = True
    152    harness.ok(threw, "Should not allow [SetterThrows] on readonly attributes")
    153 
    154    parser = parser.reset()
    155    threw = False
    156    try:
    157        parser.parse(
    158            """
    159          interface A {
    160            [Throw] readonly attribute boolean foo;
    161          };
    162        """
    163        )
    164        results = parser.finish()
    165    except WebIDL.WebIDLError:
    166        threw = True
    167    harness.ok(threw, "Should spell [Throws] correctly")
    168 
    169    parser = parser.reset()
    170    threw = False
    171    try:
    172        parser.parse(
    173            """
    174          interface A {
    175            [SameObject] readonly attribute boolean foo;
    176          };
    177        """
    178        )
    179        results = parser.finish()
    180    except WebIDL.WebIDLError:
    181        threw = True
    182    harness.ok(
    183        threw, "Should not allow [SameObject] on attributes not of interface type"
    184    )
    185 
    186    parser = parser.reset()
    187    threw = False
    188    try:
    189        parser.parse(
    190            """
    191          interface A {
    192            [SameObject] readonly attribute A foo;
    193          };
    194        """
    195        )
    196        results = parser.finish()
    197    except WebIDL.WebIDLError:
    198        threw = True
    199    harness.ok(not threw, "Should allow [SameObject] on attributes of interface type")