Skip to content

Commit

Permalink
test: tests for napi_get_all_property_names
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 committed Dec 10, 2019
1 parent 06f93b7 commit ece3006
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
7 changes: 6 additions & 1 deletion test/js-native-api/test_object/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,10 @@ assert.strictEqual(newObject.test_string, 'test string');
inherited: 1
});

const fooSymbol = Symbol('foo');

object.normal = 2;
object[Symbol('foo')] = 3;
object[fooSymbol] = 3;
Object.defineProperty(object, 'unenumerable', {
value: 4,
enumerable: false,
Expand All @@ -224,6 +226,9 @@ assert.strictEqual(newObject.test_string, 'test string');

assert.deepStrictEqual(test_object.GetPropertyNames(object),
['5', 'normal', 'inherited']);

assert.deepStrictEqual(test_object.GetSymbolNames(object),
[fooSymbol]);
}

// Verify that passing NULL to napi_set_property() results in the correct
Expand Down
30 changes: 30 additions & 0 deletions test/js-native-api/test_object/test_object.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define NAPI_EXPERIMENTAL

#include <js_native_api.h>
#include "../common.h"
#include <string.h>
Expand Down Expand Up @@ -82,6 +84,33 @@ static napi_value GetPropertyNames(napi_env env, napi_callback_info info) {
return output;
}

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

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

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

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

napi_value output;
NAPI_CALL(env,
napi_get_all_property_names(
env,
args[0],
napi_key_include_prototypes,
napi_key_skip_strings,
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 @@ -449,6 +478,7 @@ napi_value Init(napi_env env, napi_value exports) {
DECLARE_NAPI_PROPERTY("Get", Get),
DECLARE_NAPI_PROPERTY("GetNamed", GetNamed),
DECLARE_NAPI_PROPERTY("GetPropertyNames", GetPropertyNames),
DECLARE_NAPI_PROPERTY("GetSymbolNames", GetSymbolNames),
DECLARE_NAPI_PROPERTY("Set", Set),
DECLARE_NAPI_PROPERTY("SetNamed", SetNamed),
DECLARE_NAPI_PROPERTY("Has", Has),
Expand Down

0 comments on commit ece3006

Please sign in to comment.