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: Add test covg for Maybe<T> #1270

Merged
merged 6 commits into from
Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
52 changes: 49 additions & 3 deletions test/maybe/check.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "assert.h"
#include "napi.h"
#if defined(NODE_ADDON_API_ENABLE_MAYBE)

Expand All @@ -6,18 +7,63 @@ using namespace Napi;
namespace {

void VoidCallback(const CallbackInfo& info) {
Function fn = info[0].As<Function>();
Napi::Function fn = info[0].As<Function>();
Maybe<Value> fn_m = fn.Call({});
Copy link
Member

Choose a reason for hiding this comment

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

This variable represents the return value of the fn invocation. Might be worth to name it with prefix like ret, rather than fn.

Suggested change
Maybe<Value> fn_m = fn.Call({});
Maybe<Value> ret_m = fn.Call({});

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated the PR to fix this.


Maybe<Value> it = fn.Call({});
assert(fn_m.IsNothing() == true);
assert(fn_m.IsJust() == false);

it.Check();
Napi::Value placeHolder = Napi::Number::New(info.Env(), 12345);
Napi::Value unwrappedValue = fn_m.UnwrapOr(placeHolder);

assert(unwrappedValue.As<Number>().Uint32Value() == 12345);

assert(fn_m.UnwrapTo(&placeHolder) == false);
assert(placeHolder.As<Number>().Uint32Value() == 12345);

fn_m.Check();
}

void TestMaybeOperatorOverload(const CallbackInfo& info) {
Napi::Function fn_a = info[0].As<Function>();
Napi::Function fn_b = info[1].As<Function>();

assert(fn_a.Call({}) == fn_a.Call({}));
assert(fn_a.Call({}) != fn_b.Call({}));
}

void NormalJsCallback(const CallbackInfo& info) {
Napi::Function fn = info[0].As<Function>();
uint32_t magic_number = info[1].As<Number>().Uint32Value();

Maybe<Value> fn_m = fn.Call({});

assert(fn_m.IsNothing() == false);
assert(fn_m.IsJust() == true);

Napi::Value unwrappedValue = fn_m.Unwrap();
assert(unwrappedValue.IsNumber() == true);

assert(unwrappedValue.As<Number>().Uint32Value() == magic_number);

unwrappedValue =
fn_m.UnwrapOr(Napi::Number::New(info.Env(), magic_number - 1));
assert(unwrappedValue.As<Number>().Uint32Value() == magic_number);

Napi::Value placeHolder = Napi::Number::New(info.Env(), magic_number - 1);
assert(fn_m.UnwrapTo(&placeHolder) == true);
assert(placeHolder.As<Number>().Uint32Value() == magic_number);
}

} // end anonymous namespace

Object InitMaybeCheck(Env env) {
Object exports = Object::New(env);
exports.Set("voidCallback", Function::New(env, VoidCallback));
exports.Set("normalJsCallback", Function::New(env, NormalJsCallback));
exports.Set("testMaybeOverloadOp",
Function::New(env, TestMaybeOperatorOverload));
return exports;
}

#endif
10 changes: 10 additions & 0 deletions test/maybe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ function test (binding) {
}

function child (binding) {
const MAGIC_NUMBER = 12459062;
binding.normalJsCallback(() => {
return MAGIC_NUMBER;
}, MAGIC_NUMBER);

binding.testMaybeOverloadOp(
() => { return MAGIC_NUMBER; },
() => { throw Error('Foobar'); }
);

binding.voidCallback(() => {
throw new Error('foobar');
});
Expand Down