2017-03-20 14:55:26 -07:00
|
|
|
'use strict';
|
|
|
|
const common = require('../../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
|
2019-05-29 13:25:07 +08:00
|
|
|
const getterOnlyErrorRE =
|
|
|
|
/^TypeError: Cannot set property .* of #<.*> which has only a getter$/;
|
|
|
|
|
2017-03-20 14:55:26 -07:00
|
|
|
// Testing api calls for a constructor that defines properties
|
2018-01-23 21:49:25 +11:00
|
|
|
const TestConstructor = require(`./build/${common.buildType}/test_constructor`);
|
2017-03-20 14:55:26 -07:00
|
|
|
const test_object = new TestConstructor();
|
|
|
|
|
|
|
|
assert.strictEqual(test_object.echo('hello'), 'hello');
|
|
|
|
|
|
|
|
test_object.readwriteValue = 1;
|
|
|
|
assert.strictEqual(test_object.readwriteValue, 1);
|
|
|
|
test_object.readwriteValue = 2;
|
|
|
|
assert.strictEqual(test_object.readwriteValue, 2);
|
|
|
|
|
2017-07-17 11:58:38 +08:00
|
|
|
assert.throws(() => { test_object.readonlyValue = 3; },
|
|
|
|
/^TypeError: Cannot assign to read only property 'readonlyValue' of object '#<MyObject>'$/);
|
2017-03-20 14:55:26 -07:00
|
|
|
|
|
|
|
assert.ok(test_object.hiddenValue);
|
|
|
|
|
2017-04-05 10:18:56 -07:00
|
|
|
// Properties with napi_enumerable attribute should be enumerable.
|
2017-03-20 14:55:26 -07:00
|
|
|
const propertyNames = [];
|
|
|
|
for (const name in test_object) {
|
|
|
|
propertyNames.push(name);
|
|
|
|
}
|
2017-04-23 02:01:54 +03:00
|
|
|
assert.ok(propertyNames.includes('echo'));
|
|
|
|
assert.ok(propertyNames.includes('readwriteValue'));
|
|
|
|
assert.ok(propertyNames.includes('readonlyValue'));
|
|
|
|
assert.ok(!propertyNames.includes('hiddenValue'));
|
|
|
|
assert.ok(!propertyNames.includes('readwriteAccessor1'));
|
|
|
|
assert.ok(!propertyNames.includes('readwriteAccessor2'));
|
|
|
|
assert.ok(!propertyNames.includes('readonlyAccessor1'));
|
|
|
|
assert.ok(!propertyNames.includes('readonlyAccessor2'));
|
2017-04-05 10:18:56 -07:00
|
|
|
|
|
|
|
// The napi_writable attribute should be ignored for accessors.
|
|
|
|
test_object.readwriteAccessor1 = 1;
|
|
|
|
assert.strictEqual(test_object.readwriteAccessor1, 1);
|
|
|
|
assert.strictEqual(test_object.readonlyAccessor1, 1);
|
2019-05-29 13:25:07 +08:00
|
|
|
assert.throws(() => { test_object.readonlyAccessor1 = 3; }, getterOnlyErrorRE);
|
2017-04-05 10:18:56 -07:00
|
|
|
test_object.readwriteAccessor2 = 2;
|
|
|
|
assert.strictEqual(test_object.readwriteAccessor2, 2);
|
|
|
|
assert.strictEqual(test_object.readonlyAccessor2, 2);
|
2019-05-29 13:25:07 +08:00
|
|
|
assert.throws(() => { test_object.readonlyAccessor2 = 3; }, getterOnlyErrorRE);
|
2017-05-19 18:18:54 -04:00
|
|
|
|
2018-12-10 13:27:32 +01:00
|
|
|
// Validate that static properties are on the class as opposed
|
2017-05-19 18:18:54 -04:00
|
|
|
// to the instance
|
|
|
|
assert.strictEqual(TestConstructor.staticReadonlyAccessor1, 10);
|
|
|
|
assert.strictEqual(test_object.staticReadonlyAccessor1, undefined);
|
2019-05-28 21:12:24 -07:00
|
|
|
|
|
|
|
// Verify that passing NULL to napi_define_class() results in the correct
|
|
|
|
// error.
|
|
|
|
assert.deepStrictEqual(TestConstructor.TestDefineClass(), {
|
2019-06-27 00:09:24 -07:00
|
|
|
envIsNull: 'Invalid argument',
|
|
|
|
nameIsNull: 'Invalid argument',
|
|
|
|
cbIsNull: 'Invalid argument',
|
|
|
|
cbDataIsNull: 'napi_ok',
|
|
|
|
propertiesIsNull: 'Invalid argument',
|
2023-01-25 07:05:09 +01:00
|
|
|
resultIsNull: 'Invalid argument',
|
2019-05-28 21:12:24 -07:00
|
|
|
});
|