Skip to content

Commit

Permalink
[Tests] extract common helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jan 15, 2025
1 parent bfe049d commit c370ecf
Show file tree
Hide file tree
Showing 11 changed files with 653 additions and 577 deletions.
5 changes: 5 additions & 0 deletions test/helpers/clearBuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = function clearBuffer(buffer) {
new DataView(buffer).setFloat64(0, 0, true); // clear the buffer
};
9 changes: 9 additions & 0 deletions test/helpers/getNamelessFunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = function getNamelessFunction() {
var f = Object(function () {});
try {
delete f.name;
} catch (e) { /**/ }
return f;
};
18 changes: 18 additions & 0 deletions test/helpers/kludgeMatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

var assign = require('object.assign');

var hasLastIndex = 'lastIndex' in (/a/).exec('a'); // IE 8
var hasGroups = 'groups' in (/a/).exec('a'); // modern engines

module.exports = function kludgeMatch(R, matchObject) {
if (hasGroups) {
assign(matchObject, { groups: matchObject.groups });
}
if (hasLastIndex) {
assign(matchObject, { lastIndex: matchObject.lastIndex || R.lastIndex });
} else {
delete matchObject.lastIndex; // eslint-disable-line no-param-reassign
}
return matchObject;
};
24 changes: 24 additions & 0 deletions test/helpers/makeAsyncFromSyncIterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

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

module.exports = function makeAsyncFromSyncIterator(CreateAsyncFromSyncIterator, end, throwMethod, returnMethod) {
var i = 0;
var iterator = {
next: function next() {
try {
return {
done: i > end,
value: i
};
} finally {
i += 1;
}
},
'return': returnMethod,
'throw': throwMethod
};
var syncIteratorRecord = makeIteratorRecord(iterator);

return CreateAsyncFromSyncIterator(syncIteratorRecord);
};
9 changes: 9 additions & 0 deletions test/helpers/makeIteratorRecord.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = function makeIteratorRecord(iterator) {
return {
'[[Iterator]]': iterator,
'[[NextMethod]]': iterator.next,
'[[Done]]': false
};
};
18 changes: 18 additions & 0 deletions test/helpers/testAsyncIterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

module.exports = function testAsyncIterator(t, asyncIterator, expected) {
var results = arguments.length > 3 ? arguments[3] : [];

var nextResult = asyncIterator.next();

return Promise.resolve(nextResult).then(function (result) {
results.push(result);
if (!result.done && results.length < expected.length) {
t.deepEqual(result, { done: false, value: expected[results.length - 1] }, 'result ' + (results.length - 1));
return testAsyncIterator(t, asyncIterator, expected, results);
}

t.equal(results.length, expected.length, 'expected ' + expected.length + ', got ' + (result.done ? '' : 'more than ') + results.length);
return results.length;
});
};
11 changes: 11 additions & 0 deletions test/helpers/testIterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = function testIterator(t, iterator, expected) {
var resultCount = 0;
var result;
while (result = iterator.next(), !result.done && resultCount < expected.length + 1) { // eslint-disable-line no-sequences
t.deepEqual(result, { done: false, value: expected[resultCount] }, 'result ' + resultCount);
resultCount += 1;
}
t.equal(resultCount, expected.length, 'expected ' + expected.length + ', got ' + (result.done ? '' : 'more than ') + resultCount);
};
18 changes: 18 additions & 0 deletions test/helpers/testRESIterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

var v = require('es-value-fixtures');

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

module.exports = function testRegExpStringIterator(CreateRegExpStringIterator, t, regex, str, global, unicode, expected) {
var iterator = CreateRegExpStringIterator(regex, str, global, unicode);
t.equal(typeof iterator, 'object', 'iterator is an object');
t.equal(typeof iterator.next, 'function', '`.next` is a function');

t.test('has symbols', { skip: !v.hasSymbols }, function (st) {
st.equal(typeof iterator[Symbol.iterator], 'function', '[`Symbol.iterator`] is a function');
st.end();
});

testIterator(t, iterator, expected);
};
10 changes: 10 additions & 0 deletions test/helpers/throwsSentinel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

module.exports = function throwsSentinel(fn, sentinel, message) {
try {
fn();
this.fail('did not throw: ' + message);
} catch (e) {
this.equal(e, sentinel, message);
}
};
164 changes: 164 additions & 0 deletions test/helpers/v.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
'use strict';

var v = require('es-value-fixtures');
var hasBigInts = require('has-bigints')();
var isCore = require('is-core-module');

