wasm-bindgen/tests/unwrap_throw.rs
Nick Fitzgerald 0e11e4a3bd Add the UnwrapThrowExt<T> trait
An extension trait for `Option<T>` and `Result<T, E>` for unwraping the `T`
value, or throwing a JS error if it is not available.

These methods should have a smaller code size footprint than the normal
`Option::unwrap` and `Option::expect` methods, but they are specific to
working with wasm and JS.

On non-wasm32 targets, defaults to the normal unwrap/expect calls.
2019-01-31 13:26:21 -08:00

24 lines
427 B
Rust

extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[test]
fn unwrap_throw_ok() {
assert_eq!(Some(42).unwrap_throw(), 42);
let x: Result<i32, ()> = Ok(42);
assert_eq!(x.unwrap_throw(), 42);
}
#[test]
#[should_panic]
fn unwrap_throw_none() {
let x: Option<i32> = None;
x.unwrap_throw();
}
#[test]
#[should_panic]
fn unwrap_throw_err() {
let x: Result<i32, ()> = Err(());
x.unwrap_throw();
}