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

Testing wfl execution #941

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3ef2aef
Finished tests relating to fetch property from Global Object
JckXia Jan 26, 2021
5d0880f
Style fixes
JckXia Jan 26, 2021
755cf38
More style fixes
JckXia Jan 26, 2021
fe10801
Add tests to check for error messages
JckXia Jan 26, 2021
0e2e346
Refactor object tests
JckXia Jan 26, 2021
6ad9f58
Add Cpp methods required to test object set
JckXia Jan 26, 2021
3ec7774
Style fixes
JckXia Jan 27, 2021
b038181
More style fixes
JckXia Jan 27, 2021
e53dfe8
Remove extra new line
JckXia Jan 27, 2021
29d8df3
Fixing style issue
JckXia Jan 27, 2021
88ebed9
Add error handling
JckXia Jan 27, 2021
d5b8f2c
Merge https://github.com/nodejs/node-addon-api into application
JckXia Jan 27, 2021
4c74759
Add C++ helper methods for testing has_own_property function
JckXia Jan 27, 2021
8417b46
Fixing style issues
JckXia Jan 28, 2021
90cf608
Fixing more style issues
JckXia Jan 28, 2021
2c7b006
Style fix
JckXia Jan 28, 2021
152cb3f
Final style fix, trying to resolve clang issue
JckXia Jan 28, 2021
dc1eb48
Still running into the same issue
JckXia Jan 28, 2021
976012e
Add tests for checking object HasProperty
JckXia Jan 28, 2021
9c3fc04
Added tests to check that we can remove property from global object
JckXia Jan 28, 2021
36cfa46
Merge branch 'main' of github.com:JckXia/node-addon-api into application
JckXia Mar 18, 2021
f505613
Add missing tests to object operator
JckXia Mar 18, 2021
c4135e1
Fixing lint issues
JckXia Mar 19, 2021
7e8a1ca
Fixing CI error caused by type mismatch
JckXia Mar 19, 2021
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/delete_property.cc',
'globalObject/has_own_property.cc',
'globalObject/set_property.cc',
'globalObject/get_property.cc',
'globalObject/object.cc',
'object/delete_property.cc',
'object/finalizer.cc',
'object/get_property.cc',
Expand Down
28 changes: 28 additions & 0 deletions test/globalObject/delete_property.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#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(static_cast<napi_value>(key)));
}
64 changes: 64 additions & 0 deletions test/globalObject/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');
}
41 changes: 41 additions & 0 deletions test/globalObject/get_property.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#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(static_cast<napi_value>(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();

napi_value napi_key;
napi_create_int32(info.Env(), 2, &napi_key);

globalObject.Set(napi_key, "napi_attribute");
const char* CStringKey = "c_str_key";
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/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');
}
23 changes: 23 additions & 0 deletions test/globalObject/has_own_property.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#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(static_cast<napi_value>(key)));
}
51 changes: 51 additions & 0 deletions test/globalObject/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');
}
61 changes: 61 additions & 0 deletions test/globalObject/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;
}
31 changes: 31 additions & 0 deletions test/globalObject/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(static_cast<napi_value>(key), value);
}
Loading