index.js 3.0 KB

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