mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-25 10:41:34 +00:00
Add a test harness to directly execute wasm tests (#524)
* Add a test harness to directly execute wasm tests This commits adds a few new crates and infrastructure to enable comands like: cargo test --target wasm32-unknown-unknown The intention here is to make it as low-friction as possible to write wasm tests and also have them execute in a reasonable amount of time. Eventually this is also hopefully enough support to do things like headless testing! For now though this is defintely MVP status rather than fully fleshed out. There's some more information at `crates/test/README.md` about how it works and how to use it, but for now this is mainly intended to play around with locally in this repository for our own tests. * Port a numbe of `js-sys` tests to the new test framework This commit ports a number of existing tests for the `js-sys` crate over to the new test framework created in the previous commit, showing off how they can be executed as well as drastictlly simplifying the tests themselves! This is intended to be a proof of concept for now which we can refine over time. This should also show off that it's possible to incrementally move over to the new test framework.
This commit is contained in:
@ -1,114 +0,0 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use super::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::ArrayBuffer;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn new_arraybuffer() -> ArrayBuffer {
|
||||
ArrayBuffer::new(42)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(typeof wasm.new_arraybuffer(), "object");
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_view() {
|
||||
project()
|
||||
.file("src/lib.rs", r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use JsValue;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use js_sys::ArrayBuffer;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn is_view(value: JsValue) -> bool {
|
||||
ArrayBuffer::is_view(value)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.is_view(new Uint8Array(42)), true);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slice() {
|
||||
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::ArrayBuffer;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn slice(arraybuffer: &ArrayBuffer, begin: u32) -> ArrayBuffer {
|
||||
arraybuffer.slice(begin)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const arraybuffer = new ArrayBuffer(4);
|
||||
assert.equal(typeof wasm.slice(arraybuffer, 2), "object");
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slice_with_end() {
|
||||
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::ArrayBuffer;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn slice_with_end(arraybuffer: &ArrayBuffer, begin: u32, end: u32) -> ArrayBuffer {
|
||||
arraybuffer.slice_with_end(begin, end)
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const arraybuffer = new ArrayBuffer(4);
|
||||
assert.equal(typeof wasm.slice_with_end(arraybuffer, 1, 2), "object");
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
@ -1,84 +1,6 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use project;
|
||||
|
||||
#[test]
|
||||
fn keys() {
|
||||
project()
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn get_keys(this: &js_sys::Array) -> js_sys::ArrayIterator {
|
||||
this.keys()
|
||||
}
|
||||
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let numbers = [8, 5, 4, 3, 1, 2];
|
||||
let iterator = numbers.keys();
|
||||
let wasmIterator = wasm.get_keys(numbers);
|
||||
|
||||
assert.equal(iterator.toString(), wasmIterator.toString());
|
||||
assert.equal(Array.from(iterator)[0], Array.from(wasmIterator)[0]);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entries() {
|
||||
project()
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn get_entries(this: &js_sys::Array) -> js_sys::ArrayIterator {
|
||||
this.entries()
|
||||
}
|
||||
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let numbers = [8, 5, 4, 3, 1, 2];
|
||||
let iterator = numbers.entries();
|
||||
let wasmIterator = wasm.get_entries(numbers);
|
||||
let jsItem = iterator.next();
|
||||
let wasmItem = wasmIterator.next();
|
||||
|
||||
assert.equal(iterator.toString(), wasmIterator.toString());
|
||||
assert.equal(jsItem.value[1], wasmItem.value[1]);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
use super::project;
|
||||
|
||||
#[test]
|
||||
fn values() {
|
||||
|
@ -1,67 +0,0 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use project;
|
||||
|
||||
#[test]
|
||||
fn new_undefined() {
|
||||
project()
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn new_boolean() -> js_sys::Boolean {
|
||||
js_sys::Boolean::new(JsValue::undefined())
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.new_boolean().valueOf(), false);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_truely() {
|
||||
project()
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
extern crate js_sys;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn new_boolean() -> js_sys::Boolean {
|
||||
js_sys::Boolean::new(JsValue::from("foo"))
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.equal(wasm.new_boolean().valueOf(), true);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use super::project;
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
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::{ArrayBuffer, DataView};
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn test_data_view(buffer: &ArrayBuffer, offset: usize, len: usize) {
|
||||
let v = DataView::new(buffer, offset, len);
|
||||
assert_eq!(v.byte_offset(), offset);
|
||||
assert_eq!(v.byte_length(), len);
|
||||
assert_eq!(v.get_int8(0), 2);
|
||||
assert_eq!(v.get_uint8(0), 2);
|
||||
|
||||
v.set_int8(0, 42);
|
||||
assert_eq!(v.get_int8(0), 42);
|
||||
v.set_uint8(0, 255);
|
||||
assert_eq!(v.get_uint8(0), 255);
|
||||
v.set_int16(0, 32767);
|
||||
assert_eq!(v.get_int16(0), 32767);
|
||||
v.set_uint16(0, 65535);
|
||||
assert_eq!(v.get_uint16(0), 65535);
|
||||
v.set_int32(0, 123456789);
|
||||
assert_eq!(v.get_int32(0), 123456789);
|
||||
v.set_uint32(0, 3_123_456_789);
|
||||
assert_eq!(v.get_uint32(0), 3_123_456_789);
|
||||
v.set_float32(0, 100.123);
|
||||
assert_eq!(v.get_float32(0), 100.123);
|
||||
v.set_float64(0, 123456789.123456);
|
||||
assert_eq!(v.get_float64(0), 123456789.123456);
|
||||
|
||||
v.set_int8(0, 42);
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const bytes = new Int8Array(10);
|
||||
bytes[2] = 2;
|
||||
wasm.test_data_view(bytes.buffer, 2, 8);
|
||||
assert.equal(bytes[2], 42);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
#![cfg(not(target_arch = "wasm32"))]
|
||||
|
||||
extern crate wasm_bindgen_test_project_builder as project_builder;
|
||||
|
||||
fn project() -> project_builder::Project {
|
||||
@ -9,10 +11,7 @@ fn project() -> project_builder::Project {
|
||||
// Keep these tests in alphabetical order, just like the imports in `src/js.rs`.
|
||||
|
||||
mod Array;
|
||||
mod ArrayBuffer;
|
||||
mod ArrayIterator;
|
||||
mod Boolean;
|
||||
mod DataView;
|
||||
mod Date;
|
||||
mod Error;
|
||||
mod Function;
|
||||
|
30
crates/js-sys/tests/wasm/ArrayBuffer.rs
Normal file
30
crates/js-sys/tests/wasm/ArrayBuffer.rs
Normal file
@ -0,0 +1,30 @@
|
||||
use wasm_bindgen::JsValue;
|
||||
use wasm_bindgen_test::*;
|
||||
use js_sys::*;
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn new() {
|
||||
let x = ArrayBuffer::new(42);
|
||||
let y: JsValue = x.into();
|
||||
assert!(y.is_object());
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn is_view() {
|
||||
let x = Uint8Array::new(JsValue::from(42));
|
||||
assert!(ArrayBuffer::is_view(JsValue::from(x)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slice() {
|
||||
let buf = ArrayBuffer::new(4);
|
||||
let slice = buf.slice(2);
|
||||
assert!(JsValue::from(slice).is_object());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slice_with_end() {
|
||||
let buf = ArrayBuffer::new(4);
|
||||
let slice = buf.slice_with_end(1, 2);
|
||||
assert!(JsValue::from(slice).is_object());
|
||||
}
|
39
crates/js-sys/tests/wasm/ArrayIterator.rs
Normal file
39
crates/js-sys/tests/wasm/ArrayIterator.rs
Normal file
@ -0,0 +1,39 @@
|
||||
use wasm_bindgen::JsValue;
|
||||
use wasm_bindgen_test::*;
|
||||
use js_sys::*;
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn keys() {
|
||||
let array = Array::new();
|
||||
array.push(JsValue::from(1));
|
||||
array.push(JsValue::from(2));
|
||||
array.push(JsValue::from(3));
|
||||
array.push(JsValue::from(4));
|
||||
array.push(JsValue::from(5));
|
||||
|
||||
let new_array = Array::from(array.keys().into());
|
||||
|
||||
let mut result = Vec::new();
|
||||
new_array.for_each(&mut |i, _, _| result.push(i.as_f64().unwrap()));
|
||||
assert_eq!(result, [0.0, 1.0, 2.0, 3.0, 4.0]);
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn entries() {
|
||||
let array = Array::new();
|
||||
array.push(JsValue::from(1));
|
||||
array.push(JsValue::from(2));
|
||||
array.push(JsValue::from(3));
|
||||
array.push(JsValue::from(4));
|
||||
array.push(JsValue::from(5));
|
||||
|
||||
let new_array = Array::from(array.entries().into());
|
||||
|
||||
new_array.for_each(&mut |a, i, _| {
|
||||
assert!(a.is_object());
|
||||
let array: Array = a.into();
|
||||
assert_eq!(array.shift().as_f64().unwrap(), i as f64);
|
||||
assert_eq!(array.shift().as_f64().unwrap(), (i + 1) as f64);
|
||||
assert_eq!(array.length(), 0);
|
||||
});
|
||||
}
|
13
crates/js-sys/tests/wasm/Boolean.rs
Normal file
13
crates/js-sys/tests/wasm/Boolean.rs
Normal file
@ -0,0 +1,13 @@
|
||||
use wasm_bindgen::JsValue;
|
||||
use wasm_bindgen_test::*;
|
||||
use js_sys::*;
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn new_undefined() {
|
||||
assert_eq!(Boolean::new(JsValue::undefined()).value_of(), false);
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn new_truely() {
|
||||
assert_eq!(Boolean::new(JsValue::from("foo")).value_of(), true);
|
||||
}
|
39
crates/js-sys/tests/wasm/DataView.rs
Normal file
39
crates/js-sys/tests/wasm/DataView.rs
Normal file
@ -0,0 +1,39 @@
|
||||
use wasm_bindgen::JsValue;
|
||||
use wasm_bindgen_test::*;
|
||||
use js_sys::*;
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn test() {
|
||||
let bytes = Int8Array::new(JsValue::from(10));
|
||||
|
||||
// TODO: figure out how to do `bytes[2] = 2`
|
||||
bytes.subarray(2, 3).fill(2, 0, 1);
|
||||
|
||||
let v = DataView::new(&bytes.buffer(), 2, 8);
|
||||
assert_eq!(v.byte_offset(), 2);
|
||||
assert_eq!(v.byte_length(), 8);
|
||||
assert_eq!(v.get_int8(0), 2);
|
||||
assert_eq!(v.get_uint8(0), 2);
|
||||
|
||||
v.set_int8(0, 42);
|
||||
assert_eq!(v.get_int8(0), 42);
|
||||
v.set_uint8(0, 255);
|
||||
assert_eq!(v.get_uint8(0), 255);
|
||||
v.set_int16(0, 32767);
|
||||
assert_eq!(v.get_int16(0), 32767);
|
||||
v.set_uint16(0, 65535);
|
||||
assert_eq!(v.get_uint16(0), 65535);
|
||||
v.set_int32(0, 123456789);
|
||||
assert_eq!(v.get_int32(0), 123456789);
|
||||
v.set_uint32(0, 3_123_456_789);
|
||||
assert_eq!(v.get_uint32(0), 3_123_456_789);
|
||||
v.set_float32(0, 100.123);
|
||||
assert_eq!(v.get_float32(0), 100.123);
|
||||
v.set_float64(0, 123456789.123456);
|
||||
assert_eq!(v.get_float64(0), 123456789.123456);
|
||||
|
||||
v.set_int8(0, 42);
|
||||
|
||||
// TODO: figure out how to do `bytes[2]`
|
||||
bytes.subarray(2, 3).for_each(&mut |x, _, _| assert_eq!(x, 42));
|
||||
}
|
12
crates/js-sys/tests/wasm/main.rs
Normal file
12
crates/js-sys/tests/wasm/main.rs
Normal file
@ -0,0 +1,12 @@
|
||||
#![cfg(target_arch = "wasm32")]
|
||||
#![feature(use_extern_macros, wasm_import_module)]
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
extern crate js_sys;
|
||||
extern crate wasm_bindgen;
|
||||
extern crate wasm_bindgen_test;
|
||||
|
||||
pub mod ArrayBuffer;
|
||||
pub mod ArrayIterator;
|
||||
pub mod Boolean;
|
||||
pub mod DataView;
|
Reference in New Issue
Block a user