Skip to content

Commit 545162b

Browse files
vmorozaduh95
authored andcommitted
node-api: use local files for instanceof test
PR-URL: #60190 Reviewed-By: Chengzhong Wu <[email protected]>
1 parent bbca575 commit 545162b

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
constcommon=require('../../common');
3-
constfs=require('fs');
43
constassert=require('assert');
54

65
// Addon is referenced through the eval expression in testFile
76
constaddon=require(`./build/${common.buildType}/test_general`);
8-
constpath=require('path');
9-
10-
// This test depends on a number of V8 tests.
11-
constv8TestsDir=path.resolve(__dirname,'..','..','..','deps','v8',
12-
'test','mjsunit');
13-
constv8TestsDirExists=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-
functionassertTrue(assertion){
19-
returnassert.strictEqual(assertion,true);
20-
}
21-
22-
// eslint-disable-next-line no-unused-vars
23-
functionassertFalse(assertion){
24-
assert.strictEqual(assertion,false);
25-
}
26-
27-
// eslint-disable-next-line no-unused-vars
28-
functionassertEquals(leftHandSide,rightHandSide){
29-
assert.strictEqual(leftHandSide,rightHandSide);
30-
}
31-
32-
// eslint-disable-next-line no-unused-vars
33-
functionassertThrows(statement){
34-
assert.throws(function(){
35-
eval(statement);
36-
},Error);
37-
}
38-
39-
functiontestFile(fileName){
40-
try{
41-
constcontents=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-
throwerr;
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(typeofSymbol!=='undefined'&&'hasInstance'inSymbol&&
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+
constcommon=require('../../common');
33+
constaddon=require(`./build/${common.buildType}/test_general`);
34+
constassert=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+
functionTestChains(){
43+
constA={};
44+
constB={};
45+
constC={};
46+
Object.setPrototypeOf(B,A);
47+
Object.setPrototypeOf(C,B);
48+
49+
functionF(){}
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+
functionTestExceptions(){
69+
functionF(){}
70+
constitems=[1,newNumber(42),
71+
true,
72+
'string',newString('hest'),
73+
{},[],
74+
F,newF(),
75+
Object,String];
76+
77+
letexceptions=0;
78+
letinstanceofs=0;
79+
80+
for(leti=0;i<items.length;i++){
81+
for(letj=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+
functionG(){}
96+
G.prototype=undefined;
97+
assert.throws(function(){
98+
addon.doInstanceOf({},G);
99+
},Error);
100+
}
101+
102+
TestExceptions();

0 commit comments

Comments
(0)