11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst readonlyErrorRE = 51cb0ef41Sopenharmony_ci /^TypeError: Cannot assign to read only property '.*' of object '#<Object>'$/; 61cb0ef41Sopenharmony_ciconst getterOnlyErrorRE = 71cb0ef41Sopenharmony_ci /^TypeError: Cannot set property .* of #<Object> which has only a getter$/; 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci// Testing api calls for defining properties 101cb0ef41Sopenharmony_ciconst test_object = require(`./build/${common.buildType}/test_properties`); 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; }, readonlyErrorRE); 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ciassert.ok(test_object.hiddenValue); 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci// Properties with napi_enumerable attribute should be enumerable. 241cb0ef41Sopenharmony_ciconst propertyNames = []; 251cb0ef41Sopenharmony_cifor (const name in test_object) { 261cb0ef41Sopenharmony_ci propertyNames.push(name); 271cb0ef41Sopenharmony_ci} 281cb0ef41Sopenharmony_ciassert.ok(propertyNames.includes('echo')); 291cb0ef41Sopenharmony_ciassert.ok(propertyNames.includes('readwriteValue')); 301cb0ef41Sopenharmony_ciassert.ok(propertyNames.includes('readonlyValue')); 311cb0ef41Sopenharmony_ciassert.ok(!propertyNames.includes('hiddenValue')); 321cb0ef41Sopenharmony_ciassert.ok(propertyNames.includes('NameKeyValue')); 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// Validate properties created with symbol 391cb0ef41Sopenharmony_ciconst propertySymbols = Object.getOwnPropertySymbols(test_object); 401cb0ef41Sopenharmony_ciassert.strictEqual(propertySymbols[0].toString(), 'Symbol(NameKeySymbol)'); 411cb0ef41Sopenharmony_ciassert.strictEqual(propertySymbols[1].toString(), 'Symbol()'); 421cb0ef41Sopenharmony_ciassert.strictEqual(propertySymbols[2], Symbol.for('NameKeySymbolFor')); 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci// The napi_writable attribute should be ignored for accessors. 451cb0ef41Sopenharmony_ciconst readwriteAccessor1Descriptor = 461cb0ef41Sopenharmony_ci Object.getOwnPropertyDescriptor(test_object, 'readwriteAccessor1'); 471cb0ef41Sopenharmony_ciconst readonlyAccessor1Descriptor = 481cb0ef41Sopenharmony_ci Object.getOwnPropertyDescriptor(test_object, 'readonlyAccessor1'); 491cb0ef41Sopenharmony_ciassert.ok(readwriteAccessor1Descriptor.get != null); 501cb0ef41Sopenharmony_ciassert.ok(readwriteAccessor1Descriptor.set != null); 511cb0ef41Sopenharmony_ciassert.ok(readwriteAccessor1Descriptor.value === undefined); 521cb0ef41Sopenharmony_ciassert.ok(readonlyAccessor1Descriptor.get != null); 531cb0ef41Sopenharmony_ciassert.ok(readonlyAccessor1Descriptor.set === undefined); 541cb0ef41Sopenharmony_ciassert.ok(readonlyAccessor1Descriptor.value === undefined); 551cb0ef41Sopenharmony_citest_object.readwriteAccessor1 = 1; 561cb0ef41Sopenharmony_ciassert.strictEqual(test_object.readwriteAccessor1, 1); 571cb0ef41Sopenharmony_ciassert.strictEqual(test_object.readonlyAccessor1, 1); 581cb0ef41Sopenharmony_ciassert.throws(() => { test_object.readonlyAccessor1 = 3; }, getterOnlyErrorRE); 591cb0ef41Sopenharmony_citest_object.readwriteAccessor2 = 2; 601cb0ef41Sopenharmony_ciassert.strictEqual(test_object.readwriteAccessor2, 2); 611cb0ef41Sopenharmony_ciassert.strictEqual(test_object.readonlyAccessor2, 2); 621cb0ef41Sopenharmony_ciassert.throws(() => { test_object.readonlyAccessor2 = 3; }, getterOnlyErrorRE); 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ciassert.strictEqual(test_object.hasNamedProperty(test_object, 'echo'), true); 651cb0ef41Sopenharmony_ciassert.strictEqual(test_object.hasNamedProperty(test_object, 'hiddenValue'), 661cb0ef41Sopenharmony_ci true); 671cb0ef41Sopenharmony_ciassert.strictEqual(test_object.hasNamedProperty(test_object, 'doesnotexist'), 681cb0ef41Sopenharmony_ci false); 69