node/test/addons-napi/test_general/testInstanceOf.js
Ben Noordhuis d9b59def72
build,test: make building addon tests less fragile
* Get rid of recursive `make` when building the node binary.  An earlier
  commit makes GYP write out rules that we can use for proper dependency
  tracking.

* Use module name 'binding' in addons.md and addons-napi/*/binding.gyp.
  This massively simplifies the logic for generating the build rules.

* Check in auto-generated add-on tests from `doc/api/addons.md`.  The
  files change rarely and generating them dynamically causes no end of
  race conditions and special-casing during the build.

PR-URL: https://github.com/nodejs/node/pull/17407
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
2018-01-21 02:19:46 +01:00

98 lines
2.8 KiB
JavaScript

'use strict';
const fs = require('fs');
const common = require('../../common');
const assert = require('assert');
// addon is referenced through the eval expression in testFile
// eslint-disable-next-line no-unused-vars
const addon = require(`./build/${common.buildType}/binding`);
const path = require('path');
// This test depends on a number of V8 tests.
const v8TestsDir = path.resolve(__dirname, '..', '..', '..', 'deps', 'v8',
'test', 'mjsunit');
const v8TestsDirExists = fs.existsSync(v8TestsDir);
// The following assert functions are referenced by v8's unit tests
// See for instance deps/v8/test/mjsunit/instanceof.js
// eslint-disable-next-line no-unused-vars
function assertTrue(assertion) {
return assert.strictEqual(true, assertion);
}
// eslint-disable-next-line no-unused-vars
function assertFalse(assertion) {
assert.strictEqual(false, assertion);
}
// eslint-disable-next-line no-unused-vars
function assertEquals(leftHandSide, rightHandSide) {
assert.strictEqual(leftHandSide, rightHandSide);
}
// eslint-disable-next-line no-unused-vars
function assertThrows(statement) {
assert.throws(function() {
eval(statement);
}, Error);
}
function testFile(fileName) {
try {
const contents = fs.readFileSync(fileName, { encoding: 'utf8' });
eval(contents.replace(/[(]([^\s(]+)\s+instanceof\s+([^)]+)[)]/g,
'(addon.doInstanceOf($1, $2))'));
} catch (err) {
// This test depends on V8 test files, which may not exist in downloaded
// archives. Emit a warning if the tests cannot be found instead of failing.
if (err.code === 'ENOENT' && !v8TestsDirExists)
process.emitWarning(`test file ${fileName} does not exist.`);
else
throw err;
}
}
testFile(path.join(v8TestsDir, 'instanceof.js'));
testFile(path.join(v8TestsDir, 'instanceof-2.js'));
// We can only perform this test if we have a working Symbol.hasInstance
if (typeof Symbol !== 'undefined' && 'hasInstance' in Symbol &&
typeof Symbol.hasInstance === 'symbol') {
function compareToNative(theObject, theConstructor) {
assert.strictEqual(
addon.doInstanceOf(theObject, theConstructor),
(theObject instanceof theConstructor)
);
}
function MyClass() {}
Object.defineProperty(MyClass, Symbol.hasInstance, {
value: function(candidate) {
return 'mark' in candidate;
}
});
function MySubClass() {}
MySubClass.prototype = new MyClass();
let x = new MySubClass();
let y = new MySubClass();
x.mark = true;
compareToNative(x, MySubClass);
compareToNative(y, MySubClass);
compareToNative(x, MyClass);
compareToNative(y, MyClass);
x = new MyClass();
y = new MyClass();
x.mark = true;
compareToNative(x, MySubClass);
compareToNative(y, MySubClass);
compareToNative(x, MyClass);
compareToNative(y, MyClass);
}