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

test: add coverage for napi_typeof #13990

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions test/addons-napi/test_general/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ assert.ok(test_general.testGetPrototype(baseObject) !==
// test version management functions
// expected version is currently 1
assert.strictEqual(test_general.testGetVersion(), 1);

[123, 'test string', function() {}, new Object(), true,
undefined, Symbol()].forEach((val) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small readability nit

[
  123,
  'test string',
  function() {},
  new Object(),
  true,
  undefined,
  Symbol()
].forEach( ... )

assert.strictEqual(typeof val, typeof (test_general.testNapiTypeof(val)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this test is specifically for napi_typeof(), I don't understand why test_general.testNapiTypeof() returns a value of that type instead of just a string representing the type. Then, this assertion could be:

assert.strictEqual(typeof val, test_general.testNapiTypeof(val));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would probably work as well. Just not what I can up with. I'll take a look as its probably an easy change.

});

// since typeof in js return object need to validate specific case
// for null
assert.strictEqual(null, test_general.testNapiTypeof(null));
36 changes: 36 additions & 0 deletions test/addons-napi/test_general/test_general.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,41 @@ napi_value testNapiErrorCleanup(napi_env env, napi_callback_info info) {
return result;
}

napi_value testNapiTypeof(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_valuetype object_type;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think something like argument_type would be more descriptive of what this variable is.

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

napi_value result;
if (object_type == napi_number) {
NAPI_CALL(env ,napi_create_number(env, 42, &result));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comma after env is misaligned.

} else if (object_type == napi_string) {
NAPI_CALL(env, napi_create_string_utf8(env, "42", -1, &result));
} else if (object_type == napi_function) {
NAPI_CALL(env, napi_create_function(env,
NULL,
testNapiTypeof,
NULL,
&result));
} else if (object_type == napi_object) {
NAPI_CALL(env, napi_create_object(env, &result));
} else if (object_type == napi_boolean) {
NAPI_CALL(env, napi_get_boolean(env, true, &result));
} else if (object_type == napi_undefined) {
NAPI_CALL(env, napi_get_undefined(env, &result));
} else if (object_type == napi_symbol) {
napi_value symbol_description;
NAPI_CALL(env, napi_create_string_utf8(env, "NameKeySymbol", -1, &symbol_description));
NAPI_CALL(env, napi_create_symbol(env, symbol_description, &result));
} else if (object_type == napi_null) {
NAPI_CALL(env, napi_get_null(env, &result));
}
return result;
}

void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
napi_property_descriptor descriptors[] = {
DECLARE_NAPI_PROPERTY("testStrictEquals", testStrictEquals),
Expand All @@ -100,6 +135,7 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
DECLARE_NAPI_PROPERTY("getNull", getNull),
DECLARE_NAPI_PROPERTY("createNapiError", createNapiError),
DECLARE_NAPI_PROPERTY("testNapiErrorCleanup", testNapiErrorCleanup),
DECLARE_NAPI_PROPERTY("testNapiTypeof", testNapiTypeof),
};

NAPI_CALL_RETURN_VOID(env, napi_define_properties(
Expand Down