1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/test-weak-set.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,208 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +'use strict'; 1.9 + 1.10 +const { Cu } = require('chrome'); 1.11 +const { Loader } = require('sdk/test/loader'); 1.12 +const { defer } = require('sdk/core/promise'); 1.13 + 1.14 +function gc() { 1.15 + let { promise, resolve } = defer(); 1.16 + 1.17 + Cu.schedulePreciseGC(resolve); 1.18 + 1.19 + return promise; 1.20 +}; 1.21 + 1.22 +exports['test adding item'] = function(assert, done) { 1.23 + let loader = Loader(module); 1.24 + let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set'); 1.25 + 1.26 + let items = {}; 1.27 + let item = {}; 1.28 + 1.29 + add(items, item); 1.30 + 1.31 + gc(). 1.32 + then(() => { 1.33 + assert.ok(has(items, item), 1.34 + 'the item is in the weak set'); 1.35 + }). 1.36 + then(loader.unload). 1.37 + then(done, assert.fail); 1.38 +}; 1.39 + 1.40 +exports['test remove item'] = function(assert, done) { 1.41 + let loader = Loader(module); 1.42 + let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set'); 1.43 + 1.44 + let items = {}; 1.45 + let item = {}; 1.46 + 1.47 + add(items, item); 1.48 + 1.49 + remove(items, item); 1.50 + 1.51 + gc(). 1.52 + then(() => { 1.53 + assert.ok(!has(items, item), 1.54 + 'the item is not in weak set'); 1.55 + }). 1.56 + then(loader.unload). 1.57 + then(done, assert.fail); 1.58 +}; 1.59 + 1.60 +exports['test iterate'] = function(assert, done) { 1.61 + let loader = Loader(module); 1.62 + let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set'); 1.63 + 1.64 + let items = {}; 1.65 + let addedItems = [{}, {}]; 1.66 + 1.67 + add(items, addedItems[0]); 1.68 + add(items, addedItems[1]); 1.69 + add(items, addedItems[0]); // weak set shouldn't add this twice 1.70 + 1.71 + gc(). 1.72 + then(() => { 1.73 + let count = 0; 1.74 + 1.75 + for (let item of iterator(items)) { 1.76 + assert.equal(item, addedItems[count], 1.77 + 'item in the expected order'); 1.78 + 1.79 + count++; 1.80 + } 1.81 + 1.82 + assert.equal(count, 2, 1.83 + 'items in the expected number'); 1.84 + }). 1.85 + then(loader.unload). 1.86 + then(done, assert.fail); 1.87 +}; 1.88 + 1.89 +exports['test clear'] = function(assert, done) { 1.90 + let loader = Loader(module); 1.91 + let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set'); 1.92 + 1.93 + let items = {}; 1.94 + let addedItems = [{}, {}]; 1.95 + 1.96 + add(items, addedItems[0]); 1.97 + add(items, addedItems[1]); 1.98 + 1.99 + clear(items) 1.100 + 1.101 + gc(). 1.102 + then(() => { 1.103 + let count = 0; 1.104 + 1.105 + for (let item of iterator(items)) 1.106 + assert.fail('the loop should not be executed'); 1.107 + 1.108 + assert.equal(count, 0, 1.109 + 'no items in the weak set'); 1.110 + }). 1.111 + then(loader.unload). 1.112 + then(done, assert.fail); 1.113 +}; 1.114 + 1.115 +exports['test adding item without reference'] = function(assert, done) { 1.116 + let loader = Loader(module); 1.117 + let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set'); 1.118 + 1.119 + let items = {}; 1.120 + 1.121 + add(items, {}); 1.122 + 1.123 + gc(). 1.124 + then(() => { 1.125 + let count = 0; 1.126 + 1.127 + for (let item of iterator(items)) 1.128 + assert.fail('the loop should not be executed'); 1.129 + 1.130 + assert.equal(count, 0, 1.131 + 'no items in the weak set'); 1.132 + }). 1.133 + then(loader.unload). 1.134 + then(done, assert.fail); 1.135 +}; 1.136 + 1.137 +exports['test adding non object or null item'] = function(assert) { 1.138 + let loader = Loader(module); 1.139 + let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set'); 1.140 + 1.141 + let items = {}; 1.142 + 1.143 + assert.throws(() => { 1.144 + add(items, 'foo'); 1.145 + }, 1.146 + /^value is not a non-null object/, 1.147 + 'only non-null object are allowed'); 1.148 + 1.149 + assert.throws(() => { 1.150 + add(items, 0); 1.151 + }, 1.152 + /^value is not a non-null object/, 1.153 + 'only non-null object are allowed'); 1.154 + 1.155 + assert.throws(() => { 1.156 + add(items, undefined); 1.157 + }, 1.158 + /^value is not a non-null object/, 1.159 + 'only non-null object are allowed'); 1.160 + 1.161 + assert.throws(() => { 1.162 + add(items, null); 1.163 + }, 1.164 + /^value is not a non-null object/, 1.165 + 'only non-null object are allowed'); 1.166 + 1.167 + assert.throws(() => { 1.168 + add(items, true); 1.169 + }, 1.170 + /^value is not a non-null object/, 1.171 + 'only non-null object are allowed'); 1.172 +}; 1.173 + 1.174 +exports['test adding to non object or null item'] = function(assert) { 1.175 + let loader = Loader(module); 1.176 + let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set'); 1.177 + 1.178 + let item = {}; 1.179 + 1.180 + assert.throws(() => { 1.181 + add('foo', item); 1.182 + }, 1.183 + /^value is not a non-null object/, 1.184 + 'only non-null object are allowed'); 1.185 + 1.186 + assert.throws(() => { 1.187 + add(0, item); 1.188 + }, 1.189 + /^value is not a non-null object/, 1.190 + 'only non-null object are allowed'); 1.191 + 1.192 + assert.throws(() => { 1.193 + add(undefined, item); 1.194 + }, 1.195 + /^value is not a non-null object/, 1.196 + 'only non-null object are allowed'); 1.197 + 1.198 + assert.throws(() => { 1.199 + add(null, item); 1.200 + }, 1.201 + /^value is not a non-null object/, 1.202 + 'only non-null object are allowed'); 1.203 + 1.204 + assert.throws(() => { 1.205 + add(true, item); 1.206 + }, 1.207 + /^value is not a non-null object/, 1.208 + 'only non-null object are allowed'); 1.209 +}; 1.210 + 1.211 +require('test').run(exports);