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: improve n-api array func coverage #12890

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
13 changes: 10 additions & 3 deletions test/addons-napi/test_array/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,26 @@ const array = [
]
];

assert.strictEqual(test_array.Test(array, array.length + 1),
assert.strictEqual(test_array.TestGetElement(array, array.length + 1),
'Index out of bound!');
Copy link
Contributor

Choose a reason for hiding this comment

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

Totally unrelated to this PR, but just wanted to say that this implementation doesn't look correct to me. "Index out of bound!" should be thrown as an Error. As it is, "Index out of bound!" could be a value in the Array.


assert.throws(
() => {
test_array.Test(array, -2);
test_array.TestGetElement(array, -2);
},
/Invalid index\. Expects a positive integer\./
);

array.forEach(function(element, index) {
assert.strictEqual(test_array.Test(array, index), element);
assert.strictEqual(test_array.TestGetElement(array, index), element);
});


assert.deepStrictEqual(test_array.New(array), array);

assert(test_array.TestHasElement(array, 0));
assert.strictEqual(false, test_array.TestHasElement(array, array.length + 1));
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you switch the argument order to strictEqual().

Copy link
Member Author

Choose a reason for hiding this comment

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

will do.


assert(test_array.NewWithLength(0) instanceof Array);
assert(test_array.NewWithLength(1) instanceof Array);
assert(test_array.NewWithLength(2 ^ 32 - 1) instanceof Array);
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you attempting to do exponentiation here?

Copy link
Member

Choose a reason for hiding this comment

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

This would need to be (2 ** 32 - 1)

Copy link
Member Author

Choose a reason for hiding this comment

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

hmm, maybe coding at 2am in your hotel room is not always the best idea. Will fix.

67 changes: 65 additions & 2 deletions test/addons-napi/test_array/test_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <string.h>
#include "../common.h"

napi_value Test(napi_env env, napi_callback_info info) {
napi_value TestGetElement(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
Expand Down Expand Up @@ -52,6 +52,45 @@ napi_value Test(napi_env env, napi_callback_info info) {
return ret;
}

napi_value TestHasElement(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NAPI_ASSERT(env, argc >= 2, "Wrong number of arguments");

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

NAPI_ASSERT(env, valuetype0 == napi_object,
"Wrong type of arguments. Expects an array as first argument.");

napi_valuetype valuetype1;
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));

NAPI_ASSERT(env, valuetype1 == napi_number,
"Wrong type of arguments. Expects an integer as second argument.");

napi_value array = args[0];
int32_t index;
NAPI_CALL(env, napi_get_value_int32(env, args[1], &index));

bool isarray;
NAPI_CALL(env, napi_is_array(env, array, &isarray));

if (!isarray) {
return NULL;
}

bool has_element;
NAPI_CALL(env, napi_has_element(env, array, index, &has_element));

napi_value ret;
NAPI_CALL(env, napi_get_boolean(env, has_element, &ret));

return ret;
}

napi_value New(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
Expand Down Expand Up @@ -80,10 +119,34 @@ napi_value New(napi_env env, napi_callback_info info) {
return ret;
}

napi_value NewWithLength(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_ASSERT(env, argc >= 1, "Wrong number of arguments");

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

NAPI_ASSERT(env, valuetype0 == napi_number,
"Wrong type of arguments. Expects an integer the first argument.");

int32_t array_length;
NAPI_CALL(env, napi_get_value_int32(env, args[0], &array_length));

napi_value ret;
NAPI_CALL(env, napi_create_array_with_length(env, array_length, &ret));

return ret;
}

void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
napi_property_descriptor descriptors[] = {
DECLARE_NAPI_PROPERTY("Test", Test),
DECLARE_NAPI_PROPERTY("TestGetElement", TestGetElement),
DECLARE_NAPI_PROPERTY("TestHasElement", TestHasElement),
DECLARE_NAPI_PROPERTY("New", New),
DECLARE_NAPI_PROPERTY("NewWithLength", NewWithLength),
};

NAPI_CALL_RETURN_VOID(env, napi_define_properties(
Expand Down