Files
scriptc/tests/corpus/8190-assert-function-values.cjs
T
Chris Tate e00d3fd911 Check comparison operands and walk instanceof chains as Node does
- The assert comparison methods read as function values (equal,
  strictEqual, deepEqual, partialDeepStrictEqual and their negations)
  throw Node's ERR_MISSING_ARGS TypeError, `The "actual" and "expected"
  arguments must be specified`, when called with fewer than two
  arguments, where `[assert.equal][0]()` passed.
- Untyped `instanceof Object` starts at the value's prototype, so
  Object.prototype itself, whose prototype is null, is not an Object.
- Untyped `instanceof Function` follows the whole prototype chain through
  objects and functions: an object whose prototype is a function, or a
  function whose prototype was set to such an object, is a Function.
2026-10-24 16:22:03 -05:00

46 lines
2.2 KiB
JavaScript

'use strict';
// assert methods are function values: stored, passed and called through
// aliases, they assert as the methods do, and every read is the same
// function. A constant bound to a method and called directly reports
// the call as Node quotes it.
const assert = require('assert');
const { strictEqual, deepStrictEqual } = require('assert');
const strict = require('assert/strict');
const EventEmitter = require('events');
console.log(typeof assert.fail, assert.fail === assert.fail, strict.equal === assert.strictEqual, strictEqual === assert.strictEqual);
const ok = assert.ok;
ok(1);
const tryIt = (label, fn) => { try { fn(); console.log(label, 'passed'); } catch (e) { console.log(label, e.name, e.code, JSON.stringify(e.message), e.generatedMessage); } };
tryIt('ok', () => ok(0));
tryIt('ok none', () => ok());
tryIt('ok msg', () => ok(false, 'nope'));
tryIt('ok err', () => ok(false, new TypeError('own')));
tryIt('fail', () => assert.fail.call(null));
tryIt('fail msg', () => (0, assert.fail)('boom'));
tryIt('equal', () => assert.equal.call(null, 1, '1'));
tryIt('equal fail', () => [assert.equal][0](1, 2));
tryIt('notEqual fail', () => [assert.notEqual][0](1, '1'));
tryIt('strict', () => strictEqual(1, 2));
tryIt('deep', () => deepStrictEqual({ a: 1 }, { a: 1 }));
tryIt('deep fail', () => [deepStrictEqual][0]({ a: 1 }, { a: 2 }, 'differs'));
tryIt('ifError', () => [assert.ifError][0](null));
const checks = [assert.ok, assert.fail, assert.strictEqual];
checks.forEach((check, i) => console.log(i, typeof check, check === checks[i], check.name));
[1, 'x', true].forEach((value) => assert.ok(value));
console.log('done');
// The comparison methods need both operands, as Node's ERR_MISSING_ARGS
// TypeError says, also through function values.
const comparisons = [assert.equal, assert.notEqual, assert.strictEqual, assert.notStrictEqual,
assert.deepEqual, assert.notDeepEqual, assert.deepStrictEqual, assert.notDeepStrictEqual];
for (const compare of comparisons) {
for (const args of [[], [1]]) {
try {
compare(...args);
console.log(compare.name, args.length, 'passed');
} catch (e) {
console.log(compare.name, args.length, e.name, e.code, e.message);
}
}
}