Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dherman committed Dec 1, 2021
1 parent bab3b34 commit 856410d
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/types/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ macro_rules! impl_arguments {
};
}

impl_arguments! {
impl_arguments! {
[];
[
(V1, v1),
Expand Down
36 changes: 36 additions & 0 deletions test/napi/lib/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,46 @@ describe('JsFunction', function() {
assert.equal(addon.call_js_function(function(x) { return x + 1 }), 17);
});

it('call a JsFunction built in JS with call_with', function () {
assert.equal(addon.call_js_function_idiomatically(function(x) { return x + 1 }), 17);
});

it('call a JsFunction with zero args', function() {
assert.equal(addon.call_js_function_with_zero_args(), -Infinity);
});

it('call a JsFunction with one arg', function() {
assert.equal(addon.call_js_function_with_one_arg(), 1.0);
});

it('call a JsFunction with two args', function() {
assert.equal(addon.call_js_function_with_two_args(), 2.0);
});

it('call a JsFunction with three args', function() {
assert.equal(addon.call_js_function_with_three_args(), 3.0);
});

it('call a JsFunction with four args', function() {
assert.equal(addon.call_js_function_with_four_args(), 4.0);
});

it('call a JsFunction with a custom this', function() {
assert.equal(addon.call_js_function_with_custom_this(function() { return this }).secret, 42);
});

it('call a JsFunction with a heterogeneously typed tuple', function() {
assert.deepEqual(addon.call_js_function_with_heterogeneous_tuple(), [1, "hello", true]);
});

it('new a JsFunction', function () {
assert.equal(addon.construct_js_function(Date), 1970);
});

it('new a JsFunction with construct_with', function () {
assert.equal(addon.construct_js_function_idiomatically(Date), 1970);
});

it('got two parameters, a string and a number', function() {
addon.check_string_and_number("string", 42);
});
Expand Down
98 changes: 93 additions & 5 deletions test/napi/src/js/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,82 @@ pub fn call_js_function(mut cx: FunctionContext) -> JsResult<JsNumber> {
.or_throw(&mut cx)
}

pub fn call_js_function_idiomatically(mut cx: FunctionContext) -> JsResult<JsNumber> {
cx.argument::<JsFunction>(0)?
.call_with(&cx)
.this(cx.null())
.arg(cx.number(16.0))
.apply(&mut cx)
}

fn get_math_max<'a>(cx: &mut FunctionContext<'a>) -> JsResult<'a, JsFunction> {
let math = cx
.global()
.get(cx, "Math")?
.downcast_or_throw::<JsObject, _>(cx)?;
let max = math
.get(cx, "max")?
.downcast_or_throw::<JsFunction, _>(cx)?;
Ok(max)
}

pub fn call_js_function_with_zero_args(mut cx: FunctionContext) -> JsResult<JsNumber> {
get_math_max(&mut cx)?.call_with(&cx).apply(&mut cx)
}

pub fn call_js_function_with_one_arg(mut cx: FunctionContext) -> JsResult<JsNumber> {
get_math_max(&mut cx)?
.call_with(&cx)
.arg(cx.number(1.0))
.apply(&mut cx)
}

pub fn call_js_function_with_two_args(mut cx: FunctionContext) -> JsResult<JsNumber> {
get_math_max(&mut cx)?
.call_with(&cx)
.arg(cx.number(1.0))
.arg(cx.number(2.0))
.apply(&mut cx)
}

pub fn call_js_function_with_three_args(mut cx: FunctionContext) -> JsResult<JsNumber> {
get_math_max(&mut cx)?
.call_with(&cx)
.arg(cx.number(1.0))
.arg(cx.number(2.0))
.arg(cx.number(3.0))
.apply(&mut cx)
}

pub fn call_js_function_with_four_args(mut cx: FunctionContext) -> JsResult<JsNumber> {
get_math_max(&mut cx)?
.call_with(&cx)
.arg(cx.number(1.0))
.arg(cx.number(2.0))
.arg(cx.number(3.0))
.arg(cx.number(4.0))
.apply(&mut cx)
}

pub fn call_js_function_with_custom_this(mut cx: FunctionContext) -> JsResult<JsObject> {
let custom_this = cx.empty_object();
let secret = cx.number(42.0);
custom_this.set(&mut cx, "secret", secret)?;
cx.argument::<JsFunction>(0)?
.call_with(&cx)
.this(custom_this)
.apply(&mut cx)
}

pub fn call_js_function_with_heterogeneous_tuple(mut cx: FunctionContext) -> JsResult<JsArray> {
cx.global()
.get(&mut cx, "Array")?
.downcast_or_throw::<JsFunction, _>(&mut cx)?
.call_with(&cx)
.args((cx.number(1.0), cx.string("hello"), cx.boolean(true)))
.apply(&mut cx)
}

pub fn construct_js_function(mut cx: FunctionContext) -> JsResult<JsNumber> {
let f = cx.argument::<JsFunction>(0)?;
let zero = cx.number(0.0);
Expand All @@ -34,6 +110,22 @@ pub fn construct_js_function(mut cx: FunctionContext) -> JsResult<JsNumber> {
.or_throw(&mut cx)
}

pub fn construct_js_function_idiomatically(mut cx: FunctionContext) -> JsResult<JsNumber> {
let o = cx
.argument::<JsFunction>(0)?
.construct_with(&cx)
.arg(cx.number(0.0))
.apply(&mut cx)?;
let get_utc_full_year_method = o
.get(&mut cx, "getUTCFullYear")?
.downcast::<JsFunction, _>(&mut cx)
.or_throw(&mut cx)?;
get_utc_full_year_method
.call_with(&cx)
.this(o)
.apply(&mut cx)
}

trait CheckArgument<'a> {
fn check_argument<V: Value>(&mut self, i: i32) -> JsResult<'a, V>;
}
Expand Down Expand Up @@ -124,11 +216,7 @@ pub fn throw_and_catch(mut cx: FunctionContext) -> JsResult<JsValue> {
pub fn call_and_catch(mut cx: FunctionContext) -> JsResult<JsValue> {
let f: Handle<JsFunction> = cx.argument(0)?;
Ok(cx
.try_catch(|cx| {
let global = cx.global();
let args: Vec<Handle<JsValue>> = vec![];
f.call(cx, global, args)
})
.try_catch(|cx| f.call_with(cx).this(cx.global()).apply(cx))
.unwrap_or_else(|err| err))
}

Expand Down
36 changes: 36 additions & 0 deletions test/napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,43 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {

cx.export_function("return_js_function", return_js_function)?;
cx.export_function("call_js_function", call_js_function)?;
cx.export_function(
"call_js_function_idiomatically",
call_js_function_idiomatically,
)?;
cx.export_function(
"call_js_function_with_zero_args",
call_js_function_with_zero_args,
)?;
cx.export_function(
"call_js_function_with_one_arg",
call_js_function_with_one_arg,
)?;
cx.export_function(
"call_js_function_with_two_args",
call_js_function_with_two_args,
)?;
cx.export_function(
"call_js_function_with_three_args",
call_js_function_with_three_args,
)?;
cx.export_function(
"call_js_function_with_four_args",
call_js_function_with_four_args,
)?;
cx.export_function(
"call_js_function_with_custom_this",
call_js_function_with_custom_this,
)?;
cx.export_function(
"call_js_function_with_heterogeneous_tuple",
call_js_function_with_heterogeneous_tuple,
)?;
cx.export_function("construct_js_function", construct_js_function)?;
cx.export_function(
"construct_js_function_idiomatically",
construct_js_function_idiomatically,
)?;
cx.export_function("num_arguments", num_arguments)?;
cx.export_function("return_this", return_this)?;
cx.export_function("require_object_this", require_object_this)?;
Expand Down

0 comments on commit 856410d

Please sign in to comment.