From ff835948828113251bf046c3fc92946f03be1f88 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Thu, 9 Aug 2018 16:21:12 -0700 Subject: [PATCH] futures: Add sanity tests for conversion between Promises and Futures Part of #614 --- .travis.yml | 4 +-- crates/futures/Cargo.toml | 3 +++ crates/futures/tests/tests.rs | 51 +++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100755 crates/futures/tests/tests.rs diff --git a/.travis.yml b/.travis.yml index 2721f4c36..bfd64e01e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -62,9 +62,9 @@ matrix: - cargo test --target wasm32-unknown-unknown --features serde-serialize # Make sure the `std` feature works if disabled - cargo test --target wasm32-unknown-unknown -p no-std - # Make sure the `wasm-bindgen-futures` tests pass. Right now, this just - # verifies that the example program in the crate level docs compiles. + # Make sure the `wasm-bindgen-futures` tests pass. - cargo test -p wasm-bindgen-futures + - cargo test -p wasm-bindgen-futures --target wasm32-unknown-unknown addons: firefox: latest if: branch = master diff --git a/crates/futures/Cargo.toml b/crates/futures/Cargo.toml index bffa55882..1c521f1d8 100644 --- a/crates/futures/Cargo.toml +++ b/crates/futures/Cargo.toml @@ -7,3 +7,6 @@ authors = ["The wasm-bindgen Developers"] futures = "0.1.20" js-sys = { path = "../js-sys", version = '0.2.0' } wasm-bindgen = { path = "../..", version = '0.2.15' } + +[target.'cfg(target_arch = "wasm32")'.dev-dependencies] +wasm-bindgen-test = { path = '../test', version = '0.2.15' } diff --git a/crates/futures/tests/tests.rs b/crates/futures/tests/tests.rs new file mode 100755 index 000000000..a62257373 --- /dev/null +++ b/crates/futures/tests/tests.rs @@ -0,0 +1,51 @@ +#![feature(use_extern_macros)] +#![cfg(target_arch = "wasm32")] + +extern crate futures; +extern crate js_sys; +extern crate wasm_bindgen; +extern crate wasm_bindgen_futures; +extern crate wasm_bindgen_test; + +use futures::Future; +use wasm_bindgen::prelude::*; +use wasm_bindgen_futures::{future_to_promise, JsFuture}; +use wasm_bindgen_test::*; + +#[wasm_bindgen_test(async)] +fn promise_resolve_is_ok_future() -> impl Future { + let p = js_sys::Promise::resolve(&JsValue::from(42)); + JsFuture::from(p) + .map(|x| { + assert_eq!(x, 42); + }).map_err(|_| unreachable!()) +} + +#[wasm_bindgen_test(async)] +fn promise_reject_is_error_future() -> impl Future { + let p = js_sys::Promise::reject(&JsValue::from(42)); + JsFuture::from(p).map(|_| unreachable!()).or_else(|e| { + assert_eq!(e, 42); + Ok(()) + }) +} + +#[wasm_bindgen_test(async)] +fn ok_future_is_resolved_promise() -> impl Future { + let f = futures::future::ok(JsValue::from(42)); + let p = future_to_promise(f); + JsFuture::from(p) + .map(|x| { + assert_eq!(x, 42); + }).map_err(|_| unreachable!()) +} + +#[wasm_bindgen_test(async)] +fn error_future_is_rejected_promise() -> impl Future { + let f = futures::future::err(JsValue::from(42)); + let p = future_to_promise(f); + JsFuture::from(p).map(|_| unreachable!()).or_else(|e| { + assert_eq!(e, 42); + Ok(()) + }) +}