Skip to content

Commit 1c7f917

Browse files
mhdawsonaddaleax
authored andcommitted
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. PR-URL: #13510 Reviewed-By: Jason Ginchereau <[email protected]>
1 parent 98d7f25 commit 1c7f917

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

test/addons-napi/test_properties/test.js

+8
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,19 @@ assert.ok(propertyNames.includes('echo'));
2525
assert.ok(propertyNames.includes('readwriteValue'));
2626
assert.ok(propertyNames.includes('readonlyValue'));
2727
assert.ok(!propertyNames.includes('hiddenValue'));
28+
assert.ok(propertyNames.includes('NameKeyValue'));
2829
assert.ok(!propertyNames.includes('readwriteAccessor1'));
2930
assert.ok(!propertyNames.includes('readwriteAccessor2'));
3031
assert.ok(!propertyNames.includes('readonlyAccessor1'));
3132
assert.ok(!propertyNames.includes('readonlyAccessor2'));
3233

34+
// validate property created with symbol
35+
const start = 'Symbol('.length;
36+
const end = start + 'NameKeySymbol'.length;
37+
const symbolDescription =
38+
String(Object.getOwnPropertySymbols(test_object)[0]).slice(start, end);
39+
assert.strictEqual(symbolDescription, 'NameKeySymbol');
40+
3341
// The napi_writable attribute should be ignored for accessors.
3442
test_object.readwriteAccessor1 = 1;
3543
assert.strictEqual(test_object.readwriteAccessor1, 1);

test/addons-napi/test_properties/test_properties.c

+18
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,29 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
6363
napi_value number;
6464
NAPI_CALL_RETURN_VOID(env, napi_create_number(env, value_, &number));
6565

66+
napi_value name_value;
67+
NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env,
68+
"NameKeyValue",
69+
-1,
70+
&name_value));
71+
72+
napi_value symbol_description;
73+
napi_value name_symbol;
74+
NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env,
75+
"NameKeySymbol",
76+
-1,
77+
&symbol_description));
78+
NAPI_CALL_RETURN_VOID(env, napi_create_symbol(env,
79+
symbol_description,
80+
&name_symbol));
81+
6682
napi_property_descriptor properties[] = {
6783
{ "echo", 0, Echo, 0, 0, 0, napi_enumerable, 0 },
6884
{ "readwriteValue", 0, 0, 0, 0, number, napi_enumerable | napi_writable, 0 },
6985
{ "readonlyValue", 0, 0, 0, 0, number, napi_enumerable, 0},
7086
{ "hiddenValue", 0, 0, 0, 0, number, napi_default, 0},
87+
{ NULL, name_value, 0, 0, 0, number, napi_enumerable, 0},
88+
{ NULL, name_symbol, 0, 0, 0, number, napi_enumerable, 0},
7189
{ "readwriteAccessor1", 0, 0, GetValue, SetValue, 0, napi_default, 0},
7290
{ "readwriteAccessor2", 0, 0, GetValue, SetValue, 0, napi_writable, 0},
7391
{ "readonlyAccessor1", 0, 0, GetValue, NULL, 0, napi_default, 0},

0 commit comments

Comments
 (0)