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