wasm-bindgen/tests/all/js_globals/Error.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

198 lines
5.3 KiB
Rust

#![allow(non_snake_case)]
use project;
#[test]
fn new() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::js;
use wasm_bindgen::js::Error;
#[wasm_bindgen]
pub fn new_error(message: &js::JsString) -> Error {
Error::new(message)
}
"#)
.file("test.js", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const message = 'any error message';
const error = wasm.new_error(message);
assert.equal(error.message, message);
}
"#)
.test()
}
#[test]
fn message() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::js;
use wasm_bindgen::js::Error;
#[wasm_bindgen]
pub fn error_message(this: &Error) -> js::JsString {
this.message()
}
"#)
.file("test.js", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const message = 'any error message';
const error = new Error(message);
assert.equal(wasm.error_message(error), message);
}
"#)
.test()
}
#[test]
fn set_message() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::js;
use wasm_bindgen::js::Error;
#[wasm_bindgen]
pub fn error_set_message(this: &Error, message: &js::JsString) {
this.set_message(message);
}
"#)
.file("test.js", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const message = 'any error message';
const error = new Error();
wasm.error_set_message(error, message);
assert.equal(error.message, message);
}
"#)
.test()
}
#[test]
fn name() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::js;
use wasm_bindgen::js::Error;
#[wasm_bindgen]
pub fn error_name(this: &Error) -> js::JsString {
this.name()
}
"#)
.file("test.js", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const name = 'any error name';
const error = new Error();
error.name = name;
assert.equal(wasm.error_name(error), name);
}
"#)
.test()
}
#[test]
fn set_name() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::js;
use wasm_bindgen::js::Error;
#[wasm_bindgen]
pub fn error_set_name(this: &Error, name: &js::JsString) {
this.set_name(name);
}
"#)
.file("test.js", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const name = 'any error name';
const error = new Error();
wasm.error_set_name(error, name);
assert.equal(error.name, name);
}
"#)
.test()
}
#[test]
fn to_string() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::js;
use wasm_bindgen::js::Error;
#[wasm_bindgen]
pub fn error_to_string(this: &Error) -> js::JsString {
this.to_string()
}
"#)
.file("test.js", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const error = new Error('error message 1');
assert.equal(wasm.error_to_string(error), 'Error: error message 1');
error.name = undefined;
assert.equal(wasm.error_to_string(error), 'Error: error message 1');
error.name = 'error_name_1';
assert.equal(wasm.error_to_string(error), 'error_name_1: error message 1');
error.message = undefined;
assert.equal(wasm.error_to_string(error), 'error_name_1');
error.name = 'error_name_2';
assert.equal(wasm.error_to_string(error), 'error_name_2');
}
"#)
.test()
}