From 9ec03c0487aae8bdef5cae66f8022972af6173b5 Mon Sep 17 00:00:00 2001 From: Kenvin Davies Date: Tue, 28 Nov 2017 18:42:48 +0900 Subject: [PATCH] test: Add missing tests for Object::Has() methods There is no test for Object::Has() in current source tree. PR-URL: https://github.com/nodejs/node-addon-api/pull/194 Reviewed-By: Anna Henningsen Reviewed-By: Michael Dawson --- test/binding.gyp | 1 + test/index.js | 1 + test/object/has_property.cc | 27 +++++++++++++++++++++++++++ test/object/has_property.js | 36 ++++++++++++++++++++++++++++++++++++ test/object/object.cc | 11 +++++++++++ 5 files changed, 76 insertions(+) create mode 100644 test/object/has_property.cc create mode 100644 test/object/has_property.js diff --git a/test/binding.gyp b/test/binding.gyp index 45d8d0b..d771f23 100644 --- a/test/binding.gyp +++ b/test/binding.gyp @@ -14,6 +14,7 @@ 'object/delete_property.cc', 'object/get_property.cc', 'object/has_own_property.cc', + 'object/has_property.cc', 'object/object.cc', 'object/set_property.cc', 'promise.cc', diff --git a/test/index.js b/test/index.js index f772d18..e367ae5 100644 --- a/test/index.js +++ b/test/index.js @@ -20,6 +20,7 @@ let testModules = [ 'object/delete_property', 'object/get_property', 'object/has_own_property', + 'object/has_property', 'object/object', 'object/set_property', 'promise', diff --git a/test/object/has_property.cc b/test/object/has_property.cc new file mode 100644 index 0000000..0a1a459 --- /dev/null +++ b/test/object/has_property.cc @@ -0,0 +1,27 @@ +#include "napi.h" + +using namespace Napi; + +Value HasPropertyWithNapiValue(const CallbackInfo& info) { + Object obj = info[0].As(); + Name key = info[1].As(); + return Boolean::New(info.Env(), obj.Has(static_cast(key))); +} + +Value HasPropertyWithNapiWrapperValue(const CallbackInfo& info) { + Object obj = info[0].As(); + Name key = info[1].As(); + return Boolean::New(info.Env(), obj.Has(key)); +} + +Value HasPropertyWithCStyleString(const CallbackInfo& info) { + Object obj = info[0].As(); + String jsKey = info[1].As(); + return Boolean::New(info.Env(), obj.Has(jsKey.Utf8Value().c_str())); +} + +Value HasPropertyWithCppStyleString(const CallbackInfo& info) { + Object obj = info[0].As(); + String jsKey = info[1].As(); + return Boolean::New(info.Env(), obj.Has(jsKey.Utf8Value())); +} diff --git a/test/object/has_property.js b/test/object/has_property.js new file mode 100644 index 0000000..66024b3 --- /dev/null +++ b/test/object/has_property.js @@ -0,0 +1,36 @@ +'use strict'; + +const buildType = process.config.target_defaults.default_configuration; +const assert = require('assert'); + +test(require(`../build/${buildType}/binding.node`)); +test(require(`../build/${buildType}/binding_noexcept.node`)); + +function test(binding) { + function testHasProperty(nativeHasProperty) { + const obj = { one: 1 }; + + Object.defineProperty(obj, 'two', { value: 2 }); + + assert.strictEqual(nativeHasProperty(obj, 'one'), true); + assert.strictEqual(nativeHasProperty(obj, 'two'), true); + assert.strictEqual('toString' in obj, true); + assert.strictEqual(nativeHasProperty(obj, 'toString'), true); + } + + function testShouldThrowErrorIfKeyIsInvalid(nativeHasProperty) { + assert.throws(() => { + nativeHasProperty(undefined, 'test'); + }, /object was expected/); + } + + testHasProperty(binding.object.hasPropertyWithNapiValue); + testHasProperty(binding.object.hasPropertyWithNapiWrapperValue); + testHasProperty(binding.object.hasPropertyWithCStyleString); + testHasProperty(binding.object.hasPropertyWithCppStyleString); + + testShouldThrowErrorIfKeyIsInvalid(binding.object.hasPropertyWithNapiValue); + testShouldThrowErrorIfKeyIsInvalid(binding.object.hasPropertyWithNapiWrapperValue); + testShouldThrowErrorIfKeyIsInvalid(binding.object.hasPropertyWithCStyleString); + testShouldThrowErrorIfKeyIsInvalid(binding.object.hasPropertyWithCppStyleString); +} diff --git a/test/object/object.cc b/test/object/object.cc index 1a77906..50fac97 100644 --- a/test/object/object.cc +++ b/test/object/object.cc @@ -26,6 +26,12 @@ Value HasOwnPropertyWithNapiWrapperValue(const CallbackInfo& info); Value HasOwnPropertyWithCStyleString(const CallbackInfo& info); Value HasOwnPropertyWithCppStyleString(const CallbackInfo& info); +// Native wrappers for testing Object::Has() +Value HasPropertyWithNapiValue(const CallbackInfo& info); +Value HasPropertyWithNapiWrapperValue(const CallbackInfo& info); +Value HasPropertyWithCStyleString(const CallbackInfo& info); +Value HasPropertyWithCppStyleString(const CallbackInfo& info); + static bool testValue = true; Value TestGetter(const CallbackInfo& info) { @@ -161,6 +167,11 @@ Object InitObject(Env env) { exports["hasOwnPropertyWithCStyleString"] = Function::New(env, HasOwnPropertyWithCStyleString); exports["hasOwnPropertyWithCppStyleString"] = Function::New(env, HasOwnPropertyWithCppStyleString); + exports["hasPropertyWithNapiValue"] = Function::New(env, HasPropertyWithNapiValue); + exports["hasPropertyWithNapiWrapperValue"] = Function::New(env, HasPropertyWithNapiWrapperValue); + exports["hasPropertyWithCStyleString"] = Function::New(env, HasPropertyWithCStyleString); + exports["hasPropertyWithCppStyleString"] = Function::New(env, HasPropertyWithCppStyleString); + exports["createObjectUsingMagic"] = Function::New(env, CreateObjectUsingMagic); return exports;