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 tests for global objects and missing object tests using uint32 as key #939

Closed
wants to merge 3 commits 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
2 changes: 2 additions & 0 deletions test/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Object InitTypedThreadSafeFunctionUnref(Env env);
Object InitTypedThreadSafeFunction(Env env);
#endif
Object InitTypedArray(Env env);
Object InitGlobalObject(Env env);
Object InitObjectWrap(Env env);
Object InitObjectWrapConstructorException(Env env);
Object InitObjectWrapRemoveWrap(Env env);
Expand All @@ -78,6 +79,7 @@ Object Init(Env env, Object exports) {
exports.Set("asyncprogressqueueworker", InitAsyncProgressQueueWorker(env));
exports.Set("asyncprogressworker", InitAsyncProgressWorker(env));
#endif
exports.Set("globalObject", InitGlobalObject(env));
exports.Set("asyncworker", InitAsyncWorker(env));
exports.Set("persistentasyncworker", InitPersistentAsyncWorker(env));
exports.Set("basic_types_array", InitBasicTypesArray(env));
Expand Down
5 changes: 5 additions & 0 deletions test/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
'movable_callbacks.cc',
'memory_management.cc',
'name.cc',
'globalObject/global_object_delete_property.cc',
'globalObject/global_object_has_own_property.cc',
'globalObject/global_object_set_property.cc',
'globalObject/global_object_get_property.cc',
'globalObject/global_object.cc',
'object/delete_property.cc',
'object/finalizer.cc',
'object/get_property.cc',
Expand Down
61 changes: 61 additions & 0 deletions test/globalObject/global_object.cc
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;
}
27 changes: 27 additions & 0 deletions test/globalObject/global_object_delete_property.cc
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));
}
64 changes: 64 additions & 0 deletions test/globalObject/global_object_delete_property.js
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');
}
39 changes: 39 additions & 0 deletions test/globalObject/global_object_get_property.cc
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;
}
60 changes: 60 additions & 0 deletions test/globalObject/global_object_get_property.js
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');
}
22 changes: 22 additions & 0 deletions test/globalObject/global_object_has_own_property.cc
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));
}
51 changes: 51 additions & 0 deletions test/globalObject/global_object_has_own_property.js
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');
}
31 changes: 31 additions & 0 deletions test/globalObject/global_object_set_property.cc
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);
}
Loading