Skip to content

Commit

Permalink
[Tests] more accurate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed May 24, 2022
1 parent af13e9e commit 6716f45
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

"rules": {
"id-length": 0,
"multiline-comment-style": "off",
"new-cap": [2, {
"capIsNewExceptions": [
"Get",
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
"is-negative-zero": "^2.0.2",
"is-string": "^1.0.7"
},
"testling": {
"files": "test/**/*.js"
},
"auto-changelog": {
"output": "CHANGELOG.md",
"template": "keepachangelog",
Expand Down
13 changes: 8 additions & 5 deletions test/implementation.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
'use strict';

var indexOf = require('../implementation');
var implementation = require('../implementation');
var callBind = require('call-bind');
var test = require('tape');
var hasStrictMode = require('has-strict-mode')();

var runTests = require('./tests');

test('as a function', function (t) {
t.test('bad array/this value', function (st) {
st['throws'](callBind(indexOf, null, undefined, 'a'), TypeError, 'undefined is not an object');
st['throws'](callBind(indexOf, null, null, 'a'), TypeError, 'null is not an object');
t.test('bad receiver', { skip: !hasStrictMode }, function (st) {
/* eslint no-useless-call: 1 */
st['throws'](function () { implementation.call(undefined, 'a'); }, TypeError, 'undefined is not an object');
st['throws'](function () { implementation.call(null, 'a'); }, TypeError, 'null is not an object');
st.end();
});

runTests(callBind(indexOf), t);
runTests(callBind(implementation), t);

t.end();
});
11 changes: 5 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
'use strict';

var indexOf = require('..');
var callBind = require('call-bind');
var index = require('../');
var test = require('tape');
var runTests = require('./tests');

test('as a function', function (t) {
t.test('bad array/this value', function (st) {
st['throws'](callBind(indexOf, null, undefined, 'a'), TypeError, 'undefined is not an object');
st['throws'](callBind(indexOf, null, null, 'a'), TypeError, 'null is not an object');
t.test('bad array', function (st) {
st['throws'](function () { index(undefined, 'a'); }, TypeError, 'undefined is not an object');
st['throws'](function () { index(null, 'a'); }, TypeError, 'null is not an object');
st.end();
});

runTests(indexOf, t);
runTests(index, t);

t.end();
});
4 changes: 4 additions & 0 deletions test/shimmed.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var orig = Array.prototype.indexOf;

require('../auto');

var test = require('tape');
Expand All @@ -13,6 +15,8 @@ var hasStrictMode = require('has-strict-mode')();
var runTests = require('./tests');

test('shimmed', function (t) {
t.comment('shimmed: ' + (orig === Array.prototype.indexOf ? 'no' : 'yes'));

t.equal(Array.prototype.indexOf.length, 1, 'Array#indexOf has a length of 1');
t.test('Function name', { skip: !functionsHaveNames }, function (st) {
st.equal(Array.prototype.indexOf.name, 'indexOf', 'Array#indexOf has name "indexOf"');
Expand Down
20 changes: 14 additions & 6 deletions test/tests.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
'use strict';

/*
* var canDistinguishSparseFromUndefined = 0 in [undefined]; // IE 6 - 8 have a bug where this returns false.
* var undefinedIfNoSparseBug = canDistinguishSparseFromUndefined ? undefined : { valueOf: function () { return 0; } };
*/
// var canDistinguishSparseFromUndefined = 0 in [undefined]; // IE 6 - 8 have a bug where this returns false.
// var undefinedIfNoSparseBug = canDistinguishSparseFromUndefined ? undefined : { valueOf: function () { return 0; } };

// eslint-disable-next-line no-sparse-arrays, array-bracket-spacing
var holesExist = !(0 in [, ]); // FF 3 fails this check

module.exports = function (indexOf, t) {
t.equal(indexOf([], undefined), -1, 'empty array + undefined yields -1');
t.equal(indexOf([1], 1), 0, 'array + only item yields its index');
t.equal(indexOf([NaN], NaN), -1, 'array with NaN + NaN yields -1');
// eslint-disable-next-line no-sparse-arrays
t.equal(indexOf([1, , 3], undefined), -1, 'sparse array + undefined yields -1');

t.equal(
indexOf([1, , 3], undefined), // eslint-disable-line no-sparse-arrays
-1,
'sparse array + undefined yields -1',
{ skip: !holesExist }
);

t.equal([0, 1].indexOf(1, 2), -1, 'Firefox 2 bug');
};

0 comments on commit 6716f45

Please sign in to comment.