diff --git a/.travis.yml b/.travis.yml index f22b70d5..01326f24 100644 --- a/.travis.yml +++ b/.travis.yml @@ -46,6 +46,7 @@ matrix: - npm ci --verbose script: - cargo test --release + - cargo test --target wasm32-unknown-unknown # Check JS output from all tests against eslint - npm run run-lint-generated-tests # Check Examples against eslint diff --git a/Cargo.toml b/Cargo.toml index 125c83f8..1fab54cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,10 @@ wasm-bindgen-macro = { path = "crates/macro", version = "=0.2.15" } serde = { version = "1.0", optional = true } serde_json = { version = "1.0", optional = true } -[dev-dependencies] +[target.'cfg(target_arch = "wasm32")'.dev-dependencies] +wasm-bindgen-test = { path = 'crates/test', version = '=0.2.15' } + +[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] wasm-bindgen-test-project-builder = { path = "crates/test-project-builder", version = '=0.2.15' } [workspace] diff --git a/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs b/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs index c900cbcd..466334f4 100644 --- a/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs +++ b/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs @@ -80,9 +80,7 @@ fn rmain() -> Result<(), Error> { fs::create_dir(&tmpdir) .context("creating temporary directory")?; - let module = wasm_file_to_test.file_stem() - .and_then(|s| s.to_str()) - .ok_or_else(|| format_err!("invalid filename passed in"))?; + let module = "wasm-bindgen-test"; // Collect all tests that the test harness is supposed to run. We assume // that any exported function with the prefix `__wbg_test` is a test we need diff --git a/crates/cli/src/bin/wasm-bindgen-test-runner/node.rs b/crates/cli/src/bin/wasm-bindgen-test-runner/node.rs index 49895e22..48c8dfe0 100644 --- a/crates/cli/src/bin/wasm-bindgen-test-runner/node.rs +++ b/crates/cli/src/bin/wasm-bindgen-test-runner/node.rs @@ -76,6 +76,7 @@ pub fn execute(module: &str, tmpdir: &Path, args: &[OsString], tests: &[String]) let path = env::var("NODE_PATH").unwrap_or_default(); let mut path = env::split_paths(&path).collect::>(); path.push(env::current_dir().unwrap()); + path.push(tmpdir.to_path_buf()); exec( Command::new("node") .env("NODE_PATH", env::join_paths(&path).unwrap()) diff --git a/tests/all/api.rs b/tests/all/api.rs deleted file mode 100644 index 6de2739f..00000000 --- a/tests/all/api.rs +++ /dev/null @@ -1,233 +0,0 @@ -use super::project; - -#[test] -fn works() { - project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn foo() -> JsValue { - JsValue::from("foo") - } - - #[wasm_bindgen] - pub fn bar(s: &str) -> JsValue { - JsValue::from(s) - } - - #[wasm_bindgen] - pub fn baz() -> JsValue { - JsValue::from(1.0) - } - - #[wasm_bindgen] - pub fn baz2(a: &JsValue, b: &JsValue) { - assert_eq!(a.as_f64(), Some(2.0)); - assert_eq!(b.as_f64(), None); - } - - #[wasm_bindgen] - pub fn js_null() -> JsValue { - JsValue::null() - } - - #[wasm_bindgen] - pub fn js_undefined() -> JsValue { - JsValue::undefined() - } - - #[wasm_bindgen] - pub fn test_is_null_undefined( - a: &JsValue, - b: &JsValue, - c: &JsValue, - ) { - assert!(a.is_null()); - assert!(!a.is_undefined()); - - assert!(!b.is_null()); - assert!(b.is_undefined()); - - assert!(!c.is_null()); - assert!(!c.is_undefined()); - } - - #[wasm_bindgen] - pub fn get_true() -> JsValue { - JsValue::from(true) - } - - #[wasm_bindgen] - pub fn get_false() -> JsValue { - JsValue::from(false) - } - - #[wasm_bindgen] - pub fn test_bool( - a: &JsValue, - b: &JsValue, - c: &JsValue, - ) { - assert_eq!(a.as_bool(), Some(true)); - assert_eq!(format!("{:?}", a), "true"); - assert_eq!(b.as_bool(), Some(false)); - assert_eq!(c.as_bool(), None); - } - - #[wasm_bindgen] - pub fn mk_symbol() -> JsValue { - let a = JsValue::symbol(None); - assert!(a.is_symbol()); - assert_eq!(format!("{:?}", a), "Symbol(..)"); - return a - } - - #[wasm_bindgen] - pub fn mk_symbol2(s: &str) -> JsValue { - let a = JsValue::symbol(Some(s)); - assert!(a.is_symbol()); - return a - } - - #[wasm_bindgen] - pub fn assert_symbols(a: &JsValue, b: &JsValue) { - assert!(a.is_symbol()); - assert!(!b.is_symbol()); - } - - #[wasm_bindgen] - pub fn acquire_string(a: &JsValue, b: &JsValue) { - assert_eq!(a.as_string().unwrap(), "foo"); - assert_eq!(format!("{:?}", a), "\"foo\""); - assert_eq!(b.as_string(), None); - } - - #[wasm_bindgen] - pub fn acquire_string2(a: &JsValue) -> String { - a.as_string().unwrap_or("wrong".to_string()) - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - assert.strictEqual(wasm.foo(), 'foo'); - assert.strictEqual(wasm.bar('a'), 'a'); - assert.strictEqual(wasm.baz(), 1); - wasm.baz2(2, 'a'); - - assert.strictEqual(wasm.js_null(), null); - assert.strictEqual(wasm.js_undefined(), undefined); - - wasm.test_is_null_undefined(null, undefined, 1.0); - - assert.strictEqual(wasm.get_true(), true); - assert.strictEqual(wasm.get_false(), false); - wasm.test_bool(true, false, 1.0); - - assert.strictEqual(typeof(wasm.mk_symbol()), 'symbol'); - assert.strictEqual(typeof(wasm.mk_symbol2('a')), 'symbol'); - assert.strictEqual(Symbol.keyFor(wasm.mk_symbol()), undefined); - assert.strictEqual(Symbol.keyFor(wasm.mk_symbol2('b')), undefined); - - wasm.assert_symbols(Symbol(), 'a'); - wasm.acquire_string('foo', null) - assert.strictEqual(wasm.acquire_string2(''), ''); - assert.strictEqual(wasm.acquire_string2('a'), 'a'); - } - "#, - ) - .test(); -} - -#[test] -fn eq_works() { - project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn test(a: &JsValue, b: &JsValue) -> bool { - a == b - } - - #[wasm_bindgen] - pub fn test1(a: &JsValue) -> bool { - a == a - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - assert.strictEqual(wasm.test('a', 'a'), true); - assert.strictEqual(wasm.test('a', 'b'), false); - assert.strictEqual(wasm.test(NaN, NaN), false); - assert.strictEqual(wasm.test({a: 'a'}, {a: 'a'}), false); - assert.strictEqual(wasm.test1(NaN), false); - let x = {a: 'a'}; - assert.strictEqual(wasm.test(x, x), true); - assert.strictEqual(wasm.test1(x), true); - } - "#, - ) - .test(); -} - -#[test] -fn null_keeps_working() { - project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - - use wasm_bindgen::prelude::*; - - #[wasm_bindgen(module = "./foo")] - extern { - fn take_null(val: JsValue); - } - - #[wasm_bindgen] - pub fn test() { - take_null(JsValue::null()); - take_null(JsValue::null()); - } - "#, - ) - .file( - "foo.js", - r#" - import { strictEqual } from "assert"; - - export function take_null(n) { - strictEqual(n, null); - } - "#, - ) - .test(); -} diff --git a/tests/all/main.rs b/tests/all/main.rs index ec6592b6..ea2f150b 100644 --- a/tests/all/main.rs +++ b/tests/all/main.rs @@ -1,8 +1,9 @@ +#![cfg(not(target_arch = "wasm32"))] + extern crate wasm_bindgen_test_project_builder as project_builder; use project_builder::{project, run}; -mod api; mod char; mod classes; mod closures; diff --git a/tests/wasm/api.js b/tests/wasm/api.js new file mode 100644 index 00000000..5f723f71 --- /dev/null +++ b/tests/wasm/api.js @@ -0,0 +1,43 @@ +const assert = require('assert'); +const wasm = require('wasm-bindgen-test.js'); + +exports.test_works = function() { + assert.strictEqual(wasm.api_foo(), 'foo'); + assert.strictEqual(wasm.api_bar('a'), 'a'); + assert.strictEqual(wasm.api_baz(), 1); + wasm.api_baz2(2, 'a'); + + assert.strictEqual(wasm.api_js_null(), null); + assert.strictEqual(wasm.api_js_undefined(), undefined); + + wasm.api_test_is_null_undefined(null, undefined, 1.0); + + assert.strictEqual(wasm.api_get_true(), true); + assert.strictEqual(wasm.api_get_false(), false); + wasm.api_test_bool(true, false, 1.0); + + assert.strictEqual(typeof(wasm.api_mk_symbol()), 'symbol'); + assert.strictEqual(typeof(wasm.api_mk_symbol2('a')), 'symbol'); + assert.strictEqual(Symbol.keyFor(wasm.api_mk_symbol()), undefined); + assert.strictEqual(Symbol.keyFor(wasm.api_mk_symbol2('b')), undefined); + + wasm.api_assert_symbols(Symbol(), 'a'); + wasm.api_acquire_string('foo', null); + assert.strictEqual(wasm.api_acquire_string2(''), ''); + assert.strictEqual(wasm.api_acquire_string2('a'), 'a'); +}; + +exports.test_eq_works = function() { + assert.strictEqual(wasm.eq_test('a', 'a'), true); + assert.strictEqual(wasm.eq_test('a', 'b'), false); + assert.strictEqual(wasm.eq_test(NaN, NaN), false); + assert.strictEqual(wasm.eq_test({a: 'a'}, {a: 'a'}), false); + assert.strictEqual(wasm.eq_test1(NaN), false); + let x = {a: 'a'}; + assert.strictEqual(wasm.eq_test(x, x), true); + assert.strictEqual(wasm.eq_test1(x), true); +}; + +exports.assert_null = function(x) { + assert.strictEqual(x, null); +}; diff --git a/tests/wasm/api.rs b/tests/wasm/api.rs new file mode 100644 index 00000000..0b6ab5e1 --- /dev/null +++ b/tests/wasm/api.rs @@ -0,0 +1,137 @@ +use wasm_bindgen_test::*; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen(module = "tests/wasm/api.js", version = "*")] +extern { + fn test_works(); + fn test_eq_works(); + fn assert_null(v: JsValue); +} + +#[wasm_bindgen_test] +fn works() { + test_works(); +} + +#[wasm_bindgen] +pub fn api_foo() -> JsValue { + JsValue::from("foo") +} + +#[wasm_bindgen] +pub fn api_bar(s: &str) -> JsValue { + JsValue::from(s) +} + +#[wasm_bindgen] +pub fn api_baz() -> JsValue { + JsValue::from(1.0) +} + +#[wasm_bindgen] +pub fn api_baz2(a: &JsValue, b: &JsValue) { + assert_eq!(a.as_f64(), Some(2.0)); + assert_eq!(b.as_f64(), None); +} + +#[wasm_bindgen] +pub fn api_js_null() -> JsValue { + JsValue::null() +} + +#[wasm_bindgen] +pub fn api_js_undefined() -> JsValue { + JsValue::undefined() +} + +#[wasm_bindgen] +pub fn api_test_is_null_undefined( + a: &JsValue, + b: &JsValue, + c: &JsValue, +) { + assert!(a.is_null()); + assert!(!a.is_undefined()); + + assert!(!b.is_null()); + assert!(b.is_undefined()); + + assert!(!c.is_null()); + assert!(!c.is_undefined()); +} + +#[wasm_bindgen] +pub fn api_get_true() -> JsValue { + JsValue::from(true) +} + +#[wasm_bindgen] +pub fn api_get_false() -> JsValue { + JsValue::from(false) +} + +#[wasm_bindgen] +pub fn api_test_bool( + a: &JsValue, + b: &JsValue, + c: &JsValue, +) { + assert_eq!(a.as_bool(), Some(true)); + assert_eq!(format!("{:?}", a), "true"); + assert_eq!(b.as_bool(), Some(false)); + assert_eq!(c.as_bool(), None); +} + +#[wasm_bindgen] +pub fn api_mk_symbol() -> JsValue { + let a = JsValue::symbol(None); + assert!(a.is_symbol()); + assert_eq!(format!("{:?}", a), "Symbol(..)"); + return a +} + +#[wasm_bindgen] +pub fn api_mk_symbol2(s: &str) -> JsValue { + let a = JsValue::symbol(Some(s)); + assert!(a.is_symbol()); + return a +} + +#[wasm_bindgen] +pub fn api_assert_symbols(a: &JsValue, b: &JsValue) { + assert!(a.is_symbol()); + assert!(!b.is_symbol()); +} + +#[wasm_bindgen] +pub fn api_acquire_string(a: &JsValue, b: &JsValue) { + assert_eq!(a.as_string().unwrap(), "foo"); + assert_eq!(format!("{:?}", a), "\"foo\""); + assert_eq!(b.as_string(), None); +} + +#[wasm_bindgen] +pub fn api_acquire_string2(a: &JsValue) -> String { + a.as_string().unwrap_or("wrong".to_string()) +} + +#[wasm_bindgen_test] +fn eq_works() { + test_eq_works(); +} + +#[wasm_bindgen] +pub fn eq_test(a: &JsValue, b: &JsValue) -> bool { + a == b +} + +#[wasm_bindgen] +pub fn eq_test1(a: &JsValue) -> bool { + a == a +} + +#[wasm_bindgen_test] +fn null_keeps_working() { + assert_null(JsValue::null()); + assert_null(JsValue::null()); +} diff --git a/tests/wasm/main.rs b/tests/wasm/main.rs new file mode 100644 index 00000000..0040850e --- /dev/null +++ b/tests/wasm/main.rs @@ -0,0 +1,7 @@ +#![cfg(target_arch = "wasm32")] +#![feature(use_extern_macros)] + +extern crate wasm_bindgen_test; +extern crate wasm_bindgen; + +pub mod api;