Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test case for Object Set using uint32 as key #1130

Merged
merged 1 commit into from Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>();
JckXia marked this conversation as resolved.
Show resolved Hide resolved
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