Skip to content

Commit bc5147c

Browse files
JckXiamhdawson
authored andcommitted
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>
1 parent 0127813 commit bc5147c

18 files changed

+461
-0
lines changed

test/binding.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Object InitTypedThreadSafeFunctionUnref(Env env);
5858
Object InitTypedThreadSafeFunction(Env env);
5959
#endif
6060
Object InitTypedArray(Env env);
61+
Object InitGlobalObject(Env env);
6162
Object InitObjectWrap(Env env);
6263
Object InitObjectWrapConstructorException(Env env);
6364
Object InitObjectWrapRemoveWrap(Env env);
@@ -78,6 +79,7 @@ Object Init(Env env, Object exports) {
7879
exports.Set("asyncprogressqueueworker", InitAsyncProgressQueueWorker(env));
7980
exports.Set("asyncprogressworker", InitAsyncProgressWorker(env));
8081
#endif
82+
exports.Set("globalObject", InitGlobalObject(env));
8183
exports.Set("asyncworker", InitAsyncWorker(env));
8284
exports.Set("persistentasyncworker", InitPersistentAsyncWorker(env));
8385
exports.Set("basic_types_array", InitBasicTypesArray(env));

test/binding.gyp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
'movable_callbacks.cc',
2929
'memory_management.cc',
3030
'name.cc',
31+
'globalObject/global_object_delete_property.cc',
32+
'globalObject/global_object_has_own_property.cc',
33+
'globalObject/global_object_set_property.cc',
34+
'globalObject/global_object_get_property.cc',
35+
'globalObject/global_object.cc',
3136
'object/delete_property.cc',
3237
'object/finalizer.cc',
3338
'object/get_property.cc',

test/globalObject/global_object.cc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "napi.h"
2+
3+
using namespace Napi;
4+
5+
// Wrappers for testing Object::Get() for global Objects
6+
Value GetPropertyWithCppStyleStringAsKey(const CallbackInfo& info);
7+
Value GetPropertyWithCStyleStringAsKey(const CallbackInfo& info);
8+
Value GetPropertyWithInt32AsKey(const CallbackInfo& info);
9+
Value GetPropertyWithNapiValueAsKey(const CallbackInfo& info);
10+
void CreateMockTestObject(const CallbackInfo& info);
11+
12+
// Wrapper for testing Object::Set() for global Objects
13+
void SetPropertyWithCStyleStringAsKey(const CallbackInfo& info);
14+
void SetPropertyWithCppStyleStringAsKey(const CallbackInfo& info);
15+
void SetPropertyWithInt32AsKey(const CallbackInfo& info);
16+
void SetPropertyWithNapiValueAsKey(const CallbackInfo& info);
17+
18+
Value HasPropertyWithCStyleStringAsKey(const CallbackInfo& info);
19+
Value HasPropertyWithCppStyleStringAsKey(const CallbackInfo& info);
20+
Value HasPropertyWithNapiValueAsKey(const CallbackInfo& info);
21+
22+
Value DeletePropertyWithCStyleStringAsKey(const CallbackInfo& info);
23+
Value DeletePropertyWithCppStyleStringAsKey(const CallbackInfo& info);
24+
Value DeletePropertyWithInt32AsKey(const CallbackInfo& info);
25+
Value DeletePropertyWithNapiValueAsKey(const CallbackInfo& info);
26+
27+
Object InitGlobalObject(Env env) {
28+
Object exports = Object::New(env);
29+
exports["getPropertyWithInt32"] =
30+
Function::New(env, GetPropertyWithInt32AsKey);
31+
exports["getPropertyWithNapiValue"] =
32+
Function::New(env, GetPropertyWithNapiValueAsKey);
33+
exports["getPropertyWithCppString"] =
34+
Function::New(env, GetPropertyWithCppStyleStringAsKey);
35+
exports["getPropertyWithCString"] =
36+
Function::New(env, GetPropertyWithCStyleStringAsKey);
37+
exports["createMockTestObject"] = Function::New(env, CreateMockTestObject);
38+
exports["setPropertyWithCStyleString"] =
39+
Function::New(env, SetPropertyWithCStyleStringAsKey);
40+
exports["setPropertyWithCppStyleString"] =
41+
Function::New(env, SetPropertyWithCppStyleStringAsKey);
42+
exports["setPropertyWithNapiValue"] =
43+
Function::New(env, SetPropertyWithNapiValueAsKey);
44+
exports["setPropertyWithInt32"] =
45+
Function::New(env, SetPropertyWithInt32AsKey);
46+
exports["hasPropertyWithCStyleString"] =
47+
Function::New(env, HasPropertyWithCStyleStringAsKey);
48+
exports["hasPropertyWithCppStyleString"] =
49+
Function::New(env, HasPropertyWithCppStyleStringAsKey);
50+
exports["hasPropertyWithNapiValue"] =
51+
Function::New(env, HasPropertyWithNapiValueAsKey);
52+
exports["deletePropertyWithCStyleString"] =
53+
Function::New(env, DeletePropertyWithCStyleStringAsKey);
54+
exports["deletePropertyWithCppStyleString"] =
55+
Function::New(env, DeletePropertyWithCppStyleStringAsKey);
56+
exports["deletePropertyWithInt32"] =
57+
Function::New(env, DeletePropertyWithInt32AsKey);
58+
exports["deletePropertyWithNapiValue"] =
59+
Function::New(env, DeletePropertyWithNapiValueAsKey);
60+
return exports;
61+
}
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 DeletePropertyWithCStyleStringAsKey(const CallbackInfo& info) {
6+
Object globalObject = info.Env().Global();
7+
String key = info[0].As<String>();
8+
return Boolean::New(info.Env(), globalObject.Delete(key.Utf8Value().c_str()));
9+
}
10+
11+
Value DeletePropertyWithCppStyleStringAsKey(const CallbackInfo& info) {
12+
Object globalObject = info.Env().Global();
13+
String key = info[0].As<String>();
14+
return Boolean::New(info.Env(), globalObject.Delete(key.Utf8Value()));
15+
}
16+
17+
Value DeletePropertyWithInt32AsKey(const CallbackInfo& info) {
18+
Object globalObject = info.Env().Global();
19+
Number key = info[0].As<Number>();
20+
return Boolean::New(info.Env(), globalObject.Delete(key.Uint32Value()));
21+
}
22+
23+
Value DeletePropertyWithNapiValueAsKey(const CallbackInfo& info) {
24+
Object globalObject = info.Env().Global();
25+
Name key = info[0].As<Name>();
26+
return Boolean::New(info.Env(), globalObject.Delete(key));
27+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
10+
function test(binding) {
11+
const KEY_TYPE = {
12+
C_STR: 'KEY_AS_C_STRING',
13+
CPP_STR: 'KEY_AS_CPP_STRING',
14+
NAPI: 'KEY_AS_NAPI_VALUES',
15+
INT_32: 'KEY_AS_INT_32_NUM'
16+
};
17+
18+
function assertNotGlobalObjectHasNoProperty(key, keyType)
19+
{
20+
switch(keyType)
21+
{
22+
case KEY_TYPE.NAPI:
23+
assert.notStrictEqual(binding.globalObject.hasPropertyWithNapiValue(key), true);
24+
break;
25+
26+
case KEY_TYPE.C_STR:
27+
assert.notStrictEqual(binding.globalObject.hasPropertyWithCStyleString(key), true);
28+
break;
29+
30+
case KEY_TYPE.CPP_STR:
31+
assert.notStrictEqual(binding.globalObject.hasPropertyWithCppStyleString(key), true);
32+
break;
33+
34+
case KEY_TYPE.INT_32:
35+
assert.notStrictEqual(binding.globalObject.hasPropertyWithInt32(key), true);
36+
break;
37+
}
38+
}
39+
40+
function assertErrMessageIsThrown(propertyCheckExistanceFunction, errMsg) {
41+
assert.throws(() => {
42+
propertyCheckExistanceFunction(undefined);
43+
}, errMsg);
44+
}
45+
46+
binding.globalObject.createMockTestObject();
47+
48+
binding.globalObject.deletePropertyWithCStyleString('c_str_key');
49+
binding.globalObject.deletePropertyWithCppStyleString('cpp_string_key');
50+
binding.globalObject.deletePropertyWithCppStyleString('circular');
51+
binding.globalObject.deletePropertyWithInt32(15);
52+
binding.globalObject.deletePropertyWithNapiValue('2');
53+
54+
55+
assertNotGlobalObjectHasNoProperty('c_str_key',KEY_TYPE.C_STR);
56+
assertNotGlobalObjectHasNoProperty('cpp_string_key',KEY_TYPE.CPP_STR);
57+
assertNotGlobalObjectHasNoProperty('circular',KEY_TYPE.CPP_STR);
58+
assertNotGlobalObjectHasNoProperty(15,true);
59+
assertNotGlobalObjectHasNoProperty('2', KEY_TYPE.NAPI);
60+
61+
assertErrMessageIsThrown(binding.globalObject.hasPropertyWithCppStyleString, 'Error: A string was expected');
62+
assertErrMessageIsThrown(binding.globalObject.hasPropertyWithCStyleString, 'Error: A string was expected');
63+
assertErrMessageIsThrown(binding.globalObject.hasPropertyWithInt32, 'Error: A number was expected');
64+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "napi.h"
2+
3+
using namespace Napi;
4+
5+
Value GetPropertyWithNapiValueAsKey(const CallbackInfo& info) {
6+
Object globalObject = info.Env().Global();
7+
Name key = info[0].As<Name>();
8+
return globalObject.Get(key);
9+
}
10+
11+
Value GetPropertyWithInt32AsKey(const CallbackInfo& info) {
12+
Object globalObject = info.Env().Global();
13+
Number key = info[0].As<Napi::Number>();
14+
return globalObject.Get(key.Uint32Value());
15+
}
16+
17+
Value GetPropertyWithCStyleStringAsKey(const CallbackInfo& info) {
18+
Object globalObject = info.Env().Global();
19+
String cStrkey = info[0].As<String>();
20+
return globalObject.Get(cStrkey.Utf8Value().c_str());
21+
}
22+
23+
Value GetPropertyWithCppStyleStringAsKey(const CallbackInfo& info) {
24+
Object globalObject = info.Env().Global();
25+
String cppStrKey = info[0].As<String>();
26+
return globalObject.Get(cppStrKey.Utf8Value());
27+
}
28+
29+
void CreateMockTestObject(const CallbackInfo& info) {
30+
Object globalObject = info.Env().Global();
31+
Number napi_key = Number::New(info.Env(), 2);
32+
const char* CStringKey = "c_str_key";
33+
34+
globalObject.Set(napi_key, "napi_attribute");
35+
globalObject[CStringKey] = "c_string_attribute";
36+
globalObject[std::string("cpp_string_key")] = "cpp_string_attribute";
37+
globalObject[std::string("circular")] = globalObject;
38+
globalObject[(uint32_t)15] = 15;
39+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
10+
function test(binding) {
11+
const KEY_TYPE = {
12+
C_STR: 'KEY_AS_C_STRING',
13+
CPP_STR: 'KEY_AS_CPP_STRING',
14+
NAPI: 'KEY_AS_NAPI_VALUES',
15+
INT_32: 'KEY_AS_INT_32_NUM'
16+
};
17+
18+
binding.globalObject.createMockTestObject();
19+
function assertGlobalObjectPropertyIs(key, attribute, keyType) {
20+
let napiObjectAttr;
21+
switch(keyType)
22+
{
23+
case KEY_TYPE.NAPI:
24+
napiObjectAttr = binding.globalObject.getPropertyWithNapiValue(key);
25+
assert.deepStrictEqual(attribute, napiObjectAttr);
26+
break;
27+
28+
case KEY_TYPE.C_STR:
29+
napiObjectAttr = binding.globalObject.getPropertyWithCString(key);
30+
assert.deepStrictEqual(attribute, napiObjectAttr);
31+
break;
32+
33+
case KEY_TYPE.CPP_STR:
34+
napiObjectAttr = binding.globalObject.getPropertyWithCppString(key);
35+
assert.deepStrictEqual(attribute, napiObjectAttr);
36+
break;
37+
38+
case KEY_TYPE.INT_32:
39+
napiObjectAttr = binding.globalObject.getPropertyWithInt32(key);
40+
assert.deepStrictEqual(attribute, napiObjectAttr);
41+
break;
42+
}
43+
}
44+
45+
function assertErrMessageIsThrown(propertyFetchFunction, errMsg) {
46+
assert.throws(() => {
47+
propertyFetchFunction(undefined);
48+
}, errMsg);
49+
}
50+
51+
assertGlobalObjectPropertyIs('2',global['2'], KEY_TYPE.NAPI);
52+
assertGlobalObjectPropertyIs('c_str_key',global['c_str_key'],KEY_TYPE.C_STR);
53+
assertGlobalObjectPropertyIs('cpp_string_key',global['cpp_string_key'],KEY_TYPE.CPP_STR);
54+
assertGlobalObjectPropertyIs('circular',global['circular'],KEY_TYPE.CPP_STR);
55+
assertGlobalObjectPropertyIs(15, global['15'], KEY_TYPE.INT_32);
56+
57+
assertErrMessageIsThrown(binding.globalObject.getPropertyWithCString, 'Error: A string was expected');
58+
assertErrMessageIsThrown(binding.globalObject.getPropertyWithCppString, 'Error: A string was expected');
59+
assertErrMessageIsThrown(binding.globalObject.getPropertyWithInt32, 'Error: A number was expected');
60+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "napi.h"
2+
3+
using namespace Napi;
4+
5+
Value HasPropertyWithCStyleStringAsKey(const CallbackInfo& info) {
6+
Object globalObject = info.Env().Global();
7+
String key = info[0].As<String>();
8+
return Boolean::New(info.Env(),
9+
globalObject.HasOwnProperty(key.Utf8Value().c_str()));
10+
}
11+
12+
Value HasPropertyWithCppStyleStringAsKey(const CallbackInfo& info) {
13+
Object globalObject = info.Env().Global();
14+
String key = info[0].As<String>();
15+
return Boolean::New(info.Env(), globalObject.HasOwnProperty(key.Utf8Value()));
16+
}
17+
18+
Value HasPropertyWithNapiValueAsKey(const CallbackInfo& info) {
19+
Object globalObject = info.Env().Global();
20+
Name key = info[0].As<Name>();
21+
return Boolean::New(info.Env(), globalObject.HasOwnProperty(key));
22+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
10+
function test(binding) {
11+
const KEY_TYPE = {
12+
C_STR: 'KEY_AS_C_STRING',
13+
CPP_STR: 'KEY_AS_CPP_STRING',
14+
NAPI: 'KEY_AS_NAPI_VALUES',
15+
INT_32: 'KEY_AS_INT_32_NUM'
16+
};
17+
18+
function assertGlobalObjectHasProperty(key, keyType)
19+
{
20+
switch(keyType)
21+
{
22+
case KEY_TYPE.NAPI:
23+
assert.strictEqual(binding.globalObject.hasPropertyWithNapiValue(key), true);
24+
break;
25+
26+
case KEY_TYPE.C_STR:
27+
assert.strictEqual(binding.globalObject.hasPropertyWithCStyleString(key), true);
28+
break;
29+
30+
case KEY_TYPE.CPP_STR:
31+
assert.strictEqual(binding.globalObject.hasPropertyWithCppStyleString(key), true);
32+
break;
33+
}
34+
}
35+
36+
function assertErrMessageIsThrown(propertyCheckExistanceFunction, errMsg) {
37+
assert.throws(() => {
38+
propertyCheckExistanceFunction(undefined);
39+
}, errMsg);
40+
}
41+
42+
binding.globalObject.createMockTestObject();
43+
assertGlobalObjectHasProperty('c_str_key',KEY_TYPE.C_STR);
44+
assertGlobalObjectHasProperty('cpp_string_key',KEY_TYPE.CPP_STR);
45+
assertGlobalObjectHasProperty('circular',KEY_TYPE.CPP_STR);
46+
assertGlobalObjectHasProperty('2', KEY_TYPE.NAPI);
47+
48+
assertErrMessageIsThrown(binding.globalObject.hasPropertyWithCppStyleString, 'Error: A string was expected');
49+
assertErrMessageIsThrown(binding.globalObject.hasPropertyWithCStyleString, 'Error: A string was expected');
50+
assertErrMessageIsThrown(binding.globalObject.hasPropertyWithInt32, 'Error: A number was expected');
51+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "napi.h"
2+
3+
using namespace Napi;
4+
5+
void SetPropertyWithCStyleStringAsKey(const CallbackInfo& info) {
6+
Object globalObject = info.Env().Global();
7+
String key = info[0].As<String>();
8+
Value value = info[1];
9+
globalObject.Set(key.Utf8Value().c_str(), value);
10+
}
11+
12+
void SetPropertyWithCppStyleStringAsKey(const CallbackInfo& info) {
13+
Object globalObject = info.Env().Global();
14+
String key = info[0].As<String>();
15+
Value value = info[1];
16+
globalObject.Set(key.Utf8Value(), value);
17+
}
18+
19+
void SetPropertyWithInt32AsKey(const CallbackInfo& info) {
20+
Object globalObject = info.Env().Global();
21+
Number key = info[0].As<Number>();
22+
Value value = info[1];
23+
globalObject.Set(key.Uint32Value(), value);
24+
}
25+
26+
void SetPropertyWithNapiValueAsKey(const CallbackInfo& info) {
27+
Object globalObject = info.Env().Global();
28+
Name key = info[0].As<Name>();
29+
Value value = info[1];
30+
globalObject.Set(key, value);
31+
}

0 commit comments

Comments
 (0)