Move webidl tests to the webidl crate's test suite (#451)

* webidl: Remove exact-output tests

These have not been as effective, nor as easy to write and maintain, as the
project()-based integration tests.

* tests: Move webidl tests into the webidl crate's test suite
This commit is contained in:
Nick Fitzgerald
2018-07-10 14:17:33 -07:00
committed by Alex Crichton
parent 42938792c7
commit 92dd8e859f
12 changed files with 6 additions and 855 deletions

View File

@ -1,12 +1,4 @@
extern crate diff;
extern crate env_logger;
extern crate proc_macro2;
extern crate syn;
extern crate wasm_bindgen_backend as backend;
extern crate wasm_bindgen_webidl as wb_webidl;
extern crate wasm_bindgen_test_project_builder as project_builder;
use project_builder::project;
#[macro_use]
mod util;
use util::*;
assert_compile!(Event);
mod simple;

View File

@ -0,0 +1,372 @@
use super::project;
#[test]
fn method() {
project()
.file(
"foo.webidl",
r#"
[Constructor(double value)]
interface Foo {
[Pure]
boolean myCmp(Foo bar);
};
"#,
)
.file(
"foo.js",
r#"
export class Foo {
constructor(value) {
this.value = value;
}
myCmp(other) {
return this.value === other.value;
}
}
"#,
)
.file(
"src/lib.rs",
r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
pub mod foo;
use foo::Foo;
#[wasm_bindgen]
pub fn test() {
let pi = Foo::new(3.14159);
let e = Foo::new(2.71828);
// TODO: figure out why the following doesn't fail
// assert!(!pi.my_cmp(Foo::new(3.14159)));
let tmp = pi.my_cmp(Foo::new(3.14159));
assert!(tmp);
let tmp =!pi.my_cmp(Foo::new(2.71828));
assert!(tmp);
let tmp = !e.my_cmp(Foo::new(3.14159));
assert!(tmp);
let tmp = e.my_cmp(Foo::new(2.71828));
assert!(tmp);
}
"#,
)
.test();
}
#[test]
fn property() {
project()
.file(
"foo.webidl",
r#"
[Constructor(double value)]
interface Foo {
[Pure]
attribute double value;
};
"#,
)
.file(
"foo.js",
r#"
export class Foo {
constructor(value) {
this._value = value;
}
get value() {
return this._value;
}
set value(value) {
this._value = value;
}
}
"#,
)
.file(
"src/lib.rs",
r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
pub mod foo;
use foo::Foo;
#[wasm_bindgen]
pub fn test() {
let x = Foo::new(3.14159);
assert_eq!(x.value(), 3.14159);
assert_ne!(x.value(), 2.71828);
x.set_value(2.71828);
assert_ne!(x.value(), 3.14159);
assert_eq!(x.value(), 2.71828);
}
"#,
)
.test();
}
#[test]
fn named_constructor() {
project()
.file(
"foo.webidl",
r#"
[NamedConstructor=Bar(double value)]
interface Foo {
[Pure]
readonly attribute double value;
};
"#,
)
.file(
// Not a perfect test, but it gets the job done.
"foo.js",
r#"
export class Foo {
constructor() {
this._value = 0;
}
get value(){
return this._value;
}
}
export class Bar extends Foo {
constructor(_value) {
super();
this._value = _value;
}
}
"#,
)
.file(
"src/lib.rs",
r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
pub mod foo;
use foo::Foo;
#[wasm_bindgen]
pub fn test() {
let x = Foo::new(3.14159);
assert_eq!(x.value(), 3.14159);
assert_ne!(x.value(), 0.);
}
"#,
)
.test();
}
#[test]
fn static_method() {
project()
.file(
"foo.webidl",
r#"
interface Foo {
static double swap(double value);
};
"#,
)
.file(
"foo.js",
r#"
export class Foo {
static swap(value) {
const res = Foo.value;
Foo.value = value;
return res;
}
}
Foo.value = 0;
"#,
)
.file(
"src/lib.rs",
r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
pub mod foo;
use foo::Foo;
#[wasm_bindgen]
pub fn test() {
assert_eq!(Foo::swap(3.14159), 0.);
assert_eq!(Foo::swap(2.71828), 3.14159);
assert_ne!(Foo::swap(2.71828), 3.14159);
assert_eq!(Foo::swap(3.14159), 2.71828);
assert_ne!(Foo::swap(3.14159), 2.71828);
}
"#,
)
.test();
}
#[test]
fn static_property() {
project()
.file(
"foo.webidl",
r#"
interface Foo {
static attribute double value;
};
"#,
)
.file(
"foo.js",
r#"
export class Foo {
static get value(){
return Foo._value;
}
static set value(value) {
Foo._value = value;
}
}
Foo._value = 0;
"#,
)
.file(
"src/lib.rs",
r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
pub mod foo;
use foo::Foo;
#[wasm_bindgen]
pub fn test() {
assert_eq!(Foo::value(), 0.);
Foo::set_value(3.14159);
assert_eq!(Foo::value(), 3.14159);
assert_ne!(Foo::value(), 2.71828);
Foo::set_value(2.71828);
assert_eq!(Foo::value(), 2.71828);
assert_ne!(Foo::value(), 3.14159);
}
"#,
)
.test();
}
#[test]
fn one_method_using_an_undefined_import_doesnt_break_all_other_methods() {
project()
.file(
"foo.webidl",
r#"
[Constructor()]
interface Foo {
boolean ok_method();
boolean bad_method(UndefinedType undef);
};
"#,
)
.file(
"foo.js",
r#"
export class Foo {
constructor() {}
ok_method() {
return true;
}
}
"#,
)
.file(
"src/lib.rs",
r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
pub mod foo;
#[wasm_bindgen]
pub fn test() {
let f = foo::Foo::new();
assert!(f.ok_method());
}
"#,
)
.test();
}
#[test]
fn unforgeable_is_structural() {
project()
.file(
"foo.webidl",
r#"
[Constructor()]
interface Foo {
[Unforgeable] readonly attribute short uno;
readonly attribute short dos;
};
"#,
)
.file(
"foo.js",
r#"
export class Foo {
constructor() {
this.uno = 1;
}
get dos() {
return 2;
}
}
"#,
)
.file(
"src/lib.rs",
r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
pub mod foo;
#[wasm_bindgen]
pub fn test() {
let f = foo::Foo::new();
assert_eq!(f.uno(), 1);
assert_eq!(f.dos(), 2);
}
"#,
)
.test();
}

