• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../../common');
3const assert = require('assert');
4const addon = require(`./build/${common.buildType}/6_object_wrap`);
5
6const getterOnlyErrorRE =
7  /^TypeError: Cannot set property .* of #<.*> which has only a getter$/;
8
9const valueDescriptor = Object.getOwnPropertyDescriptor(
10  addon.MyObject.prototype, 'value');
11const valueReadonlyDescriptor = Object.getOwnPropertyDescriptor(
12  addon.MyObject.prototype, 'valueReadonly');
13const plusOneDescriptor = Object.getOwnPropertyDescriptor(
14  addon.MyObject.prototype, 'plusOne');
15assert.strictEqual(typeof valueDescriptor.get, 'function');
16assert.strictEqual(typeof valueDescriptor.set, 'function');
17assert.strictEqual(valueDescriptor.value, undefined);
18assert.strictEqual(valueDescriptor.enumerable, false);
19assert.strictEqual(valueDescriptor.configurable, false);
20assert.strictEqual(typeof valueReadonlyDescriptor.get, 'function');
21assert.strictEqual(valueReadonlyDescriptor.set, undefined);
22assert.strictEqual(valueReadonlyDescriptor.value, undefined);
23assert.strictEqual(valueReadonlyDescriptor.enumerable, false);
24assert.strictEqual(valueReadonlyDescriptor.configurable, false);
25
26assert.strictEqual(plusOneDescriptor.get, undefined);
27assert.strictEqual(plusOneDescriptor.set, undefined);
28assert.strictEqual(typeof plusOneDescriptor.value, 'function');
29assert.strictEqual(plusOneDescriptor.enumerable, false);
30assert.strictEqual(plusOneDescriptor.configurable, false);
31
32const obj = new addon.MyObject(9);
33assert.strictEqual(obj.value, 9);
34obj.value = 10;
35assert.strictEqual(obj.value, 10);
36assert.strictEqual(obj.valueReadonly, 10);
37assert.throws(() => { obj.valueReadonly = 14; }, getterOnlyErrorRE);
38assert.strictEqual(obj.plusOne(), 11);
39assert.strictEqual(obj.plusOne(), 12);
40assert.strictEqual(obj.plusOne(), 13);
41
42assert.strictEqual(obj.multiply().value, 13);
43assert.strictEqual(obj.multiply(10).value, 130);
44
45const newobj = obj.multiply(-1);
46assert.strictEqual(newobj.value, -13);
47assert.strictEqual(newobj.valueReadonly, -13);
48assert.notStrictEqual(obj, newobj);
49