Alex Crichton eee71de0ce
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!
2018-08-01 15:52:24 -05:00

86 lines
2.6 KiB
Rust

//! Support for printing status information of a test suite in a browser.
//!
//! Currently this is quite simple, rendering the same as the console tests in
//! node.js. Output here is rendered in a `pre`, however.
use wasm_bindgen::prelude::*;
use js_sys::Error;
/// Implementation of `Formatter` for browsers.
///
/// Routes all output to a `pre` on the page currently. Eventually this probably
/// wants to be a pretty table with colors and folding and whatnot.
pub struct Browser {
pre: Element,
}
#[wasm_bindgen]
extern {
type HTMLDocument;
static document: HTMLDocument;
#[wasm_bindgen(method, structural)]
fn getElementById(this: &HTMLDocument, id: &str) -> Element;
type Element;
#[wasm_bindgen(method, getter = innerHTML, structural)]
fn inner_html(this: &Element) -> String;
#[wasm_bindgen(method, setter = innerHTML, structural)]
fn set_inner_html(this: &Element, html: &str);
type BrowserError;
#[wasm_bindgen(method, getter, structural)]
fn stack(this: &BrowserError) -> JsValue;
}
impl Browser {
/// Creates a new instance of `Browser`, assuming that its APIs will work
/// (requires `Node::new()` to have return `None` first).
pub fn new() -> Browser {
let pre = document.getElementById("output");
pre.set_inner_html("");
Browser {
pre,
}
}
}
impl super::Formatter for Browser {
fn writeln(&self, line: &str) {
let mut html = self.pre.inner_html();
html.push_str(&line);
html.push_str("\n");
self.pre.set_inner_html(&html);
}
fn log_test(&self, name: &str, result: &Result<(), JsValue>) {
let s = if result.is_ok() { "ok" } else { "FAIL" };
self.writeln(&format!("test {} ... {}", name, s));
}
fn stringify_error(&self, err: &JsValue) -> String {
// TODO: this should be a checked cast to `Error`
let err = Error::from(err.clone());
let name = String::from(err.name());
let message = String::from(err.message());
let err = BrowserError::from(JsValue::from(err));
let stack = err.stack();
let header = format!("{}: {}", name, message);
let stack = match stack.as_string() {
Some(stack) => stack,
None => return header,
};
// If the `stack` variable contains the name/message already, this is
// probably a chome-like error which is already rendered well, so just
// return this info
if stack.contains(&header) {
return stack
}
// Fallback to make sure we don't lose any info
format!("{}\n{}", header, stack)
}
}