Skip to content

Commit 77d8197

Browse files
authored
node-api: use local files for instanceof test
PR-URL: #60190 Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 7c0995c commit 77d8197

File tree

3 files changed

+440
-49
lines changed

3 files changed

+440
-49
lines changed

test/js-native-api/test_general/testInstanceOf.js

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,9 @@
11
'use strict';
22
const common = require('../../common');
3-
const fs = require('fs');
43
const assert = require('assert');
54

65
// Addon is referenced through the eval expression in testFile
76
const addon = require(`./build/${common.buildType}/test_general`);
8-
const path = require('path');
9-
10-
// This test depends on a number of V8 tests.
11-
const v8TestsDir = path.resolve(__dirname, '..', '..', '..', 'deps', 'v8',
12-
'test', 'mjsunit');
13-
const v8TestsDirExists = fs.existsSync(v8TestsDir);
14-
15-
// The following assert functions are referenced by v8's unit tests
16-
// See for instance deps/v8/test/mjsunit/instanceof.js
17-
// eslint-disable-next-line no-unused-vars
18-
function assertTrue(assertion) {
19-
return assert.strictEqual(assertion, true);
20-
}
21-
22-
// eslint-disable-next-line no-unused-vars
23-
function assertFalse(assertion) {
24-
assert.strictEqual(assertion, false);
25-
}
26-
27-
// eslint-disable-next-line no-unused-vars
28-
function assertEquals(leftHandSide, rightHandSide) {
29-
assert.strictEqual(leftHandSide, rightHandSide);
30-
}
31-
32-
// eslint-disable-next-line no-unused-vars
33-
function assertThrows(statement) {
34-
assert.throws(function() {
35-
eval(statement);
36-
}, Error);
37-
}
38-
39-
function testFile(fileName) {
40-
try {
41-
const contents = fs.readFileSync(fileName, { encoding: 'utf8' });
42-
eval(contents.replace(/[(]([^\s(]+)\s+instanceof\s+([^)]+)[)]/g,
43-
'(addon.doInstanceOf($1, $2))'));
44-
} catch (err) {
45-
// This test depends on V8 test files, which may not exist in downloaded
46-
// archives. Emit a warning if the tests cannot be found instead of failing.
47-
if (err.code === 'ENOENT' && !v8TestsDirExists)
48-
process.emitWarning(`test file ${fileName} does not exist.`);
49-
else
50-
throw err;
51-
}
52-
}
53-
54-
testFile(path.join(v8TestsDir, 'instanceof.js'));
55-
testFile(path.join(v8TestsDir, 'instanceof-2.js'));
567

578
// We can only perform this test if we have a working Symbol.hasInstance
589
if (typeof Symbol !== 'undefined' && 'hasInstance' in Symbol &&
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// This test is adopted from V8's test suite.
2+
// See deps/v8/test/mjsunit/instanceof.js in Node.js source repository.
3+
//
4+
// Copyright 2008 the V8 project authors. All rights reserved.
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, are permitted provided that the following conditions are
7+
// met:
8+
//
9+
// * Redistributions of source code must retain the above copyright
10+
// notice, this list of conditions and the following disclaimer.
11+
// * Redistributions in binary form must reproduce the above
12+
// copyright notice, this list of conditions and the following
13+
// disclaimer in the documentation and/or other materials provided
14+
// with the distribution.
15+
// * Neither the name of Google Inc. nor the names of its
16+
// contributors may be used to endorse or promote products derived
17+
// from this software without specific prior written permission.
18+
//
19+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
'use strict';
31+
32+
const common = require('../../common');
33+
const addon = require(`./build/${common.buildType}/test_general`);
34+
const assert = require('assert');
35+
36+
assert.ok(addon.doInstanceOf({}, Object));
37+
assert.ok(addon.doInstanceOf([], Object));
38+
39+
assert.ok(!addon.doInstanceOf({}, Array));
40+
assert.ok(addon.doInstanceOf([], Array));
41+
42+
function TestChains() {
43+
const A = {};
44+
const B = {};
45+
const C = {};
46+
Object.setPrototypeOf(B, A);
47+
Object.setPrototypeOf(C, B);
48+
49+
function F() { }
50+
F.prototype = A;
51+
assert.ok(addon.doInstanceOf(C, F));
52+
assert.ok(addon.doInstanceOf(B, F));
53+
assert.ok(!addon.doInstanceOf(A, F));
54+
55+
F.prototype = B;
56+
assert.ok(addon.doInstanceOf(C, F));
57+
assert.ok(!addon.doInstanceOf(B, F));
58+
assert.ok(!addon.doInstanceOf(A, F));
59+
60+
F.prototype = C;
61+
assert.ok(!addon.doInstanceOf(C, F));
62+
assert.ok(!addon.doInstanceOf(B, F));
63+
assert.ok(!addon.doInstanceOf(A, F));
64+
}
65+
66+
TestChains();
67+
68+
function TestExceptions() {
69+
function F() { }
70+
const items = [ 1, new Number(42),
71+
true,
72+
'string', new String('hest'),
73+
{}, [],
74+
F, new F(),
75+
Object, String ];
76+
77+
let exceptions = 0;
78+
let instanceofs = 0;
79+
80+
for (let i = 0; i < items.length; i++) {
81+
for (let j = 0; j < items.length; j++) {
82+
try {
83+
if (addon.doInstanceOf(items[i], items[j])) instanceofs++;
84+
} catch (e) {
85+
assert.ok(addon.doInstanceOf(e, TypeError));
86+
exceptions++;
87+
}
88+
}
89+
}
90+
assert.strictEqual(instanceofs, 10);
91+
assert.strictEqual(exceptions, 88);
92+
93+
// Make sure to throw an exception if the function prototype
94+
// isn't a proper JavaScript object.
95+
function G() { }
96+
G.prototype = undefined;
97+
assert.throws(function() {
98+
addon.doInstanceOf({}, G);
99+
}, Error);
100+
}
101+
102+
TestExceptions();

0 commit comments

Comments
 (0)