Skip to content

Commit cc43f39

Browse files
committed
feat: port test_general V8 instanceof suites to CTS
1 parent 425c7d1 commit cc43f39

2 files changed

Lines changed: 434 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
31+
const addon = loadAddon('test_general');
32+
33+
assert.ok(addon.doInstanceOf({}, Object));
34+
assert.ok(addon.doInstanceOf([], Object));
35+
36+
assert.ok(!addon.doInstanceOf({}, Array));
37+
assert.ok(addon.doInstanceOf([], Array));
38+
39+
function TestChains() {
40+
const A = {};
41+
const B = {};
42+
const C = {};
43+
Object.setPrototypeOf(B, A);
44+
Object.setPrototypeOf(C, B);
45+
46+
function F() { }
47+
F.prototype = A;
48+
assert.ok(addon.doInstanceOf(C, F));
49+
assert.ok(addon.doInstanceOf(B, F));
50+
assert.ok(!addon.doInstanceOf(A, F));
51+
52+
F.prototype = B;
53+
assert.ok(addon.doInstanceOf(C, F));
54+
assert.ok(!addon.doInstanceOf(B, F));
55+
assert.ok(!addon.doInstanceOf(A, F));
56+
57+
F.prototype = C;
58+
assert.ok(!addon.doInstanceOf(C, F));
59+
assert.ok(!addon.doInstanceOf(B, F));
60+
assert.ok(!addon.doInstanceOf(A, F));
61+
}
62+
63+
TestChains();
64+
65+
function TestExceptions() {
66+
function F() { }
67+
const items = [ 1, new Number(42),
68+
true,
69+
'string', new String('hest'),
70+
{}, [],
71+
F, new F(),
72+
Object, String ];
73+
74+
let exceptions = 0;
75+
let instanceofs = 0;
76+
77+
for (let i = 0; i < items.length; i++) {
78+
for (let j = 0; j < items.length; j++) {
79+
try {
80+
if (addon.doInstanceOf(items[i], items[j])) instanceofs++;
81+
} catch (e) {
82+
assert.ok(addon.doInstanceOf(e, TypeError));
83+
exceptions++;
84+
}
85+
}
86+
}
87+
assert.strictEqual(instanceofs, 10);
88+
assert.strictEqual(exceptions, 88);
89+
90+
// Make sure to throw an exception if the function prototype
91+
// isn't a proper JavaScript object.
92+
function G() { }
93+
G.prototype = undefined;
94+
assert.throws(function() {
95+
addon.doInstanceOf({}, G);
96+
}, Error);
97+
}
98+
99+
TestExceptions();

0 commit comments

Comments
 (0)