211 lines
5.7 KiB
Rust
Raw Normal View History

Dramatically improving the build time of web-sys (#2012) * Pre-generating web-sys * Fixing build errors * Minor refactor for the unit tests * Changing to generate #[wasm_bindgen} annotations * Fixing code generation * Adding in main bin to wasm-bindgen-webidl * Fixing more problems * Adding in support for unstable APIs * Fixing bug with code generation * More code generation fixes * Improving the webidl program * Removing unnecessary cfg from the generated code * Splitting doc comments onto separate lines * Improving the generation for unstable features * Adding in support for string values in enums * Now runs rustfmt on the mod.rs file * Fixing codegen for constructors * Fixing webidl-tests * Fixing build errors * Another fix for build errors * Renaming typescript_name to typescript_type * Adding in docs for typescript_type * Adding in CI script to verify that web-sys is up to date * Fixing CI script * Fixing CI script * Don't suppress git diff output * Remove duplicate definitions of `Location` Looks to be a preexisting bug in wasm-bindgen? * Regenerate webidl * Try to get the git diff command right * Handle named constructors in WebIDL * Remove stray rustfmt.toml * Add back NamedConstructorBar definition in tests * Run stable rustfmt over everything * Don't run Cargo in a build script Instead refactor things so webidl-tests can use the Rust-code-generation as a library in a build script. Also fixes `cargo fmt` in the repository. * Fixup generated code * Running web-sys checks on stable * Improving the code generation a little * Running rustfmt Co-authored-by: Alex Crichton <alex@alexcrichton.com>
2020-03-03 00:39:36 +01:00
use crate::generated::*;
use js_sys::Object;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen_test::*;
#[wasm_bindgen_test]
fn interfaces_inherit_from_object() {
let m = Method::new(42.0).unwrap();
assert!(m.is_instance_of::<Object>());
let _: &Object = m.as_ref();
}
#[wasm_bindgen_test]
fn method() {
let pi = Method::new(3.14159).unwrap();
let e = Method::new(2.71828).unwrap();
assert!(pi.my_cmp(&pi));
assert!(!pi.my_cmp(&e));
assert!(!e.my_cmp(&pi));
assert!(e.my_cmp(&e));
}
#[wasm_bindgen_test]
fn property() {
let x = Property::new(3.14159).unwrap();
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);
}
#[wasm_bindgen_test]
fn named_constructor() {
let x = NamedConstructor::new(3.14159).unwrap();
assert_eq!(x.value(), 3.14159);
assert_ne!(x.value(), 0.);
}
#[wasm_bindgen_test]
fn static_method() {
assert_eq!(StaticMethod::swap(3.14159), 0.);
assert_eq!(StaticMethod::swap(2.71828), 3.14159);
assert_ne!(StaticMethod::swap(2.71828), 3.14159);
assert_eq!(StaticMethod::swap(3.14159), 2.71828);
assert_ne!(StaticMethod::swap(3.14159), 2.71828);
}
#[wasm_bindgen_test]
fn static_property() {
assert_eq!(StaticProperty::value(), 0.);
StaticProperty::set_value(3.14159);
assert_eq!(StaticProperty::value(), 3.14159);
assert_ne!(StaticProperty::value(), 2.71828);
StaticProperty::set_value(2.71828);
assert_eq!(StaticProperty::value(), 2.71828);
assert_ne!(StaticProperty::value(), 3.14159);
}
#[wasm_bindgen_test]
fn one_method_with_an_undefined_import_doesnt_break_all_other_methods() {
let f = UndefinedMethod::new().unwrap();
assert!(f.ok_method());
}
2018-08-03 16:28:35 +03:00
#[wasm_bindgen_test]
2018-08-13 23:18:16 +03:00
fn nullable_method() {
let f = NullableMethod::new().unwrap();
2018-08-03 16:28:35 +03:00
assert!(f.opt(Some(15)) == Some(16));
assert!(f.opt(None) == None);
}
2018-09-17 14:51:25 -07:00
#[wasm_bindgen]
extern "C" {
2018-09-17 14:51:25 -07:00
fn get_global_method() -> GlobalMethod;
}
2018-08-05 18:05:20 +03:00
#[wasm_bindgen_test]
fn global_method() {
2018-09-17 14:51:25 -07:00
let x = get_global_method();
assert_eq!(x.m(), 123);
2018-08-07 00:42:47 +03:00
}
#[wasm_bindgen_test]
fn indexing() {
let f = Indexing::new().unwrap();
assert_eq!(f.get(123).unwrap(), -1);
2018-08-07 00:42:47 +03:00
f.set(123, 456);
assert_eq!(f.get(123).unwrap(), 456);
2018-08-07 00:42:47 +03:00
f.delete(123);
assert_eq!(f.get(123).unwrap(), -1);
2018-08-05 18:05:20 +03:00
}
2018-08-13 23:18:16 +03:00
#[wasm_bindgen_test]
fn optional_and_union_arguments() {
let f = OptionalAndUnionArguments::new().unwrap();
assert_eq!(
f.m("abc"),
"string, abc, boolean, true, number, 123, number, 456"
);
assert_eq!(
f.m_with_b("abc", false),
"string, abc, boolean, false, number, 123, number, 456"
);
assert_eq!(
f.m_with_b_and_i16("abc", false, 5),
"string, abc, boolean, false, number, 5, number, 456"
);
assert_eq!(
f.m_with_b_and_str("abc", false, "5"),
"string, abc, boolean, false, string, 5, number, 456"
);
assert_eq!(
f.m_with_b_and_i16_and_opt_i32("abc", false, 5, Some(10)),
"string, abc, boolean, false, number, 5, number, 10"
);
assert_eq!(
f.m_with_b_and_i16_and_opt_f64("abc", false, 5, Some(10.0)),
"string, abc, boolean, false, number, 5, number, 10"
);
assert_eq!(
f.m_with_b_and_i16_and_opt_bool("abc", false, 5, Some(true)),
"string, abc, boolean, false, number, 5, boolean, true"
);
assert_eq!(
f.m_with_b_and_str_and_opt_i32("abc", false, "5", Some(10)),
"string, abc, boolean, false, string, 5, number, 10"
);
assert_eq!(
f.m_with_b_and_str_and_opt_f64("abc", false, "5", Some(12.0)),
"string, abc, boolean, false, string, 5, number, 12"
);
assert_eq!(
f.m_with_b_and_str_and_opt_bool("abc", false, "5", Some(true)),
"string, abc, boolean, false, string, 5, boolean, true"
);
2018-08-13 23:18:16 +03:00
}
#[wasm_bindgen_test]
fn variadic() {
let f = Variadic::new().unwrap();
assert_eq!(f.sum_5(1, 2, 3, 4, 5), 15);
assert_eq!(
f.sum(&::js_sys::Array::of5(
&JsValue::from_f64(1f64),
&JsValue::from_f64(2f64),
&JsValue::from_f64(3f64),
&JsValue::from_f64(4f64),
&JsValue::from_f64(5f64),
)),
15
);
}
#[wasm_bindgen_test]
fn unforgeable_is_structural() {
let f = Unforgeable::new().unwrap();
assert_eq!(f.uno(), 1);
assert_eq!(f.dos(), 2);
}
#[wasm_bindgen_test]
fn partial_interface() {
let f = PartialInterface::new().unwrap();
assert_eq!(f.un(), 1);
assert_eq!(f.deux(), 2);
assert_eq!(f.trois(), 3);
assert_eq!(f.quatre(), 4);
}
#[wasm_bindgen_test]
fn mixin() {
let f = MixinFoo::new(1).unwrap();
assert_eq!(f.bar(), 1);
MixinFoo::set_default_bar(7);
f.add_to_bar(MixinFoo::default_bar());
assert_eq!(f.bar(), 8);
}
#[wasm_bindgen_test]
fn overload_naming() {
let o = Overloads::new().unwrap();
o.foo();
o.foo_with_arg("x");
o.foo_with_arg_and_i32("x", 3);
o.foo_with_arg_and_f32("x", 2.0);
o.foo_with_arg_and_i16("x", 5);
}
#[wasm_bindgen_test]
fn callback() {
let o = InvokeCallback::new().unwrap();
{
static mut HIT: bool = false;
let cb = Closure::wrap(Box::new(move || unsafe {
HIT = true;
}) as Box<dyn FnMut()>);
o.invoke(cb.as_ref().unchecked_ref());
assert!(unsafe { HIT });
}
let cb = Closure::wrap(Box::new(move |a, b| a + b) as Box<dyn FnMut(u32, u32) -> u32>);
assert_eq!(o.call_add(cb.as_ref().unchecked_ref()), 3);
let cb = Closure::wrap(
Box::new(move |a: String, b| a.repeat(b)) as Box<dyn FnMut(String, usize) -> String>
);
assert_eq!(o.call_repeat(cb.as_ref().unchecked_ref()), "abababab");
}