index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict';
  2. var test = require('tape');
  3. var inspect = require('object-inspect');
  4. var forEach = require('foreach');
  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.end();
  30. });
  31. test('has', function (t) {
  32. forEach([null, undefined, true, false, 'foo', '', 42, 0], function (primitive) {
  33. t['throws'](
  34. function () { SLOT.has(primitive, ''); },
  35. TypeError,
  36. inspect(primitive) + ' is not an Object'
  37. );
  38. });
  39. forEach([null, undefined, true, false, 42, 0, {}, [], function () {}, /a/g], function (nonString) {
  40. t['throws'](
  41. function () { SLOT.has({}, nonString); },
  42. TypeError,
  43. inspect(nonString) + ' is not a String'
  44. );
  45. });
  46. var o = {};
  47. t.equal(SLOT.has(o, 'nonexistent'), false, 'nonexistent slot yields false');
  48. SLOT.set(o, 'foo');
  49. t.equal(SLOT.has(o, 'foo'), true, 'existent slot yields true');
  50. t.end();
  51. });
  52. test('get', function (t) {
  53. forEach([null, undefined, true, false, 'foo', '', 42, 0], function (primitive) {
  54. t['throws'](
  55. function () { SLOT.get(primitive, ''); },
  56. TypeError,
  57. inspect(primitive) + ' is not an Object'
  58. );
  59. });
  60. forEach([null, undefined, true, false, 42, 0, {}, [], function () {}, /a/g], function (nonString) {
  61. t['throws'](
  62. function () { SLOT.get({}, nonString); },
  63. TypeError,
  64. inspect(nonString) + ' is not a String'
  65. );
  66. });
  67. var o = {};
  68. t.equal(SLOT.get(o, 'nonexistent'), undefined, 'nonexistent slot is undefined');
  69. var v = {};
  70. SLOT.set(o, 'f', v);
  71. t.equal(SLOT.get(o, 'f'), v, '"get" retrieves value set by "set"');
  72. t.end();
  73. });
  74. test('set', function (t) {
  75. forEach([null, undefined, true, false, 'foo', '', 42, 0], function (primitive) {
  76. t['throws'](
  77. function () { SLOT.set(primitive, ''); },
  78. TypeError,
  79. inspect(primitive) + ' is not an Object'
  80. );
  81. });
  82. forEach([null, undefined, true, false, 42, 0, {}, [], function () {}, /a/g], function (nonString) {
  83. t['throws'](
  84. function () { SLOT.set({}, nonString); },
  85. TypeError,
  86. inspect(nonString) + ' is not a String'
  87. );
  88. });
  89. var o = function () {};
  90. t.equal(SLOT.get(o, 'f'), undefined, 'slot not set');
  91. SLOT.set(o, 'f', 42);
  92. t.equal(SLOT.get(o, 'f'), 42, 'slot was set');
  93. SLOT.set(o, 'f', Infinity);
  94. t.equal(SLOT.get(o, 'f'), Infinity, 'slot was set again');
  95. t.end();
  96. });