From 3dd18d65748efb4af9b8ca66f8d8c5521d8f2dec Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sat, 10 Aug 2019 23:08:23 -0700 Subject: [PATCH] [Tests] add tests --- package.json | 10 +++++++-- test/index.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 test/index.js diff --git a/package.json b/package.json index 7a20d06..27b9b94 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..73688ac --- /dev/null +++ b/test/index.js @@ -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(); +});