wasm-bindgen/tests/import-class.rs

126 lines
2.8 KiB
Rust
Raw Normal View History

2018-02-05 14:24:25 -08:00
extern crate test_support;
#[test]
fn simple() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
wasm_bindgen! {
extern struct Math {
fn random() -> f64;
2018-02-05 16:39:11 -08:00
fn log(a: f64) -> f64;
2018-02-05 14:24:25 -08:00
}
pub fn get_random() -> f64 {
Math::random()
}
2018-02-05 16:39:11 -08:00
pub fn do_log(a: f64) -> f64 {
Math::log(a)
}
2018-02-05 14:24:25 -08:00
}
"#)
.file("test.ts", r#"
import * as wasm from "./out";
2018-02-05 16:39:11 -08:00
import * as assert from "assert";
2018-02-05 14:24:25 -08:00
export function test() {
wasm.get_random();
2018-02-05 16:39:11 -08:00
assert.strictEqual(wasm.do_log(1.0), Math.log(1.0));
}
"#)
.test();
}
#[test]
fn import_class() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
wasm_bindgen! {
#[wasm_module = "./test"]
extern struct Foo {
fn bar();
}
pub fn bar() {
Foo::bar();
}
}
"#)
.file("test.ts", r#"
import * as wasm from "./out";
import * as assert from "assert";
let called = false;
export class Foo {
static bar() {
called = true;
}
}
export function test() {
wasm.bar();
assert.strictEqual(called, true);
}
"#)
.test();
}
#[test]
fn construct() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
wasm_bindgen! {
#[wasm_module = "./test"]
extern struct Foo {
fn create() -> Foo;
fn doit(&self);
}
pub fn bar() {
let foo = Foo::bar();
}
}
"#)
.file("test.ts", r#"
import * as wasm from "./out";
import * as assert from "assert";
let called = false;
export class Foo {
static create() {
return new Foo();
}
doit() {
called = true;
}
}
export function test() {
wasm.bar();
assert.strictEqual(called, true);
2018-02-05 14:24:25 -08:00
}
"#)
.test();
}