Skip to content

Commit

Permalink
[Tests] use es-value-fixtures and for-each
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Dec 1, 2024
1 parent ec4dd52 commit ecacfa0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 35 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@
"@ljharb/eslint-config": "^21.1.1",
"auto-changelog": "^2.5.0",
"encoding": "^0.1.13",
"es-value-fixtures": "^1.5.0",
"eslint": "=8.8.0",
"has-symbols": "^1.0.3",
"for-each": "^0.3.3",
"in-publish": "^2.0.1",
"npmignore": "^0.3.1",
"nyc": "^10.3.2",
Expand Down
48 changes: 14 additions & 34 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,42 @@

var test = require('tape');
var inspect = require('object-inspect');
var whichBoxedPrimitive = require('..');
var forEach = require('for-each');
var v = require('es-value-fixtures');

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

var forEach = function (arr, func) {
var i;
for (i = 0; i < arr.length; ++i) {
func(arr[i], i, arr);
}
};

var hasSymbols = require('has-symbols')();
var hasBigInts = typeof BigInt === 'function';

var primitives = [
true,
false,
42,
NaN,
Infinity,
'',
'foo'
].concat(
hasSymbols ? [Symbol(), Symbol.iterator] : [],
hasBigInts ? BigInt(42) : []
);
var whichBoxedPrimitive = require('../');

var objects = [
/a/g,
new Date(),
function () {},
[],
{}
];
].concat(v.objects);

test('isBoxedPrimitive', function (t) {
t.test('unboxed primitives', function (st) {
forEach([null, undefined].concat(primitives), function (primitive) {
st.equal(null, whichBoxedPrimitive(primitive), debug(primitive, 'is a primitive, but not a boxed primitive'));
forEach(v.primitives, function (primitive) {
st.equal(null, whichBoxedPrimitive(primitive), inspect(primitive) + ' is a primitive, but not a boxed primitive');
});
st.end();
});

t.test('boxed primitives', function (st) {
forEach(primitives, function (primitive) {
var boxed = Object(primitive);
var expected = boxed.constructor.name;
st.equal(typeof expected, 'string', 'expected is string');
st.equal(whichBoxedPrimitive(boxed), expected, debug(boxed, 'is a boxed primitive: ' + expected));
forEach(v.primitives, function (primitive) {
if (primitive != null) { // eslint-disable-line eqeqeq
var boxed = Object(primitive);
var expected = boxed.constructor.name;
st.equal(typeof expected, 'string', 'expected is string');
st.equal(whichBoxedPrimitive(boxed), expected, inspect(boxed) + ' is a boxed primitive: ' + expected);
}
});
st.end();
});

t.test('non-primitive objects', function (st) {
forEach(objects, function (object) {
st.equal(undefined, whichBoxedPrimitive(object), debug(object, 'is not a primitive, boxed or otherwise'));
st.equal(undefined, whichBoxedPrimitive(object), inspect(object) + ' is not a primitive, boxed or otherwise');
});
st.end();
});
Expand Down

0 comments on commit ecacfa0

Please sign in to comment.