Skip to content

Commit

Permalink
Add test for Object::operator[]
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseExposito committed Mar 5, 2021
1 parent 785c38f commit cc65d0a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
'object/has_property.cc',
'object/object.cc',
'object/set_property.cc',
'object/subscript_operator.cc',
'promise.cc',
'run_script.cc',
'threadsafe_function/threadsafe_function_ctx.cc',
Expand Down
15 changes: 15 additions & 0 deletions test/object/object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ Value HasPropertyWithCppStyleString(const CallbackInfo& info);
Value AddFinalizer(const CallbackInfo& info);
Value AddFinalizerWithHint(const CallbackInfo& info);

// Native wrappers for testing Object::operator []
Value SubscriptGetWithCStyleString(const CallbackInfo& info);
Value SubscriptGetWithCppStyleString(const CallbackInfo& info);
Value SubscriptGetAtIndex(const CallbackInfo& info);
void SubscriptSetWithCStyleString(const CallbackInfo& info);
void SubscriptSetWithCppStyleString(const CallbackInfo& info);
void SubscriptSetAtIndex(const CallbackInfo& info);

static bool testValue = true;
// Used to test void* Data() integrity
struct UserDataHolder {
Expand Down Expand Up @@ -279,5 +287,12 @@ Object InitObject(Env env) {

exports["instanceOf"] = Function::New(env, InstanceOf);

exports["subscriptGetWithCStyleString"] = Function::New(env, SubscriptGetWithCStyleString);
exports["subscriptGetWithCppStyleString"] = Function::New(env, SubscriptGetWithCppStyleString);
exports["subscriptGetAtIndex"] = Function::New(env, SubscriptGetAtIndex);
exports["subscriptSetWithCStyleString"] = Function::New(env, SubscriptSetWithCStyleString);
exports["subscriptSetWithCppStyleString"] = Function::New(env, SubscriptSetWithCppStyleString);
exports["subscriptSetAtIndex"] = Function::New(env, SubscriptSetAtIndex);

return exports;
}
42 changes: 42 additions & 0 deletions test/object/subscript_operator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "napi.h"

using namespace Napi;

Value SubscriptGetWithCStyleString(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
String jsKey = info[1].As<String>();
return obj[jsKey.Utf8Value().c_str()];
}

Value SubscriptGetWithCppStyleString(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
String jsKey = info[1].As<String>();
return obj[jsKey.Utf8Value()];
}

Value SubscriptGetAtIndex(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
uint32_t index = info[1].As<Napi::Number>();
return obj[index];
}

void SubscriptSetWithCStyleString(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
String jsKey = info[1].As<String>();
Value value = info[2];
obj[jsKey.Utf8Value().c_str()] = value;
}

void SubscriptSetWithCppStyleString(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
String jsKey = info[1].As<String>();
Value value = info[2];
obj[jsKey.Utf8Value()] = value;
}

void SubscriptSetAtIndex(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
uint32_t index = info[1].As<Napi::Number>();
Value value = info[2];
obj[index] = value;
}
18 changes: 18 additions & 0 deletions test/object/subscript_operator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'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 testProperty(obj, key, value, nativeGetProperty, nativeSetProperty) {
nativeSetProperty(obj, key, value);
assert.strictEqual(nativeGetProperty(obj, key), value);
}

testProperty({}, 'key', 'value', binding.object.subscriptGetWithCStyleString, binding.object.subscriptSetWithCStyleString);
testProperty({ key: 'override me' }, 'key', 'value', binding.object.subscriptGetWithCppStyleString, binding.object.subscriptSetWithCppStyleString);
testProperty({}, 0, 'value', binding.object.subscriptGetAtIndex, binding.object.subscriptSetAtIndex);
}

0 comments on commit cc65d0a

Please sign in to comment.