Skip to content

Commit

Permalink
add tests for non-enumerable properties
Browse files Browse the repository at this point in the history
  • Loading branch information
vmoroz committed Apr 13, 2022
1 parent 9917174 commit 6633d34
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 9 deletions.
24 changes: 19 additions & 5 deletions test/js-native-api/test_object/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const object = {
assert.strictEqual(test_object.Get(object, 'hello'), 'world');
assert.strictEqual(test_object.GetNamed(object, 'hello'), 'world');
assert.deepStrictEqual(test_object.Get(object, 'array'),
[ 1, 94, 'str', 12.321, { test: 'obj in arr' } ]);
[1, 94, 'str', 12.321, { test: 'obj in arr' }]);
assert.deepStrictEqual(test_object.Get(object, 'newObject'),
{ test: 'obj in obj' });

Expand Down Expand Up @@ -54,7 +54,7 @@ assert.strictEqual(newObject.test_string, 'test string');

{
// Verify that napi_has_own_property() fails if property is not a name.
[true, false, null, undefined, {}, [], 0, 1, () => {}].forEach((value) => {
[true, false, null, undefined, {}, [], 0, 1, () => { }].forEach((value) => {
assert.throws(() => {
test_object.HasOwn({}, value);
}, /^Error: A string or symbol was expected$/);
Expand Down Expand Up @@ -238,7 +238,7 @@ assert.strictEqual(newObject.test_string, 'test string');
value: 4,
enumerable: false,
writable: true,
configurable: false
configurable: true
});
Object.defineProperty(object, 'writable', {
value: 4,
Expand All @@ -264,19 +264,33 @@ assert.strictEqual(newObject.test_string, 'test string');
assert.deepStrictEqual(test_object.GetSymbolNames(object),
[fooSymbol]);

assert.deepStrictEqual(test_object.GetWritableNames(object),
assert.deepStrictEqual(test_object.GetEnumerableWritableNames(object),
['5',
'normal',
'writable',
fooSymbol,
'inherited']);

assert.deepStrictEqual(test_object.GetConfigurableNames(object),
assert.deepStrictEqual(test_object.GetOwnWritableNames(object),
['5',
'normal',
'unenumerable',
'writable',
fooSymbol]);

assert.deepStrictEqual(test_object.GetEnumerableConfigurableNames(object),
['5',
'normal',
'configurable',
fooSymbol,
'inherited']);

assert.deepStrictEqual(test_object.GetOwnConfigurableNames(object),
['5',
'normal',
'unenumerable',
'configurable',
fooSymbol]);
}

// Verify that passing NULL to napi_set_property() results in the correct
Expand Down
67 changes: 63 additions & 4 deletions test/js-native-api/test_object/test_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ static napi_value GetSymbolNames(napi_env env, napi_callback_info info) {
return output;
}

static napi_value GetWritableNames(napi_env env, napi_callback_info info) {
static napi_value GetEnumerableWritableNames(napi_env env,
napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
Expand Down Expand Up @@ -134,7 +135,35 @@ static napi_value GetWritableNames(napi_env env, napi_callback_info info) {
return output;
}

static napi_value GetConfigurableNames(napi_env env, napi_callback_info info) {
static napi_value GetOwnWritableNames(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments");

napi_valuetype value_type0;
NODE_API_CALL(env, napi_typeof(env, args[0], &value_type0));

NODE_API_ASSERT(
env,
value_type0 == napi_object,
"Wrong type of arguments. Expects an object as first argument.");

napi_value output;
NODE_API_CALL(env,
napi_get_all_property_names(env,
args[0],
napi_key_own_only,
napi_key_writable,
napi_key_numbers_to_strings,
&output));

return output;
}

static napi_value GetEnumerableConfigurableNames(napi_env env,
napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
Expand Down Expand Up @@ -162,6 +191,34 @@ static napi_value GetConfigurableNames(napi_env env, napi_callback_info info) {
return output;
}

static napi_value GetOwnConfigurableNames(napi_env env,
napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments");

napi_valuetype value_type0;
NODE_API_CALL(env, napi_typeof(env, args[0], &value_type0));

NODE_API_ASSERT(
env,
value_type0 == napi_object,
"Wrong type of arguments. Expects an object as first argument.");

napi_value output;
NODE_API_CALL(env,
napi_get_all_property_names(env,
args[0],
napi_key_own_only,
napi_key_configurable,
napi_key_numbers_to_strings,
&output));

return output;
}

static napi_value Set(napi_env env, napi_callback_info info) {
size_t argc = 3;
napi_value args[3];
Expand Down Expand Up @@ -592,8 +649,10 @@ napi_value Init(napi_env env, napi_value exports) {
DECLARE_NODE_API_PROPERTY("GetNamed", GetNamed),
DECLARE_NODE_API_PROPERTY("GetPropertyNames", GetPropertyNames),
DECLARE_NODE_API_PROPERTY("GetSymbolNames", GetSymbolNames),
DECLARE_NODE_API_PROPERTY("GetWritableNames", GetWritableNames),
DECLARE_NODE_API_PROPERTY("GetConfigurableNames", GetConfigurableNames),
DECLARE_NODE_API_PROPERTY("GetEnumerableWritableNames", GetEnumerableWritableNames),
DECLARE_NODE_API_PROPERTY("GetOwnWritableNames", GetOwnWritableNames),
DECLARE_NODE_API_PROPERTY("GetEnumerableConfigurableNames", GetEnumerableConfigurableNames),
DECLARE_NODE_API_PROPERTY("GetOwnConfigurableNames", GetOwnConfigurableNames),
DECLARE_NODE_API_PROPERTY("Set", Set),
DECLARE_NODE_API_PROPERTY("SetNamed", SetNamed),
DECLARE_NODE_API_PROPERTY("Has", Has),
Expand Down

0 comments on commit 6633d34

Please sign in to comment.