View File

@ -1,172 +0,0 @@
use std::env;
use std::fs::File;
use std::io::{self, Write};
use std::process;
use std::sync::{Once, ONCE_INIT};
use diff;
use env_logger;
use wb_webidl;
fn rustfmt<S: Into<String>>(source: S) -> (String, String) {
let source = source.into();
static CHECK_RUSTFMT: Once = ONCE_INIT;
CHECK_RUSTFMT.call_once(|| {
let have_working_rustfmt = process::Command::new("rustup")
.args(&["run", "nightly", "rustfmt", "--version"])
.stdout(process::Stdio::null())
.stderr(process::Stdio::null())
.status()
.ok()
.map_or(false, |status| status.success());
if !have_working_rustfmt {
panic!(
"
The latest `rustfmt` is required to run the `wasm-bindgen` test suite. Install
`rustfmt` with:
$ rustup component add rustfmt-preview --toolchain nightly
"
);
}
});
let mut child = process::Command::new("rustup")
.args(&[
"run",
"nightly",
"rustfmt",
"--config-path",
concat!(env!("CARGO_MANIFEST_DIR"), "/tests/rustfmt.toml"),
])
.stdin(process::Stdio::piped())
.stdout(process::Stdio::piped())
.stderr(process::Stdio::piped())
.spawn()
.expect("should spawn `rustup run nightly rustfmt`");
let mut stdin = child.stdin.take().unwrap();
let mut stdout = child.stdout.take().unwrap();
let mut stderr = child.stderr.take().unwrap();
// Write to stdin in a new thread, so that we can read from stdout on this
// thread. This keeps the child from blocking on writing to its stdout which
// might block us from writing to its stdin.
let stdin_handle = ::std::thread::spawn(move || stdin.write_all(source.as_bytes()));
// Read stderr on a new thread for similar reasons.
let stderr_handle = ::std::thread::spawn(move || {
let mut output = vec![];
io::copy(&mut stderr, &mut output).map(|_| String::from_utf8_lossy(&output).to_string())
});
let mut output = vec![];
io::copy(&mut stdout, &mut output).expect("Should copy stdout into vec OK");
// Ignore actual rustfmt status because it is often non-zero for trivial
// things.
let _ = child.wait().expect("should wait on rustfmt child OK");
stdin_handle
.join()
.expect("writer thread should not have panicked")
.expect("should have written to child rustfmt's stdin OK");
let formatted = String::from_utf8(output).expect("rustfmt should only emit valid utf-8");
let stderr = stderr_handle
.join()
.expect("stderr reader thread should not have panicked")
.expect("should have read child rustfmt's stderr OK");
(formatted, stderr)
}
fn strip_wasm_bindgen_generated(source: &str) -> String {
let lines: Vec<_> = source
.lines()
.filter(|l| !l.contains("__WASM_BINDGEN_GENERATED"))
.collect();
lines.join("\n")
}
pub fn assert_compile(webidl: &str, expected: &str, expected_file: &str) {
static INIT_ENV_LOGGER: Once = ONCE_INIT;
INIT_ENV_LOGGER.call_once(|| {
env_logger::init();
});
let actual = wb_webidl::compile(webidl).expect("should compile the webidl source OK");
let (actual_orig, actual_stderr) = rustfmt(actual);
let (expected, expected_stderr) = rustfmt(expected);
let actual = strip_wasm_bindgen_generated(&actual_orig);
let expected = strip_wasm_bindgen_generated(&expected);
if expected == actual {
return;
}
if env::var("UPDATE_EXPECTED").is_ok() {
File::create(expected_file)
.unwrap()
.write_all(actual_orig.as_bytes())
.unwrap();
return
}
eprintln!("rustfmt(expected) stderr:");
eprintln!("{}", expected_stderr);
eprintln!();
eprintln!("rustfmt(actual) stderr:");
eprintln!("{}", actual_stderr);
eprintln!();
eprintln!(
"assert_compile failed: actual compiled output and expected compiled output do not match:"
);
eprintln!("--- expected");
eprintln!("+++ actual");
for d in diff::lines(&expected, &actual) {
match d {
diff::Result::Left(l) => eprintln!("-{}", l),
diff::Result::Right(r) => eprintln!("+{}", r),
diff::Result::Both(b, _) => eprintln!(" {}", b),
}
}
panic!()
}
macro_rules! assert_compile {
($test_name:ident) => {
#[test]
#[allow(non_snake_case)]
fn $test_name() {
let webidl_source = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/",
stringify!($test_name),
".webidl"
));
let expected_output = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/expected/",
stringify!($test_name),
".rs"
));
let expected_file = concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/expected/",
stringify!($test_name),
".rs"
);
$crate::assert_compile(webidl_source, expected_output, expected_file);
}
};
}