Port Error tests to wasm

This commit is contained in:
Alex Crichton 2018-07-20 12:21:41 -07:00
parent cc8e7edd83
commit 20e9267775
4 changed files with 38 additions and 198 deletions

View File

@ -1,197 +0,0 @@
#![allow(non_snake_case)]
use project;
#[test]
fn new() {
project()
.file("src/lib.rs", r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
use js_sys::Error;
#[wasm_bindgen]
pub fn new_error(message: &js_sys::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(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
use js_sys::Error;
#[wasm_bindgen]
pub fn error_message(this: &Error) -> js_sys::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(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
use js_sys::Error;
#[wasm_bindgen]
pub fn error_set_message(this: &Error, message: &js_sys::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(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
use js_sys::Error;
#[wasm_bindgen]
pub fn error_name(this: &Error) -> js_sys::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(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
use js_sys::Error;
#[wasm_bindgen]
pub fn error_set_name(this: &Error, name: &js_sys::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(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
use js_sys::Error;
#[wasm_bindgen]
pub fn error_to_string(this: &Error) -> js_sys::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()
}

View File

@ -11,7 +11,6 @@ fn project() -> project_builder::Project {
// Keep these tests in alphabetical order, just like the imports in `src/js.rs`.
mod ArrayIterator;
mod Error;
mod Function;
mod Generator;
mod Intl;

View File

@ -0,0 +1,37 @@
use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;
use js_sys::*;
#[wasm_bindgen_test]
fn new() {
let error = Error::new(&"some message".into());
assert_eq!(JsValue::from(error.message()), "some message");
}
#[wasm_bindgen_test]
fn set_message() {
let error = Error::new(&"test".into());
error.set_message(&"another".into());
assert_eq!(JsValue::from(error.message()), "another");
}
#[wasm_bindgen_test]
fn name() {
let error = Error::new(&"test".into());
assert_eq!(JsValue::from(error.name()), "Error");
}
#[wasm_bindgen_test]
fn set_name() {
let error = Error::new(&"test".into());
error.set_name(&"different".into());
assert_eq!(JsValue::from(error.name()), "different");
}
#[wasm_bindgen_test]
fn to_string() {
let error = Error::new(&"error message 1".into());
assert_eq!(JsValue::from(error.to_string()), "Error: error message 1");
error.set_name(&"error_name_1".into());
assert_eq!(JsValue::from(error.to_string()), "error_name_1: error message 1");
}

View File

@ -12,3 +12,4 @@ pub mod ArrayIterator;
pub mod Boolean;
pub mod DataView;
pub mod Date;
pub mod Error;