summaryrefslogtreecommitdiffstats
path: root/dom/bindings/parser/tests/test_bytestring.py
blob: 248d2716f3647d3a7b0713ab0db107ddc0ea5da4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- coding: UTF-8 -*-

import WebIDL


def WebIDLTest(parser, harness):
    parser.parse(
        """
        interface TestByteString {
          attribute ByteString bs;
          attribute DOMString ds;
          };
    """
    )

    results = parser.finish()

    harness.ok(True, "TestByteString interface parsed without error.")

    harness.check(len(results), 1, "Should be one production")
    harness.ok(isinstance(results[0], WebIDL.IDLInterface), "Should be an IDLInterface")
    iface = results[0]
    harness.check(
        iface.identifier.QName(), "::TestByteString", "Interface has the right QName"
    )
    harness.check(
        iface.identifier.name, "TestByteString", "Interface has the right name"
    )
    harness.check(iface.parent, None, "Interface has no parent")

    members = iface.members
    harness.check(len(members), 2, "Should be two productions")

    attr = members[0]
    harness.ok(isinstance(attr, WebIDL.IDLAttribute), "Should be an IDLAttribute")
    harness.check(
        attr.identifier.QName(), "::TestByteString::bs", "Attr has correct QName"
    )
    harness.check(attr.identifier.name, "bs", "Attr has correct name")
    harness.check(str(attr.type), "ByteString", "Attr type is the correct name")
    harness.ok(attr.type.isByteString(), "Should be ByteString type")
    harness.ok(attr.type.isString(), "Should be String collective type")
    harness.ok(not attr.type.isDOMString(), "Should be not be DOMString type")

    # now check we haven't broken DOMStrings in the process.
    attr = members[1]
    harness.ok(isinstance(attr, WebIDL.IDLAttribute), "Should be an IDLAttribute")
    harness.check(
        attr.identifier.QName(), "::TestByteString::ds", "Attr has correct QName"
    )
    harness.check(attr.identifier.name, "ds", "Attr has correct name")
    harness.check(str(attr.type), "String", "Attr type is the correct name")
    harness.ok(attr.type.isDOMString(), "Should be DOMString type")
    harness.ok(attr.type.isString(), "Should be String collective type")
    harness.ok(not attr.type.isByteString(), "Should be not be ByteString type")

    # Cannot represent constant ByteString in IDL.
    threw = False
    try:
        parser.parse(
            """
            interface ConstByteString {
              const ByteString foo = "hello"
              };
        """
        )
    except WebIDL.WebIDLError:
        threw = True
    harness.ok(
        threw, "Should have thrown a WebIDL error for ByteString default in interface"
    )

    # Can have optional ByteStrings with default values
    try:
        parser.parse(
            """
            interface OptionalByteString {
              undefined passByteString(optional ByteString arg = "hello");
              };
        """
        )
        parser.finish()
    except WebIDL.WebIDLError as e:
        harness.ok(
            False,
            "Should not have thrown a WebIDL error for ByteString "
            "default in dictionary. " + str(e),
        )

    # Can have a default ByteString value in a dictionary
    try:
        parser.parse(
            """
        dictionary OptionalByteStringDict {
          ByteString item = "some string";
        };
        """
        )
        parser.finish()
    except WebIDL.WebIDLError as e:
        harness.ok(
            False,
            "Should not have thrown a WebIDL error for ByteString "
            "default in dictionary. " + str(e),
        )

    # Don't allow control characters in ByteString literals
    threw = False
    try:
        parser.parse(
            """
        dictionary OptionalByteStringDict2 {
          ByteString item = "\x03";
        };
        """
        )
        parser.finish()
    except WebIDL.WebIDLError:
        threw = True

    harness.ok(
        threw,
        "Should have thrown a WebIDL error for invalid ByteString "
        "default in dictionary",
    )