Skip to content

Commit

Permalink
test: add coverage for napi_property_descriptor
Browse files Browse the repository at this point in the history
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.

PR-URL: #13510
Reviewed-By: Jason Ginchereau <[email protected]>
  • Loading branch information
mhdawson committed Jun 9, 2017
1 parent c28418a commit 01f4d9a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions test/addons-napi/test_properties/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions test/addons-napi/test_properties/test_properties.c
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit 01f4d9a

Please sign in to comment.