index.js 2.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
'use strict';

var test = require('tape');
var inspect = require('object-inspect');
var forEach = require('foreach');

var SLOT = require('../');

test('assert', function (t) {
	forEach([null, undefined, true, false, 'foo', '', 42, 0], function (primitive) {
		t['throws'](
			function () { SLOT.assert(primitive, ''); },
			TypeError,
			inspect(primitive) + ' is not an Object'
		);
	});

	forEach([null, undefined, true, false, 42, 0, {}, [], function () {}, /a/g], function (nonString) {
		t['throws'](
			function () { SLOT.assert({}, nonString); },
			TypeError,
			inspect(nonString) + ' is not a String'
		);
	});

	t['throws'](
		function () { SLOT.assert({}, 'whatever'); },
		TypeError,
		'nonexistent slot throws'
	);

	var o = {};
	SLOT.set(o, 'x');
	t.doesNotThrow(function () { SLOT.assert(o, 'x'); }, 'existent slot noops');

	t.end();
});

test('has', function (t) {
	forEach([null, undefined, true, false, 'foo', '', 42, 0], function (primitive) {
		t['throws'](
			function () { SLOT.has(primitive, ''); },
			TypeError,
			inspect(primitive) + ' is not an Object'
		);
	});

	forEach([null, undefined, true, false, 42, 0, {}, [], function () {}, /a/g], function (nonString) {
		t['throws'](
			function () { SLOT.has({}, nonString); },
			TypeError,
			inspect(nonString) + ' is not a String'
		);
	});

	var o = {};

	t.equal(SLOT.has(o, 'nonexistent'), false, 'nonexistent slot yields false');

	SLOT.set(o, 'foo');
	t.equal(SLOT.has(o, 'foo'), true, 'existent slot yields true');

	t.end();
});

test('get', function (t) {
	forEach([null, undefined, true, false, 'foo', '', 42, 0], function (primitive) {
		t['throws'](
			function () { SLOT.get(primitive, ''); },
			TypeError,
			inspect(primitive) + ' is not an Object'
		);
	});

	forEach([null, undefined, true, false, 42, 0, {}, [], function () {}, /a/g], function (nonString) {
		t['throws'](
			function () { SLOT.get({}, nonString); },
			TypeError,
			inspect(nonString) + ' is not a String'
		);
	});

	var o = {};
	t.equal(SLOT.get(o, 'nonexistent'), undefined, 'nonexistent slot is undefined');

	var v = {};
	SLOT.set(o, 'f', v);
	t.equal(SLOT.get(o, 'f'), v, '"get" retrieves value set by "set"');

	t.end();
});

test('set', function (t) {
	forEach([null, undefined, true, false, 'foo', '', 42, 0], function (primitive) {
		t['throws'](
			function () { SLOT.set(primitive, ''); },
			TypeError,
			inspect(primitive) + ' is not an Object'
		);
	});

	forEach([null, undefined, true, false, 42, 0, {}, [], function () {}, /a/g], function (nonString) {
		t['throws'](
			function () { SLOT.set({}, nonString); },
			TypeError,
			inspect(nonString) + ' is not a String'
		);
	});

	var o = function () {};
	t.equal(SLOT.get(o, 'f'), undefined, 'slot not set');

	SLOT.set(o, 'f', 42);
	t.equal(SLOT.get(o, 'f'), 42, 'slot was set');

	SLOT.set(o, 'f', Infinity);
	t.equal(SLOT.get(o, 'f'), Infinity, 'slot was set again');

	t.end();
});