From 1350dc58aaeee335e44609494caef641ef42590a Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Tue, 23 May 2017 17:26:37 -0400 Subject: [PATCH 1/2] test: add coverage for napi_has_named_property Add test to cover napi_has_named_property --- test/addons-napi/test_properties/test.js | 4 +++ .../test_properties/test_properties.c | 25 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/test/addons-napi/test_properties/test.js b/test/addons-napi/test_properties/test.js index a8127a27860eb3..9e0be244c6191b 100644 --- a/test/addons-napi/test_properties/test.js +++ b/test/addons-napi/test_properties/test.js @@ -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')); +assert.ok(test_object.hasNamedProperty(test_object, 'hiddenValue')); +assert.ok(!test_object.hasNamedProperty(test_object, 'doesnotexist')); diff --git a/test/addons-napi/test_properties/test_properties.c b/test/addons-napi/test_properties/test_properties.c index 67e6b1a0d5ed0a..ee8068b5c9d53f 100644 --- a/test/addons-napi/test_properties/test_properties.c +++ b/test/addons-napi/test_properties/test_properties.c @@ -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; + size_t copied; + buffer[buffer_size - 1] = 0; + 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 + 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)); @@ -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( From 0af30ca6be44e4000367d52a93e4e28b596db140 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Wed, 24 May 2017 10:10:38 -0400 Subject: [PATCH 2/2] squash: address comments --- test/addons-napi/test_properties/test.js | 8 +++++--- test/addons-napi/test_properties/test_properties.c | 6 ++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/test/addons-napi/test_properties/test.js b/test/addons-napi/test_properties/test.js index 9e0be244c6191b..0f37af81b7ffad 100644 --- a/test/addons-napi/test_properties/test.js +++ b/test/addons-napi/test_properties/test.js @@ -40,6 +40,8 @@ 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')); -assert.ok(test_object.hasNamedProperty(test_object, 'hiddenValue')); -assert.ok(!test_object.hasNamedProperty(test_object, 'doesnotexist')); +assert.strictEqual(test_object.hasNamedProperty(test_object, 'echo'), true); +assert.strictEqual(test_object.hasNamedProperty(test_object, 'hiddenValue'), + true); +assert.strictEqual(test_object.hasNamedProperty(test_object, 'doesnotexist'), + false); diff --git a/test/addons-napi/test_properties/test_properties.c b/test/addons-napi/test_properties/test_properties.c index ee8068b5c9d53f..575d750a7d936b 100644 --- a/test/addons-napi/test_properties/test_properties.c +++ b/test/addons-napi/test_properties/test_properties.c @@ -46,13 +46,11 @@ napi_value HasNamedProperty(napi_env env, napi_callback_info info) { // Extract the name of the property to check char buffer[128]; - size_t buffer_size = 128; size_t copied; - buffer[buffer_size - 1] = 0; NAPI_CALL(env, - napi_get_value_string_utf8(env, args[1], buffer, buffer_size - 1, &copied)); + napi_get_value_string_utf8(env, args[1], buffer, sizeof(buffer), &copied)); - // do the check and create the boolean retutn value + // do the check and create the boolean return value bool value; napi_value result; NAPI_CALL(env, napi_has_named_property(env, args[0], buffer, &value));