Skip to content

Commit 60374e0

Browse files
romandevmhdawson
authored andcommitted
test: Add missing tests for Object::Has() methods
There is no test for Object::Has() in current source tree. PR-URL: #194 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 6d518e4 commit 60374e0

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

test/binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
'object/delete_property.cc',
1515
'object/get_property.cc',
1616
'object/has_own_property.cc',
17+
'object/has_property.cc',
1718
'object/object.cc',
1819
'object/set_property.cc',
1920
'promise.cc',

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ let testModules = [
2020
'object/delete_property',
2121
'object/get_property',
2222
'object/has_own_property',
23+
'object/has_property',
2324
'object/object',
2425
'object/set_property',
2526
'promise',

test/object/has_property.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "napi.h"
2+
3+
using namespace Napi;
4+
5+
Value HasPropertyWithNapiValue(const CallbackInfo& info) {
6+
Object obj = info[0].As<Object>();
7+
Name key = info[1].As<Name>();
8+
return Boolean::New(info.Env(), obj.Has(static_cast<napi_value>(key)));
9+
}
10+
11+
Value HasPropertyWithNapiWrapperValue(const CallbackInfo& info) {
12+
Object obj = info[0].As<Object>();
13+
Name key = info[1].As<Name>();
14+
return Boolean::New(info.Env(), obj.Has(key));
15+
}
16+
17+
Value HasPropertyWithCStyleString(const CallbackInfo& info) {
18+
Object obj = info[0].As<Object>();
19+
String jsKey = info[1].As<String>();
20+
return Boolean::New(info.Env(), obj.Has(jsKey.Utf8Value().c_str()));
21+
}
22+
23+
Value HasPropertyWithCppStyleString(const CallbackInfo& info) {
24+
Object obj = info[0].As<Object>();
25+
String jsKey = info[1].As<String>();
26+
return Boolean::New(info.Env(), obj.Has(jsKey.Utf8Value()));
27+
}

test/object/has_property.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const buildType = process.config.target_defaults.default_configuration;
4+
const assert = require('assert');
5+
6+
test(require(`../build/${buildType}/binding.node`));
7+
test(require(`../build/${buildType}/binding_noexcept.node`));
8+
9+
function test(binding) {
10+
function testHasProperty(nativeHasProperty) {
11+
const obj = { one: 1 };
12+
13+
Object.defineProperty(obj, 'two', { value: 2 });
14+
15+
assert.strictEqual(nativeHasProperty(obj, 'one'), true);
16+
assert.strictEqual(nativeHasProperty(obj, 'two'), true);
17+
assert.strictEqual('toString' in obj, true);
18+
assert.strictEqual(nativeHasProperty(obj, 'toString'), true);
19+
}
20+
21+
function testShouldThrowErrorIfKeyIsInvalid(nativeHasProperty) {
22+
assert.throws(() => {
23+
nativeHasProperty(undefined, 'test');
24+
}, /object was expected/);
25+
}
26+
27+
testHasProperty(binding.object.hasPropertyWithNapiValue);
28+
testHasProperty(binding.object.hasPropertyWithNapiWrapperValue);
29+
testHasProperty(binding.object.hasPropertyWithCStyleString);
30+
testHasProperty(binding.object.hasPropertyWithCppStyleString);
31+
32+
testShouldThrowErrorIfKeyIsInvalid(binding.object.hasPropertyWithNapiValue);
33+
testShouldThrowErrorIfKeyIsInvalid(binding.object.hasPropertyWithNapiWrapperValue);
34+
testShouldThrowErrorIfKeyIsInvalid(binding.object.hasPropertyWithCStyleString);
35+
testShouldThrowErrorIfKeyIsInvalid(binding.object.hasPropertyWithCppStyleString);
36+
}

test/object/object.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ Value HasOwnPropertyWithNapiWrapperValue(const CallbackInfo& info);
2626
Value HasOwnPropertyWithCStyleString(const CallbackInfo& info);
2727
Value HasOwnPropertyWithCppStyleString(const CallbackInfo& info);
2828

29+
// Native wrappers for testing Object::Has()
30+
Value HasPropertyWithNapiValue(const CallbackInfo& info);
31+
Value HasPropertyWithNapiWrapperValue(const CallbackInfo& info);
32+
Value HasPropertyWithCStyleString(const CallbackInfo& info);
33+
Value HasPropertyWithCppStyleString(const CallbackInfo& info);
34+
2935
static bool testValue = true;
3036

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

170+
exports["hasPropertyWithNapiValue"] = Function::New(env, HasPropertyWithNapiValue);
171+
exports["hasPropertyWithNapiWrapperValue"] = Function::New(env, HasPropertyWithNapiWrapperValue);
172+
exports["hasPropertyWithCStyleString"] = Function::New(env, HasPropertyWithCStyleString);
173+
exports["hasPropertyWithCppStyleString"] = Function::New(env, HasPropertyWithCppStyleString);
174+
164175
exports["createObjectUsingMagic"] = Function::New(env, CreateObjectUsingMagic);
165176

166177
return exports;

0 commit comments

Comments
 (0)