Skip to content

Commit 7f126c2

Browse files
mhdawsonMylesBorins
authored andcommitted
test: add coverage for napi_typeof
We had some, but not complete coverage indirectly through other tests. Add test to validate it specifically and covers cases that were not being covered. Backport-PR-URL: #19447 PR-URL: #13990 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 8e2a26d commit 7f126c2

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

test/addons-napi/test_general/test.js

+16
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,19 @@ assert.ok(test_general.testGetPrototype(baseObject) !==
3434
// test version management funcitons
3535
// expected version is currently 1
3636
assert.strictEqual(test_general.testGetVersion(), 1);
37+
38+
[
39+
123,
40+
'test string',
41+
function() {},
42+
new Object(),
43+
true,
44+
undefined,
45+
Symbol()
46+
].forEach((val) => {
47+
assert.strictEqual(test_general.testNapiTypeof(val), typeof val);
48+
});
49+
50+
// since typeof in js return object need to validate specific case
51+
// for null
52+
assert.strictEqual(test_general.testNapiTypeof(null), 'null');

test/addons-napi/test_general/test_general.c

+31-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ napi_value testGetVersion(napi_env env, napi_callback_info info) {
2929
uint32_t version;
3030
napi_value result;
3131
NAPI_CALL(env, napi_get_version(env, &version));
32-
NAPI_CALL(env ,napi_create_number(env, version, &result));
32+
NAPI_CALL(env, napi_create_number(env, version, &result));
3333
return result;
3434
}
3535

@@ -90,6 +90,35 @@ napi_value testNapiErrorCleanup(napi_env env, napi_callback_info info) {
9090
return result;
9191
}
9292

93+
napi_value testNapiTypeof(napi_env env, napi_callback_info info) {
94+
size_t argc = 1;
95+
napi_value args[1];
96+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
97+
98+
napi_valuetype argument_type;
99+
NAPI_CALL(env, napi_typeof(env, args[0], &argument_type));
100+
101+
napi_value result;
102+
if (argument_type == napi_number) {
103+
NAPI_CALL(env, napi_create_string_utf8(env, "number", -1, &result));
104+
} else if (argument_type == napi_string) {
105+
NAPI_CALL(env, napi_create_string_utf8(env, "string", -1, &result));
106+
} else if (argument_type == napi_function) {
107+
NAPI_CALL(env, napi_create_string_utf8(env, "function", -1, &result));
108+
} else if (argument_type == napi_object) {
109+
NAPI_CALL(env, napi_create_string_utf8(env, "object", -1, &result));
110+
} else if (argument_type == napi_boolean) {
111+
NAPI_CALL(env, napi_create_string_utf8(env, "boolean", -1, &result));
112+
} else if (argument_type == napi_undefined) {
113+
NAPI_CALL(env, napi_create_string_utf8(env, "undefined", -1, &result));
114+
} else if (argument_type == napi_symbol) {
115+
NAPI_CALL(env, napi_create_string_utf8(env, "symbol", -1, &result));
116+
} else if (argument_type == napi_null) {
117+
NAPI_CALL(env, napi_create_string_utf8(env, "null", -1, &result));
118+
}
119+
return result;
120+
}
121+
93122
void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
94123
napi_property_descriptor descriptors[] = {
95124
DECLARE_NAPI_PROPERTY("testStrictEquals", testStrictEquals),
@@ -100,6 +129,7 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
100129
DECLARE_NAPI_PROPERTY("getNull", getNull),
101130
DECLARE_NAPI_PROPERTY("createNapiError", createNapiError),
102131
DECLARE_NAPI_PROPERTY("testNapiErrorCleanup", testNapiErrorCleanup),
132+
DECLARE_NAPI_PROPERTY("testNapiTypeof", testNapiTypeof),
103133
};
104134

105135
NAPI_CALL_RETURN_VOID(env, napi_define_properties(

0 commit comments

Comments
 (0)