11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst getterOnlyErrorRE =
61cb0ef41Sopenharmony_ci  /^TypeError: Cannot set property .* of #<.*> which has only a getter$/;
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// Testing api calls for a constructor that defines properties
91cb0ef41Sopenharmony_ciconst TestConstructor = require(`./build/${common.buildType}/test_constructor`);
101cb0ef41Sopenharmony_ciconst test_object = new TestConstructor();
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciassert.strictEqual(test_object.echo('hello'), 'hello');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_citest_object.readwriteValue = 1;
151cb0ef41Sopenharmony_ciassert.strictEqual(test_object.readwriteValue, 1);
161cb0ef41Sopenharmony_citest_object.readwriteValue = 2;
171cb0ef41Sopenharmony_ciassert.strictEqual(test_object.readwriteValue, 2);
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciassert.throws(() => { test_object.readonlyValue = 3; },
201cb0ef41Sopenharmony_ci              /^TypeError: Cannot assign to read only property 'readonlyValue' of object '#<MyObject>'$/);
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciassert.ok(test_object.hiddenValue);
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci// Properties with napi_enumerable attribute should be enumerable.
251cb0ef41Sopenharmony_ciconst propertyNames = [];
261cb0ef41Sopenharmony_cifor (const name in test_object) {
271cb0ef41Sopenharmony_ci  propertyNames.push(name);
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ciassert.ok(propertyNames.includes('echo'));
301cb0ef41Sopenharmony_ciassert.ok(propertyNames.includes('readwriteValue'));
311cb0ef41Sopenharmony_ciassert.ok(propertyNames.includes('readonlyValue'));
321cb0ef41Sopenharmony_ciassert.ok(!propertyNames.includes('hiddenValue'));
331cb0ef41Sopenharmony_ciassert.ok(!propertyNames.includes('readwriteAccessor1'));
341cb0ef41Sopenharmony_ciassert.ok(!propertyNames.includes('readwriteAccessor2'));
351cb0ef41Sopenharmony_ciassert.ok(!propertyNames.includes('readonlyAccessor1'));
361cb0ef41Sopenharmony_ciassert.ok(!propertyNames.includes('readonlyAccessor2'));
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci// The napi_writable attribute should be ignored for accessors.
391cb0ef41Sopenharmony_citest_object.readwriteAccessor1 = 1;
401cb0ef41Sopenharmony_ciassert.strictEqual(test_object.readwriteAccessor1, 1);
411cb0ef41Sopenharmony_ciassert.strictEqual(test_object.readonlyAccessor1, 1);
421cb0ef41Sopenharmony_ciassert.throws(() => { test_object.readonlyAccessor1 = 3; }, getterOnlyErrorRE);
431cb0ef41Sopenharmony_citest_object.readwriteAccessor2 = 2;
441cb0ef41Sopenharmony_ciassert.strictEqual(test_object.readwriteAccessor2, 2);
451cb0ef41Sopenharmony_ciassert.strictEqual(test_object.readonlyAccessor2, 2);
461cb0ef41Sopenharmony_ciassert.throws(() => { test_object.readonlyAccessor2 = 3; }, getterOnlyErrorRE);
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci// Validate that static properties are on the class as opposed
491cb0ef41Sopenharmony_ci// to the instance
501cb0ef41Sopenharmony_ciassert.strictEqual(TestConstructor.staticReadonlyAccessor1, 10);
511cb0ef41Sopenharmony_ciassert.strictEqual(test_object.staticReadonlyAccessor1, undefined);
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci// Verify that passing NULL to napi_define_class() results in the correct
541cb0ef41Sopenharmony_ci// error.
551cb0ef41Sopenharmony_ciassert.deepStrictEqual(TestConstructor.TestDefineClass(), {
561cb0ef41Sopenharmony_ci  envIsNull: 'Invalid argument',
571cb0ef41Sopenharmony_ci  nameIsNull: 'Invalid argument',
581cb0ef41Sopenharmony_ci  cbIsNull: 'Invalid argument',
591cb0ef41Sopenharmony_ci  cbDataIsNull: 'napi_ok',
601cb0ef41Sopenharmony_ci  propertiesIsNull: 'Invalid argument',
611cb0ef41Sopenharmony_ci  resultIsNull: 'Invalid argument',
621cb0ef41Sopenharmony_ci});
63