mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-06 01:21:22 +00:00
Merge pull request #216 from fitzgen/webidl
More WebIDL testing infrastructure
This commit is contained in:
commit
ba4697bfb1
@ -14,6 +14,12 @@ matrix:
|
|||||||
script: cargo test
|
script: cargo test
|
||||||
env: RUST_BACKTRACE=1
|
env: RUST_BACKTRACE=1
|
||||||
|
|
||||||
|
# WebIDL tests pass on nightly
|
||||||
|
- rust: nightly
|
||||||
|
before_install: rustup component add rustfmt-preview --toolchain nightly
|
||||||
|
script: (cd crates/webidl && cargo test)
|
||||||
|
env: RUST_BACKTRACE=1
|
||||||
|
|
||||||
# Dist linux binary
|
# Dist linux binary
|
||||||
- env: TARGET=x86_64-unknown-linux-musl DEPLOY=1
|
- env: TARGET=x86_64-unknown-linux-musl DEPLOY=1
|
||||||
rust: nightly
|
rust: nightly
|
||||||
|
@ -9,10 +9,12 @@ path = "tests/all/lib.rs"
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
wasm-bindgen-backend = { version = "=0.2.11", path = "../backend", features = ["extra-traits"] }
|
wasm-bindgen-backend = { version = "=0.2.11", path = "../backend", features = ["extra-traits"] }
|
||||||
|
diff = "0.1.11"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
failure = "0.1"
|
failure = "0.1"
|
||||||
proc-macro2 = "0.4"
|
proc-macro2 = "0.4"
|
||||||
|
quote = '0.6'
|
||||||
syn = { version = '0.14', features = ['full'] }
|
syn = { version = '0.14', features = ['full'] }
|
||||||
wasm-bindgen-backend = { version = "=0.2.11", path = "../backend" }
|
wasm-bindgen-backend = { version = "=0.2.11", path = "../backend" }
|
||||||
webidl = "0.6.0"
|
webidl = "0.6.0"
|
||||||
|
@ -10,12 +10,14 @@ emitted for the types and methods described in the WebIDL.
|
|||||||
|
|
||||||
extern crate failure;
|
extern crate failure;
|
||||||
extern crate proc_macro2;
|
extern crate proc_macro2;
|
||||||
|
extern crate quote;
|
||||||
extern crate syn;
|
extern crate syn;
|
||||||
extern crate wasm_bindgen_backend as backend;
|
extern crate wasm_bindgen_backend as backend;
|
||||||
extern crate webidl;
|
extern crate webidl;
|
||||||
|
|
||||||
use failure::ResultExt;
|
use failure::ResultExt;
|
||||||
use proc_macro2::Ident;
|
use proc_macro2::Ident;
|
||||||
|
use quote::ToTokens;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::{self, Read};
|
use std::io::{self, Read};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
@ -43,6 +45,26 @@ pub fn parse(webidl_source: &str) -> Result<backend::ast::Program> {
|
|||||||
Ok(program)
|
Ok(program)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Compile the given WebIDL file into Rust source text containing
|
||||||
|
/// `wasm-bindgen` bindings to the things described in the WebIDL.
|
||||||
|
pub fn compile_file(webidl_path: &Path) -> Result<String> {
|
||||||
|
let ast = parse_file(webidl_path)?;
|
||||||
|
Ok(compile_ast(&ast))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Compile the given WebIDL source text into Rust source text containing
|
||||||
|
/// `wasm-bindgen` bindings to the things described in the WebIDL.
|
||||||
|
pub fn compile(webidl_source: &str) -> Result<String> {
|
||||||
|
let ast = parse(webidl_source)?;
|
||||||
|
Ok(compile_ast(&ast))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compile_ast(ast: &backend::ast::Program) -> String {
|
||||||
|
let mut tokens = proc_macro2::TokenStream::new();
|
||||||
|
ast.to_tokens(&mut tokens);
|
||||||
|
tokens.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
trait WebidlParse {
|
trait WebidlParse {
|
||||||
fn webidl_parse(&self, program: &mut backend::ast::Program) -> Result<()>;
|
fn webidl_parse(&self, program: &mut backend::ast::Program) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
@ -1,24 +0,0 @@
|
|||||||
use super::backend;
|
|
||||||
use proc_macro2;
|
|
||||||
use syn;
|
|
||||||
|
|
||||||
assert_parse!(
|
|
||||||
event,
|
|
||||||
include_str!("./Event.webidl"),
|
|
||||||
backend::ast::Program {
|
|
||||||
exports: vec![],
|
|
||||||
imports: vec![backend::ast::Import {
|
|
||||||
module: None,
|
|
||||||
version: None,
|
|
||||||
js_namespace: None,
|
|
||||||
kind: backend::ast::ImportKind::Type(backend::ast::ImportType {
|
|
||||||
vis: syn::Visibility::Public(syn::VisPublic {
|
|
||||||
pub_token: Default::default(),
|
|
||||||
}),
|
|
||||||
name: syn::Ident::new("Event", proc_macro2::Span::call_site()),
|
|
||||||
}),
|
|
||||||
}],
|
|
||||||
enums: vec![],
|
|
||||||
structs: vec![],
|
|
||||||
}
|
|
||||||
);
|
|
@ -1,21 +1,41 @@
|
|||||||
|
extern crate diff;
|
||||||
extern crate proc_macro2;
|
extern crate proc_macro2;
|
||||||
extern crate syn;
|
extern crate syn;
|
||||||
extern crate wasm_bindgen_backend as backend;
|
extern crate wasm_bindgen_backend as backend;
|
||||||
extern crate wasm_bindgen_webidl as wb_webidl;
|
extern crate wasm_bindgen_webidl as wb_webidl;
|
||||||
|
|
||||||
pub fn assert_parse(webidl: &str, expected: backend::ast::Program) {
|
#[macro_use]
|
||||||
let actual = wb_webidl::parse(webidl).expect("should parse the webidl source OK");
|
mod util;
|
||||||
assert_eq!(expected, actual);
|
use util::*;
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! assert_parse {
|
/// Tests for parsing WebIDL into an expected wasm-bindgen AST.
|
||||||
($test_name:ident, $webidl_source:expr, $expected_ast:expr) => {
|
mod parse {
|
||||||
#[test]
|
use super::*;
|
||||||
fn $test_name() {
|
|
||||||
$crate::assert_parse($webidl_source, $expected_ast);
|
assert_parse!(empty, backend::ast::Program::default());
|
||||||
|
|
||||||
|
assert_parse!(
|
||||||
|
Event,
|
||||||
|
backend::ast::Program {
|
||||||
|
exports: vec![],
|
||||||
|
imports: vec![backend::ast::Import {
|
||||||
|
module: None,
|
||||||
|
version: None,
|
||||||
|
js_namespace: None,
|
||||||
|
kind: backend::ast::ImportKind::Type(backend::ast::ImportType {
|
||||||
|
vis: syn::Visibility::Public(syn::VisPublic {
|
||||||
|
pub_token: Default::default(),
|
||||||
|
}),
|
||||||
|
name: syn::Ident::new("Event", proc_macro2::Span::call_site()),
|
||||||
|
}),
|
||||||
|
}],
|
||||||
|
enums: vec![],
|
||||||
|
structs: vec![],
|
||||||
}
|
}
|
||||||
};
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
mod event;
|
/// Tests for compiling WebIDL into Rust bindings.
|
||||||
mod simple;
|
mod compile {
|
||||||
|
assert_compile!(Event);
|
||||||
|
}
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
use super::backend;
|
|
||||||
|
|
||||||
assert_parse!(empty, "", backend::ast::Program::default());
|
|
171
crates/webidl/tests/all/util.rs
Normal file
171
crates/webidl/tests/all/util.rs
Normal file
@ -0,0 +1,171 @@
|
|||||||
|
use backend;
|
||||||
|
use diff;
|
||||||
|
use std::io::{self, Write};
|
||||||
|
use std::process;
|
||||||
|
use std::sync::{Once, ONCE_INIT};
|
||||||
|
use wb_webidl;
|
||||||
|
|
||||||
|
pub fn assert_parse(webidl: &str, expected: backend::ast::Program) {
|
||||||
|
let actual = wb_webidl::parse(webidl).expect("should parse the webidl source OK");
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! assert_parse {
|
||||||
|
($test_name:ident, $expected_ast:expr) => {
|
||||||
|
#[test]
|
||||||
|
#[allow(non_snake_case)]
|
||||||
|
fn $test_name() {
|
||||||
|
let webidl_source = include_str!(concat!(
|
||||||
|
env!("CARGO_MANIFEST_DIR"),
|
||||||
|
"/tests/fixtures/",
|
||||||
|
stringify!($test_name),
|
||||||
|
".webidl"
|
||||||
|
));
|
||||||
|
$crate::assert_parse(webidl_source, $expected_ast);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
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: String) -> 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) {
|
||||||
|
let actual = wb_webidl::compile(webidl).expect("should compile the webidl source OK");
|
||||||
|
|
||||||
|
let (actual, actual_stderr) = rustfmt(actual);
|
||||||
|
let (expected, expected_stderr) = rustfmt(expected);
|
||||||
|
|
||||||
|
let actual = strip_wasm_bindgen_generated(actual);
|
||||||
|
let expected = strip_wasm_bindgen_generated(expected);
|
||||||
|
|
||||||
|
if expected == actual {
|
||||||
|
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"
|
||||||
|
));
|
||||||
|
$crate::assert_compile(webidl_source, expected_output);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
58
crates/webidl/tests/expected/Event.rs
Normal file
58
crates/webidl/tests/expected/Event.rs
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
#[allow(bad_style)]
|
||||||
|
pub struct Event {
|
||||||
|
obj: ::wasm_bindgen::JsValue,
|
||||||
|
}
|
||||||
|
impl ::wasm_bindgen::describe::WasmDescribe for Event {
|
||||||
|
fn describe() {
|
||||||
|
::wasm_bindgen::JsValue::describe();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl ::wasm_bindgen::convert::IntoWasmAbi for Event {
|
||||||
|
type Abi = <::wasm_bindgen::JsValue as ::wasm_bindgen::convert::IntoWasmAbi>::Abi;
|
||||||
|
fn into_abi(self, extra: &mut ::wasm_bindgen::convert::Stack) -> Self::Abi {
|
||||||
|
self.obj.into_abi(extra)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl ::wasm_bindgen::convert::FromWasmAbi for Event {
|
||||||
|
type Abi = <::wasm_bindgen::JsValue as ::wasm_bindgen::convert::FromWasmAbi>::Abi;
|
||||||
|
unsafe fn from_abi(js: Self::Abi, extra: &mut ::wasm_bindgen::convert::Stack) -> Self {
|
||||||
|
Event {
|
||||||
|
obj: ::wasm_bindgen::JsValue::from_abi(js, extra),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'a> ::wasm_bindgen::convert::IntoWasmAbi for &'a Event {
|
||||||
|
type Abi = <&'a ::wasm_bindgen::JsValue as ::wasm_bindgen::convert::IntoWasmAbi>::Abi;
|
||||||
|
fn into_abi(self, extra: &mut ::wasm_bindgen::convert::Stack) -> Self::Abi {
|
||||||
|
(&self.obj).into_abi(extra)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl ::wasm_bindgen::convert::RefFromWasmAbi for Event {
|
||||||
|
type Abi = <::wasm_bindgen::JsValue as ::wasm_bindgen::convert::RefFromWasmAbi>::Abi;
|
||||||
|
type Anchor = ::wasm_bindgen::__rt::core::mem::ManuallyDrop<Event>;
|
||||||
|
unsafe fn ref_from_abi(
|
||||||
|
js: Self::Abi,
|
||||||
|
extra: &mut ::wasm_bindgen::convert::Stack,
|
||||||
|
) -> Self::Anchor {
|
||||||
|
let tmp =
|
||||||
|
<::wasm_bindgen::JsValue as ::wasm_bindgen::convert::RefFromWasmAbi>::ref_from_abi(
|
||||||
|
js, extra,
|
||||||
|
);
|
||||||
|
::wasm_bindgen::__rt::core::mem::ManuallyDrop::new(Event {
|
||||||
|
obj: ::wasm_bindgen::__rt::core::mem::ManuallyDrop::into_inner(tmp),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<::wasm_bindgen::JsValue> for Event {
|
||||||
|
fn from(obj: ::wasm_bindgen::JsValue) -> Event {
|
||||||
|
Event { obj }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<Event> for ::wasm_bindgen::JsValue {
|
||||||
|
fn from(obj: Event) -> ::wasm_bindgen::JsValue {
|
||||||
|
obj.obj
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[allow(non_upper_case_globals)]
|
||||||
|
#[wasm_custom_section = "__wasm_bindgen_unstable"]
|
||||||
|
const __WASM_BINDGEN_GENERATED_wasm_bindgen_webidl_0_1_0_0 : [ u8 ; 180usize ] = * b"\xB0\0\0\0{\"exports\":[],\"enums\":[],\"imports\":[{\"module\":null,\"version\":null,\"js_namespace\":null,\"kind\":{\"kind\":\"type\"}}],\"structs\":[],\"version\":\"0.2.11 (3879f6f42)\",\"schema_version\":\"4\"}" ;
|
0
crates/webidl/tests/fixtures/empty.webidl
vendored
Normal file
0
crates/webidl/tests/fixtures/empty.webidl
vendored
Normal file
0
crates/webidl/tests/rustfmt.toml
Normal file
0
crates/webidl/tests/rustfmt.toml
Normal file
Loading…
x
Reference in New Issue
Block a user