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 missing tests for Object::Has() methods #194

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'object/delete_property.cc',
'object/get_property.cc',
'object/has_own_property.cc',
'object/has_property.cc',
'object/object.cc',
'object/set_property.cc',
'promise.cc',
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ let testModules = [
'object/delete_property',
'object/get_property',
'object/has_own_property',
'object/has_property',
'object/object',
'object/set_property',
'promise',
Expand Down
27 changes: 27 additions & 0 deletions test/object/has_property.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "napi.h"

using namespace Napi;

Value HasPropertyWithNapiValue(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
Name key = info[1].As<Name>();
return Boolean::New(info.Env(), obj.Has(static_cast<napi_value>(key)));
}

Value HasPropertyWithNapiWrapperValue(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
Name key = info[1].As<Name>();
return Boolean::New(info.Env(), obj.Has(key));
}

Value HasPropertyWithCStyleString(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
String jsKey = info[1].As<String>();
return Boolean::New(info.Env(), obj.Has(jsKey.Utf8Value().c_str()));
}

Value HasPropertyWithCppStyleString(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
String jsKey = info[1].As<String>();
return Boolean::New(info.Env(), obj.Has(jsKey.Utf8Value()));
}
36 changes: 36 additions & 0 deletions test/object/has_property.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'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 testHasProperty(nativeHasProperty) {
const obj = { one: 1 };

Object.defineProperty(obj, 'two', { value: 2 });

assert.strictEqual(nativeHasProperty(obj, 'one'), true);
assert.strictEqual(nativeHasProperty(obj, 'two'), true);
assert.strictEqual('toString' in obj, true);
assert.strictEqual(nativeHasProperty(obj, 'toString'), true);
}

function testShouldThrowErrorIfKeyIsInvalid(nativeHasProperty) {
assert.throws(() => {
nativeHasProperty(undefined, 'test');
}, /object was expected/);
}

testHasProperty(binding.object.hasPropertyWithNapiValue);
testHasProperty(binding.object.hasPropertyWithNapiWrapperValue);
testHasProperty(binding.object.hasPropertyWithCStyleString);
testHasProperty(binding.object.hasPropertyWithCppStyleString);

testShouldThrowErrorIfKeyIsInvalid(binding.object.hasPropertyWithNapiValue);
testShouldThrowErrorIfKeyIsInvalid(binding.object.hasPropertyWithNapiWrapperValue);
testShouldThrowErrorIfKeyIsInvalid(binding.object.hasPropertyWithCStyleString);
testShouldThrowErrorIfKeyIsInvalid(binding.object.hasPropertyWithCppStyleString);
}
11 changes: 11 additions & 0 deletions test/object/object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ Value HasOwnPropertyWithNapiWrapperValue(const CallbackInfo& info);
Value HasOwnPropertyWithCStyleString(const CallbackInfo& info);
Value HasOwnPropertyWithCppStyleString(const CallbackInfo& info);

// Native wrappers for testing Object::Has()
Value HasPropertyWithNapiValue(const CallbackInfo& info);
Value HasPropertyWithNapiWrapperValue(const CallbackInfo& info);
Value HasPropertyWithCStyleString(const CallbackInfo& info);
Value HasPropertyWithCppStyleString(const CallbackInfo& info);

static bool testValue = true;

Value TestGetter(const CallbackInfo& info) {
Expand Down Expand Up @@ -161,6 +167,11 @@ Object InitObject(Env env) {
exports["hasOwnPropertyWithCStyleString"] = Function::New(env, HasOwnPropertyWithCStyleString);
exports["hasOwnPropertyWithCppStyleString"] = Function::New(env, HasOwnPropertyWithCppStyleString);

exports["hasPropertyWithNapiValue"] = Function::New(env, HasPropertyWithNapiValue);
exports["hasPropertyWithNapiWrapperValue"] = Function::New(env, HasPropertyWithNapiWrapperValue);
exports["hasPropertyWithCStyleString"] = Function::New(env, HasPropertyWithCStyleString);
exports["hasPropertyWithCppStyleString"] = Function::New(env, HasPropertyWithCppStyleString);

exports["createObjectUsingMagic"] = Function::New(env, CreateObjectUsingMagic);

return exports;
Expand Down