test-weak-set.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. 'use strict';
  5. const { Cu } = require('chrome');
  6. const { Loader } = require('sdk/test/loader');
  7. const { defer } = require('sdk/core/promise');
  8. function gc() {
  9. let { promise, resolve } = defer();
  10. Cu.schedulePreciseGC(resolve);
  11. return promise;
  12. };
  13. exports['test adding item'] = function(assert, done) {
  14. let loader = Loader(module);
  15. let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
  16. let items = {};
  17. let item = {};
  18. add(items, item);
  19. gc().
  20. then(() => {
  21. assert.ok(has(items, item),
  22. 'the item is in the weak set');
  23. }).
  24. then(loader.unload).
  25. then(done, assert.fail);
  26. };
  27. exports['test remove item'] = function(assert, done) {
  28. let loader = Loader(module);
  29. let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
  30. let items = {};
  31. let item = {};
  32. add(items, item);
  33. remove(items, item);
  34. gc().
  35. then(() => {
  36. assert.ok(!has(items, item),
  37. 'the item is not in weak set');
  38. }).
  39. then(loader.unload).
  40. then(done, assert.fail);
  41. };
  42. exports['test iterate'] = function(assert, done) {
  43. let loader = Loader(module);
  44. let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
  45. let items = {};
  46. let addedItems = [{}, {}];
  47. add(items, addedItems[0]);
  48. add(items, addedItems[1]);
  49. add(items, addedItems[0]); // weak set shouldn't add this twice
  50. gc().
  51. then(() => {
  52. let count = 0;
  53. for (let item of iterator(items)) {
  54. assert.equal(item, addedItems[count],
  55. 'item in the expected order');
  56. count++;
  57. }
  58. assert.equal(count, 2,
  59. 'items in the expected number');
  60. }).
  61. then(loader.unload).
  62. then(done, assert.fail);
  63. };
  64. exports['test clear'] = function(assert, done) {
  65. let loader = Loader(module);
  66. let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
  67. let items = {};
  68. let addedItems = [{}, {}];
  69. add(items, addedItems[0]);
  70. add(items, addedItems[1]);
  71. clear(items)
  72. gc().
  73. then(() => {
  74. let count = 0;
  75. for (let item of iterator(items))
  76. assert.fail('the loop should not be executed');
  77. assert.equal(count, 0,
  78. 'no items in the weak set');
  79. }).
  80. then(loader.unload).
  81. then(done, assert.fail);
  82. };
  83. exports['test adding item without reference'] = function(assert, done) {
  84. let loader = Loader(module);
  85. let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
  86. let items = {};
  87. add(items, {});
  88. gc().
  89. then(() => {
  90. let count = 0;
  91. for (let item of iterator(items))
  92. assert.fail('the loop should not be executed');
  93. assert.equal(count, 0,
  94. 'no items in the weak set');
  95. }).
  96. then(loader.unload).
  97. then(done, assert.fail);
  98. };
  99. exports['test adding non object or null item'] = function(assert) {
  100. let loader = Loader(module);
  101. let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
  102. let items = {};
  103. assert.throws(() => {
  104. add(items, 'foo');
  105. },
  106. /^value is not a non-null object/,
  107. 'only non-null object are allowed');
  108. assert.throws(() => {
  109. add(items, 0);
  110. },
  111. /^value is not a non-null object/,
  112. 'only non-null object are allowed');
  113. assert.throws(() => {
  114. add(items, undefined);
  115. },
  116. /^value is not a non-null object/,
  117. 'only non-null object are allowed');
  118. assert.throws(() => {
  119. add(items, null);
  120. },
  121. /^value is not a non-null object/,
  122. 'only non-null object are allowed');
  123. assert.throws(() => {
  124. add(items, true);
  125. },
  126. /^value is not a non-null object/,
  127. 'only non-null object are allowed');
  128. };
  129. exports['test adding to non object or null item'] = function(assert) {
  130. let loader = Loader(module);
  131. let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
  132. let item = {};
  133. assert.throws(() => {
  134. add('foo', item);
  135. },
  136. /^value is not a non-null object/,
  137. 'only non-null object are allowed');
  138. assert.throws(() => {
  139. add(0, item);
  140. },
  141. /^value is not a non-null object/,
  142. 'only non-null object are allowed');
  143. assert.throws(() => {
  144. add(undefined, item);
  145. },
  146. /^value is not a non-null object/,
  147. 'only non-null object are allowed');
  148. assert.throws(() => {
  149. add(null, item);
  150. },
  151. /^value is not a non-null object/,
  152. 'only non-null object are allowed');
  153. assert.throws(() => {
  154. add(true, item);
  155. },
  156. /^value is not a non-null object/,
  157. 'only non-null object are allowed');
  158. };
  159. require('test').run(exports);