|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 'use strict'; |
|
6 |
|
7 const { Cu } = require('chrome'); |
|
8 const { Loader } = require('sdk/test/loader'); |
|
9 const { defer } = require('sdk/core/promise'); |
|
10 |
|
11 function gc() { |
|
12 let { promise, resolve } = defer(); |
|
13 |
|
14 Cu.schedulePreciseGC(resolve); |
|
15 |
|
16 return promise; |
|
17 }; |
|
18 |
|
19 exports['test adding item'] = function(assert, done) { |
|
20 let loader = Loader(module); |
|
21 let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set'); |
|
22 |
|
23 let items = {}; |
|
24 let item = {}; |
|
25 |
|
26 add(items, item); |
|
27 |
|
28 gc(). |
|
29 then(() => { |
|
30 assert.ok(has(items, item), |
|
31 'the item is in the weak set'); |
|
32 }). |
|
33 then(loader.unload). |
|
34 then(done, assert.fail); |
|
35 }; |
|
36 |
|
37 exports['test remove item'] = function(assert, done) { |
|
38 let loader = Loader(module); |
|
39 let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set'); |
|
40 |
|
41 let items = {}; |
|
42 let item = {}; |
|
43 |
|
44 add(items, item); |
|
45 |
|
46 remove(items, item); |
|
47 |
|
48 gc(). |
|
49 then(() => { |
|
50 assert.ok(!has(items, item), |
|
51 'the item is not in weak set'); |
|
52 }). |
|
53 then(loader.unload). |
|
54 then(done, assert.fail); |
|
55 }; |
|
56 |
|
57 exports['test iterate'] = function(assert, done) { |
|
58 let loader = Loader(module); |
|
59 let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set'); |
|
60 |
|
61 let items = {}; |
|
62 let addedItems = [{}, {}]; |
|
63 |
|
64 add(items, addedItems[0]); |
|
65 add(items, addedItems[1]); |
|
66 add(items, addedItems[0]); // weak set shouldn't add this twice |
|
67 |
|
68 gc(). |
|
69 then(() => { |
|
70 let count = 0; |
|
71 |
|
72 for (let item of iterator(items)) { |
|
73 assert.equal(item, addedItems[count], |
|
74 'item in the expected order'); |
|
75 |
|
76 count++; |
|
77 } |
|
78 |
|
79 assert.equal(count, 2, |
|
80 'items in the expected number'); |
|
81 }). |
|
82 then(loader.unload). |
|
83 then(done, assert.fail); |
|
84 }; |
|
85 |
|
86 exports['test clear'] = function(assert, done) { |
|
87 let loader = Loader(module); |
|
88 let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set'); |
|
89 |
|
90 let items = {}; |
|
91 let addedItems = [{}, {}]; |
|
92 |
|
93 add(items, addedItems[0]); |
|
94 add(items, addedItems[1]); |
|
95 |
|
96 clear(items) |
|
97 |
|
98 gc(). |
|
99 then(() => { |
|
100 let count = 0; |
|
101 |
|
102 for (let item of iterator(items)) |
|
103 assert.fail('the loop should not be executed'); |
|
104 |
|
105 assert.equal(count, 0, |
|
106 'no items in the weak set'); |
|
107 }). |
|
108 then(loader.unload). |
|
109 then(done, assert.fail); |
|
110 }; |
|
111 |
|
112 exports['test adding item without reference'] = function(assert, done) { |
|
113 let loader = Loader(module); |
|
114 let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set'); |
|
115 |
|
116 let items = {}; |
|
117 |
|
118 add(items, {}); |
|
119 |
|
120 gc(). |
|
121 then(() => { |
|
122 let count = 0; |
|
123 |
|
124 for (let item of iterator(items)) |
|
125 assert.fail('the loop should not be executed'); |
|
126 |
|
127 assert.equal(count, 0, |
|
128 'no items in the weak set'); |
|
129 }). |
|
130 then(loader.unload). |
|
131 then(done, assert.fail); |
|
132 }; |
|
133 |
|
134 exports['test adding non object or null item'] = function(assert) { |
|
135 let loader = Loader(module); |
|
136 let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set'); |
|
137 |
|
138 let items = {}; |
|
139 |
|
140 assert.throws(() => { |
|
141 add(items, 'foo'); |
|
142 }, |
|
143 /^value is not a non-null object/, |
|
144 'only non-null object are allowed'); |
|
145 |
|
146 assert.throws(() => { |
|
147 add(items, 0); |
|
148 }, |
|
149 /^value is not a non-null object/, |
|
150 'only non-null object are allowed'); |
|
151 |
|
152 assert.throws(() => { |
|
153 add(items, undefined); |
|
154 }, |
|
155 /^value is not a non-null object/, |
|
156 'only non-null object are allowed'); |
|
157 |
|
158 assert.throws(() => { |
|
159 add(items, null); |
|
160 }, |
|
161 /^value is not a non-null object/, |
|
162 'only non-null object are allowed'); |
|
163 |
|
164 assert.throws(() => { |
|
165 add(items, true); |
|
166 }, |
|
167 /^value is not a non-null object/, |
|
168 'only non-null object are allowed'); |
|
169 }; |
|
170 |
|
171 exports['test adding to non object or null item'] = function(assert) { |
|
172 let loader = Loader(module); |
|
173 let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set'); |
|
174 |
|
175 let item = {}; |
|
176 |
|
177 assert.throws(() => { |
|
178 add('foo', item); |
|
179 }, |
|
180 /^value is not a non-null object/, |
|
181 'only non-null object are allowed'); |
|
182 |
|
183 assert.throws(() => { |
|
184 add(0, item); |
|
185 }, |
|
186 /^value is not a non-null object/, |
|
187 'only non-null object are allowed'); |
|
188 |
|
189 assert.throws(() => { |
|
190 add(undefined, item); |
|
191 }, |
|
192 /^value is not a non-null object/, |
|
193 'only non-null object are allowed'); |
|
194 |
|
195 assert.throws(() => { |
|
196 add(null, item); |
|
197 }, |
|
198 /^value is not a non-null object/, |
|
199 'only non-null object are allowed'); |
|
200 |
|
201 assert.throws(() => { |
|
202 add(true, item); |
|
203 }, |
|
204 /^value is not a non-null object/, |
|
205 'only non-null object are allowed'); |
|
206 }; |
|
207 |
|
208 require('test').run(exports); |