wasm-bindgen/tests/all/import_class.rs
Alex Crichton efa4a2b8fa
Speed up Travis by running Webpack in fewer tests (#381)
* Reorganize Travis configuration

* Add a `JOB` env var descriptor to all matrix entries. Not used anywhere but is
  useful when viewing the whole build on Travis's web interface.
* Reorganize where builds are located, moving slow builds first and fast ones
  last.
* Change checking the CLI builds from `cargo build` to `cargo check`
* Use YAML references to reduce some duplication

* Print some more timing statistics for each test

* Extract `Project` helper in tests to a module

This'll help make it a bit more extensible over time. At the same time the
methods are also slightly reorganized to read more clearly from top to bottom.

* Migrate all tests away from Webpack

Wepback can take a significant amount of time to execute and when it's
multiplied by hundreds of tests that adds up really quickly! After investigating
Node's `--experimental-modules` option it looks like it's suitable for our use
so this switches all tests to using JS files (moving away from TypeScript as
well) with `--experimental-modules` with Node.

Tests will be selectively re-enabled with webpack and node.js specific output
(that doesn't require `--experimental-modules`), coming in later commits.

* Restore the node test for node.js output

Ensures it's workable as-is

* Only generate typescript with webpack

* Only read wasm files for webpack

* Skip package.json/node_modules for now

* Only generate webpack config if needed

* Start a dedicated test module for typescript

Will hopefully verify the generated Typescript compiles OK.

* Remove unneeded `node` method

* Fixup some rebase conflicts

* Don't run asmjs example on travis

* Fixup generator tests

* Attempt to fix windows

* Comment windows fix

* More test fixes

* More exclusions

* More test fixes

* Relax eslint regex

Catch mjs modules as well

* Fix eslint

* Speed up travis on examples slightly
2018-07-04 22:37:09 -05:00

456 lines
12 KiB
Rust

use super::project;
#[test]
fn simple() {
project()
.file(
"src/lib.rs",
r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn get_random() -> f64 {
random()
}
#[wasm_bindgen]
pub fn do_log(a: f64) -> f64 {
log(a)
}
#[wasm_bindgen]
extern {
#[wasm_bindgen(js_namespace = Math)]
fn random() -> f64;
#[wasm_bindgen(js_namespace = Math)]
fn log(a: f64) -> f64;
}
"#,
)
.file(
"test.js",
r#"
import * as wasm from "./out";
import * as assert from "assert";
export function test() {
wasm.get_random();
assert.strictEqual(wasm.do_log(1.0), Math.log(1.0));
}
"#,
)
.test();
}
#[test]
fn import_class() {
project()
.file(
"src/lib.rs",
r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./another")]
extern {
#[wasm_bindgen(js_namespace = Foo)]
fn bar();
}
#[wasm_bindgen]
pub fn baz() {
bar();
}
"#,
)
.file(
"test.js",
r#"
import { baz } from "./out";
import { called } from "./another";
import * as assert from "assert";
export function test() {
baz();
assert.strictEqual(called, true);
}
"#,
)
.file(
"another.js",
r#"
export let called = false;
export class Foo {
static bar() {
called = true;
}
}
"#,
)
.test();
}
#[test]
fn construct() {
project()
.file(
"src/lib.rs",
r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./another")]
extern {
type Foo;
#[wasm_bindgen(js_namespace = Foo)]
fn create() -> Foo;
#[wasm_bindgen(method)]
fn get_internal_string(this: &Foo) -> String;
#[wasm_bindgen(method)]
fn append_to_internal_string(this: &Foo, s: &str);
#[wasm_bindgen(method)]
fn assert_internal_string(this: &Foo, s: &str);
}
#[wasm_bindgen]
pub fn run() {
let f = Foo::create();
assert_eq!(f.get_internal_string(), "this");
f.append_to_internal_string(" foo");
f.assert_internal_string("this foo");
}
"#,
)
.file(
"test.js",
r#"
import { run } from "./out";
import { called } from "./another";
import * as assert from "assert";
export function test() {
run();
assert.strictEqual(called, true);
}
"#,
)
.file(
"another.js",
r#"
import * as assert from "assert";
export let called = false;
export class Foo {
static create() {
const ret = new Foo();
ret.internal_string = 'this';
return ret;
}
get_internal_string() {
return this.internal_string;
}
append_to_internal_string(s) {
this.internal_string += s;
}
assert_internal_string(s) {
assert.strictEqual(this.internal_string, s);
called = true;
}
}
Foo.internal_string = '';
"#,
)
.test();
}
#[test]
fn new_constructors() {
project()
.file(
"src/lib.rs",
r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./another")]
extern {
type Foo;
#[wasm_bindgen(constructor)]
fn new(arg: i32) -> Foo;
#[wasm_bindgen(method)]
fn get(this: &Foo) -> i32;
}
#[wasm_bindgen]
pub fn run() {
let f = Foo::new(1);
assert_eq!(f.get(), 2);
}
"#,
)
.file(
"test.js",
r#"
import { run } from "./out";
export function test() {
run();
}
"#,
)
.file(
"another.js",
r#"
export class Foo {
constructor(field) {
this.field = field;
}
get() {
return this.field + 1;
}
}
"#,
)
.test();
}
#[test]
fn switch_methods() {
project()
.file(
"src/lib.rs",
r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./another")]
extern {
type Foo;
#[wasm_bindgen(constructor)]
fn new() -> Foo;
#[wasm_bindgen(js_namespace = Foo)]
fn a();
#[wasm_bindgen(method)]
fn b(this: &Foo);
}
#[wasm_bindgen]
pub fn a() {
Foo::a();
}
#[wasm_bindgen]
pub fn b() {
Foo::new().b();
}
"#,
)
.file(
"test.js",
r#"
import { a, b } from "./out";
import { Foo, called } from "./another";
import * as assert from "assert";
export function test() {
assert.strictEqual(called.a, false);
a();
assert.strictEqual(called.a, true);
called.a = false;
Foo.a = function() {};
assert.strictEqual(called.a, false);
a();
assert.strictEqual(called.a, true);
called.a = false;
assert.strictEqual(called.a, false);
b();
assert.strictEqual(called.a, true);
called.a = false;
Foo.prototype.b = function() {};
assert.strictEqual(called.a, false);
b();
assert.strictEqual(called.a, true);
}
"#,
)
.file(
"another.js",
r#"
export let called = { a: false };
export class Foo {
constructor() {
}
static a() {
called.a = true;
}
b() {
called.a = true;
}
}
"#,
)
.test();
}
#[test]
fn properties() {
project()
.file(
"src/lib.rs",
r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./another")]
extern {
type Foo;
#[wasm_bindgen(constructor)]
fn new() -> Foo;
#[wasm_bindgen(getter, method)]
fn a(this: &Foo) -> i32;
#[wasm_bindgen(setter, method)]
fn set_a(this: &Foo, a: i32);
}
#[wasm_bindgen]
pub fn run() {
let a = Foo::new();
assert_eq!(a.a(), 1);
a.set_a(2);
assert_eq!(a.a(), 2);
}
"#,
)
.file(
"test.js",
r#"
import { run } from "./out";
export function test() {
run();
}
"#,
)
.file(
"another.js",
r#"
export class Foo {
constructor() {
this.num = 1;
}
get a() {
return this.num;
}
set a(val) {
this.num = val;
}
}
"#,
)
.test();
}
#[test]
fn rename_setter_getter() {
project()
.file(
"src/lib.rs",
r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./another")]
extern {
type Foo;
#[wasm_bindgen(constructor)]
fn new() -> Foo;
#[wasm_bindgen(getter = a, method)]
fn test(this: &Foo) -> i32;
#[wasm_bindgen(setter = a, method)]
fn another(this: &Foo, a: i32);
}
#[wasm_bindgen]
pub fn run() {
let a = Foo::new();
assert_eq!(a.test(), 1);
a.another(2);
assert_eq!(a.test(), 2);
}
"#,
)
.file(
"test.js",
r#"
import { run } from "./out";
export function test() {
run();
}
"#,
)
.file(
"another.js",
r#"
export class Foo {
constructor() {
this.num = 1;
}
get a() {
return this.num;
}
set a(val) {
this.num = val;
}
}
"#,
)
.test();
}