Skip to content

Commit

Permalink
Add test case for Object Set using uint32 as key
Browse files Browse the repository at this point in the history
  • Loading branch information
meixg committed Feb 18, 2022
1 parent 2c88a7e commit a840d51
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions test/object/object.cc
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions test/object/set_property.cc
Expand Up @@ -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<Object>();
Number key = info[1].As<Number>();
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<Object>();
String jsKey = info[1].As<String>();
Expand Down
11 changes: 6 additions & 5 deletions test/object/set_property.js
Expand Up @@ -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/);
Expand All @@ -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);
Expand Down

0 comments on commit a840d51

Please sign in to comment.