Skip to content

Commit

Permalink
review(neon): Add test to try_catch
Browse files Browse the repository at this point in the history
  • Loading branch information
kjvalencik committed Dec 9, 2020
1 parent a68f115 commit eb43562
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions test/dynamic/lib/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ describe('JsFunction', function() {
assert.equal(addon.call_and_catch(() => { return 42 }), 42);
});

it('can return Rust type from cx.try_catch', function() {
const n = Math.random();
assert.strictEqual(addon.get_number_or_default(n), n);
assert.strictEqual(addon.get_number_or_default(), 0);
});

it('propagates a panic with cx.try_catch', function() {
assert.throws(function() {
addon.panic_and_catch();
Expand Down
7 changes: 7 additions & 0 deletions test/dynamic/native/src/js/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ pub fn call_and_catch(mut cx: FunctionContext) -> JsResult<JsValue> {
}).unwrap_or_else(|err| err))
}

pub fn get_number_or_default(mut cx: FunctionContext) -> JsResult<JsNumber> {
let n = cx.try_catch(|cx| Ok(cx.argument::<JsNumber>(0)?.value()))
.unwrap_or(0.0);

Ok(cx.number(n))
}

pub fn panic_and_catch(mut cx: FunctionContext) -> JsResult<JsValue> {
Ok(cx.try_catch(|_| { panic!("oh no") })
.unwrap_or_else(|err| err))
Expand Down
1 change: 1 addition & 0 deletions test/dynamic/native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
cx.export_function("panic_after_throw", panic_after_throw)?;
cx.export_function("throw_and_catch", throw_and_catch)?;
cx.export_function("call_and_catch", call_and_catch)?;
cx.export_function("get_number_or_default", get_number_or_default)?;
cx.export_function("panic_and_catch", panic_and_catch)?;
cx.export_function("unexpected_throw_and_catch", unexpected_throw_and_catch)?;
cx.export_function("downcast_error", downcast_error)?;
Expand Down
6 changes: 6 additions & 0 deletions test/napi/lib/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ describe('JsFunction', function() {
assert.equal(addon.call_and_catch(() => { return 42 }), 42);
});

it('can return Rust type from cx.try_catch', function() {
const n = Math.random();
assert.strictEqual(addon.get_number_or_default(n), n);
assert.strictEqual(addon.get_number_or_default(), 0);
});

it('distinguishes calls from constructs', function() {
assert.equal(addon.is_construct.call({}).wasConstructed, false);
assert.equal((new addon.is_construct()).wasConstructed, true);
Expand Down
7 changes: 7 additions & 0 deletions test/napi/src/js/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ pub fn call_and_catch(mut cx: FunctionContext) -> JsResult<JsValue> {
}).unwrap_or_else(|err| err))
}

pub fn get_number_or_default(mut cx: FunctionContext) -> JsResult<JsNumber> {
let n = cx.try_catch(|cx| Ok(cx.argument::<JsNumber>(0)?.value(cx)))
.unwrap_or(0.0);

Ok(cx.number(n))
}

pub fn is_construct(mut cx: FunctionContext) -> JsResult<JsObject> {
let this = cx.this();
let construct = match cx.kind() {
Expand Down
1 change: 1 addition & 0 deletions test/napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {

cx.export_function("throw_and_catch", throw_and_catch)?;
cx.export_function("call_and_catch", call_and_catch)?;
cx.export_function("get_number_or_default", get_number_or_default)?;
cx.export_function("is_construct", is_construct)?;

fn call_get_own_property_names(mut cx: FunctionContext) -> JsResult<JsArray> {
Expand Down

0 comments on commit eb43562

Please sign in to comment.