From 1e25062fa16e29e5e803bb3a730223130dcfdfac Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Tue, 6 Jun 2017 18:24:33 -0400 Subject: [PATCH] test: add coverage for napi_property_descriptor We did not have test coverage for using a napi_value pointing to a string or symbol for the name when creating a property. Add that coverage. Backport-PR-URL: https://github.com/nodejs/node/pull/19447 PR-URL: https://github.com/nodejs/node/pull/13510 Reviewed-By: Jason Ginchereau --- test/addons-napi/test_properties/test.js | 8 ++++++++ .../test_properties/test_properties.c | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/test/addons-napi/test_properties/test.js b/test/addons-napi/test_properties/test.js index 0f37af81b7ffad..8d11a669d46daf 100644 --- a/test/addons-napi/test_properties/test.js +++ b/test/addons-napi/test_properties/test.js @@ -25,11 +25,19 @@ assert.ok(propertyNames.includes('echo')); assert.ok(propertyNames.includes('readwriteValue')); assert.ok(propertyNames.includes('readonlyValue')); assert.ok(!propertyNames.includes('hiddenValue')); +assert.ok(propertyNames.includes('NameKeyValue')); assert.ok(!propertyNames.includes('readwriteAccessor1')); assert.ok(!propertyNames.includes('readwriteAccessor2')); assert.ok(!propertyNames.includes('readonlyAccessor1')); assert.ok(!propertyNames.includes('readonlyAccessor2')); +// validate property created with symbol +const start = 'Symbol('.length; +const end = start + 'NameKeySymbol'.length; +const symbolDescription = + String(Object.getOwnPropertySymbols(test_object)[0]).slice(start, end); +assert.strictEqual(symbolDescription, 'NameKeySymbol'); + // The napi_writable attribute should be ignored for accessors. test_object.readwriteAccessor1 = 1; assert.strictEqual(test_object.readwriteAccessor1, 1); diff --git a/test/addons-napi/test_properties/test_properties.c b/test/addons-napi/test_properties/test_properties.c index 575d750a7d936b..3053fda4864e8f 100644 --- a/test/addons-napi/test_properties/test_properties.c +++ b/test/addons-napi/test_properties/test_properties.c @@ -63,11 +63,29 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_value number; NAPI_CALL_RETURN_VOID(env, napi_create_number(env, value_, &number)); + napi_value name_value; + NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, + "NameKeyValue", + -1, + &name_value)); + + napi_value symbol_description; + napi_value name_symbol; + NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, + "NameKeySymbol", + -1, + &symbol_description)); + NAPI_CALL_RETURN_VOID(env, napi_create_symbol(env, + symbol_description, + &name_symbol)); + napi_property_descriptor properties[] = { { "echo", 0, Echo, 0, 0, 0, napi_enumerable, 0 }, { "readwriteValue", 0, 0, 0, 0, number, napi_enumerable | napi_writable, 0 }, { "readonlyValue", 0, 0, 0, 0, number, napi_enumerable, 0}, { "hiddenValue", 0, 0, 0, 0, number, napi_default, 0}, + { NULL, name_value, 0, 0, 0, number, napi_enumerable, 0}, + { NULL, name_symbol, 0, 0, 0, number, napi_enumerable, 0}, { "readwriteAccessor1", 0, 0, GetValue, SetValue, 0, napi_default, 0}, { "readwriteAccessor2", 0, 0, GetValue, SetValue, 0, napi_writable, 0}, { "readonlyAccessor1", 0, 0, GetValue, NULL, 0, napi_default, 0},