Skip to content

Commit 2a91d59

Browse files
mhdawsonaddaleax
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. PR-URL: #13990 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 21ee4b1 commit 2a91d59

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

‎test/addons-napi/test_general/test.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,19 @@ assert.ok(test_general.testGetPrototype(baseObject) !==
3434
// test version management functions
3535
// expected version is currently 1
3636
assert.strictEqual(test_general.testGetVersion(),1);
37+
38+
[
39+
123,
40+
'test string',
41+
function(){},
42+
newObject(),
43+
true,
44+
undefined,
45+
Symbol()
46+
].forEach((val)=>{
47+
assert.strictEqual(test_general.testNapiTypeof(val),typeofval);
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‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ napi_value testGetVersion(napi_env env, napi_callback_info info){
2929
uint32_tversion;
3030
napi_valueresult;
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
returnresult;
3434
}
3535

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

93+
napi_valuetestNapiTypeof(napi_envenv, napi_callback_infoinfo){
94+
size_targc=1;
95+
napi_valueargs[1];
96+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
97+
98+
napi_valuetypeargument_type;
99+
NAPI_CALL(env, napi_typeof(env, args[0], &argument_type));
100+
101+
napi_valueresult;
102+
if (argument_type==napi_number){
103+
NAPI_CALL(env, napi_create_string_utf8(env, "number", -1, &result));
104+
} elseif (argument_type==napi_string){
105+
NAPI_CALL(env, napi_create_string_utf8(env, "string", -1, &result));
106+
} elseif (argument_type==napi_function){
107+
NAPI_CALL(env, napi_create_string_utf8(env, "function", -1, &result));
108+
} elseif (argument_type==napi_object){
109+
NAPI_CALL(env, napi_create_string_utf8(env, "object", -1, &result));
110+
} elseif (argument_type==napi_boolean){
111+
NAPI_CALL(env, napi_create_string_utf8(env, "boolean", -1, &result));
112+
} elseif (argument_type==napi_undefined){
113+
NAPI_CALL(env, napi_create_string_utf8(env, "undefined", -1, &result));
114+
} elseif (argument_type==napi_symbol){
115+
NAPI_CALL(env, napi_create_string_utf8(env, "symbol", -1, &result));
116+
} elseif (argument_type==napi_null){
117+
NAPI_CALL(env, napi_create_string_utf8(env, "null", -1, &result));
118+
}
119+
returnresult;
120+
}
121+
93122
voidInit(napi_envenv, napi_valueexports, napi_valuemodule, void*priv){
94123
napi_property_descriptordescriptors[] ={
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)