From eb0cddd0e6d52533e6273d211407d1ef34008e66 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Thu, 29 Jun 2017 13:07:03 -0400 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/13990 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- test/addons-napi/test_general/test.js | 16 ++++++++++ test/addons-napi/test_general/test_general.c | 32 +++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/test/addons-napi/test_general/test.js b/test/addons-napi/test_general/test.js index 2aff480eeb2a69..32d26475cd6b86 100644 --- a/test/addons-napi/test_general/test.js +++ b/test/addons-napi/test_general/test.js @@ -34,3 +34,19 @@ 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) => { + assert.strictEqual(test_general.testNapiTypeof(val), typeof val); +}); + +// since typeof in js return object need to validate specific case +// for null +assert.strictEqual(test_general.testNapiTypeof(null), 'null'); diff --git a/test/addons-napi/test_general/test_general.c b/test/addons-napi/test_general/test_general.c index 0b69cc41e29fb5..ab2428b97528ee 100644 --- a/test/addons-napi/test_general/test_general.c +++ b/test/addons-napi/test_general/test_general.c @@ -29,7 +29,7 @@ napi_value testGetVersion(napi_env env, napi_callback_info info) { uint32_t version; napi_value result; NAPI_CALL(env, napi_get_version(env, &version)); - NAPI_CALL(env ,napi_create_number(env, version, &result)); + NAPI_CALL(env, napi_create_number(env, version, &result)); return result; } @@ -90,6 +90,35 @@ 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 argument_type; + NAPI_CALL(env, napi_typeof(env, args[0], &argument_type)); + + napi_value result; + if (argument_type == napi_number) { + NAPI_CALL(env, napi_create_string_utf8(env, "number", -1, &result)); + } else if (argument_type == napi_string) { + NAPI_CALL(env, napi_create_string_utf8(env, "string", -1, &result)); + } else if (argument_type == napi_function) { + NAPI_CALL(env, napi_create_string_utf8(env, "function", -1, &result)); + } else if (argument_type == napi_object) { + NAPI_CALL(env, napi_create_string_utf8(env, "object", -1, &result)); + } else if (argument_type == napi_boolean) { + NAPI_CALL(env, napi_create_string_utf8(env, "boolean", -1, &result)); + } else if (argument_type == napi_undefined) { + NAPI_CALL(env, napi_create_string_utf8(env, "undefined", -1, &result)); + } else if (argument_type == napi_symbol) { + NAPI_CALL(env, napi_create_string_utf8(env, "symbol", -1, &result)); + } else if (argument_type == napi_null) { + NAPI_CALL(env, napi_create_string_utf8(env, "null", -1, &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), @@ -100,6 +129,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(