From bc5147cc4ab7e79d6c11a08790082c958970b887 Mon Sep 17 00:00:00 2001 From: JckXia Date: Mon, 25 Jan 2021 19:13:53 -0500 Subject: [PATCH] Finished tests relating to fetch property from Global Object PR-URL: https://github.com/nodejs/node-addon-api/pull/939/ Reviewed-By: Nicola Del Gobbo Reviewed-By: Michael Dawson --- test/binding.cc | 2 + test/binding.gyp | 5 ++ test/globalObject/global_object.cc | 61 ++++++++++++++++++ .../global_object_delete_property.cc | 27 ++++++++ .../global_object_delete_property.js | 64 +++++++++++++++++++ .../global_object_get_property.cc | 39 +++++++++++ .../global_object_get_property.js | 60 +++++++++++++++++ .../global_object_has_own_property.cc | 22 +++++++ .../global_object_has_own_property.js | 51 +++++++++++++++ .../global_object_set_property.cc | 31 +++++++++ .../global_object_set_property.js | 61 ++++++++++++++++++ test/object/delete_property.cc | 6 ++ test/object/delete_property.js | 6 ++ test/object/get_property.cc | 6 ++ test/object/get_property.js | 4 ++ test/object/has_property.cc | 6 ++ test/object/has_property.js | 3 + test/object/object.cc | 7 ++ 18 files changed, 461 insertions(+) create mode 100644 test/globalObject/global_object.cc create mode 100644 test/globalObject/global_object_delete_property.cc create mode 100644 test/globalObject/global_object_delete_property.js create mode 100644 test/globalObject/global_object_get_property.cc create mode 100644 test/globalObject/global_object_get_property.js create mode 100644 test/globalObject/global_object_has_own_property.cc create mode 100644 test/globalObject/global_object_has_own_property.js create mode 100644 test/globalObject/global_object_set_property.cc create mode 100644 test/globalObject/global_object_set_property.js diff --git a/test/binding.cc b/test/binding.cc index fa37a8d53..1ed7cb053 100644 --- a/test/binding.cc +++ b/test/binding.cc @@ -58,6 +58,7 @@ Object InitTypedThreadSafeFunctionUnref(Env env); Object InitTypedThreadSafeFunction(Env env); #endif Object InitTypedArray(Env env); +Object InitGlobalObject(Env env); Object InitObjectWrap(Env env); Object InitObjectWrapConstructorException(Env env); Object InitObjectWrapRemoveWrap(Env env); @@ -78,6 +79,7 @@ Object Init(Env env, Object exports) { exports.Set("asyncprogressqueueworker", InitAsyncProgressQueueWorker(env)); exports.Set("asyncprogressworker", InitAsyncProgressWorker(env)); #endif + exports.Set("globalObject", InitGlobalObject(env)); exports.Set("asyncworker", InitAsyncWorker(env)); exports.Set("persistentasyncworker", InitPersistentAsyncWorker(env)); exports.Set("basic_types_array", InitBasicTypesArray(env)); diff --git a/test/binding.gyp b/test/binding.gyp index c9f82ee20..8f67fee55 100644 --- a/test/binding.gyp +++ b/test/binding.gyp @@ -28,6 +28,11 @@ 'movable_callbacks.cc', 'memory_management.cc', 'name.cc', + 'globalObject/global_object_delete_property.cc', + 'globalObject/global_object_has_own_property.cc', + 'globalObject/global_object_set_property.cc', + 'globalObject/global_object_get_property.cc', + 'globalObject/global_object.cc', 'object/delete_property.cc', 'object/finalizer.cc', 'object/get_property.cc', diff --git a/test/globalObject/global_object.cc b/test/globalObject/global_object.cc new file mode 100644 index 000000000..00ef2ba8f --- /dev/null +++ b/test/globalObject/global_object.cc @@ -0,0 +1,61 @@ +#include "napi.h" + +using namespace Napi; + +// Wrappers for testing Object::Get() for global Objects +Value GetPropertyWithCppStyleStringAsKey(const CallbackInfo& info); +Value GetPropertyWithCStyleStringAsKey(const CallbackInfo& info); +Value GetPropertyWithInt32AsKey(const CallbackInfo& info); +Value GetPropertyWithNapiValueAsKey(const CallbackInfo& info); +void CreateMockTestObject(const CallbackInfo& info); + +// Wrapper for testing Object::Set() for global Objects +void SetPropertyWithCStyleStringAsKey(const CallbackInfo& info); +void SetPropertyWithCppStyleStringAsKey(const CallbackInfo& info); +void SetPropertyWithInt32AsKey(const CallbackInfo& info); +void SetPropertyWithNapiValueAsKey(const CallbackInfo& info); + +Value HasPropertyWithCStyleStringAsKey(const CallbackInfo& info); +Value HasPropertyWithCppStyleStringAsKey(const CallbackInfo& info); +Value HasPropertyWithNapiValueAsKey(const CallbackInfo& info); + +Value DeletePropertyWithCStyleStringAsKey(const CallbackInfo& info); +Value DeletePropertyWithCppStyleStringAsKey(const CallbackInfo& info); +Value DeletePropertyWithInt32AsKey(const CallbackInfo& info); +Value DeletePropertyWithNapiValueAsKey(const CallbackInfo& info); + +Object InitGlobalObject(Env env) { + Object exports = Object::New(env); + exports["getPropertyWithInt32"] = + Function::New(env, GetPropertyWithInt32AsKey); + exports["getPropertyWithNapiValue"] = + Function::New(env, GetPropertyWithNapiValueAsKey); + exports["getPropertyWithCppString"] = + Function::New(env, GetPropertyWithCppStyleStringAsKey); + exports["getPropertyWithCString"] = + Function::New(env, GetPropertyWithCStyleStringAsKey); + exports["createMockTestObject"] = Function::New(env, CreateMockTestObject); + exports["setPropertyWithCStyleString"] = + Function::New(env, SetPropertyWithCStyleStringAsKey); + exports["setPropertyWithCppStyleString"] = + Function::New(env, SetPropertyWithCppStyleStringAsKey); + exports["setPropertyWithNapiValue"] = + Function::New(env, SetPropertyWithNapiValueAsKey); + exports["setPropertyWithInt32"] = + Function::New(env, SetPropertyWithInt32AsKey); + exports["hasPropertyWithCStyleString"] = + Function::New(env, HasPropertyWithCStyleStringAsKey); + exports["hasPropertyWithCppStyleString"] = + Function::New(env, HasPropertyWithCppStyleStringAsKey); + exports["hasPropertyWithNapiValue"] = + Function::New(env, HasPropertyWithNapiValueAsKey); + exports["deletePropertyWithCStyleString"] = + Function::New(env, DeletePropertyWithCStyleStringAsKey); + exports["deletePropertyWithCppStyleString"] = + Function::New(env, DeletePropertyWithCppStyleStringAsKey); + exports["deletePropertyWithInt32"] = + Function::New(env, DeletePropertyWithInt32AsKey); + exports["deletePropertyWithNapiValue"] = + Function::New(env, DeletePropertyWithNapiValueAsKey); + return exports; +} diff --git a/test/globalObject/global_object_delete_property.cc b/test/globalObject/global_object_delete_property.cc new file mode 100644 index 000000000..8064e5864 --- /dev/null +++ b/test/globalObject/global_object_delete_property.cc @@ -0,0 +1,27 @@ +#include "napi.h" + +using namespace Napi; + +Value DeletePropertyWithCStyleStringAsKey(const CallbackInfo& info) { + Object globalObject = info.Env().Global(); + String key = info[0].As(); + return Boolean::New(info.Env(), globalObject.Delete(key.Utf8Value().c_str())); +} + +Value DeletePropertyWithCppStyleStringAsKey(const CallbackInfo& info) { + Object globalObject = info.Env().Global(); + String key = info[0].As(); + return Boolean::New(info.Env(), globalObject.Delete(key.Utf8Value())); +} + +Value DeletePropertyWithInt32AsKey(const CallbackInfo& info) { + Object globalObject = info.Env().Global(); + Number key = info[0].As(); + return Boolean::New(info.Env(), globalObject.Delete(key.Uint32Value())); +} + +Value DeletePropertyWithNapiValueAsKey(const CallbackInfo& info) { + Object globalObject = info.Env().Global(); + Name key = info[0].As(); + return Boolean::New(info.Env(), globalObject.Delete(key)); +} \ No newline at end of file diff --git a/test/globalObject/global_object_delete_property.js b/test/globalObject/global_object_delete_property.js new file mode 100644 index 000000000..b6334f1ce --- /dev/null +++ b/test/globalObject/global_object_delete_property.js @@ -0,0 +1,64 @@ +'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) { + const KEY_TYPE = { + C_STR: 'KEY_AS_C_STRING', + CPP_STR: 'KEY_AS_CPP_STRING', + NAPI: 'KEY_AS_NAPI_VALUES', + INT_32: 'KEY_AS_INT_32_NUM' + }; + + function assertNotGlobalObjectHasNoProperty(key, keyType) + { + switch(keyType) + { + case KEY_TYPE.NAPI: + assert.notStrictEqual(binding.globalObject.hasPropertyWithNapiValue(key), true); + break; + + case KEY_TYPE.C_STR: + assert.notStrictEqual(binding.globalObject.hasPropertyWithCStyleString(key), true); + break; + + case KEY_TYPE.CPP_STR: + assert.notStrictEqual(binding.globalObject.hasPropertyWithCppStyleString(key), true); + break; + + case KEY_TYPE.INT_32: + assert.notStrictEqual(binding.globalObject.hasPropertyWithInt32(key), true); + break; + } + } + + function assertErrMessageIsThrown(propertyCheckExistanceFunction, errMsg) { + assert.throws(() => { + propertyCheckExistanceFunction(undefined); + }, errMsg); + } + + binding.globalObject.createMockTestObject(); + + binding.globalObject.deletePropertyWithCStyleString('c_str_key'); + binding.globalObject.deletePropertyWithCppStyleString('cpp_string_key'); + binding.globalObject.deletePropertyWithCppStyleString('circular'); + binding.globalObject.deletePropertyWithInt32(15); + binding.globalObject.deletePropertyWithNapiValue('2'); + + + assertNotGlobalObjectHasNoProperty('c_str_key',KEY_TYPE.C_STR); + assertNotGlobalObjectHasNoProperty('cpp_string_key',KEY_TYPE.CPP_STR); + assertNotGlobalObjectHasNoProperty('circular',KEY_TYPE.CPP_STR); + assertNotGlobalObjectHasNoProperty(15,true); + assertNotGlobalObjectHasNoProperty('2', KEY_TYPE.NAPI); + + assertErrMessageIsThrown(binding.globalObject.hasPropertyWithCppStyleString, 'Error: A string was expected'); + assertErrMessageIsThrown(binding.globalObject.hasPropertyWithCStyleString, 'Error: A string was expected'); + assertErrMessageIsThrown(binding.globalObject.hasPropertyWithInt32, 'Error: A number was expected'); +} diff --git a/test/globalObject/global_object_get_property.cc b/test/globalObject/global_object_get_property.cc new file mode 100644 index 000000000..bd402abf2 --- /dev/null +++ b/test/globalObject/global_object_get_property.cc @@ -0,0 +1,39 @@ +#include "napi.h" + +using namespace Napi; + +Value GetPropertyWithNapiValueAsKey(const CallbackInfo& info) { + Object globalObject = info.Env().Global(); + Name key = info[0].As(); + return globalObject.Get(key); +} + +Value GetPropertyWithInt32AsKey(const CallbackInfo& info) { + Object globalObject = info.Env().Global(); + Number key = info[0].As(); + return globalObject.Get(key.Uint32Value()); +} + +Value GetPropertyWithCStyleStringAsKey(const CallbackInfo& info) { + Object globalObject = info.Env().Global(); + String cStrkey = info[0].As(); + return globalObject.Get(cStrkey.Utf8Value().c_str()); +} + +Value GetPropertyWithCppStyleStringAsKey(const CallbackInfo& info) { + Object globalObject = info.Env().Global(); + String cppStrKey = info[0].As(); + return globalObject.Get(cppStrKey.Utf8Value()); +} + +void CreateMockTestObject(const CallbackInfo& info) { + Object globalObject = info.Env().Global(); + Number napi_key = Number::New(info.Env(), 2); + const char* CStringKey = "c_str_key"; + + globalObject.Set(napi_key, "napi_attribute"); + globalObject[CStringKey] = "c_string_attribute"; + globalObject[std::string("cpp_string_key")] = "cpp_string_attribute"; + globalObject[std::string("circular")] = globalObject; + globalObject[(uint32_t)15] = 15; +} diff --git a/test/globalObject/global_object_get_property.js b/test/globalObject/global_object_get_property.js new file mode 100644 index 000000000..3630f4398 --- /dev/null +++ b/test/globalObject/global_object_get_property.js @@ -0,0 +1,60 @@ +'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) { + const KEY_TYPE = { + C_STR: 'KEY_AS_C_STRING', + CPP_STR: 'KEY_AS_CPP_STRING', + NAPI: 'KEY_AS_NAPI_VALUES', + INT_32: 'KEY_AS_INT_32_NUM' + }; + + binding.globalObject.createMockTestObject(); + function assertGlobalObjectPropertyIs(key, attribute, keyType) { + let napiObjectAttr; + switch(keyType) + { + case KEY_TYPE.NAPI: + napiObjectAttr = binding.globalObject.getPropertyWithNapiValue(key); + assert.deepStrictEqual(attribute, napiObjectAttr); + break; + + case KEY_TYPE.C_STR: + napiObjectAttr = binding.globalObject.getPropertyWithCString(key); + assert.deepStrictEqual(attribute, napiObjectAttr); + break; + + case KEY_TYPE.CPP_STR: + napiObjectAttr = binding.globalObject.getPropertyWithCppString(key); + assert.deepStrictEqual(attribute, napiObjectAttr); + break; + + case KEY_TYPE.INT_32: + napiObjectAttr = binding.globalObject.getPropertyWithInt32(key); + assert.deepStrictEqual(attribute, napiObjectAttr); + break; + } + } + + function assertErrMessageIsThrown(propertyFetchFunction, errMsg) { + assert.throws(() => { + propertyFetchFunction(undefined); + }, errMsg); + } + + assertGlobalObjectPropertyIs('2',global['2'], KEY_TYPE.NAPI); + assertGlobalObjectPropertyIs('c_str_key',global['c_str_key'],KEY_TYPE.C_STR); + assertGlobalObjectPropertyIs('cpp_string_key',global['cpp_string_key'],KEY_TYPE.CPP_STR); + assertGlobalObjectPropertyIs('circular',global['circular'],KEY_TYPE.CPP_STR); + assertGlobalObjectPropertyIs(15, global['15'], KEY_TYPE.INT_32); + + assertErrMessageIsThrown(binding.globalObject.getPropertyWithCString, 'Error: A string was expected'); + assertErrMessageIsThrown(binding.globalObject.getPropertyWithCppString, 'Error: A string was expected'); + assertErrMessageIsThrown(binding.globalObject.getPropertyWithInt32, 'Error: A number was expected'); +} diff --git a/test/globalObject/global_object_has_own_property.cc b/test/globalObject/global_object_has_own_property.cc new file mode 100644 index 000000000..1dfb4e281 --- /dev/null +++ b/test/globalObject/global_object_has_own_property.cc @@ -0,0 +1,22 @@ +#include "napi.h" + +using namespace Napi; + +Value HasPropertyWithCStyleStringAsKey(const CallbackInfo& info) { + Object globalObject = info.Env().Global(); + String key = info[0].As(); + return Boolean::New(info.Env(), + globalObject.HasOwnProperty(key.Utf8Value().c_str())); +} + +Value HasPropertyWithCppStyleStringAsKey(const CallbackInfo& info) { + Object globalObject = info.Env().Global(); + String key = info[0].As(); + return Boolean::New(info.Env(), globalObject.HasOwnProperty(key.Utf8Value())); +} + +Value HasPropertyWithNapiValueAsKey(const CallbackInfo& info) { + Object globalObject = info.Env().Global(); + Name key = info[0].As(); + return Boolean::New(info.Env(), globalObject.HasOwnProperty(key)); +} \ No newline at end of file diff --git a/test/globalObject/global_object_has_own_property.js b/test/globalObject/global_object_has_own_property.js new file mode 100644 index 000000000..65c8c6a71 --- /dev/null +++ b/test/globalObject/global_object_has_own_property.js @@ -0,0 +1,51 @@ +'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) { + const KEY_TYPE = { + C_STR: 'KEY_AS_C_STRING', + CPP_STR: 'KEY_AS_CPP_STRING', + NAPI: 'KEY_AS_NAPI_VALUES', + INT_32: 'KEY_AS_INT_32_NUM' + }; + + function assertGlobalObjectHasProperty(key, keyType) + { + switch(keyType) + { + case KEY_TYPE.NAPI: + assert.strictEqual(binding.globalObject.hasPropertyWithNapiValue(key), true); + break; + + case KEY_TYPE.C_STR: + assert.strictEqual(binding.globalObject.hasPropertyWithCStyleString(key), true); + break; + + case KEY_TYPE.CPP_STR: + assert.strictEqual(binding.globalObject.hasPropertyWithCppStyleString(key), true); + break; + } + } + + function assertErrMessageIsThrown(propertyCheckExistanceFunction, errMsg) { + assert.throws(() => { + propertyCheckExistanceFunction(undefined); + }, errMsg); + } + + binding.globalObject.createMockTestObject(); + assertGlobalObjectHasProperty('c_str_key',KEY_TYPE.C_STR); + assertGlobalObjectHasProperty('cpp_string_key',KEY_TYPE.CPP_STR); + assertGlobalObjectHasProperty('circular',KEY_TYPE.CPP_STR); + assertGlobalObjectHasProperty('2', KEY_TYPE.NAPI); + + assertErrMessageIsThrown(binding.globalObject.hasPropertyWithCppStyleString, 'Error: A string was expected'); + assertErrMessageIsThrown(binding.globalObject.hasPropertyWithCStyleString, 'Error: A string was expected'); + assertErrMessageIsThrown(binding.globalObject.hasPropertyWithInt32, 'Error: A number was expected'); +} diff --git a/test/globalObject/global_object_set_property.cc b/test/globalObject/global_object_set_property.cc new file mode 100644 index 000000000..7065bee56 --- /dev/null +++ b/test/globalObject/global_object_set_property.cc @@ -0,0 +1,31 @@ +#include "napi.h" + +using namespace Napi; + +void SetPropertyWithCStyleStringAsKey(const CallbackInfo& info) { + Object globalObject = info.Env().Global(); + String key = info[0].As(); + Value value = info[1]; + globalObject.Set(key.Utf8Value().c_str(), value); +} + +void SetPropertyWithCppStyleStringAsKey(const CallbackInfo& info) { + Object globalObject = info.Env().Global(); + String key = info[0].As(); + Value value = info[1]; + globalObject.Set(key.Utf8Value(), value); +} + +void SetPropertyWithInt32AsKey(const CallbackInfo& info) { + Object globalObject = info.Env().Global(); + Number key = info[0].As(); + Value value = info[1]; + globalObject.Set(key.Uint32Value(), value); +} + +void SetPropertyWithNapiValueAsKey(const CallbackInfo& info) { + Object globalObject = info.Env().Global(); + Name key = info[0].As(); + Value value = info[1]; + globalObject.Set(key, value); +} \ No newline at end of file diff --git a/test/globalObject/global_object_set_property.js b/test/globalObject/global_object_set_property.js new file mode 100644 index 000000000..06f01a611 --- /dev/null +++ b/test/globalObject/global_object_set_property.js @@ -0,0 +1,61 @@ +'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) { + const KEY_TYPE = { + C_STR: 'KEY_AS_C_STRING', + CPP_STR: 'KEY_AS_CPP_STRING', + NAPI: 'KEY_AS_NAPI_VALUES', + INT_32: 'KEY_AS_INT_32_NUM' + }; + + function setGlobalObjectKeyValue(key, value, keyType) { + switch(keyType) + { + case KEY_TYPE.CPP_STR: + binding.globalObject.setPropertyWithCppStyleString(key,value); + break; + + case KEY_TYPE.C_STR: + binding.globalObject.setPropertyWithCStyleString(key,value); + break; + + case KEY_TYPE.INT_32: + binding.globalObject.setPropertyWithInt32(key,value); + break; + + case KEY_TYPE.NAPI: + binding.globalObject.setPropertyWithNapiValue(key,value); + break; + } + } + + function assertErrMessageIsThrown(nativeObjectSetFunction, errMsg) { + assert.throws(() => { + nativeObjectSetFunction(undefined, 1); + }, errMsg); + } + + + setGlobalObjectKeyValue("cKey","cValue",KEY_TYPE.CPP_STR); + setGlobalObjectKeyValue(1,10,KEY_TYPE.INT_32); + setGlobalObjectKeyValue("napi_key","napi_value",KEY_TYPE.NAPI); + setGlobalObjectKeyValue("cppKey","cppValue",KEY_TYPE.CPP_STR); + setGlobalObjectKeyValue("circular",global,KEY_TYPE.NAPI); + + assert.deepStrictEqual(global["circular"], global); + assert.deepStrictEqual(global["cppKey"],"cppValue"); + assert.deepStrictEqual(global["napi_key"],"napi_value"); + assert.deepStrictEqual(global[1],10); + assert.deepStrictEqual(global["cKey"],"cValue"); + + assertErrMessageIsThrown(binding.globalObject.setPropertyWithCppStyleString, 'Error: A string was expected'); + assertErrMessageIsThrown(binding.globalObject.setPropertyWithCStyleString, 'Error: A string was expected'); + assertErrMessageIsThrown(binding.globalObject.setPropertyWithInt32, 'Error: A number was expected'); +} diff --git a/test/object/delete_property.cc b/test/object/delete_property.cc index bd2488435..80caa7e31 100644 --- a/test/object/delete_property.cc +++ b/test/object/delete_property.cc @@ -2,6 +2,12 @@ using namespace Napi; +Value DeletePropertyWithUint32(const CallbackInfo& info) { + Object obj = info[0].As(); + Number key = info[1].As(); + return Boolean::New(info.Env(), obj.Delete(key.Uint32Value())); +} + Value DeletePropertyWithNapiValue(const CallbackInfo& info) { Object obj = info[0].As(); Name key = info[1].As(); diff --git a/test/object/delete_property.js b/test/object/delete_property.js index 8c313d03e..b65e063b0 100644 --- a/test/object/delete_property.js +++ b/test/object/delete_property.js @@ -25,6 +25,12 @@ function test(binding) { }, /Cannot convert undefined or null to object/); } + const testObj = { 15 : 42 , three: 3}; + + binding.object.deletePropertyWithUint32(testObj,15); + + assert.strictEqual(testObj.hasOwnProperty(15),false); + testDeleteProperty(binding.object.deletePropertyWithNapiValue); testDeleteProperty(binding.object.deletePropertyWithNapiWrapperValue); testDeleteProperty(binding.object.deletePropertyWithCStyleString); diff --git a/test/object/get_property.cc b/test/object/get_property.cc index 0cdaa50d7..8069da465 100644 --- a/test/object/get_property.cc +++ b/test/object/get_property.cc @@ -14,6 +14,12 @@ Value GetPropertyWithNapiWrapperValue(const CallbackInfo& info) { return obj.Get(key); } +Value GetPropertyWithUint32(const CallbackInfo& info) { + Object obj = info[0].As(); + Number key = info[1].As(); + return obj.Get(key.Uint32Value()); +} + Value GetPropertyWithCStyleString(const CallbackInfo& info) { Object obj = info[0].As(); String jsKey = info[1].As(); diff --git a/test/object/get_property.js b/test/object/get_property.js index 8028165c3..b2b68d914 100644 --- a/test/object/get_property.js +++ b/test/object/get_property.js @@ -23,6 +23,10 @@ function test(binding) { }, /Cannot convert undefined or null to object/); } + const testObject = { 42: 100 }; + const property = binding.object.getPropertyWithUint32(testObject, 42); + assert.strictEqual(property,100) + const nativeFunctions = [ binding.object.getPropertyWithNapiValue, binding.object.getPropertyWithNapiWrapperValue, diff --git a/test/object/has_property.cc b/test/object/has_property.cc index 0a1a45942..669935cc1 100644 --- a/test/object/has_property.cc +++ b/test/object/has_property.cc @@ -20,6 +20,12 @@ Value HasPropertyWithCStyleString(const CallbackInfo& info) { return Boolean::New(info.Env(), obj.Has(jsKey.Utf8Value().c_str())); } +Value HasPropertyWithUint32(const CallbackInfo& info) { + Object obj = info[0].As(); + Number jsKey = info[1].As(); + return Boolean::New(info.Env(), obj.Has(jsKey.Uint32Value())); +} + Value HasPropertyWithCppStyleString(const CallbackInfo& info) { Object obj = info[0].As(); String jsKey = info[1].As(); diff --git a/test/object/has_property.js b/test/object/has_property.js index a1b942dfb..f283fc77e 100644 --- a/test/object/has_property.js +++ b/test/object/has_property.js @@ -24,6 +24,9 @@ function test(binding) { }, /Cannot convert undefined or null to object/); } + const objectWithInt32Key = { 12: 101 }; + assert.strictEqual(binding.object.hasPropertyWithUint32(objectWithInt32Key,12),true); + testHasProperty(binding.object.hasPropertyWithNapiValue); testHasProperty(binding.object.hasPropertyWithNapiWrapperValue); testHasProperty(binding.object.hasPropertyWithCStyleString); diff --git a/test/object/object.cc b/test/object/object.cc index b2f6b5f95..3ee454e77 100644 --- a/test/object/object.cc +++ b/test/object/object.cc @@ -3,6 +3,7 @@ using namespace Napi; // Native wrappers for testing Object::Get() +Value GetPropertyWithUint32(const CallbackInfo& info); Value GetPropertyWithNapiValue(const CallbackInfo& info); Value GetPropertyWithNapiWrapperValue(const CallbackInfo& info); Value GetPropertyWithCStyleString(const CallbackInfo& info); @@ -15,6 +16,7 @@ void SetPropertyWithCStyleString(const CallbackInfo& info); void SetPropertyWithCppStyleString(const CallbackInfo& info); // Native wrappers for testing Object::Delete() +Value DeletePropertyWithUint32(const CallbackInfo& info); Value DeletePropertyWithNapiValue(const CallbackInfo& info); Value DeletePropertyWithNapiWrapperValue(const CallbackInfo& info); Value DeletePropertyWithCStyleString(const CallbackInfo& info); @@ -27,6 +29,7 @@ Value HasOwnPropertyWithCStyleString(const CallbackInfo& info); Value HasOwnPropertyWithCppStyleString(const CallbackInfo& info); // Native wrappers for testing Object::Has() +Value HasPropertyWithUint32(const CallbackInfo& info); Value HasPropertyWithNapiValue(const CallbackInfo& info); Value HasPropertyWithNapiWrapperValue(const CallbackInfo& info); Value HasPropertyWithCStyleString(const CallbackInfo& info); @@ -265,6 +268,7 @@ Object InitObject(Env env) { exports["defineProperties"] = Function::New(env, DefineProperties); exports["defineValueProperty"] = Function::New(env, DefineValueProperty); + exports["getPropertyWithUint32"] = Function::New(env, GetPropertyWithUint32); exports["getPropertyWithNapiValue"] = Function::New(env, GetPropertyWithNapiValue); exports["getPropertyWithNapiWrapperValue"] = Function::New(env, GetPropertyWithNapiWrapperValue); exports["getPropertyWithCStyleString"] = Function::New(env, GetPropertyWithCStyleString); @@ -275,6 +279,8 @@ Object InitObject(Env env) { exports["setPropertyWithCStyleString"] = Function::New(env, SetPropertyWithCStyleString); exports["setPropertyWithCppStyleString"] = Function::New(env, SetPropertyWithCppStyleString); + exports["deletePropertyWithUint32"] = + Function::New(env, DeletePropertyWithUint32); exports["deletePropertyWithNapiValue"] = Function::New(env, DeletePropertyWithNapiValue); exports["deletePropertyWithNapiWrapperValue"] = Function::New(env, DeletePropertyWithNapiWrapperValue); exports["deletePropertyWithCStyleString"] = Function::New(env, DeletePropertyWithCStyleString); @@ -285,6 +291,7 @@ Object InitObject(Env env) { exports["hasOwnPropertyWithCStyleString"] = Function::New(env, HasOwnPropertyWithCStyleString); exports["hasOwnPropertyWithCppStyleString"] = Function::New(env, HasOwnPropertyWithCppStyleString); + exports["hasPropertyWithUint32"] = Function::New(env, HasPropertyWithUint32); exports["hasPropertyWithNapiValue"] = Function::New(env, HasPropertyWithNapiValue); exports["hasPropertyWithNapiWrapperValue"] = Function::New(env, HasPropertyWithNapiWrapperValue); exports["hasPropertyWithCStyleString"] = Function::New(env, HasPropertyWithCStyleString);