summaryrefslogtreecommitdiffstats
path: root/dom/bindings/test/test_bug1123516_maplikesetlike.html
blob: 18ede38ace357cb6d7e68e46f5c6d7b51601ce05 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<!-- Any copyright is dedicated to the Public Domain.
- http://creativecommons.org/publicdomain/zero/1.0/ -->
<!DOCTYPE HTML>
<html>
    <head>
        <title>Test Maplike Interface</title>
        <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
        <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    </head>
    <body>
        <script class="testbody" type="application/javascript">
         SimpleTest.waitForExplicitFinish();
         SpecialPowers.pushPrefEnv({set: [['dom.expose_test_interfaces', true]]}, function() {

             base_properties = [["has", "function", 1],
                                ["entries", "function", 0],
                                ["keys", "function", 0],
                                ["values", "function", 0],
                                ["forEach", "function", 1],
                                ["size", "number"]];
             maplike_properties = base_properties.concat([["set", "function", 2]]);
             setlike_properties = base_properties;
             rw_properties = [["clear", "function", 0],
                              ["delete", "function", 1]];
             setlike_rw_properties = base_properties.concat(rw_properties).concat([["add", "function", 1]]);
             maplike_rw_properties = maplike_properties.concat(rw_properties).concat([["get", "function",1]]);
             var testExistence = function testExistence(prefix, obj, properties) {
                 for (var [name, type, args] of properties) {
                     // Properties are somewhere up the proto chain, hasOwnProperty won't work
                     isnot(obj[name], undefined,
                        `${prefix} object has property ${name}`);

                     is(typeof obj[name], type,
                        `${prefix} object property ${name} is a ${type}`);
                     // Check function length
                     if (type == "function") {
                         is(obj[name].length, args,
                            `${prefix} object property ${name} is length ${args}`);
                         is(obj[name].name, name,
                            `${prefix} object method name is ${name}`);
                     }

                     // Find where property is on proto chain, check for enumerablility there.
                     var owner = obj;
                     while (owner) {
                         var propDesc = Object.getOwnPropertyDescriptor(owner, name);
                         if (propDesc) {
                             ok(!propDesc.enumerable,
                                `${prefix} object property ${name} is not enumerable`);
                             break;
                         }
                         owner = Object.getPrototypeOf(owner);
                     }
                 }
             }

             var m;
             var testSet;
             var testIndex;
             // Simple map creation and functionality test
             info("SimpleMap: Testing simple map creation and functionality");
             m = new TestInterfaceMaplike();
             ok(m, "SimpleMap: got a TestInterfaceMaplike object");
             testExistence("SimpleMap: ", m, maplike_rw_properties);
             is(m.size, 0, "SimpleMap: size should be zero");
             ok(!m.has("test"), "SimpleMap: maplike has should return false");
             is(m.get("test"), undefined, "SimpleMap: maplike get should return undefined on bogus lookup");
             m1 = m.set("test", 1);
             is(m, m1, "SimpleMap: return from set should be map object");
             is(m.size, 1, "SimpleMap: size should be 1");
             ok(m.has("test"), "SimpleMap: maplike has should return true");
             is(m.get("test"), 1, "SimpleMap: maplike get should return value entered");
             m2 = m.set("test2", 2);
             is(m.size, 2, "SimpleMap: size should be 2");
             testSet = [["test", 1], ["test2", 2]];
             testIndex = 0;
             m.forEach(function(v, k, o) {
                 "use strict";
                 is(o, m, "SimpleMap: foreach obj is correct");
                 is(k, testSet[testIndex][0], "SimpleMap: foreach map key: " + k + " = " + testSet[testIndex][0]);
                 is(v, testSet[testIndex][1], "SimpleMap: foreach map value: " + v + " = " + testSet[testIndex][1]);
                 testIndex += 1;
             });
             is(testIndex, 2, "SimpleMap: foreach ran correct number of times");
             ok(m.has("test2"), "SimpleMap: maplike has should return true");
             is(m.get("test2"), 2, "SimpleMap: maplike get should return value entered");
             is(m.delete("test2"), true, "SimpleMap: maplike deletion should return boolean");
             is(m.size, 1, "SimpleMap: size should be 1");
             iterable = false;
             for (var e of m) {
                 iterable = true;
                 is(e[0], "test", "SimpleMap: iterable first array element should be key");
                 is(e[1], 1, "SimpleMap: iterable second array element should be value");
             }
             is(m[Symbol.iterator].length, 0, "SimpleMap: @@iterator symbol is correct length");
             is(m[Symbol.iterator].name, "entries", "SimpleMap: @@iterator symbol has correct name");
             is(m[Symbol.iterator], m.entries, 'SimpleMap: @@iterator is an alias for "entries"');
             ok(iterable, "SimpleMap: @@iterator symbol resolved correctly");
             for (var k of m.keys()) {
                 is(k, "test", "SimpleMap: first keys element should be 'test'");
             }
             for (var v of m.values()) {
                 is(v, 1, "SimpleMap: first values elements should be 1");
             }
             for (var e of m.entries()) {
                 is(e[0], "test", "SimpleMap: entries first array element should be 'test'");
                 is(e[1], 1, "SimpleMap: entries second array element should be 1");
             }
             m.clear();
             is(m.size, 0, "SimpleMap: size should be 0 after clear");

             // Simple set creation and functionality test
             info("SimpleSet: Testing simple set creation and functionality");
             m = new TestInterfaceSetlike();
             ok(m, "SimpleSet: got a TestInterfaceSetlike object");
             testExistence("SimpleSet: ", m, setlike_rw_properties);
             is(m.size, 0, "SimpleSet: size should be zero");
             ok(!m.has("test"), "SimpleSet: maplike has should return false");
             m1 = m.add("test");
             is(m, m1, "SimpleSet: return from set should be map object");
             is(m.size, 1, "SimpleSet: size should be 1");
             ok(m.has("test"), "SimpleSet: maplike has should return true");
             m2 = m.add("test2");
             is(m.size, 2, "SimpleSet: size should be 2");
             testSet = ["test", "test2"];
             testIndex = 0;
             m.forEach(function(v, k, o) {
                 "use strict";
                 is(o, m, "SimpleSet: foreach obj is correct");
                 is(k, testSet[testIndex], "SimpleSet: foreach set key: " + k + " = " + testSet[testIndex]);
                 testIndex += 1;
             });
             is(testIndex, 2, "SimpleSet: foreach ran correct number of times");
             ok(m.has("test2"), "SimpleSet: maplike has should return true");
             is(m.delete("test2"), true, "SimpleSet: maplike deletion should return true");
             is(m.size, 1, "SimpleSet: size should be 1");
             iterable = false;
             for (var e of m) {
                 iterable = true;
                 is(e, "test", "SimpleSet: iterable first array element should be key");
             }
             is(m[Symbol.iterator].length, 0, "SimpleSet: @@iterator symbol is correct length");
             is(m[Symbol.iterator].name, "values", "SimpleSet: @@iterator symbol has correct name");
             is(m[Symbol.iterator], m.values, 'SimpleSet: @@iterator is an alias for "values"');
             ok(iterable, "SimpleSet: @@iterator symbol resolved correctly");
             for (var k of m.keys()) {
                 is(k, "test", "SimpleSet: first keys element should be 'test'");
             }
             for (var v of m.values()) {
                 is(v, "test", "SimpleSet: first values elements should be 'test'");
             }
             for (var e of m.entries()) {
                 is(e[0], "test", "SimpleSet: Entries first array element should be 'test'");
                 is(e[1], "test", "SimpleSet: Entries second array element should be 'test'");
             }
             m.clear();
             is(m.size, 0, "SimpleSet: size should be 0 after clear");

             // Map convenience function test
             info("Testing map convenience functions");
             m = new TestInterfaceMaplike();
             ok(m, "MapConvenience: got a TestInterfaceMaplike object");
             is(m.size, 0, "MapConvenience: size should be zero");
             ok(!m.hasInternal("test"), "MapConvenience: maplike hasInternal should return false");
             m.setInternal("test", 1);
             is(m.size, 1, "MapConvenience: size should be 1");
             ok(m.hasInternal("test"), "MapConvenience: maplike hasInternal should return true");
             is(m.get("test"), 1, "MapConvenience: maplike get should return value entered");
             m2 = m.setInternal("test2", 2);
             is(m.size, 2, "size should be 2");
             ok(m.hasInternal("test2"), "MapConvenience: maplike hasInternal should return true");
             is(m.get("test2"), 2, "MapConvenience: maplike get should return value entered");
             is(m.deleteInternal("test2"), true, "MapConvenience: maplike deleteInternal should return true");
             is(m.size, 1, "MapConvenience: size should be 1");
             m.clearInternal();
             is(m.size, 0, "MapConvenience: size should be 0 after clearInternal");

             // Map convenience function test using objects and readonly

             info("Testing Map convenience function test using objects and readonly");
             m = new TestInterfaceMaplikeObject();
             ok(m, "ReadOnlyMapConvenience: got a TestInterfaceMaplikeObject object");
             is(m.size, 0, "ReadOnlyMapConvenience: size should be zero");
             is(m["set"], undefined, "ReadOnlyMapConvenience: readonly map, should be no set function");
             is(m["clear"], undefined, "ReadOnlyMapConvenience: readonly map, should be no clear function");
             is(m["delete"], undefined, "ReadOnlyMapConvenience: readonly map, should be no delete function");
             ok(!m.hasInternal("test"), "ReadOnlyMapConvenience: maplike hasInternal should return false");
             m.setInternal("test");
             is(m.size, 1, "size should be 1");
             ok(m.hasInternal("test"), "ReadOnlyMapConvenience: maplike hasInternal should return true");
             m2 = m.setInternal("test2");
             is(m.size, 2, "size should be 2");
             ok(m.hasInternal("test2"), "ReadOnlyMapConvenience: maplike hasInternal should return true");
             is(m.deleteInternal("test2"), true, "ReadOnlyMapConvenience: maplike deleteInternal should return true");
             is(m.size, 1, "ReadOnlyMapConvenience: size should be 1");
             m.clearInternal();
             is(m.size, 0, "ReadOnlyMapConvenience: size should be 0 after clearInternal");

             // JS implemented map creation convenience function test

             info("JSMapConvenience: Testing JS implemented map creation convenience functions");
             m = new TestInterfaceJSMaplike();
             ok(m, "JSMapConvenience: got a TestInterfaceJSMaplike object");
             is(m.size, 0, "JSMapConvenience: size should be zero");
             ok(!m.has("test"), "JSMapConvenience: maplike has should return false");
             m.setInternal("test", 1);
             is(m.size, 1, "JSMapConvenience: size should be 1");
             ok(m.has("test"), "JSMapConvenience: maplike has should return true");
             is(m.get("test"), 1, "JSMapConvenience: maplike get should return value entered");
             m2 = m.setInternal("test2", 2);
             is(m.size, 2, "JSMapConvenience: size should be 2");
             ok(m.has("test2"), "JSMapConvenience: maplike has should return true");
             is(m.get("test2"), 2, "JSMapConvenience: maplike get should return value entered");
             is(m.deleteInternal("test2"), true, "JSMapConvenience: maplike deleteInternal should return true");
             is(m.size, 1, "JSMapConvenience: size should be 1");
             for (var k of m.keys()) {
                 is(k, "test", "JSMapConvenience: first keys element should be 'test'");
             }
             for (var v of m.values()) {
                 is(v, 1, "JSMapConvenience: first values elements should be 1");
             }
             for (var e of m.entries()) {
                 is(e[0], "test", "JSMapConvenience: entries first array element should be 'test'");
                 is(e[1], 1, "JSMapConvenience: entries second array element should be 1");
             }
             m.clearInternal();
             is(m.size, 0, "JSMapConvenience: size should be 0 after clearInternal");

             // Test this override for forEach
             info("ForEachThisOverride: Testing this override for forEach");
             m = new TestInterfaceMaplike();
             m.set("test", 1);
             m.forEach(function(v, k, o) {
                 "use strict";
                 is(o, m, "ForEachThisOverride: foreach obj is correct");
                 is(this, 5, "ForEachThisOverride: 'this' value should be correct");
             }, 5);

             // Test defaulting arguments on maplike to undefined
             info("MapArgsDefault: Testing maplike defaulting arguments to undefined");
             m = new TestInterfaceMaplike();
             m.set();
             is(m.size, 1, "MapArgsDefault: should have 1 entry");
             m.forEach(function(v, k) {
                 "use strict";
                 is(typeof k, "string", "MapArgsDefault: key is a string");
                 is(k, "undefined", "MapArgsDefault: key is the string undefined");
                 is(v, 0, "MapArgsDefault: value is 0");
             });
             is(m.get(), 0, "MapArgsDefault: no argument to get() returns correct value");
             m.delete();
             is(m.size, 0, "MapArgsDefault: should have 0 entries");

             // Test defaulting arguments on setlike to undefined
             info("SetArgsDefault: Testing setlike defaulting arguments to undefined");
             m = new TestInterfaceSetlike();
             m.add();
             is(m.size, 1, "SetArgsDefault: should have 1 entry");
             m.forEach(function(v, k) {
                 "use strict";
                 is(typeof k, "string", "SetArgsDefault: key is a string");
                 is(k, "undefined", "SetArgsDefault: key is the string undefined");
             });
             m.delete();
             is(m.size, 0, "SetArgsDefault: should have 0 entries");

             SimpleTest.finish();
         });
        </script>
    </body>
</html>