mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-13 04:51:23 +00:00
Support asynchronous tests (#600)
* Tweak the implementation of heap closures This commit updates the implementation of the `Closure` type to internally store an `Rc` and be suitable for dropping a `Closure` during the execution of the closure. This is currently needed for promises but may be generally useful as well! * Support asynchronous tests This commit adds support for executing tests asynchronously. This is modeled by tests returning a `Future` instead of simply executing inline, and is signified with `#[wasm_bindgen_test(async)]`. Support for this is added through a new `wasm-bindgen-futures` crate which is a binding between the `futures` crate and JS `Promise` objects. Lots more details can be found in the details of the commit, but one of the end results is that the `web-sys` tests are now entirely contained in the same test suite and don't need `npm install` to be run to execute them! * Review tweaks * Add some bindings for `Function.call` to `js_sys` Name them `call0`, `call1`, `call2`, ... for the number of arguments being passed. * Use oneshots channels with `JsFuture` It did indeed clean up the implementation!
This commit is contained in:
16
crates/test/sample/Cargo.toml
Normal file
16
crates/test/sample/Cargo.toml
Normal file
@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "sample"
|
||||
version = "0.1.0"
|
||||
authors = ["The wasm-bindgen Authors"]
|
||||
|
||||
[lib]
|
||||
test = false
|
||||
|
||||
[dependencies]
|
||||
futures = "0.1"
|
||||
js-sys = { path = '../../js-sys' }
|
||||
wasm-bindgen = { path = '../../..' }
|
||||
wasm-bindgen-futures = { path = '../../futures' }
|
||||
|
||||
[dev-dependencies]
|
||||
wasm-bindgen-test = { path = '..' }
|
4
crates/test/sample/README.md
Normal file
4
crates/test/sample/README.md
Normal file
@ -0,0 +1,4 @@
|
||||
# Sample test crate
|
||||
|
||||
This is a dummy crate used to test changes to the `wasm-bindgen-test` crate,
|
||||
this'll never be published nor tested on CI, it's intended for human inspection.
|
64
crates/test/sample/src/lib.rs
Normal file
64
crates/test/sample/src/lib.rs
Normal file
@ -0,0 +1,64 @@
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate futures;
|
||||
extern crate js_sys;
|
||||
extern crate wasm_bindgen;
|
||||
extern crate wasm_bindgen_futures;
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use futures::prelude::*;
|
||||
use js_sys::Promise;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen_futures::JsFuture;
|
||||
|
||||
pub struct Timeout {
|
||||
id: u32,
|
||||
inner: JsFuture,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
#[wasm_bindgen(js_name = setTimeout)]
|
||||
fn set_timeout(closure: JsValue, millis: f64) -> u32;
|
||||
|
||||
#[wasm_bindgen(js_name = clearTimeout)]
|
||||
fn clear_timeout(id: u32);
|
||||
}
|
||||
|
||||
impl Timeout {
|
||||
pub fn new(dur: Duration) -> Timeout {
|
||||
let millis = dur.as_secs()
|
||||
.checked_mul(1000)
|
||||
.unwrap()
|
||||
.checked_add(dur.subsec_millis() as u64)
|
||||
.unwrap() as f64; // TODO: checked cast
|
||||
|
||||
let mut id = None;
|
||||
let promise = Promise::new(&mut |resolve, _reject| {
|
||||
id = Some(set_timeout(resolve.into(), millis));
|
||||
});
|
||||
|
||||
Timeout {
|
||||
id: id.unwrap(),
|
||||
inner: JsFuture::from(promise),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Future for Timeout {
|
||||
type Item = ();
|
||||
type Error = JsValue;
|
||||
|
||||
fn poll(&mut self) -> Poll<(), JsValue> {
|
||||
let _obj = try_ready!(self.inner.poll());
|
||||
Ok(().into())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Timeout {
|
||||
fn drop(&mut self) {
|
||||
clear_timeout(self.id);
|
||||
}
|
||||
}
|
10
crates/test/sample/tests/browser.rs
Normal file
10
crates/test/sample/tests/browser.rs
Normal file
@ -0,0 +1,10 @@
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate futures;
|
||||
extern crate sample;
|
||||
extern crate wasm_bindgen;
|
||||
extern crate wasm_bindgen_test;
|
||||
|
||||
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
|
||||
|
||||
pub mod common;
|
45
crates/test/sample/tests/common/mod.rs
Normal file
45
crates/test/sample/tests/common/mod.rs
Normal file
@ -0,0 +1,45 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use futures::prelude::*;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen_test::*;
|
||||
use sample::Timeout;
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn pass() {
|
||||
console_log!("DO NOT SEE ME");
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test(async)]
|
||||
fn pass_after_2s() -> impl Future<Item = (), Error = JsValue> {
|
||||
console_log!("immediate log");
|
||||
Timeout::new(Duration::new(1, 0))
|
||||
.and_then(|()| {
|
||||
console_log!("log after 1s");
|
||||
Timeout::new(Duration::new(1, 0)).map(|()| {
|
||||
console_log!("log at end");
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn fail() {
|
||||
console_log!("helpful messsage, please see me");
|
||||
panic!("this is a failing test");
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test(async)]
|
||||
fn fail_after_3s() -> impl Future<Item = (), Error = JsValue> {
|
||||
console_log!("immediate log");
|
||||
Timeout::new(Duration::new(1, 0))
|
||||
.and_then(|()| {
|
||||
console_log!("log after 1s");
|
||||
Timeout::new(Duration::new(1, 0)).and_then(|()| {
|
||||
console_log!("log after 2s");
|
||||
Timeout::new(Duration::new(1, 0)).map(|()| {
|
||||
panic!("end");
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
8
crates/test/sample/tests/node.rs
Normal file
8
crates/test/sample/tests/node.rs
Normal file
@ -0,0 +1,8 @@
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate futures;
|
||||
extern crate sample;
|
||||
extern crate wasm_bindgen;
|
||||
extern crate wasm_bindgen_test;
|
||||
|
||||
pub mod common;
|
Reference in New Issue
Block a user