Skip to content

Commit a590709

Browse files
mhdawsonjasnell
authored andcommitted
test: add coverage for napi_has_named_property
Add test to cover napi_has_named_property PR-URL: #13178 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jason Ginchereau <[email protected]>
1 parent cd2824c commit a590709

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

test/addons-napi/test_properties/test.js

+6
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,9 @@ test_object.readwriteAccessor2 = 2;
3939
assert.strictEqual(test_object.readwriteAccessor2, 2);
4040
assert.strictEqual(test_object.readonlyAccessor2, 2);
4141
assert.throws(() => { test_object.readonlyAccessor2 = 3; }, TypeError);
42+
43+
assert.strictEqual(test_object.hasNamedProperty(test_object, 'echo'), true);
44+
assert.strictEqual(test_object.hasNamedProperty(test_object, 'hiddenValue'),
45+
true);
46+
assert.strictEqual(test_object.hasNamedProperty(test_object, 'doesnotexist'),
47+
false);

test/addons-napi/test_properties/test_properties.c

+23
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ napi_value Echo(napi_env env, napi_callback_info info) {
3737
return args[0];
3838
}
3939

40+
napi_value HasNamedProperty(napi_env env, napi_callback_info info) {
41+
size_t argc = 2;
42+
napi_value args[2];
43+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
44+
45+
NAPI_ASSERT(env, argc == 2, "Wrong number of arguments");
46+
47+
// Extract the name of the property to check
48+
char buffer[128];
49+
size_t copied;
50+
NAPI_CALL(env,
51+
napi_get_value_string_utf8(env, args[1], buffer, sizeof(buffer), &copied));
52+
53+
// do the check and create the boolean return value
54+
bool value;
55+
napi_value result;
56+
NAPI_CALL(env, napi_has_named_property(env, args[0], buffer, &value));
57+
NAPI_CALL(env, napi_get_boolean(env, value, &result));
58+
59+
return result;
60+
}
61+
4062
void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
4163
napi_value number;
4264
NAPI_CALL_RETURN_VOID(env, napi_create_number(env, value_, &number));
@@ -50,6 +72,7 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
5072
{ "readwriteAccessor2", 0, 0, GetValue, SetValue, 0, napi_writable, 0},
5173
{ "readonlyAccessor1", 0, 0, GetValue, NULL, 0, napi_default, 0},
5274
{ "readonlyAccessor2", 0, 0, GetValue, NULL, 0, napi_writable, 0},
75+
{ "hasNamedProperty", 0, HasNamedProperty, 0, 0, 0, napi_default, 0 },
5376
};
5477

5578
NAPI_CALL_RETURN_VOID(env, napi_define_properties(

0 commit comments

Comments
 (0)