-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finished tests relating to fetch property from Global Object
PR-URL: #939 Reviewed-By: Nicola Del Gobbo <nicoladelgobbo@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
- Loading branch information
Showing
18 changed files
with
461 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,61 @@ | ||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
// Wrappers for testing Object::Get() for global Objects | ||
Value GetPropertyWithCppStyleStringAsKey(const CallbackInfo& info); | ||
Value GetPropertyWithCStyleStringAsKey(const CallbackInfo& info); | ||
Value GetPropertyWithInt32AsKey(const CallbackInfo& info); | ||
Value GetPropertyWithNapiValueAsKey(const CallbackInfo& info); | ||
void CreateMockTestObject(const CallbackInfo& info); | ||
|
||
// Wrapper for testing Object::Set() for global Objects | ||
void SetPropertyWithCStyleStringAsKey(const CallbackInfo& info); | ||
void SetPropertyWithCppStyleStringAsKey(const CallbackInfo& info); | ||
void SetPropertyWithInt32AsKey(const CallbackInfo& info); | ||
void SetPropertyWithNapiValueAsKey(const CallbackInfo& info); | ||
|
||
Value HasPropertyWithCStyleStringAsKey(const CallbackInfo& info); | ||
Value HasPropertyWithCppStyleStringAsKey(const CallbackInfo& info); | ||
Value HasPropertyWithNapiValueAsKey(const CallbackInfo& info); | ||
|
||
Value DeletePropertyWithCStyleStringAsKey(const CallbackInfo& info); | ||
Value DeletePropertyWithCppStyleStringAsKey(const CallbackInfo& info); | ||
Value DeletePropertyWithInt32AsKey(const CallbackInfo& info); | ||
Value DeletePropertyWithNapiValueAsKey(const CallbackInfo& info); | ||
|
||
Object InitGlobalObject(Env env) { | ||
Object exports = Object::New(env); | ||
exports["getPropertyWithInt32"] = | ||
Function::New(env, GetPropertyWithInt32AsKey); | ||
exports["getPropertyWithNapiValue"] = | ||
Function::New(env, GetPropertyWithNapiValueAsKey); | ||
exports["getPropertyWithCppString"] = | ||
Function::New(env, GetPropertyWithCppStyleStringAsKey); | ||
exports["getPropertyWithCString"] = | ||
Function::New(env, GetPropertyWithCStyleStringAsKey); | ||
exports["createMockTestObject"] = Function::New(env, CreateMockTestObject); | ||
exports["setPropertyWithCStyleString"] = | ||
Function::New(env, SetPropertyWithCStyleStringAsKey); | ||
exports["setPropertyWithCppStyleString"] = | ||
Function::New(env, SetPropertyWithCppStyleStringAsKey); | ||
exports["setPropertyWithNapiValue"] = | ||
Function::New(env, SetPropertyWithNapiValueAsKey); | ||
exports["setPropertyWithInt32"] = | ||
Function::New(env, SetPropertyWithInt32AsKey); | ||
exports["hasPropertyWithCStyleString"] = | ||
Function::New(env, HasPropertyWithCStyleStringAsKey); | ||
exports["hasPropertyWithCppStyleString"] = | ||
Function::New(env, HasPropertyWithCppStyleStringAsKey); | ||
exports["hasPropertyWithNapiValue"] = | ||
Function::New(env, HasPropertyWithNapiValueAsKey); | ||
exports["deletePropertyWithCStyleString"] = | ||
Function::New(env, DeletePropertyWithCStyleStringAsKey); | ||
exports["deletePropertyWithCppStyleString"] = | ||
Function::New(env, DeletePropertyWithCppStyleStringAsKey); | ||
exports["deletePropertyWithInt32"] = | ||
Function::New(env, DeletePropertyWithInt32AsKey); | ||
exports["deletePropertyWithNapiValue"] = | ||
Function::New(env, DeletePropertyWithNapiValueAsKey); | ||
return exports; | ||
} |
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 DeletePropertyWithCStyleStringAsKey(const CallbackInfo& info) { | ||
Object globalObject = info.Env().Global(); | ||
String key = info[0].As<String>(); | ||
return Boolean::New(info.Env(), globalObject.Delete(key.Utf8Value().c_str())); | ||
} | ||
|
||
Value DeletePropertyWithCppStyleStringAsKey(const CallbackInfo& info) { | ||
Object globalObject = info.Env().Global(); | ||
String key = info[0].As<String>(); | ||
return Boolean::New(info.Env(), globalObject.Delete(key.Utf8Value())); | ||
} | ||
|
||
Value DeletePropertyWithInt32AsKey(const CallbackInfo& info) { | ||
Object globalObject = info.Env().Global(); | ||
Number key = info[0].As<Number>(); | ||
return Boolean::New(info.Env(), globalObject.Delete(key.Uint32Value())); | ||
} | ||
|
||
Value DeletePropertyWithNapiValueAsKey(const CallbackInfo& info) { | ||
Object globalObject = info.Env().Global(); | ||
Name key = info[0].As<Name>(); | ||
return Boolean::New(info.Env(), globalObject.Delete(key)); | ||
} |
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,64 @@ | ||
'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) { | ||
const KEY_TYPE = { | ||
C_STR: 'KEY_AS_C_STRING', | ||
CPP_STR: 'KEY_AS_CPP_STRING', | ||
NAPI: 'KEY_AS_NAPI_VALUES', | ||
INT_32: 'KEY_AS_INT_32_NUM' | ||
}; | ||
|
||
function assertNotGlobalObjectHasNoProperty(key, keyType) | ||
{ | ||
switch(keyType) | ||
{ | ||
case KEY_TYPE.NAPI: | ||
assert.notStrictEqual(binding.globalObject.hasPropertyWithNapiValue(key), true); | ||
break; | ||
|
||
case KEY_TYPE.C_STR: | ||
assert.notStrictEqual(binding.globalObject.hasPropertyWithCStyleString(key), true); | ||
break; | ||
|
||
case KEY_TYPE.CPP_STR: | ||
assert.notStrictEqual(binding.globalObject.hasPropertyWithCppStyleString(key), true); | ||
break; | ||
|
||
case KEY_TYPE.INT_32: | ||
assert.notStrictEqual(binding.globalObject.hasPropertyWithInt32(key), true); | ||
break; | ||
} | ||
} | ||
|
||
function assertErrMessageIsThrown(propertyCheckExistanceFunction, errMsg) { | ||
assert.throws(() => { | ||
propertyCheckExistanceFunction(undefined); | ||
}, errMsg); | ||
} | ||
|
||
binding.globalObject.createMockTestObject(); | ||
|
||
binding.globalObject.deletePropertyWithCStyleString('c_str_key'); | ||
binding.globalObject.deletePropertyWithCppStyleString('cpp_string_key'); | ||
binding.globalObject.deletePropertyWithCppStyleString('circular'); | ||
binding.globalObject.deletePropertyWithInt32(15); | ||
binding.globalObject.deletePropertyWithNapiValue('2'); | ||
|
||
|
||
assertNotGlobalObjectHasNoProperty('c_str_key',KEY_TYPE.C_STR); | ||
assertNotGlobalObjectHasNoProperty('cpp_string_key',KEY_TYPE.CPP_STR); | ||
assertNotGlobalObjectHasNoProperty('circular',KEY_TYPE.CPP_STR); | ||
assertNotGlobalObjectHasNoProperty(15,true); | ||
assertNotGlobalObjectHasNoProperty('2', KEY_TYPE.NAPI); | ||
|
||
assertErrMessageIsThrown(binding.globalObject.hasPropertyWithCppStyleString, 'Error: A string was expected'); | ||
assertErrMessageIsThrown(binding.globalObject.hasPropertyWithCStyleString, 'Error: A string was expected'); | ||
assertErrMessageIsThrown(binding.globalObject.hasPropertyWithInt32, 'Error: A number was expected'); | ||
} |
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,39 @@ | ||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
Value GetPropertyWithNapiValueAsKey(const CallbackInfo& info) { | ||
Object globalObject = info.Env().Global(); | ||
Name key = info[0].As<Name>(); | ||
return globalObject.Get(key); | ||
} | ||
|
||
Value GetPropertyWithInt32AsKey(const CallbackInfo& info) { | ||
Object globalObject = info.Env().Global(); | ||
Number key = info[0].As<Napi::Number>(); | ||
return globalObject.Get(key.Uint32Value()); | ||
} | ||
|
||
Value GetPropertyWithCStyleStringAsKey(const CallbackInfo& info) { | ||
Object globalObject = info.Env().Global(); | ||
String cStrkey = info[0].As<String>(); | ||
return globalObject.Get(cStrkey.Utf8Value().c_str()); | ||
} | ||
|
||
Value GetPropertyWithCppStyleStringAsKey(const CallbackInfo& info) { | ||
Object globalObject = info.Env().Global(); | ||
String cppStrKey = info[0].As<String>(); | ||
return globalObject.Get(cppStrKey.Utf8Value()); | ||
} | ||
|
||
void CreateMockTestObject(const CallbackInfo& info) { | ||
Object globalObject = info.Env().Global(); | ||
Number napi_key = Number::New(info.Env(), 2); | ||
const char* CStringKey = "c_str_key"; | ||
|
||
globalObject.Set(napi_key, "napi_attribute"); | ||
globalObject[CStringKey] = "c_string_attribute"; | ||
globalObject[std::string("cpp_string_key")] = "cpp_string_attribute"; | ||
globalObject[std::string("circular")] = globalObject; | ||
globalObject[(uint32_t)15] = 15; | ||
} |
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,60 @@ | ||
'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) { | ||
const KEY_TYPE = { | ||
C_STR: 'KEY_AS_C_STRING', | ||
CPP_STR: 'KEY_AS_CPP_STRING', | ||
NAPI: 'KEY_AS_NAPI_VALUES', | ||
INT_32: 'KEY_AS_INT_32_NUM' | ||
}; | ||
|
||
binding.globalObject.createMockTestObject(); | ||
function assertGlobalObjectPropertyIs(key, attribute, keyType) { | ||
let napiObjectAttr; | ||
switch(keyType) | ||
{ | ||
case KEY_TYPE.NAPI: | ||
napiObjectAttr = binding.globalObject.getPropertyWithNapiValue(key); | ||
assert.deepStrictEqual(attribute, napiObjectAttr); | ||
break; | ||
|
||
case KEY_TYPE.C_STR: | ||
napiObjectAttr = binding.globalObject.getPropertyWithCString(key); | ||
assert.deepStrictEqual(attribute, napiObjectAttr); | ||
break; | ||
|
||
case KEY_TYPE.CPP_STR: | ||
napiObjectAttr = binding.globalObject.getPropertyWithCppString(key); | ||
assert.deepStrictEqual(attribute, napiObjectAttr); | ||
break; | ||
|
||
case KEY_TYPE.INT_32: | ||
napiObjectAttr = binding.globalObject.getPropertyWithInt32(key); | ||
assert.deepStrictEqual(attribute, napiObjectAttr); | ||
break; | ||
} | ||
} | ||
|
||
function assertErrMessageIsThrown(propertyFetchFunction, errMsg) { | ||
assert.throws(() => { | ||
propertyFetchFunction(undefined); | ||
}, errMsg); | ||
} | ||
|
||
assertGlobalObjectPropertyIs('2',global['2'], KEY_TYPE.NAPI); | ||
assertGlobalObjectPropertyIs('c_str_key',global['c_str_key'],KEY_TYPE.C_STR); | ||
assertGlobalObjectPropertyIs('cpp_string_key',global['cpp_string_key'],KEY_TYPE.CPP_STR); | ||
assertGlobalObjectPropertyIs('circular',global['circular'],KEY_TYPE.CPP_STR); | ||
assertGlobalObjectPropertyIs(15, global['15'], KEY_TYPE.INT_32); | ||
|
||
assertErrMessageIsThrown(binding.globalObject.getPropertyWithCString, 'Error: A string was expected'); | ||
assertErrMessageIsThrown(binding.globalObject.getPropertyWithCppString, 'Error: A string was expected'); | ||
assertErrMessageIsThrown(binding.globalObject.getPropertyWithInt32, 'Error: A number was expected'); | ||
} |
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,22 @@ | ||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
Value HasPropertyWithCStyleStringAsKey(const CallbackInfo& info) { | ||
Object globalObject = info.Env().Global(); | ||
String key = info[0].As<String>(); | ||
return Boolean::New(info.Env(), | ||
globalObject.HasOwnProperty(key.Utf8Value().c_str())); | ||
} | ||
|
||
Value HasPropertyWithCppStyleStringAsKey(const CallbackInfo& info) { | ||
Object globalObject = info.Env().Global(); | ||
String key = info[0].As<String>(); | ||
return Boolean::New(info.Env(), globalObject.HasOwnProperty(key.Utf8Value())); | ||
} | ||
|
||
Value HasPropertyWithNapiValueAsKey(const CallbackInfo& info) { | ||
Object globalObject = info.Env().Global(); | ||
Name key = info[0].As<Name>(); | ||
return Boolean::New(info.Env(), globalObject.HasOwnProperty(key)); | ||
} |
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,51 @@ | ||
'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) { | ||
const KEY_TYPE = { | ||
C_STR: 'KEY_AS_C_STRING', | ||
CPP_STR: 'KEY_AS_CPP_STRING', | ||
NAPI: 'KEY_AS_NAPI_VALUES', | ||
INT_32: 'KEY_AS_INT_32_NUM' | ||
}; | ||
|
||
function assertGlobalObjectHasProperty(key, keyType) | ||
{ | ||
switch(keyType) | ||
{ | ||
case KEY_TYPE.NAPI: | ||
assert.strictEqual(binding.globalObject.hasPropertyWithNapiValue(key), true); | ||
break; | ||
|
||
case KEY_TYPE.C_STR: | ||
assert.strictEqual(binding.globalObject.hasPropertyWithCStyleString(key), true); | ||
break; | ||
|
||
case KEY_TYPE.CPP_STR: | ||
assert.strictEqual(binding.globalObject.hasPropertyWithCppStyleString(key), true); | ||
break; | ||
} | ||
} | ||
|
||
function assertErrMessageIsThrown(propertyCheckExistanceFunction, errMsg) { | ||
assert.throws(() => { | ||
propertyCheckExistanceFunction(undefined); | ||
}, errMsg); | ||
} | ||
|
||
binding.globalObject.createMockTestObject(); | ||
assertGlobalObjectHasProperty('c_str_key',KEY_TYPE.C_STR); | ||
assertGlobalObjectHasProperty('cpp_string_key',KEY_TYPE.CPP_STR); | ||
assertGlobalObjectHasProperty('circular',KEY_TYPE.CPP_STR); | ||
assertGlobalObjectHasProperty('2', KEY_TYPE.NAPI); | ||
|
||
assertErrMessageIsThrown(binding.globalObject.hasPropertyWithCppStyleString, 'Error: A string was expected'); | ||
assertErrMessageIsThrown(binding.globalObject.hasPropertyWithCStyleString, 'Error: A string was expected'); | ||
assertErrMessageIsThrown(binding.globalObject.hasPropertyWithInt32, 'Error: A number was expected'); | ||
} |
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,31 @@ | ||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
void SetPropertyWithCStyleStringAsKey(const CallbackInfo& info) { | ||
Object globalObject = info.Env().Global(); | ||
String key = info[0].As<String>(); | ||
Value value = info[1]; | ||
globalObject.Set(key.Utf8Value().c_str(), value); | ||
} | ||
|
||
void SetPropertyWithCppStyleStringAsKey(const CallbackInfo& info) { | ||
Object globalObject = info.Env().Global(); | ||
String key = info[0].As<String>(); | ||
Value value = info[1]; | ||
globalObject.Set(key.Utf8Value(), value); | ||
} | ||
|
||
void SetPropertyWithInt32AsKey(const CallbackInfo& info) { | ||
Object globalObject = info.Env().Global(); | ||
Number key = info[0].As<Number>(); | ||
Value value = info[1]; | ||
globalObject.Set(key.Uint32Value(), value); | ||
} | ||
|
||
void SetPropertyWithNapiValueAsKey(const CallbackInfo& info) { | ||
Object globalObject = info.Env().Global(); | ||
Name key = info[0].As<Name>(); | ||
Value value = info[1]; | ||
globalObject.Set(key, value); | ||
} |
Oops, something went wrong.