123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- /* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
- 'use strict';
- const { Cu } = require('chrome');
- const { Loader } = require('sdk/test/loader');
- const { defer } = require('sdk/core/promise');
- function gc() {
- let { promise, resolve } = defer();
- Cu.schedulePreciseGC(resolve);
- return promise;
- };
- exports['test adding item'] = function(assert, done) {
- let loader = Loader(module);
- let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
- let items = {};
- let item = {};
- add(items, item);
- gc().
- then(() => {
- assert.ok(has(items, item),
- 'the item is in the weak set');
- }).
- then(loader.unload).
- then(done, assert.fail);
- };
- exports['test remove item'] = function(assert, done) {
- let loader = Loader(module);
- let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
- let items = {};
- let item = {};
- add(items, item);
- remove(items, item);
- gc().
- then(() => {
- assert.ok(!has(items, item),
- 'the item is not in weak set');
- }).
- then(loader.unload).
- then(done, assert.fail);
- };
- exports['test iterate'] = function(assert, done) {
- let loader = Loader(module);
- let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
- let items = {};
- let addedItems = [{}, {}];
- add(items, addedItems[0]);
- add(items, addedItems[1]);
- add(items, addedItems[0]); // weak set shouldn't add this twice
- gc().
- then(() => {
- let count = 0;
- for (let item of iterator(items)) {
- assert.equal(item, addedItems[count],
- 'item in the expected order');
- count++;
- }
- assert.equal(count, 2,
- 'items in the expected number');
- }).
- then(loader.unload).
- then(done, assert.fail);
- };
- exports['test clear'] = function(assert, done) {
- let loader = Loader(module);
- let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
- let items = {};
- let addedItems = [{}, {}];
- add(items, addedItems[0]);
- add(items, addedItems[1]);
- clear(items)
- gc().
- then(() => {
- let count = 0;
- for (let item of iterator(items))
- assert.fail('the loop should not be executed');
- assert.equal(count, 0,
- 'no items in the weak set');
- }).
- then(loader.unload).
- then(done, assert.fail);
- };
- exports['test adding item without reference'] = function(assert, done) {
- let loader = Loader(module);
- let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
- let items = {};
- add(items, {});
- gc().
- then(() => {
- let count = 0;
- for (let item of iterator(items))
- assert.fail('the loop should not be executed');
- assert.equal(count, 0,
- 'no items in the weak set');
- }).
- then(loader.unload).
- then(done, assert.fail);
- };
- exports['test adding non object or null item'] = function(assert) {
- let loader = Loader(module);
- let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
- let items = {};
- assert.throws(() => {
- add(items, 'foo');
- },
- /^value is not a non-null object/,
- 'only non-null object are allowed');
- assert.throws(() => {
- add(items, 0);
- },
- /^value is not a non-null object/,
- 'only non-null object are allowed');
- assert.throws(() => {
- add(items, undefined);
- },
- /^value is not a non-null object/,
- 'only non-null object are allowed');
- assert.throws(() => {
- add(items, null);
- },
- /^value is not a non-null object/,
- 'only non-null object are allowed');
- assert.throws(() => {
- add(items, true);
- },
- /^value is not a non-null object/,
- 'only non-null object are allowed');
- };
- exports['test adding to non object or null item'] = function(assert) {
- let loader = Loader(module);
- let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
- let item = {};
- assert.throws(() => {
- add('foo', item);
- },
- /^value is not a non-null object/,
- 'only non-null object are allowed');
- assert.throws(() => {
- add(0, item);
- },
- /^value is not a non-null object/,
- 'only non-null object are allowed');
- assert.throws(() => {
- add(undefined, item);
- },
- /^value is not a non-null object/,
- 'only non-null object are allowed');
- assert.throws(() => {
- add(null, item);
- },
- /^value is not a non-null object/,
- 'only non-null object are allowed');
- assert.throws(() => {
- add(true, item);
- },
- /^value is not a non-null object/,
- 'only non-null object are allowed');
- };
- require('test').run(exports);
|