From a840d51d2177cce8e552cf3e62b93440126e0885 Mon Sep 17 00:00:00 2001 From: meixg Date: Wed, 9 Feb 2022 14:37:24 +0800 Subject: [PATCH] Add test case for Object Set using uint32 as key --- test/object/object.cc | 2 ++ test/object/set_property.cc | 8 ++++++++ test/object/set_property.js | 11 ++++++----- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/test/object/object.cc b/test/object/object.cc index 6227b237a..934aacd3b 100644 --- a/test/object/object.cc +++ b/test/object/object.cc @@ -11,6 +11,7 @@ Value GetPropertyWithCStyleString(const CallbackInfo& info); Value GetPropertyWithCppStyleString(const CallbackInfo& info); // Native wrappers for testing Object::Set() +Value SetPropertyWithUint32(const CallbackInfo& info); Value SetPropertyWithNapiValue(const CallbackInfo& info); Value SetPropertyWithNapiWrapperValue(const CallbackInfo& info); Value SetPropertyWithCStyleString(const CallbackInfo& info); @@ -299,6 +300,7 @@ Object InitObject(Env env) { exports["getPropertyWithCStyleString"] = Function::New(env, GetPropertyWithCStyleString); exports["getPropertyWithCppStyleString"] = Function::New(env, GetPropertyWithCppStyleString); + exports["setPropertyWithUint32"] = Function::New(env, SetPropertyWithUint32); exports["setPropertyWithNapiValue"] = Function::New(env, SetPropertyWithNapiValue); exports["setPropertyWithNapiWrapperValue"] = Function::New(env, SetPropertyWithNapiWrapperValue); exports["setPropertyWithCStyleString"] = Function::New(env, SetPropertyWithCStyleString); diff --git a/test/object/set_property.cc b/test/object/set_property.cc index 5f10b4f09..8171568d6 100644 --- a/test/object/set_property.cc +++ b/test/object/set_property.cc @@ -19,6 +19,14 @@ Value SetPropertyWithNapiWrapperValue(const CallbackInfo& info) { return Boolean::New(info.Env(), MaybeUnwrapOr(obj.Set(key, value), false)); } +Value SetPropertyWithUint32(const CallbackInfo& info) { + Object obj = info[0].As(); + Number key = info[1].As(); + Value value = info[2]; + return Boolean::New(info.Env(), + MaybeUnwrapOr(obj.Set(key.Uint32Value(), value), false)); +} + Value SetPropertyWithCStyleString(const CallbackInfo& info) { Object obj = info[0].As(); String jsKey = info[1].As(); diff --git a/test/object/set_property.js b/test/object/set_property.js index 86fab3b75..b2a5fa12a 100644 --- a/test/object/set_property.js +++ b/test/object/set_property.js @@ -4,14 +4,14 @@ const assert = require('assert'); module.exports = require('../common').runTest(test); -function test(binding) { - function testSetProperty(nativeSetProperty) { +function test (binding) { + function testSetProperty (nativeSetProperty, key = 'test') { const obj = {}; - assert.strictEqual(nativeSetProperty(obj, 'test', 1), true); - assert.strictEqual(obj.test, 1); + assert.strictEqual(nativeSetProperty(obj, key, 1), true); + assert.strictEqual(obj[key], 1); } - function testShouldThrowErrorIfKeyIsInvalid(nativeSetProperty) { + function testShouldThrowErrorIfKeyIsInvalid (nativeSetProperty) { assert.throws(() => { nativeSetProperty(undefined, 'test', 1); }, /Cannot convert undefined or null to object/); @@ -21,6 +21,7 @@ function test(binding) { testSetProperty(binding.object.setPropertyWithNapiWrapperValue); testSetProperty(binding.object.setPropertyWithCStyleString); testSetProperty(binding.object.setPropertyWithCppStyleString); + testSetProperty(binding.object.setPropertyWithUint32, 12); testShouldThrowErrorIfKeyIsInvalid(binding.object.setPropertyWithNapiValue); testShouldThrowErrorIfKeyIsInvalid(binding.object.setPropertyWithNapiWrapperValue);