|
1 | | -'use strict'; |
2 | | -const common = require('../../common'); |
3 | | -const assert = require('assert'); |
4 | | - |
5 | | -// Testing api calls for a constructor that defines properties |
6 | | -const TestConstructor = require(`./build/${common.buildType}/test_constructor`); |
7 | | -const test_object = new TestConstructor(); |
8 | | - |
9 | | -assert.strictEqual(test_object.echo('hello'), 'hello'); |
10 | | - |
11 | | -test_object.readwriteValue = 1; |
12 | | -assert.strictEqual(test_object.readwriteValue, 1); |
13 | | -test_object.readwriteValue = 2; |
14 | | -assert.strictEqual(test_object.readwriteValue, 2); |
15 | | - |
16 | | -assert.throws(() => { test_object.readonlyValue = 3; }, |
17 | | - /^TypeError: Cannot assign to read only property 'readonlyValue' of object '#<MyObject>'$/); |
18 | | - |
19 | | -assert.ok(test_object.hiddenValue); |
20 | | - |
21 | | -// Properties with napi_enumerable attribute should be enumerable. |
22 | | -const propertyNames = []; |
23 | | -for (const name in test_object) { |
24 | | - propertyNames.push(name); |
25 | | -} |
26 | | -assert.ok(propertyNames.includes('echo')); |
27 | | -assert.ok(propertyNames.includes('readwriteValue')); |
28 | | -assert.ok(propertyNames.includes('readonlyValue')); |
29 | | -assert.ok(!propertyNames.includes('hiddenValue')); |
30 | | -assert.ok(!propertyNames.includes('readwriteAccessor1')); |
31 | | -assert.ok(!propertyNames.includes('readwriteAccessor2')); |
32 | | -assert.ok(!propertyNames.includes('readonlyAccessor1')); |
33 | | -assert.ok(!propertyNames.includes('readonlyAccessor2')); |
34 | | - |
35 | | -// The napi_writable attribute should be ignored for accessors. |
36 | | -test_object.readwriteAccessor1 = 1; |
37 | | -assert.strictEqual(test_object.readwriteAccessor1, 1); |
38 | | -assert.strictEqual(test_object.readonlyAccessor1, 1); |
39 | | -assert.throws(() => { test_object.readonlyAccessor1 = 3; }, |
40 | | - /^TypeError: Cannot assign to read only property 'readonlyAccessor1' of object '#<MyObject>'$/); |
41 | | -test_object.readwriteAccessor2 = 2; |
42 | | -assert.strictEqual(test_object.readwriteAccessor2, 2); |
43 | | -assert.strictEqual(test_object.readonlyAccessor2, 2); |
44 | | -assert.throws(() => { test_object.readonlyAccessor2 = 3; }, |
45 | | - /^TypeError: Cannot assign to read only property 'readonlyAccessor2' of object '#<MyObject>'$/); |
46 | | - |
47 | | -// validate that static properties are on the class as opposed |
48 | | -// to the instance |
49 | | -assert.strictEqual(TestConstructor.staticReadonlyAccessor1, 10); |
50 | | -assert.strictEqual(test_object.staticReadonlyAccessor1, undefined); |
| 1 | +'use strict'; |
| 2 | +const common = require('../../common'); |
| 3 | +const assert = require('assert'); |
| 4 | + |
| 5 | +// Testing api calls for a constructor that defines properties |
| 6 | +const TestConstructor = require(`./build/${common.buildType}/test_constructor`); |
| 7 | +const test_object = new TestConstructor(); |
| 8 | + |
| 9 | +assert.strictEqual(test_object.echo('hello'), 'hello'); |
| 10 | + |
| 11 | +test_object.readwriteValue = 1; |
| 12 | +assert.strictEqual(test_object.readwriteValue, 1); |
| 13 | +test_object.readwriteValue = 2; |
| 14 | +assert.strictEqual(test_object.readwriteValue, 2); |
| 15 | + |
| 16 | +assert.throws(() => { test_object.readonlyValue = 3; }, |
| 17 | + common.engineSpecificMessage({ |
| 18 | + v8: /^TypeError: Cannot assign to read only property 'readonlyValue' of object '#<MyObject>'$/, |
| 19 | + chakracore: /^TypeError: Assignment to read-only properties is not allowed in strict mode$/})); |
| 20 | + |
| 21 | +assert.ok(test_object.hiddenValue); |
| 22 | + |
| 23 | +// Properties with napi_enumerable attribute should be enumerable. |
| 24 | +const propertyNames = []; |
| 25 | +for (const name in test_object) { |
| 26 | + propertyNames.push(name); |
| 27 | +} |
| 28 | +assert.ok(propertyNames.includes('echo')); |
| 29 | +assert.ok(propertyNames.includes('readwriteValue')); |
| 30 | +assert.ok(propertyNames.includes('readonlyValue')); |
| 31 | +assert.ok(!propertyNames.includes('hiddenValue')); |
| 32 | +assert.ok(!propertyNames.includes('readwriteAccessor1')); |
| 33 | +assert.ok(!propertyNames.includes('readwriteAccessor2')); |
| 34 | +assert.ok(!propertyNames.includes('readonlyAccessor1')); |
| 35 | +assert.ok(!propertyNames.includes('readonlyAccessor2')); |
| 36 | + |
| 37 | +// The napi_writable attribute should be ignored for accessors. |
| 38 | +test_object.readwriteAccessor1 = 1; |
| 39 | +assert.strictEqual(test_object.readwriteAccessor1, 1); |
| 40 | +assert.strictEqual(test_object.readonlyAccessor1, 1); |
| 41 | +assert.throws(() => { test_object.readonlyAccessor1 = 3; }, |
| 42 | + common.engineSpecificMessage({ |
| 43 | + v8: /^TypeError: Cannot assign to read only property 'readonlyAccessor1' of object '#<MyObject>'$/, |
| 44 | + chakracore: /^TypeError: Assignment to read-only properties is not allowed in strict mode$/})); |
| 45 | +test_object.readwriteAccessor2 = 2; |
| 46 | +assert.strictEqual(test_object.readwriteAccessor2, 2); |
| 47 | +assert.strictEqual(test_object.readonlyAccessor2, 2); |
| 48 | +assert.throws(() => { test_object.readonlyAccessor2 = 3; }, |
| 49 | + common.engineSpecificMessage({ |
| 50 | + v8: /^TypeError: Cannot assign to read only property 'readonlyAccessor2' of object '#<MyObject>'$/, |
| 51 | + chakracore: /^TypeError: Assignment to read-only properties is not allowed in strict mode$/})); |
| 52 | + |
| 53 | +// validate that static properties are on the class as opposed |
| 54 | +// to the instance |
| 55 | +assert.strictEqual(TestConstructor.staticReadonlyAccessor1, 10); |
| 56 | +assert.strictEqual(test_object.staticReadonlyAccessor1, undefined); |
0 commit comments