Skip to content

Commit

Permalink
test: Add test covg for Maybe<T> (#1270)
Browse files Browse the repository at this point in the history
* test: Add unit test coverage for Maybe<T> class
  • Loading branch information
JckXia authored Jan 22, 2023
1 parent 35d9d66 commit fdc6263
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
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> ret = fn.Call({});

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

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

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

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

ret.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> ret = fn.Call({});

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

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

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

unwrappedValue =
ret.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(ret.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

0 comments on commit fdc6263

Please sign in to comment.