Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add coverage for napi_has_named_property #13178

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions test/addons-napi/test_properties/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ test_object.readwriteAccessor2 = 2;
assert.strictEqual(test_object.readwriteAccessor2, 2);
assert.strictEqual(test_object.readonlyAccessor2, 2);
assert.throws(() => { test_object.readonlyAccessor2 = 3; }, TypeError);

assert.ok(test_object.hasNamedProperty(test_object, 'echo'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to use strictEqual() here to make sure we're actually getting a Boolean back.

assert.ok(test_object.hasNamedProperty(test_object, 'hiddenValue'));
assert.ok(!test_object.hasNamedProperty(test_object, 'doesnotexist'));
25 changes: 25 additions & 0 deletions test/addons-napi/test_properties/test_properties.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ napi_value Echo(napi_env env, napi_callback_info info) {
return args[0];
}

napi_value HasNamedProperty(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NAPI_ASSERT(env, argc == 2, "Wrong number of arguments");

// Extract the name of the property to check
char buffer[128];
size_t buffer_size = 128;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, this is obviously correct, but I get all tingly seeing this not just use sizeof(buffer). 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not an excuse but I copied from the existing code in another file, using sizeof does seem better so I'll move it over to that.

size_t copied;
buffer[buffer_size - 1] = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

napi_get_value_string_utf8() will always null-terminate, so this line should not be necessary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok thanks.

NAPI_CALL(env,
napi_get_value_string_utf8(env, args[1], buffer, buffer_size - 1, &copied));

// do the check and create the boolean retutn value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: retutn

bool value;
napi_value result;
NAPI_CALL(env, napi_has_named_property(env, args[0], buffer, &value));
NAPI_CALL(env, napi_get_boolean(env, value, &result));

return result;
}

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));
Expand All @@ -50,6 +74,7 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
{ "readwriteAccessor2", 0, 0, GetValue, SetValue, 0, napi_writable, 0},
{ "readonlyAccessor1", 0, 0, GetValue, NULL, 0, napi_default, 0},
{ "readonlyAccessor2", 0, 0, GetValue, NULL, 0, napi_writable, 0},
{ "hasNamedProperty", 0, HasNamedProperty, 0, 0, 0, napi_default, 0 },
};

NAPI_CALL_RETURN_VOID(env, napi_define_properties(
Expand Down