var leadingPoo = '\uD83D';
var trailingPoo = '\uDCA9';
var wholePoo = leadingPoo + trailingPoo;

var msPerSecond = 1e3;
var msPerMinute = 60 * msPerSecond;
var msPerHour = 60 * msPerMinute;
var msPerDay = 24 * msPerHour;

var unknowns = [].concat(
v.primitives,
v.objects
);
var allButSyms = [].concat(
v.objects,
v.nonSymbolPrimitives
);
var invalidTATypes = [].concat(
v.nonStrings,
'not a valid type'
);
var nonFiniteNumbers = [].concat(
v.infinities,
NaN
);
var notInts = [].concat(
v.nonNumbers,
v.nonIntegerNumbers,
nonFiniteNumbers,
[],
new Date()
);

var elementSizes = {
__proto__: null,
$Int8Array: 1,
$Uint8Array: 1,
$Uint8ClampedArray: 1,
$Int16Array: 2,
$Uint16Array: 2,
$Int32Array: 4,
$Uint32Array: 4,
$BigInt64Array: 8,
$BigUint64Array: 8,
$Float32Array: 4,
$Float64Array: 8
};

var unclampedUnsignedIntegerTypes = [
'Int8',
'Int16',
'Int32'
];
var clampedTypes = [
'Uint8C'
];
var unclampedSignedIntegerTypes = [
'Uint8',
'Uint16',
'Uint32'
];
var unclampedIntegerTypes = [].concat(
unclampedUnsignedIntegerTypes,
unclampedSignedIntegerTypes
);
var floatTypes = [
'Float32',
'Float64'
];
var integerTypes = [].concat(
unclampedIntegerTypes,
clampedTypes
);
var bigIntTypes = [
'BigInt64',
'BigUint64'
];
var numberTypes = [].concat(
floatTypes,
integerTypes
);
var nonUnclampedIntegerTypes = [].concat(
floatTypes,
bigIntTypes
);
var unsignedElementTypes = [].concat(
unclampedSignedIntegerTypes,
hasBigInts ? 'BigUint64' : []
);
var signedElementTypes = [].concat(
unclampedUnsignedIntegerTypes,
floatTypes,
hasBigInts ? 'BigInt64' : []
);
var allTypes = [].concat(
numberTypes,
hasBigInts ? bigIntTypes : []
);
var nonTATypes = [].concat(
v.nonStrings,
'',
'Byte'
);

var canDistinguishSparseFromUndefined = 0 in [undefined]; // IE 6 - 8 have a bug where this returns false

// IE 9 does not throw in strict mode when writability/configurability/extensibility is violated
var noThrowOnStrictViolation = (function () {
try {
delete [].length;
return true;
} catch (e) {
}
return false;
}());

var isBigIntTAType = function isBigIntTAType(type) {
return type.slice(0, 3) === 'Big';
};

/* globals postMessage */
var canDetach = typeof structuredClone === 'function' || typeof postMessage === 'function' || isCore('worker_threads');

module.exports = {
poo: {
leading: leadingPoo,
trailing: trailingPoo,
whole: wholePoo
},
hasBigInts: hasBigInts,
msPerSecond: msPerSecond,
msPerMinute: msPerMinute,
msPerHour: msPerHour,
msPerDay: msPerDay,
unknowns: unknowns,
allButSyms: allButSyms,
invalidTATypes: invalidTATypes,
nonFiniteNumbers: nonFiniteNumbers,
notInts: notInts,
elementSizes: elementSizes,
unclampedUnsignedIntegerTypes: unclampedUnsignedIntegerTypes,
clampedTypes: clampedTypes,
unclampedSignedIntegerTypes: unclampedSignedIntegerTypes,
unclampedIntegerTypes: unclampedIntegerTypes,
floatTypes: floatTypes,
integerTypes: integerTypes,
bigIntTypes: bigIntTypes,
numberTypes: numberTypes,
nonUnclampedIntegerTypes: nonUnclampedIntegerTypes,
unsignedElementTypes: unsignedElementTypes,
signedElementTypes: signedElementTypes,
allTypes: allTypes,
nonTATypes: nonTATypes,
canDistinguishSparseFromUndefined: canDistinguishSparseFromUndefined,
noThrowOnStrictViolation: noThrowOnStrictViolation,
isBigIntTAType: isBigIntTAType,
canDetach: canDetach
};
Loading

0 comments on commit c370ecf

Please sign in to comment.