-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add missing tests for Object::Has() methods
There is no test for Object::Has() in current source tree. PR-URL: nodejs/node-addon-api#194 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
- Loading branch information
Showing
5 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters