From efd6b2abac8b511d4327a042f3ed7320d9482081 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 20 Jul 2018 11:34:54 -0700 Subject: [PATCH] Migrate Array tests to `wasm-bindgen-test` --- crates/js-sys/src/lib.rs | 160 ++++- crates/js-sys/tests/all/Array.rs | 1018 ----------------------------- crates/js-sys/tests/all/main.rs | 1 - crates/js-sys/tests/wasm/Array.rs | 279 ++++++++ crates/js-sys/tests/wasm/main.rs | 1 + src/lib.rs | 43 ++ 6 files changed, 461 insertions(+), 1041 deletions(-) delete mode 100644 crates/js-sys/tests/all/Array.rs create mode 100644 crates/js-sys/tests/wasm/Array.rs diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index fc1a8ef6..9b74ce08 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -174,7 +174,7 @@ extern "C" { /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex #[wasm_bindgen(method, js_name = findIndex)] - pub fn find_index(this: &Array, predicate: &mut FnMut(JsValue, u32, Array) -> bool) -> u32; + pub fn find_index(this: &Array, predicate: &mut FnMut(JsValue, u32, Array) -> bool) -> i32; /// The `forEach()` method executes a provided function once for each array element. /// @@ -610,6 +610,23 @@ extern "C" { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill #[wasm_bindgen(method)] pub fn fill(this: &Float32Array, value: f32, start: u32, end: u32) -> Float32Array; + + /// The `buffer` accessor property represents the `ArrayBuffer` referenced + /// by a `TypedArray` at construction time. + #[wasm_bindgen(getter, method)] + pub fn buffer(this: &Float32Array) -> ArrayBuffer; + + /// The `subarray()` method stores multiple values in the typed array, + /// reading input values from a specified array. + #[wasm_bindgen(method)] + pub fn subarray(this: &Float32Array, begin: u32, end: u32) -> Float32Array; + + /// The `forEach()` method executes a provided function once per array + /// element. This method has the same algorithm as + /// `Array.prototype.forEach()`. `TypedArray` is one of the typed array + /// types here. + #[wasm_bindgen(method, js_name = forEach)] + pub fn for_each(this: &Float32Array, callback: &mut FnMut(f32, u32, Float32Array)); } // Float64Array @@ -629,6 +646,23 @@ extern "C" { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill #[wasm_bindgen(method)] pub fn fill(this: &Float64Array, value: f64, start: u32, end: u32) -> Float64Array; + + /// The `buffer` accessor property represents the `ArrayBuffer` referenced + /// by a `TypedArray` at construction time. + #[wasm_bindgen(getter, method)] + pub fn buffer(this: &Float64Array) -> ArrayBuffer; + + /// The `subarray()` method stores multiple values in the typed array, + /// reading input values from a specified array. + #[wasm_bindgen(method)] + pub fn subarray(this: &Float64Array, begin: u64, end: u64) -> Float64Array; + + /// The `forEach()` method executes a provided function once per array + /// element. This method has the same algorithm as + /// `Array.prototype.forEach()`. `TypedArray` is one of the typed array + /// types here. + #[wasm_bindgen(method, js_name = forEach)] + pub fn for_each(this: &Float64Array, callback: &mut FnMut(f64, u64, Float64Array)); } // Function @@ -735,26 +769,6 @@ extern "C" { #[wasm_bindgen(getter, method)] pub fn buffer(this: &Int8Array) -> ArrayBuffer; - // /// The `byteLength` accessor property represents the length (in bytes) of a - // /// typed array. - // #[wasm_bindgen(getter, method, js_name = byteLength)] - // pub fn byte_length(this: &Int8Array) -> u32; - // - // /// The `byteOffset` accessor property represents the offset (in bytes) of a - // /// typed array from the start of its `ArrayBuffer`. - // #[wasm_bindgen(getter, method, js_name = byteOffset)] - // pub fn byte_offset(this: &Int8Array) -> u32; - // - // /// The `length` accessor property represents the length (in elements) of a - // /// typed array. - // #[wasm_bindgen(getter, method)] - // pub fn length(this: &Int8Array) -> u32; - // - // /// The `set()` method stores multiple values in the typed array, reading - // /// input values from a specified array. - // #[wasm_bindgen(method)] - // pub fn set(this: &Int8Array, value: &JsValue, offset: u32); - /// The `subarray()` method stores multiple values in the typed array, /// reading input values from a specified array. #[wasm_bindgen(method)] @@ -785,6 +799,23 @@ extern "C" { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill #[wasm_bindgen(method)] pub fn fill(this: &Int16Array, value: i16, start: u32, end: u32) -> Int16Array; + + /// The `buffer` accessor property represents the `ArrayBuffer` referenced + /// by a `TypedArray` at construction time. + #[wasm_bindgen(getter, method)] + pub fn buffer(this: &Int16Array) -> ArrayBuffer; + + /// The `subarray()` method stores multiple values in the typed array, + /// reading input values from a specified array. + #[wasm_bindgen(method)] + pub fn subarray(this: &Int16Array, begin: u32, end: u32) -> Int16Array; + + /// The `forEach()` method executes a provided function once per array + /// element. This method has the same algorithm as + /// `Array.prototype.forEach()`. `TypedArray` is one of the typed array + /// types here. + #[wasm_bindgen(method, js_name = forEach)] + pub fn for_each(this: &Int16Array, callback: &mut FnMut(i16, u32, Int16Array)); } // Int32Array @@ -804,6 +835,23 @@ extern "C" { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill #[wasm_bindgen(method)] pub fn fill(this: &Int32Array, value: i32, start: u32, end: u32) -> Int32Array; + + /// The `buffer` accessor property represents the `ArrayBuffer` referenced + /// by a `TypedArray` at construction time. + #[wasm_bindgen(getter, method)] + pub fn buffer(this: &Int32Array) -> ArrayBuffer; + + /// The `subarray()` method stores multiple values in the typed array, + /// reading input values from a specified array. + #[wasm_bindgen(method)] + pub fn subarray(this: &Int32Array, begin: u32, end: u32) -> Int32Array; + + /// The `forEach()` method executes a provided function once per array + /// element. This method has the same algorithm as + /// `Array.prototype.forEach()`. `TypedArray` is one of the typed array + /// types here. + #[wasm_bindgen(method, js_name = forEach)] + pub fn for_each(this: &Int32Array, callback: &mut FnMut(i32, u32, Int32Array)); } // Map @@ -1340,7 +1388,7 @@ extern "C" { /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date #[wasm_bindgen(constructor)] - pub fn new() -> Date; + pub fn new(init: JsValue) -> Date; /// The `Date.now()` method returns the number of milliseconds /// elapsed since January 1, 1970 00:00:00 UTC. @@ -1913,6 +1961,23 @@ extern "C" { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill #[wasm_bindgen(method)] pub fn fill(this: &Uint8Array, value: u8, start: u32, end: u32) -> Uint8Array; + + /// The `buffer` accessor property represents the `ArrayBuffer` referenced + /// by a `TypedArray` at construction time. + #[wasm_bindgen(getter, method)] + pub fn buffer(this: &Uint8Array) -> ArrayBuffer; + + /// The `subarray()` method stores multiple values in the typed array, + /// reading input values from a specified array. + #[wasm_bindgen(method)] + pub fn subarray(this: &Uint8Array, begin: u32, end: u32) -> Uint8Array; + + /// The `forEach()` method executes a provided function once per array + /// element. This method has the same algorithm as + /// `Array.prototype.forEach()`. `TypedArray` is one of the typed array + /// types here. + #[wasm_bindgen(method, js_name = forEach)] + pub fn for_each(this: &Uint8Array, callback: &mut FnMut(u8, u32, Uint8Array)); } // Uint8ClampedArray @@ -1934,6 +1999,23 @@ extern "C" { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill #[wasm_bindgen(method)] pub fn fill(this: &Uint8ClampedArray, value: u8, start: u32, end: u32) -> Uint8ClampedArray; + + /// The `buffer` accessor property represents the `ArrayBuffer` referenced + /// by a `TypedArray` at construction time. + #[wasm_bindgen(getter, method)] + pub fn buffer(this: &Uint8ClampedArray) -> ArrayBuffer; + + /// The `subarray()` method stores multiple values in the typed array, + /// reading input values from a specified array. + #[wasm_bindgen(method)] + pub fn subarray(this: &Uint8ClampedArray, begin: u32, end: u32) -> Uint8ClampedArray; + + /// The `forEach()` method executes a provided function once per array + /// element. This method has the same algorithm as + /// `Array.prototype.forEach()`. `TypedArray` is one of the typed array + /// types here. + #[wasm_bindgen(method, js_name = forEach)] + pub fn for_each(this: &Uint8ClampedArray, callback: &mut FnMut(u8, u32, Uint8ClampedArray)); } // Uint16Array @@ -1953,6 +2035,23 @@ extern "C" { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill #[wasm_bindgen(method)] pub fn fill(this: &Uint16Array, value: u16, start: u32, end: u32) -> Uint16Array; + + /// The `buffer` accessor property represents the `ArrayBuffer` referenced + /// by a `TypedArray` at construction time. + #[wasm_bindgen(getter, method)] + pub fn buffer(this: &Uint16Array) -> ArrayBuffer; + + /// The `subarray()` method stores multiple values in the typed array, + /// reading input values from a specified array. + #[wasm_bindgen(method)] + pub fn subarray(this: &Uint16Array, begin: u32, end: u32) -> Uint16Array; + + /// The `forEach()` method executes a provided function once per array + /// element. This method has the same algorithm as + /// `Array.prototype.forEach()`. `TypedArray` is one of the typed array + /// types here. + #[wasm_bindgen(method, js_name = forEach)] + pub fn for_each(this: &Uint16Array, callback: &mut FnMut(u16, u32, Uint16Array)); } // Uint32Array @@ -1972,6 +2071,23 @@ extern "C" { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill #[wasm_bindgen(method)] pub fn fill(this: &Uint32Array, value: u32, start: u32, end: u32) -> Uint32Array; + + /// The `buffer` accessor property represents the `ArrayBuffer` referenced + /// by a `TypedArray` at construction time. + #[wasm_bindgen(getter, method)] + pub fn buffer(this: &Uint32Array) -> ArrayBuffer; + + /// The `subarray()` method stores multiple values in the typed array, + /// reading input values from a specified array. + #[wasm_bindgen(method)] + pub fn subarray(this: &Uint32Array, begin: u32, end: u32) -> Uint32Array; + + /// The `forEach()` method executes a provided function once per array + /// element. This method has the same algorithm as + /// `Array.prototype.forEach()`. `TypedArray` is one of the typed array + /// types here. + #[wasm_bindgen(method, js_name = forEach)] + pub fn for_each(this: &Uint32Array, callback: &mut FnMut(u32, u32, Uint32Array)); } // WeakMap diff --git a/crates/js-sys/tests/all/Array.rs b/crates/js-sys/tests/all/Array.rs deleted file mode 100644 index 52d2535a..00000000 --- a/crates/js-sys/tests/all/Array.rs +++ /dev/null @@ -1,1018 +0,0 @@ -#![allow(non_snake_case)] - -use project; - -#[test] -fn filter() { - 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 keep_numbers(array: &js_sys::Array) -> js_sys::Array { - array.filter(&mut |x, _, _| x.as_f64().is_some()) - } - - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let characters = ["a", "c", "x", "n"]; - let numbers = [1, 2, 3, 4]; - let mixed = ["a", 1, "b", 2]; - - assert.deepStrictEqual(wasm.keep_numbers(characters), []); - assert.deepStrictEqual(wasm.keep_numbers(numbers), numbers); - assert.deepStrictEqual(wasm.keep_numbers(mixed), [1, 2]); - } - "#, - ) - .test() -} - -#[test] -fn index_of() { - 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_index_of(this: &js_sys::Array, value: JsValue, from_index: i32) -> i32 { - this.index_of(value, from_index) - } - - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let characters = ["a", "c", "x", "n"]; - let index = wasm.get_index_of(characters, "x", 0); - let notFoundIndex = wasm.get_index_of(characters, "z", 0); - - assert.equal(index, 2); - assert.equal(notFoundIndex, -1); - - let withFromIndex = wasm.get_index_of(characters, "x", -3); - let withFromIndexNotFound = wasm.get_index_of(characters, "a", -2); - - assert.equal(withFromIndex, 2); - assert.equal(withFromIndexNotFound, -1); - } - "#, - ) - .test() -} - -#[test] -fn is_array() { - 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 is_array(value: &JsValue) -> bool { - js_sys::Array::is_array(value) - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - assert.ok(wasm.is_array([])); - assert.ok(wasm.is_array([1])); - assert.ok(wasm.is_array(new Array())); - assert.ok(wasm.is_array(new Array('a', 'b', 'c', 'd'))); - assert.ok(wasm.is_array(new Array(3))); - assert.ok(wasm.is_array(Array.prototype)); - - assert.ok(!wasm.is_array({})); - assert.ok(!wasm.is_array(null)); - assert.ok(!wasm.is_array(undefined)); - assert.ok(!wasm.is_array(17)); - assert.ok(!wasm.is_array('Array')); - assert.ok(!wasm.is_array(true)); - assert.ok(!wasm.is_array(false)); - assert.ok(!wasm.is_array({ __proto__: Array.prototype })); - } - "#, - ) - .test() -} - -#[test] -fn sort() { - 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 sort_array(this: &js_sys::Array) -> js_sys::Array { - this.sort() - } - - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let numbers = [3, 1, 6, 2]; - let sorted = wasm.sort_array(numbers); - - assert.deepStrictEqual(sorted, [1, 2, 3, 6]) - } - "#, - ) - .test() -} - -#[test] -fn some() { - 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 has_elem(array: &js_sys::Array, arg: JsValue) -> bool { - array.some(&mut |elem| arg == elem) - } - - "#) - .file("test.js", r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let elements = ["z", 1, "y", 2]; - - assert.deepStrictEqual(wasm.has_elem(elements, 2), true); - assert.deepStrictEqual(wasm.has_elem(elements, "y"), true); - assert.deepStrictEqual(wasm.has_elem(elements, "not an element"), false); - } - "#) - .test() -} - -#[test] -fn last_index_of() { - 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_last_index_of(this: &js_sys::Array, value: JsValue, from_index: i32) -> i32 { - this.last_index_of(value, from_index) - } - - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let characters = ["a", "x", "c", "x", "n"]; - let index = wasm.get_last_index_of(characters, "x", 5); - let notFoundIndex = wasm.get_last_index_of(characters, "z", 5); - - assert.equal(index, 3); - assert.equal(notFoundIndex, -1); - - let withFromIndex = wasm.get_last_index_of(characters, "x", 2); - let withFromIndexNotFound = wasm.get_last_index_of(characters, "x", 0); - - assert.equal(withFromIndex, 1); - assert.equal(withFromIndexNotFound, -1); - } - "#, - ) - .test() -} - -#[test] -fn join() { - 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 join_array(this: &js_sys::Array, delimiter: &str) -> js_sys::JsString { - this.join(delimiter) - } - - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let characters = ["a", "c", "x", "n"]; - let stringValue = wasm.join_array(characters, ", "); - - assert.equal("a, c, x, n", stringValue); - let withForwardSlash = wasm.join_array(characters, "/"); - assert.equal("a/c/x/n", withForwardSlash); - } - "#, - ) - .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::*; - - #[wasm_bindgen] - pub fn create_slice(this: &js_sys::Array, start: u32, end: u32) -> js_sys::Array { - this.slice(start, end) - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let characters = ["a", "c", "x", "n", 1, "8"]; - let subset = wasm.create_slice(characters, 1, 3); - - assert.equal(subset[0], "c"); - assert.equal(subset[1], "x"); - } - "#, - ) - .test() -} - -#[test] -fn fill() { - 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 fill_with(this: &js_sys::Array, value: JsValue, start: u32, end: u32) -> js_sys::Array { - this.fill(value, start, end) - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let characters = ["a", "c", "x", "n", 1, "8"]; - let subset = wasm.fill_with(characters, 0, 0, 3); - - assert.equal(subset[0], 0); - assert.equal(subset[4], 1); - } - "#, - ) - .test() -} - -#[test] -fn copy_within() { - 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 copy_values_within_array(this: &js_sys::Array, target: i32, start: i32, end: i32) -> js_sys::Array { - this.copy_within(target, start, end) - } - - "#) - .file("test.js", r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let characters = [8, 5, 4, 3, 1, 2] - wasm.copy_values_within_array(characters, 1, 4, 5); - - assert.equal(characters[1], 1); - - // if negatives were used - wasm.copy_values_within_array(characters, -1, -3, -2); - assert.equal(characters[5], 3); - } - "#) - .test() -} - -#[test] -fn pop() { - 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 pop_in_it(this: &js_sys::Array) -> JsValue { - this.pop() - } - - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let characters = [8, 5, 4, 3, 1, 2] - let item = wasm.pop_in_it(characters); - assert.equal(item, 2); - assert.equal(characters.length, 5); - } - "#, - ) - .test() -} - -#[test] -fn push() { - 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 push_it_along(this: &js_sys::Array, value: JsValue) -> u32 { - this.push(value) - } - - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let characters = [8, 5, 4, 3, 1, 2] - let length = wasm.push_it_along(characters, "a"); - assert.equal(length, 7); - assert.equal(characters[6], "a"); - } - "#, - ) - .test() -} - -#[test] -fn reverse() { - 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 reverse_array(this: &js_sys::Array) -> js_sys::Array { - this.reverse() - } - - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let characters = [8, 5, 4, 3, 1, 2] - let reversed = wasm.reverse_array(characters); - assert.equal(reversed[0], 2); - assert.equal(reversed[5], 8); - } - "#, - ) - .test() -} - -#[test] -fn shift() { - 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 shift_item(this: &js_sys::Array) -> JsValue { - this.shift() - } - - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let characters = [8, 5, 4, 3, 1, 2] - let shiftedItem = wasm.shift_item(characters); - - assert.equal(shiftedItem, 8); - assert.equal(characters.length, 5); - } - "#, - ) - .test() -} - -#[test] -fn unshift() { - 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 unshift_item(this: &js_sys::Array, value: JsValue) -> u32 { - this.unshift(value) - } - - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let characters = [8, 5, 4, 3, 1, 2] - let length = wasm.unshift_item(characters, "abba"); - - assert.equal(length, 7); - assert.equal(characters[0], "abba"); - } - "#, - ) - .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::*; - - #[wasm_bindgen] - pub fn array_to_string(this: &js_sys::Array) -> js_sys::JsString { - this.to_string() - } - - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let characters = [8, 5, 4, 3, 1, 2] - let arrayString = wasm.array_to_string(characters); - - assert.equal(arrayString, "8,5,4,3,1,2"); - } - "#, - ) - .test() -} - -#[test] -fn includes() { - 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 array_includes(this: &js_sys::Array, value: JsValue, from_index: i32) -> bool { - this.includes(value, from_index) - } - - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let characters = [8, 5, 4, 3, 1, 2] - let isTwoIncluded = wasm.array_includes(characters, 2, 0); - let isNineIncluded = wasm.array_includes(characters, 9, 0); - - assert.ok(isTwoIncluded); - assert.ok(!isNineIncluded); - - let isThreeIncluded = wasm.array_includes(characters, 3, 4); - assert.ok(!isThreeIncluded); - } - "#, - ) - .test() -} - -#[test] -fn concat() { - 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 array_concat(this: &js_sys::Array, arr: &js_sys::Array) -> js_sys::Array { - this.concat(arr) - } - - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let arr1 = [1, 2, 3]; - let arr2 = [4, 5, 6]; - - let new_array = wasm.array_concat(arr1, arr2) - assert.deepStrictEqual(new_array, [1, 2, 3, 4, 5, 6]); - } - "#, - ) - .test() -} - -#[test] -fn length() { - 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 array_length(this: &js_sys::Array) -> u32 { - this.length() - } - - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let characters = [8, 5, 4, 3, 1, 2] - let charactersLength = wasm.array_length(characters); - assert.equal(charactersLength, 6); - - var empty = []; - let emptyLength = wasm.array_length(empty); - assert.equal(emptyLength, 0); - } - "#, - ) - .test() -} - -#[test] -fn every() { - 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 array_every_number_is_even(array: &js_sys::Array) -> bool { - array.every(&mut |el, _, _| el.as_f64().unwrap() % 2f64 == 0f64) - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - const arrayEven = [2, 4, 6, 8]; - const arrayOdd = [1, 3, 5, 7]; - const arrayMixed = [2, 3, 4, 5]; - - assert.ok(wasm.array_every_number_is_even(arrayEven)); - assert.ok(!wasm.array_every_number_is_even(arrayOdd)); - assert.ok(!wasm.array_every_number_is_even(arrayMixed)); - } - "#, - ) - .test() -} - -#[test] -fn find() { - 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 array_find_first_even_number(array: &js_sys::Array) -> JsValue { - array.find(&mut |el, _, _| el.as_f64().unwrap() % 2f64 == 0f64) - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - const arrayEven = [2, 4, 6, 8]; - const arrayOdd = [1, 3, 5, 7]; - const arrayMixed = [3, 5, 7, 10]; - - assert.equal(wasm.array_find_first_even_number(arrayEven), 2); - assert.equal(wasm.array_find_first_even_number(arrayOdd), undefined); - assert.equal(wasm.array_find_first_even_number(arrayMixed), 10); - } - "#, - ) - .test() -} - -#[test] -fn map() { - project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - use JsValue; - - #[wasm_bindgen] - pub fn array_map(array: &js_sys::Array) -> js_sys::Array { - array.map(&mut |el, _, _| JsValue::from_f64(el.as_f64().unwrap().sqrt())) - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - const numbers = [1, 4, 9]; - assert.deepStrictEqual(wasm.array_map(numbers), [1, 2, 3]); - } - "#, - ) - .test() -} - -#[test] -fn reduce() { - project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - use JsValue; - - #[wasm_bindgen] - pub fn array_reduce(array: &js_sys::Array) -> JsValue { - array.reduce(&mut |ac, cr, _, _| JsValue::from_str(&format!("{}{}", &ac.as_string().unwrap(), &cr.as_string().unwrap().as_str())), JsValue::from_str("")) - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - assert.equal(wasm.array_reduce(['0', '1', '2', '3', '4']), '01234'); - } - "#, - ) - .test() -} - -#[test] -fn reduce_right() { - project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - use JsValue; - - #[wasm_bindgen] - pub fn array_reduce_right(array: &js_sys::Array) -> JsValue { - array.reduce_right(&mut |ac, cr, _, _| JsValue::from_str(&format!("{}{}", &ac.as_string().unwrap(), &cr.as_string().unwrap().as_str())), JsValue::from_str("")) - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - assert.equal(wasm.array_reduce_right(['0', '1', '2', '3', '4']), '43210'); - } - "#, - ) - .test() -} - -#[test] -fn find_index() { - 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 array_find_first_even_number_index(array: &js_sys::Array) -> u32 { - array.find_index(&mut |el, _, _| el.as_f64().unwrap() % 2. == 0.) - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - const arrayEven = [2, 4, 6, 8]; - const arrayOdd = [1, 3, 5, 7]; - const arrayMixed = [3, 5, 7, 10]; - - assert.equal(wasm.array_find_first_even_number_index(arrayEven), 0); - assert.equal(wasm.array_find_first_even_number_index(arrayOdd), -1); - assert.equal(wasm.array_find_first_even_number_index(arrayMixed), 3); - } - "#, - ) - .test() -} - -#[test] -fn to_locale_string() { - project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - use JsValue; - - #[wasm_bindgen] - pub fn array_to_locale_string(array: &js_sys::Array, locale: &JsValue, options: &JsValue) -> js_sys::JsString { - array.to_locale_string(locale, options) - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - const output = wasm.array_to_locale_string([1, 'a', new Date('21 Dec 1997 14:12:00 UTC')], 'en', {timeZone: 'UTC'}); - assert.equal(typeof output, 'string'); - assert.ok(output.length > 0); - } - "#, - ) - .test() -} - -#[test] -fn for_each() { - 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 sum_indices_of_evens(array: &js_sys::Array) -> u32 { - let mut res = 0; - array.for_each(&mut |elem: JsValue, i, _| { - match elem.as_f64() { - Some(val) if val % 2. == 0. => res += i, - _ => { } - } - }); - - res - } - "#) - .file("test.js", r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - const arrayEven = [2, 4, 6, 8]; - const arrayEvenExpected = 0 + 1 + 2 + 3; - const arrayEvenActual = wasm.sum_indices_of_evens(arrayEven); - assert.equal(arrayEvenActual, arrayEvenExpected); - - const arrayOdd = [1, 3, 5, 7]; - const arrayOddExpected = 0; - const arrayOddActual = wasm.sum_indices_of_evens(arrayOdd); - assert.equal(arrayOddActual, arrayOddExpected); - - const arrayMixed = [3, 5, 7, 10]; - const arrayMixedExpected = 3; - const arrayMixedActual = wasm.sum_indices_of_evens(arrayMixed); - assert.equal(arrayMixedActual, arrayMixedExpected); - } - "#) - .test() -} diff --git a/crates/js-sys/tests/all/main.rs b/crates/js-sys/tests/all/main.rs index 9a198062..a8f333e0 100644 --- a/crates/js-sys/tests/all/main.rs +++ b/crates/js-sys/tests/all/main.rs @@ -10,7 +10,6 @@ fn project() -> project_builder::Project { // Keep these tests in alphabetical order, just like the imports in `src/js.rs`. -mod Array; mod ArrayIterator; mod Date; mod Error; diff --git a/crates/js-sys/tests/wasm/Array.rs b/crates/js-sys/tests/wasm/Array.rs new file mode 100644 index 00000000..d1536853 --- /dev/null +++ b/crates/js-sys/tests/wasm/Array.rs @@ -0,0 +1,279 @@ +use wasm_bindgen::JsValue; +use wasm_bindgen_test::*; +use js_sys::*; + +macro_rules! js_array { + ($($e:expr),*) => ({ + let __x = Array::new(); + $(__x.push(JsValue::from($e));)* + __x + }) +} + +macro_rules! array { + ($($e:expr),*) => ({ + let mut __x = Vec::new(); + $(__x.push(JsValue::from($e));)* + __x + }) +} + +fn to_rust(arr: &Array) -> Vec { + let mut result = Vec::with_capacity(arr.length() as usize); + arr.for_each(&mut |x, _, _| result.push(x)); + result +} + +#[wasm_bindgen_test] +fn filter() { + let array = js_array!["a", "c", "x", "n"]; + assert!(array.filter(&mut |x, _, _| x.as_f64().is_some()).length() == 0); + + let array = js_array![1, 2, 3, 4]; + assert_eq!(array.filter(&mut |x, _, _| x.as_f64().is_some()).length(), 4); + + let array = js_array!["a", 1, "b", 2]; + assert_eq!(array.filter(&mut |x, _, _| x.as_f64().is_some()).length(), 2); +} + +#[wasm_bindgen_test] +fn index_of() { + let chars = js_array!["a", "c", "x", "n"]; + assert_eq!(chars.index_of("x".into(), 0), 2); + assert_eq!(chars.index_of("z".into(), 0), -1); + assert_eq!(chars.index_of("x".into(), -3), 2); + assert_eq!(chars.index_of("z".into(), -2), -1); +} + +#[wasm_bindgen_test] +fn is_array() { + assert!(Array::is_array(&Array::new().into())); + assert!(Array::is_array(&js_array![1].into())); + assert!(!Array::is_array(&JsValue::null())); + assert!(!Array::is_array(&JsValue::undefined())); + assert!(!Array::is_array(&10.into())); + assert!(!Array::is_array(&"x".into())); + assert!(!Array::is_array(&true.into())); + assert!(!Array::is_array(&false.into())); +} + +#[wasm_bindgen_test] +fn sort() { + let array = js_array![3, 1, 6, 2]; + let sorted = array.sort(); + assert_eq!(to_rust(&sorted), array![1, 2, 3, 6]); +} + +#[wasm_bindgen_test] +fn some() { + let array = js_array!["z", 1, "y", 2]; + assert!(array.some(&mut |e| e == JsValue::from(2))); + assert!(array.some(&mut |e| e == JsValue::from("y"))); + assert!(!array.some(&mut |e| e == JsValue::from("nope"))); +} + +#[wasm_bindgen_test] +fn last_index_of() { + let characters = js_array!["a", "x", "c", "x", "n"]; + assert_eq!(characters.last_index_of("x".into(), 5), 3); + assert_eq!(characters.last_index_of("z".into(), 5), -1); + assert_eq!(characters.last_index_of("x".into(), 2), 1); + assert_eq!(characters.last_index_of("x".into(), 0), -1); +} + +#[wasm_bindgen_test] +fn join() { + let characters = js_array!["a", "c", "x", "n"]; + assert_eq!(String::from(characters.join(", ")), "a, c, x, n"); + assert_eq!(String::from(characters.join("/")), "a/c/x/n"); +} + +#[wasm_bindgen_test] +fn slice() { + let characters = js_array!["a", "c", "x", "n", 1, "8"]; + let subset = characters.slice(1, 3); + + assert_eq!(to_rust(&subset), array!["c", "x"]); +} + +#[wasm_bindgen_test] +fn fill() { + let characters = js_array!["a", "c", "x", "n", 1, "8"]; + let subset = characters.fill(0.into(), 0, 3); + + assert_eq!(to_rust(&subset), array![0, 0, 0, "n", 1, "8"]); +} + +#[wasm_bindgen_test] +fn copy_within() { + let characters = js_array![8, 5, 4, 3, 1, 2]; + characters.copy_within(1, 4, 5); + + assert_eq!(to_rust(&characters)[1], JsValue::from(1)); + + // if negatives were used + characters.copy_within(-1, -3, -2); + assert_eq!(to_rust(&characters)[5], JsValue::from(3)); +} + +#[wasm_bindgen_test] +fn pop() { + let characters = js_array![8, 5, 4, 3, 1, 2]; + let item = characters.pop(); + assert_eq!(item, JsValue::from(2)); + assert_eq!(characters.length(), 5); +} + +#[wasm_bindgen_test] +fn push() { + let characters = js_array![8, 5, 4, 3, 1, 2]; + let length = characters.push("a".into()); + assert_eq!(length, 7); + assert_eq!(to_rust(&characters)[6], "a"); +} + +#[wasm_bindgen_test] +fn reverse() { + let characters = js_array![8, 5, 4, 3, 1, 2]; + let reversed = characters.reverse(); + assert_eq!(to_rust(&reversed), array![2, 1, 3, 4, 5, 8]); +} + +#[wasm_bindgen_test] +fn shift() { + let characters = js_array![8, 5, 4, 3, 1, 2]; + let shiftedItem = characters.shift(); + + assert_eq!(shiftedItem, 8); + assert_eq!(characters.length(), 5); +} + +#[wasm_bindgen_test] +fn unshift() { + let characters = js_array![8, 5, 4, 3, 1, 2]; + let length = characters.unshift("abba".into()); + + assert_eq!(length, 7); + assert_eq!(to_rust(&characters)[0], "abba"); +} + +#[wasm_bindgen_test] +fn to_string() { + let characters = js_array![8, 5, 4, 3, 1, 2]; + assert_eq!(String::from(characters.to_string()), "8,5,4,3,1,2"); +} + +#[wasm_bindgen_test] +fn includes() { + let characters = js_array![8, 5, 4, 3, 1, 2]; + assert!(characters.includes(2.into(), 0)); + assert!(!characters.includes(9.into(), 0)); + assert!(!characters.includes(3.into(), 4)); +} + +#[wasm_bindgen_test] +fn concat() { + let arr1 = js_array![1, 2, 3]; + let arr2 = js_array![4, 5, 6]; + + let new_array = arr1.concat(&arr2); + assert_eq!(to_rust(&new_array), array![1, 2, 3, 4, 5, 6]); +} + +#[wasm_bindgen_test] +fn length() { + let characters = js_array![8, 5, 4, 3, 1, 2]; + assert_eq!(characters.length(), 6); + assert_eq!(Array::new().length(), 0); +} + +#[wasm_bindgen_test] +fn every() { + let even = js_array![2, 4, 6, 8]; + assert!(even.every(&mut |x, _, _| x.as_f64().unwrap() % 2.0 == 0.0)); + let odd = js_array![1, 3, 5, 7]; + assert!(!odd.every(&mut |x, _, _| x.as_f64().unwrap() % 2.0 == 0.0)); + let mixed = js_array![2, 3, 4, 5]; + assert!(!mixed.every(&mut |x, _, _| x.as_f64().unwrap() % 2.0 == 0.0)); +} + +#[wasm_bindgen_test] +fn find() { + let even = js_array![2, 4, 6, 8]; + assert_eq!(even.find(&mut |x, _, _| x.as_f64().unwrap() % 2.0 == 0.0), 2); + let odd = js_array![1, 3, 5, 7]; + assert_eq!( + odd.find(&mut |x, _, _| x.as_f64().unwrap() % 2.0 == 0.0), + JsValue::undefined(), + ); + let mixed = js_array![3, 5, 7, 10]; + assert_eq!(mixed.find(&mut |x, _, _| x.as_f64().unwrap() % 2.0 == 0.0), 10); +} + +#[wasm_bindgen_test] +fn map() { + let numbers = js_array![1, 4, 9]; + let sqrt = numbers.map(&mut |x, _, _| x.as_f64().unwrap().sqrt().into()); + assert_eq!(to_rust(&sqrt), array![1, 2, 3]); +} + +#[wasm_bindgen_test] +fn reduce() { + let arr = js_array!["0", "1", "2", "3", "4"] + .reduce( + &mut |ac, cr, _, _| { + format!("{}{}", &ac.as_string().unwrap(), &cr.as_string().unwrap()) + .into() + }, + "".into(), + ); + assert_eq!(arr, "01234"); +} + +#[wasm_bindgen_test] +fn reduce_right() { + let arr = js_array!["0", "1", "2", "3", "4"] + .reduce_right( + &mut |ac, cr, _, _| { + format!("{}{}", &ac.as_string().unwrap(), &cr.as_string().unwrap()) + .into() + }, + "".into(), + ); + assert_eq!(arr, "43210"); +} + +#[wasm_bindgen_test] +fn find_index() { + let even = js_array![2, 4, 6, 8]; + assert_eq!(even.find_index(&mut |e, _, _| e.as_f64().unwrap() % 2. == 0.), 0); + let odd = js_array![1, 3, 5, 7]; + assert_eq!(odd.find_index(&mut |e, _, _| e.as_f64().unwrap() % 2. == 0.), -1); + let mixed = js_array![3, 5, 7, 10]; + assert_eq!(mixed.find_index(&mut |e, _, _| e.as_f64().unwrap() % 2. == 0.), 3); +} + +#[wasm_bindgen_test] +fn to_locale_string() { + let output = js_array![1, "a", Date::new("21 Dec 1997 14:12:00 UTC".into())] + .to_locale_string(&"en".into(), &JsValue::undefined()); + assert!(String::from(output).len() > 0); +} + +#[wasm_bindgen_test] +fn for_each() { + fn sum_indices_of_evens(array: &Array) -> u32 { + let mut res = 0; + array.for_each(&mut |elem: JsValue, i, _| { + match elem.as_f64() { + Some(val) if val % 2. == 0. => res += i, + _ => { } + } + }); + res + } + + assert_eq!(sum_indices_of_evens(&js_array![2, 4, 6, 8]), 0 + 1 + 2 + 3); + assert_eq!(sum_indices_of_evens(&js_array![1, 3, 5, 7]), 0); + assert_eq!(sum_indices_of_evens(&js_array![3, 5, 7, 10]), 3); +} diff --git a/crates/js-sys/tests/wasm/main.rs b/crates/js-sys/tests/wasm/main.rs index d690c77e..67ba89f9 100644 --- a/crates/js-sys/tests/wasm/main.rs +++ b/crates/js-sys/tests/wasm/main.rs @@ -6,6 +6,7 @@ extern crate js_sys; extern crate wasm_bindgen; extern crate wasm_bindgen_test; +pub mod Array; pub mod ArrayBuffer; pub mod ArrayIterator; pub mod Boolean; diff --git a/src/lib.rs b/src/lib.rs index 632b270c..c241272f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -290,6 +290,37 @@ impl PartialEq for JsValue { } } +impl PartialEq for JsValue { + fn eq(&self, other: &bool) -> bool { + self.as_bool() == Some(*other) + } +} + +impl PartialEq for JsValue { + fn eq(&self, other: &str) -> bool { + *self == JsValue::from_str(other) + } +} + +impl<'a> PartialEq<&'a str> for JsValue { + fn eq(&self, other: &&'a str) -> bool { + >::eq(self, other) + } +} + +if_std! { + impl PartialEq for JsValue { + fn eq(&self, other: &String) -> bool { + >::eq(self, other) + } + } + impl<'a> PartialEq<&'a String> for JsValue { + fn eq(&self, other: &&'a String) -> bool { + >::eq(self, other) + } + } +} + impl<'a> From<&'a str> for JsValue { fn from(s: &'a str) -> JsValue { JsValue::from_str(s) @@ -302,6 +333,12 @@ if_std! { JsValue::from_str(s) } } + + impl From for JsValue { + fn from(s: String) -> JsValue { + JsValue::from_str(&s) + } + } } impl From for JsValue { @@ -312,6 +349,12 @@ impl From for JsValue { macro_rules! numbers { ($($n:ident)*) => ($( + impl PartialEq<$n> for JsValue { + fn eq(&self, other: &$n) -> bool { + self.as_f64() == Some(f64::from(*other)) + } + } + impl From<$n> for JsValue { fn from(n: $n) -> JsValue { JsValue::from_f64(n.into())