wasm-bindgen/tests/all/webidl/simple.rs

280 lines
6.7 KiB
Rust
Raw Normal View History

use super::project;
#[test]
2018-06-15 01:35:48 +03:00
fn method() {
project()
.file(
"foo.webidl",
r#"
2018-06-15 01:35:48 +03:00
[Constructor(double value)]
interface Foo {
[Pure]
2018-06-15 01:35:48 +03:00
boolean myCmp(Foo bar);
};
"#,
)
.file(
"foo.ts",
r#"
export class Foo {
2018-06-15 01:35:48 +03:00
constructor(private value: number) {
this.value = value;
}
2018-06-15 01:35:48 +03:00
myCmp(other: Foo): boolean {
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;
2018-06-15 01:35:48 +03:00
use foo::Foo;
#[wasm_bindgen]
2018-06-15 01:35:48 +03:00
pub fn test() {
let pi = Foo::new(3.14159);
let e = Foo::new(2.71828);
2018-06-18 23:10:07 +03:00
// TODO: figure out why the following doesn't fail
// assert!(!pi.my_cmp(Foo::new(3.14159)));
2018-06-15 01:35:48 +03:00
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);
}
"#,
)
2018-06-15 01:35:48 +03:00
.test();
}
#[test]
fn property() {
project()
.file(
"foo.webidl",
r#"
[Constructor(double value)]
interface Foo {
[Pure]
attribute double value;
};
"#,
)
.file(
"foo.ts",
r#"
export class Foo {
constructor(private _value: number) {
this._value = _value;
}
get value(): number {
return this._value;
}
set value(_value: number) {
this._value = _value;
}
}
"#,
)
.file(
2018-06-15 01:35:48 +03:00
"src/lib.rs",
r#"
2018-06-15 01:35:48 +03:00
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
2018-06-15 01:35:48 +03:00
pub mod foo;
use foo::Foo;
#[wasm_bindgen]
pub fn test() {
let x = Foo::new(3.14159);
2018-06-18 23:10:07 +03:00
assert_eq!(x.value(), 3.14159);
assert_ne!(x.value(), 2.71828);
2018-06-15 01:35:48 +03:00
x.set_value(2.71828);
2018-06-18 23:10:07 +03:00
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.ts",
r#"
export class Foo {
protected _value: number = 0;
get value(): number {
return this._value;
}
}
export class Bar extends Foo {
constructor(_value: number) {
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);
2018-06-18 23:10:07 +03:00
assert_eq!(x.value(), 3.14159);
assert_ne!(x.value(), 0.);
}
"#,
)
.test();
}
2018-06-15 05:21:33 +03:00
#[test]
fn static_method() {
project()
.file(
"foo.webidl",
r#"
interface Foo {
static double swap(double value);
};
"#,
)
.file(
"foo.ts",
r#"
export class Foo {
private static value: number = 0;
static swap(value: number): number {
const res = Foo.value;
Foo.value = value;
return res;
}
}
"#,
)
.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() {
2018-06-18 23:10:07 +03:00
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);
2018-06-15 05:21:33 +03:00
}
"#,
)
.test();
}
#[test]
fn static_property() {
project()
.file(
"foo.webidl",
r#"
interface Foo {
static attribute double value;
};
"#,
)
.file(
"foo.ts",
r#"
export class Foo {
private static _value: number = 0;
static get value(): number {
return Foo._value;
}
static set value(_value: number) {
Foo._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() {
2018-06-18 23:10:07 +03:00
assert_eq!(Foo::value(), 0.);
Foo::set_value(3.14159);
2018-06-18 23:10:07 +03:00
assert_eq!(Foo::value(), 3.14159);
assert_ne!(Foo::value(), 2.71828);
Foo::set_value(2.71828);
2018-06-18 23:10:07 +03:00
assert_eq!(Foo::value(), 2.71828);
assert_ne!(Foo::value(), 3.14159);
}
"#,
)
.test();
2018-06-18 23:10:07 +03:00
}