-
-
Notifications
You must be signed in to change notification settings - Fork 12
feat: port test_conversions to CTS #38
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
Open
bavulapati
wants to merge
7
commits into
nodejs:main
Choose a base branch
from
bavulapati:feat/port-test-conversions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7d6a7c9
feat: port test_conversions to CTS
bavulapati 4d14c81
docs: update PORTING.md with the test status
bavulapati b6693a0
feat: port test_constructor to CTS (#29)
kraenhansen 430e217
docs: update PORTING.md with the test status
bavulapati 8c01460
resolve conflitcts
bavulapati 9ef3fee
resolve conflicts
bavulapati dfbbe22
Merge branch 'main' into feat/port-test-conversions
bavulapati File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| add_node_api_cts_addon(test_conversions test_conversions.c test_null.c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| "use strict"; | ||
| const test = loadAddon("test_conversions"); | ||
|
|
||
| const boolExpected = /boolean was expected/; | ||
| const numberExpected = /number was expected/; | ||
| const stringExpected = /string was expected/; | ||
|
|
||
| const testSym = Symbol("test"); | ||
|
|
||
| assert.strictEqual(test.asBool(false), false); | ||
| assert.strictEqual(test.asBool(true), true); | ||
| assert.throws(() => test.asBool(undefined), boolExpected); | ||
| assert.throws(() => test.asBool(null), boolExpected); | ||
| assert.throws(() => test.asBool(Number.NaN), boolExpected); | ||
| assert.throws(() => test.asBool(0), boolExpected); | ||
| assert.throws(() => test.asBool(""), boolExpected); | ||
| assert.throws(() => test.asBool("0"), boolExpected); | ||
| assert.throws(() => test.asBool(1), boolExpected); | ||
| assert.throws(() => test.asBool("1"), boolExpected); | ||
| assert.throws(() => test.asBool("true"), boolExpected); | ||
| assert.throws(() => test.asBool({}), boolExpected); | ||
| assert.throws(() => test.asBool([]), boolExpected); | ||
| assert.throws(() => test.asBool(testSym), boolExpected); | ||
|
|
||
| [test.asInt32, test.asUInt32, test.asInt64].forEach((asInt) => { | ||
| assert.strictEqual(asInt(0), 0); | ||
| assert.strictEqual(asInt(1), 1); | ||
| assert.strictEqual(asInt(1.0), 1); | ||
| assert.strictEqual(asInt(1.1), 1); | ||
| assert.strictEqual(asInt(1.9), 1); | ||
| assert.strictEqual(asInt(0.9), 0); | ||
| assert.strictEqual(asInt(999.9), 999); | ||
| assert.strictEqual(asInt(Number.NaN), 0); | ||
| assert.throws(() => asInt(undefined), numberExpected); | ||
| assert.throws(() => asInt(null), numberExpected); | ||
| assert.throws(() => asInt(false), numberExpected); | ||
| assert.throws(() => asInt(""), numberExpected); | ||
| assert.throws(() => asInt("1"), numberExpected); | ||
| assert.throws(() => asInt({}), numberExpected); | ||
| assert.throws(() => asInt([]), numberExpected); | ||
| assert.throws(() => asInt(testSym), numberExpected); | ||
| }); | ||
|
|
||
| assert.strictEqual(test.asInt32(-1), -1); | ||
| assert.strictEqual(test.asInt64(-1), -1); | ||
| assert.strictEqual(test.asUInt32(-1), Math.pow(2, 32) - 1); | ||
|
|
||
| assert.strictEqual(test.asDouble(0), 0); | ||
| assert.strictEqual(test.asDouble(1), 1); | ||
| assert.strictEqual(test.asDouble(1.0), 1.0); | ||
| assert.strictEqual(test.asDouble(1.1), 1.1); | ||
| assert.strictEqual(test.asDouble(1.9), 1.9); | ||
| assert.strictEqual(test.asDouble(0.9), 0.9); | ||
| assert.strictEqual(test.asDouble(999.9), 999.9); | ||
| assert.strictEqual(test.asDouble(-1), -1); | ||
| assert.ok(Number.isNaN(test.asDouble(Number.NaN))); | ||
| assert.throws(() => test.asDouble(undefined), numberExpected); | ||
| assert.throws(() => test.asDouble(null), numberExpected); | ||
| assert.throws(() => test.asDouble(false), numberExpected); | ||
| assert.throws(() => test.asDouble(""), numberExpected); | ||
| assert.throws(() => test.asDouble("1"), numberExpected); | ||
| assert.throws(() => test.asDouble({}), numberExpected); | ||
| assert.throws(() => test.asDouble([]), numberExpected); | ||
| assert.throws(() => test.asDouble(testSym), numberExpected); | ||
|
|
||
| assert.strictEqual(test.asString(""), ""); | ||
| assert.strictEqual(test.asString("test"), "test"); | ||
| assert.throws(() => test.asString(undefined), stringExpected); | ||
| assert.throws(() => test.asString(null), stringExpected); | ||
| assert.throws(() => test.asString(false), stringExpected); | ||
| assert.throws(() => test.asString(1), stringExpected); | ||
| assert.throws(() => test.asString(1.1), stringExpected); | ||
| assert.throws(() => test.asString(Number.NaN), stringExpected); | ||
| assert.throws(() => test.asString({}), stringExpected); | ||
| assert.throws(() => test.asString([]), stringExpected); | ||
| assert.throws(() => test.asString(testSym), stringExpected); | ||
|
|
||
| assert.strictEqual(test.toBool(true), true); | ||
| assert.strictEqual(test.toBool(1), true); | ||
| assert.strictEqual(test.toBool(-1), true); | ||
| assert.strictEqual(test.toBool("true"), true); | ||
| assert.strictEqual(test.toBool("false"), true); | ||
| assert.strictEqual(test.toBool({}), true); | ||
| assert.strictEqual(test.toBool([]), true); | ||
| assert.strictEqual(test.toBool(testSym), true); | ||
| assert.strictEqual(test.toBool(false), false); | ||
| assert.strictEqual(test.toBool(undefined), false); | ||
| assert.strictEqual(test.toBool(null), false); | ||
| assert.strictEqual(test.toBool(0), false); | ||
| assert.strictEqual(test.toBool(Number.NaN), false); | ||
| assert.strictEqual(test.toBool(""), false); | ||
|
|
||
| assert.strictEqual(test.toNumber(0), 0); | ||
| assert.strictEqual(test.toNumber(1), 1); | ||
| assert.strictEqual(test.toNumber(1.1), 1.1); | ||
| assert.strictEqual(test.toNumber(-1), -1); | ||
| assert.strictEqual(test.toNumber("0"), 0); | ||
| assert.strictEqual(test.toNumber("1"), 1); | ||
| assert.strictEqual(test.toNumber("1.1"), 1.1); | ||
| assert.strictEqual(test.toNumber([]), 0); | ||
| assert.strictEqual(test.toNumber(false), 0); | ||
| assert.strictEqual(test.toNumber(null), 0); | ||
| assert.strictEqual(test.toNumber(""), 0); | ||
| assert.ok(Number.isNaN(test.toNumber(Number.NaN))); | ||
| assert.ok(Number.isNaN(test.toNumber({}))); | ||
| assert.ok(Number.isNaN(test.toNumber(undefined))); | ||
| assert.throws(() => test.toNumber(testSym), TypeError); | ||
|
|
||
| assert.deepStrictEqual({}, test.toObject({})); | ||
| assert.deepStrictEqual({ test: 1 }, test.toObject({ test: 1 })); | ||
| assert.deepStrictEqual([], test.toObject([])); | ||
| assert.deepStrictEqual([1, 2, 3], test.toObject([1, 2, 3])); | ||
| assert.deepStrictEqual(new Boolean(false), test.toObject(false)); | ||
| assert.deepStrictEqual(new Boolean(true), test.toObject(true)); | ||
| assert.deepStrictEqual(new String(""), test.toObject("")); | ||
| assert.deepStrictEqual(new Number(0), test.toObject(0)); | ||
| assert.deepStrictEqual(new Number(Number.NaN), test.toObject(Number.NaN)); | ||
| assert.deepStrictEqual(new Object(testSym), test.toObject(testSym)); | ||
| assert.notStrictEqual(test.toObject(false), false); | ||
| assert.notStrictEqual(test.toObject(true), true); | ||
| assert.notStrictEqual(test.toObject(""), ""); | ||
| assert.notStrictEqual(test.toObject(0), 0); | ||
| assert.ok(!Number.isNaN(test.toObject(Number.NaN))); | ||
|
|
||
| assert.strictEqual(test.toString(""), ""); | ||
| assert.strictEqual(test.toString("test"), "test"); | ||
| assert.strictEqual(test.toString(undefined), "undefined"); | ||
| assert.strictEqual(test.toString(null), "null"); | ||
| assert.strictEqual(test.toString(false), "false"); | ||
| assert.strictEqual(test.toString(true), "true"); | ||
| assert.strictEqual(test.toString(0), "0"); | ||
| assert.strictEqual(test.toString(1.1), "1.1"); | ||
| assert.strictEqual(test.toString(Number.NaN), "NaN"); | ||
| assert.strictEqual(test.toString({}), "[object Object]"); | ||
| assert.strictEqual(test.toString({ toString: () => "test" }), "test"); | ||
| assert.strictEqual(test.toString([]), ""); | ||
| assert.strictEqual(test.toString([1, 2, 3]), "1,2,3"); | ||
| assert.throws(() => test.toString(testSym), TypeError); | ||
|
|
||
| assert.deepStrictEqual(test.testNull.getValueBool(), { | ||
| envIsNull: "Invalid argument", | ||
| valueIsNull: "Invalid argument", | ||
| resultIsNull: "Invalid argument", | ||
| inputTypeCheck: "A boolean was expected", | ||
| }); | ||
|
|
||
| assert.deepStrictEqual(test.testNull.getValueInt32(), { | ||
| envIsNull: "Invalid argument", | ||
| valueIsNull: "Invalid argument", | ||
| resultIsNull: "Invalid argument", | ||
| inputTypeCheck: "A number was expected", | ||
| }); | ||
|
|
||
| assert.deepStrictEqual(test.testNull.getValueUint32(), { | ||
| envIsNull: "Invalid argument", | ||
| valueIsNull: "Invalid argument", | ||
| resultIsNull: "Invalid argument", | ||
| inputTypeCheck: "A number was expected", | ||
| }); | ||
|
|
||
| assert.deepStrictEqual(test.testNull.getValueInt64(), { | ||
| envIsNull: "Invalid argument", | ||
| valueIsNull: "Invalid argument", | ||
| resultIsNull: "Invalid argument", | ||
| inputTypeCheck: "A number was expected", | ||
| }); | ||
|
|
||
| assert.deepStrictEqual(test.testNull.getValueDouble(), { | ||
| envIsNull: "Invalid argument", | ||
| valueIsNull: "Invalid argument", | ||
| resultIsNull: "Invalid argument", | ||
| inputTypeCheck: "A number was expected", | ||
| }); | ||
|
|
||
| assert.deepStrictEqual(test.testNull.coerceToBool(), { | ||
| envIsNull: "Invalid argument", | ||
| valueIsNull: "Invalid argument", | ||
| resultIsNull: "Invalid argument", | ||
| inputTypeCheck: "napi_ok", | ||
| }); | ||
|
|
||
| assert.deepStrictEqual(test.testNull.coerceToObject(), { | ||
| envIsNull: "Invalid argument", | ||
| valueIsNull: "Invalid argument", | ||
| resultIsNull: "Invalid argument", | ||
| inputTypeCheck: "napi_ok", | ||
| }); | ||
|
|
||
| assert.deepStrictEqual(test.testNull.coerceToString(), { | ||
| envIsNull: "Invalid argument", | ||
| valueIsNull: "Invalid argument", | ||
| resultIsNull: "Invalid argument", | ||
| inputTypeCheck: "napi_ok", | ||
| }); | ||
|
|
||
| assert.deepStrictEqual(test.testNull.getValueStringUtf8(), { | ||
| envIsNull: "Invalid argument", | ||
| valueIsNull: "Invalid argument", | ||
| wrongTypeIn: "A string was expected", | ||
| bufAndOutLengthIsNull: "Invalid argument", | ||
| }); | ||
|
|
||
| assert.deepStrictEqual(test.testNull.getValueStringLatin1(), { | ||
| envIsNull: "Invalid argument", | ||
| valueIsNull: "Invalid argument", | ||
| wrongTypeIn: "A string was expected", | ||
| bufAndOutLengthIsNull: "Invalid argument", | ||
| }); | ||
|
|
||
| assert.deepStrictEqual(test.testNull.getValueStringUtf16(), { | ||
| envIsNull: "Invalid argument", | ||
| valueIsNull: "Invalid argument", | ||
| wrongTypeIn: "A string was expected", | ||
| bufAndOutLengthIsNull: "Invalid argument", | ||
| }); |
158 changes: 158 additions & 0 deletions
158
tests/js-native-api/test_conversions/test_conversions.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| #include <js_native_api.h> | ||
| #include "../common.h" | ||
| #include "../entry_point.h" | ||
| #include "test_null.h" | ||
|
|
||
| static napi_value AsBool(napi_env env, napi_callback_info info) { | ||
| size_t argc = 1; | ||
| napi_value args[1]; | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
|
||
| bool value; | ||
| NODE_API_CALL(env, napi_get_value_bool(env, args[0], &value)); | ||
|
|
||
| napi_value output; | ||
| NODE_API_CALL(env, napi_get_boolean(env, value, &output)); | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| static napi_value AsInt32(napi_env env, napi_callback_info info) { | ||
| size_t argc = 1; | ||
| napi_value args[1]; | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
|
||
| int32_t value; | ||
| NODE_API_CALL(env, napi_get_value_int32(env, args[0], &value)); | ||
|
|
||
| napi_value output; | ||
| NODE_API_CALL(env, napi_create_int32(env, value, &output)); | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| static napi_value AsUInt32(napi_env env, napi_callback_info info) { | ||
| size_t argc = 1; | ||
| napi_value args[1]; | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
|
||
| uint32_t value; | ||
| NODE_API_CALL(env, napi_get_value_uint32(env, args[0], &value)); | ||
|
|
||
| napi_value output; | ||
| NODE_API_CALL(env, napi_create_uint32(env, value, &output)); | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| static napi_value AsInt64(napi_env env, napi_callback_info info) { | ||
| size_t argc = 1; | ||
| napi_value args[1]; | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
|
||
| int64_t value; | ||
| NODE_API_CALL(env, napi_get_value_int64(env, args[0], &value)); | ||
|
|
||
| napi_value output; | ||
| NODE_API_CALL(env, napi_create_int64(env, (double)value, &output)); | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| static napi_value AsDouble(napi_env env, napi_callback_info info) { | ||
| size_t argc = 1; | ||
| napi_value args[1]; | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
|
||
| double value; | ||
| NODE_API_CALL(env, napi_get_value_double(env, args[0], &value)); | ||
|
|
||
| napi_value output; | ||
| NODE_API_CALL(env, napi_create_double(env, value, &output)); | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| static napi_value AsString(napi_env env, napi_callback_info info) { | ||
| size_t argc = 1; | ||
| napi_value args[1]; | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
|
||
| char value[100]; | ||
| NODE_API_CALL(env, | ||
| napi_get_value_string_utf8(env, args[0], value, sizeof(value), NULL)); | ||
|
|
||
| napi_value output; | ||
| NODE_API_CALL(env, napi_create_string_utf8( | ||
| env, value, NAPI_AUTO_LENGTH, &output)); | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| static napi_value ToBool(napi_env env, napi_callback_info info) { | ||
| size_t argc = 1; | ||
| napi_value args[1]; | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
|
||
| napi_value output; | ||
| NODE_API_CALL(env, napi_coerce_to_bool(env, args[0], &output)); | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| static napi_value ToNumber(napi_env env, napi_callback_info info) { | ||
| size_t argc = 1; | ||
| napi_value args[1]; | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
|
||
| napi_value output; | ||
| NODE_API_CALL(env, napi_coerce_to_number(env, args[0], &output)); | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| static napi_value ToObject(napi_env env, napi_callback_info info) { | ||
| size_t argc = 1; | ||
| napi_value args[1]; | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
|
||
| napi_value output; | ||
| NODE_API_CALL(env, napi_coerce_to_object(env, args[0], &output)); | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| static napi_value ToString(napi_env env, napi_callback_info info) { | ||
| size_t argc = 1; | ||
| napi_value args[1]; | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
|
||
| napi_value output; | ||
| NODE_API_CALL(env, napi_coerce_to_string(env, args[0], &output)); | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| EXTERN_C_START | ||
| napi_value Init(napi_env env, napi_value exports) { | ||
| napi_property_descriptor descriptors[] = { | ||
| DECLARE_NODE_API_PROPERTY("asBool", AsBool), | ||
| DECLARE_NODE_API_PROPERTY("asInt32", AsInt32), | ||
| DECLARE_NODE_API_PROPERTY("asUInt32", AsUInt32), | ||
| DECLARE_NODE_API_PROPERTY("asInt64", AsInt64), | ||
| DECLARE_NODE_API_PROPERTY("asDouble", AsDouble), | ||
| DECLARE_NODE_API_PROPERTY("asString", AsString), | ||
| DECLARE_NODE_API_PROPERTY("toBool", ToBool), | ||
| DECLARE_NODE_API_PROPERTY("toNumber", ToNumber), | ||
| DECLARE_NODE_API_PROPERTY("toObject", ToObject), | ||
| DECLARE_NODE_API_PROPERTY("toString", ToString), | ||
| }; | ||
|
|
||
| NODE_API_CALL(env, napi_define_properties( | ||
| env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); | ||
|
|
||
| init_test_null(env, exports); | ||
|
|
||
| return exports; | ||
| } | ||
| EXTERN_C_END |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 54 should be updated instead.