summaryrefslogtreecommitdiffstats
path: root/dom/bindings/parser/tests/test_implements.py
blob: 04c47d92abebb51cf61ca5fa448bd6e194dda823 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# 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("""
            A implements B;
            interface B {
              attribute long x;
            };
            interface A {
              attribute long y;
            };
        """)
        results = parser.finish()
    except:
        threw = True

    harness.ok(not threw, "Should not have thrown on implements statement "
               "before interfaces")
    harness.check(len(results), 3, "We have three statements")
    harness.ok(isinstance(results[1], WebIDL.IDLInterface), "B is an interface")
    harness.check(len(results[1].members), 1, "B has one member")
    A = results[2]
    harness.ok(isinstance(A, WebIDL.IDLInterface), "A is an interface")
    harness.check(len(A.members), 2, "A has two members")
    harness.check(A.members[0].identifier.name, "y", "First member is 'y'")
    harness.check(A.members[1].identifier.name, "x", "Second member is 'x'")

    # Duplicated member names not allowed
    threw = False
    try:
        parser.parse("""
            C implements D;
            interface D {
              attribute long x;
            };
            interface C {
              attribute long x;
            };
        """)
        parser.finish()
    except:
        threw = True

    harness.ok(threw, "Should have thrown on implemented interface duplicating "
               "a name on base interface")

    # Same, but duplicated across implemented interfaces
    threw = False
    try:
        parser.parse("""
            E implements F;
            E implements G;
            interface F {
              attribute long x;
            };
            interface G {
              attribute long x;
            };
            interface E {};
        """)
        parser.finish()
    except:
        threw = True

    harness.ok(threw, "Should have thrown on implemented interfaces "
               "duplicating each other's member names")

    # Same, but duplicated across indirectly implemented interfaces
    threw = False
    try:
        parser.parse("""
            H implements I;
            H implements J;
            I implements K;
            interface K {
              attribute long x;
            };
            interface L {
              attribute long x;
            };
            interface I {};
            interface J : L {};
            interface H {};
        """)
        parser.finish()
    except:
        threw = True

    harness.ok(threw, "Should have thrown on indirectly implemented interfaces "
               "duplicating each other's member names")

    # Same, but duplicated across an implemented interface and its parent
    threw = False
    try:
        parser.parse("""
            M implements N;
            interface O {
              attribute long x;
            };
            interface N : O {
              attribute long x;
            };
            interface M {};
        """)
        parser.finish()
    except:
        threw = True

    harness.ok(threw, "Should have thrown on implemented interface and its "
               "ancestor duplicating member names")

    # Reset the parser so we can actually find things where we expect
    # them in the list
    parser = parser.reset()

    # Diamonds should be allowed
    threw = False
    try:
        parser.parse("""
            P implements Q;
            P implements R;
            Q implements S;
            R implements S;
            interface Q {};
            interface R {};
            interface S {
              attribute long x;
            };
            interface P {};
        """)
        results = parser.finish()
    except:
        threw = True

    harness.ok(not threw, "Diamond inheritance is fine")
    harness.check(results[6].identifier.name, "S", "We should be looking at 'S'")
    harness.check(len(results[6].members), 1, "S should have one member")
    harness.check(results[6].members[0].identifier.name, "x",
                  "S's member should be 'x'")

    parser = parser.reset()
    threw = False
    try:
        parser.parse("""
            interface TestInterface {
            };
            callback interface TestCallbackInterface {
            };
            TestInterface implements TestCallbackInterface;
        """)
        results = parser.finish()
    except:
        threw = True

    harness.ok(threw,
               "Should not allow callback interfaces on the right-hand side "
               "of 'implements'")

    parser = parser.reset()
    threw = False
    try:
        parser.parse("""
            interface TestInterface {
            };
            callback interface TestCallbackInterface {
            };
            TestCallbackInterface implements TestInterface;
        """)
        results = parser.finish()
    except:
        threw = True

    harness.ok(threw,
               "Should not allow callback interfaces on the left-hand side of "
               "'implements'")
    
    parser = parser.reset()
    threw = False
    try:
        parser.parse("""
            interface TestInterface {
            };
            dictionary Dict {
            };
            Dict implements TestInterface;
        """)
        results = parser.finish()
    except:
        threw = True

    harness.ok(threw,
               "Should not allow non-interfaces on the left-hand side "
               "of 'implements'")

    parser = parser.reset()
    threw = False
    try:
        parser.parse("""
            interface TestInterface {
            };
            dictionary Dict {
            };
            TestInterface implements Dict;
        """)
        results = parser.finish()
    except:
        threw = True

    harness.ok(threw,
               "Should not allow non-interfaces on the right-hand side "
               "of 'implements'")