node-api: use local files for instanceof test · nodejs/node@6ab83cf
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();