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