Skip to content

Commit

Permalink
[Tests] add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Aug 11, 2019
1 parent 472fb41 commit 3dd18d6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"scripts": {
"lint": "eslint .",
"pretest": "npm run lint",
"test": "echo \"Error: no test specified\" && exit 1"
"tests-only": "node test",
"test": "npm run tests-only",
"posttest": "npx aud"
},
"repository": {
"type": "git",
Expand All @@ -28,7 +30,11 @@
"homepage": "https://github.com/ljharb/unbox-primitive#readme",
"devDependencies": {
"@ljharb/eslint-config": "^14.0.2",
"eslint": "^6.1.0"
"eslint": "^6.1.0",
"for-each": "^0.3.3",
"object-inspect": "^1.6.0",
"object-is": "^1.0.1",
"tape": "^4.11.0"
},
"dependencies": {
"function-bind": "^1.1.1",
Expand Down
59 changes: 59 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

var test = require('tape');
var inspect = require('object-inspect');
var is = require('object-is');
var forEach = require('for-each');
var hasSymbols = require('has-symbols')();
var hasBigInts = require('has-bigints')();

var unboxPrimitive = require('..');

var debug = function (v, m) { return inspect(v) + ' ' + m; };

test('primitives', function (t) {
var primitives = [
true,
false,
'',
'foo',
42,
NaN,
Infinity,
0
];
if (hasSymbols) {
primitives.push(Symbol(), Symbol.iterator, Symbol('f'));
}
if (hasBigInts) {
primitives.push(BigInt(42), BigInt(0));
}
forEach(primitives, function (primitive) {
var obj = Object(primitive);
t.ok(
is(unboxPrimitive(obj), primitive),
debug(obj, 'unboxes to ' + inspect(primitive))
);
});

t.end();
});

test('objects', function (t) {
var objects = [
{},
[],
function () {},
/a/g,
new Date()
];
forEach(objects, function (object) {
t['throws'](
function () { unboxPrimitive(object); },
TypeError,
debug(object, 'is not a primitive')
);
});

t.end();
});

0 comments on commit 3dd18d6

Please sign in to comment.