index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. 'use strict';
  2. var test = require('tape');
  3. var inspect = require('object-inspect');
  4. var forEach = require('for-each');
  5. var SLOT = require('../');
  6. test('assert', function (t) {
  7. forEach([null, undefined, true, false, 'foo', '', 42, 0], function (primitive) {
  8. t['throws'](
  9. // @ts-expect-error
  10. function () { SLOT.assert(primitive, ''); },
  11. TypeError,
  12. inspect(primitive) + ' is not an Object'
  13. );
  14. });
  15. forEach([null, undefined, true, false, 42, 0, {}, [], function () {}, /a/g], function (nonString) {
  16. t['throws'](
  17. // @ts-expect-error
  18. function () { SLOT.assert({}, nonString); },
  19. TypeError,
  20. inspect(nonString) + ' is not a String'
  21. );
  22. });
  23. t['throws'](
  24. function () { SLOT.assert({}, '[[whatever]]'); },
  25. TypeError,
  26. 'nonexistent slot throws'
  27. );
  28. var o = {};
  29. SLOT.set(o, 'x');
  30. t.doesNotThrow(function () { SLOT.assert(o, 'x'); }, 'existent slot noops');
  31. t['throws'](function () { SLOT.assert(o, 'y'); }, 'thing with a slot throws on a nonexistent slot');
  32. t.end();
  33. });
  34. test('has', function (t) {
  35. forEach([null, undefined, true, false, 'foo', '', 42, 0], function (primitive) {
  36. t['throws'](
  37. // @ts-expect-error
  38. function () { SLOT.has(primitive, ''); },
  39. TypeError,
  40. inspect(primitive) + ' is not an Object'
  41. );
  42. });
  43. forEach([null, undefined, true, false, 42, 0, {}, [], function () {}, /a/g], function (nonString) {
  44. t['throws'](
  45. // @ts-expect-error
  46. function () { SLOT.has({}, nonString); },
  47. TypeError,
  48. inspect(nonString) + ' is not a String'
  49. );
  50. });
  51. var o = {};
  52. t.equal(SLOT.has(o, '[[nonexistent]]'), false, 'nonexistent slot yields false');
  53. SLOT.set(o, 'foo');
  54. t.equal(SLOT.has(o, 'foo'), true, 'existent slot yields true');
  55. t.end();
  56. });
  57. test('get', function (t) {
  58. forEach([null, undefined, true, false, 'foo', '', 42, 0], function (primitive) {
  59. t['throws'](
  60. // @ts-expect-error
  61. function () { SLOT.get(primitive, ''); },
  62. TypeError,
  63. inspect(primitive) + ' is not an Object'
  64. );
  65. });
  66. forEach([null, undefined, true, false, 42, 0, {}, [], function () {}, /a/g], function (nonString) {
  67. t['throws'](
  68. // @ts-expect-error
  69. function () { SLOT.get({}, nonString); },
  70. TypeError,
  71. inspect(nonString) + ' is not a String'
  72. );
  73. });
  74. var o = {};
  75. t.equal(SLOT.get(o, 'nonexistent'), undefined, 'nonexistent slot is undefined');
  76. var v = {};
  77. SLOT.set(o, 'f', v);
  78. t.equal(SLOT.get(o, 'f'), v, '"get" retrieves value set by "set"');
  79. t.end();
  80. });
  81. test('set', function (t) {
  82. forEach([null, undefined, true, false, 'foo', '', 42, 0], function (primitive) {
  83. t['throws'](
  84. // @ts-expect-error
  85. function () { SLOT.set(primitive, ''); },
  86. TypeError,
  87. inspect(primitive) + ' is not an Object'
  88. );
  89. });
  90. forEach([null, undefined, true, false, 42, 0, {}, [], function () {}, /a/g], function (nonString) {
  91. t['throws'](
  92. // @ts-expect-error
  93. function () { SLOT.set({}, nonString); },
  94. TypeError,
  95. inspect(nonString) + ' is not a String'
  96. );
  97. });
  98. var o = function () {};
  99. t.equal(SLOT.get(o, 'f'), undefined, 'slot not set');
  100. SLOT.set(o, 'f', 42);
  101. t.equal(SLOT.get(o, 'f'), 42, 'slot was set');
  102. SLOT.set(o, 'f', Infinity);
  103. t.equal(SLOT.get(o, 'f'), Infinity, 'slot was set again');
  104. t.end();
  105. });