mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-25 18:51:35 +00:00
Migrate Array tests to wasm-bindgen-test
This commit is contained in:
@ -174,7 +174,7 @@ extern "C" {
|
|||||||
///
|
///
|
||||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
|
||||||
#[wasm_bindgen(method, js_name = 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.
|
/// 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
|
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
|
||||||
#[wasm_bindgen(method)]
|
#[wasm_bindgen(method)]
|
||||||
pub fn fill(this: &Float32Array, value: f32, start: u32, end: u32) -> Float32Array;
|
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
|
// Float64Array
|
||||||
@ -629,6 +646,23 @@ extern "C" {
|
|||||||
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
|
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
|
||||||
#[wasm_bindgen(method)]
|
#[wasm_bindgen(method)]
|
||||||
pub fn fill(this: &Float64Array, value: f64, start: u32, end: u32) -> Float64Array;
|
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
|
// Function
|
||||||
@ -735,26 +769,6 @@ extern "C" {
|
|||||||
#[wasm_bindgen(getter, method)]
|
#[wasm_bindgen(getter, method)]
|
||||||
pub fn buffer(this: &Int8Array) -> ArrayBuffer;
|
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,
|
/// The `subarray()` method stores multiple values in the typed array,
|
||||||
/// reading input values from a specified array.
|
/// reading input values from a specified array.
|
||||||
#[wasm_bindgen(method)]
|
#[wasm_bindgen(method)]
|
||||||
@ -785,6 +799,23 @@ extern "C" {
|
|||||||
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
|
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
|
||||||
#[wasm_bindgen(method)]
|
#[wasm_bindgen(method)]
|
||||||
pub fn fill(this: &Int16Array, value: i16, start: u32, end: u32) -> Int16Array;
|
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
|
// Int32Array
|
||||||
@ -804,6 +835,23 @@ extern "C" {
|
|||||||
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
|
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
|
||||||
#[wasm_bindgen(method)]
|
#[wasm_bindgen(method)]
|
||||||
pub fn fill(this: &Int32Array, value: i32, start: u32, end: u32) -> Int32Array;
|
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
|
// Map
|
||||||
@ -1340,7 +1388,7 @@ extern "C" {
|
|||||||
///
|
///
|
||||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
|
||||||
#[wasm_bindgen(constructor)]
|
#[wasm_bindgen(constructor)]
|
||||||
pub fn new() -> Date;
|
pub fn new(init: JsValue) -> Date;
|
||||||
|
|
||||||
/// The `Date.now()` method returns the number of milliseconds
|
/// The `Date.now()` method returns the number of milliseconds
|
||||||
/// elapsed since January 1, 1970 00:00:00 UTC.
|
/// 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
|
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
|
||||||
#[wasm_bindgen(method)]
|
#[wasm_bindgen(method)]
|
||||||
pub fn fill(this: &Uint8Array, value: u8, start: u32, end: u32) -> Uint8Array;
|
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
|
// Uint8ClampedArray
|
||||||
@ -1934,6 +1999,23 @@ extern "C" {
|
|||||||
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
|
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
|
||||||
#[wasm_bindgen(method)]
|
#[wasm_bindgen(method)]
|
||||||
pub fn fill(this: &Uint8ClampedArray, value: u8, start: u32, end: u32) -> Uint8ClampedArray;
|
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
|
// Uint16Array
|
||||||
@ -1953,6 +2035,23 @@ extern "C" {
|
|||||||
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
|
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
|
||||||
#[wasm_bindgen(method)]
|
#[wasm_bindgen(method)]
|
||||||
pub fn fill(this: &Uint16Array, value: u16, start: u32, end: u32) -> Uint16Array;
|
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
|
// Uint32Array
|
||||||
@ -1972,6 +2071,23 @@ extern "C" {
|
|||||||
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
|
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
|
||||||
#[wasm_bindgen(method)]
|
#[wasm_bindgen(method)]
|
||||||
pub fn fill(this: &Uint32Array, value: u32, start: u32, end: u32) -> Uint32Array;
|
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
|
// WeakMap
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -10,7 +10,6 @@ fn project() -> project_builder::Project {
|
|||||||
|
|
||||||
// Keep these tests in alphabetical order, just like the imports in `src/js.rs`.
|
// Keep these tests in alphabetical order, just like the imports in `src/js.rs`.
|
||||||
|
|
||||||
mod Array;
|
|
||||||
mod ArrayIterator;
|
mod ArrayIterator;
|
||||||
mod Date;
|
mod Date;
|
||||||
mod Error;
|
mod Error;
|
||||||
|
279
crates/js-sys/tests/wasm/Array.rs
Normal file
279
crates/js-sys/tests/wasm/Array.rs
Normal file
@ -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<JsValue> {
|
||||||
|
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);
|
||||||
|
}
|
@ -6,6 +6,7 @@ extern crate js_sys;
|
|||||||
extern crate wasm_bindgen;
|
extern crate wasm_bindgen;
|
||||||
extern crate wasm_bindgen_test;
|
extern crate wasm_bindgen_test;
|
||||||
|
|
||||||
|
pub mod Array;
|
||||||
pub mod ArrayBuffer;
|
pub mod ArrayBuffer;
|
||||||
pub mod ArrayIterator;
|
pub mod ArrayIterator;
|
||||||
pub mod Boolean;
|
pub mod Boolean;
|
||||||
|
43
src/lib.rs
43
src/lib.rs
@ -290,6 +290,37 @@ impl PartialEq for JsValue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PartialEq<bool> for JsValue {
|
||||||
|
fn eq(&self, other: &bool) -> bool {
|
||||||
|
self.as_bool() == Some(*other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq<str> 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 {
|
||||||
|
<JsValue as PartialEq<str>>::eq(self, other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if_std! {
|
||||||
|
impl PartialEq<String> for JsValue {
|
||||||
|
fn eq(&self, other: &String) -> bool {
|
||||||
|
<JsValue as PartialEq<str>>::eq(self, other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'a> PartialEq<&'a String> for JsValue {
|
||||||
|
fn eq(&self, other: &&'a String) -> bool {
|
||||||
|
<JsValue as PartialEq<str>>::eq(self, other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a str> for JsValue {
|
impl<'a> From<&'a str> for JsValue {
|
||||||
fn from(s: &'a str) -> JsValue {
|
fn from(s: &'a str) -> JsValue {
|
||||||
JsValue::from_str(s)
|
JsValue::from_str(s)
|
||||||
@ -302,6 +333,12 @@ if_std! {
|
|||||||
JsValue::from_str(s)
|
JsValue::from_str(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<String> for JsValue {
|
||||||
|
fn from(s: String) -> JsValue {
|
||||||
|
JsValue::from_str(&s)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<bool> for JsValue {
|
impl From<bool> for JsValue {
|
||||||
@ -312,6 +349,12 @@ impl From<bool> for JsValue {
|
|||||||
|
|
||||||
macro_rules! numbers {
|
macro_rules! numbers {
|
||||||
($($n:ident)*) => ($(
|
($($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 {
|
impl From<$n> for JsValue {
|
||||||
fn from(n: $n) -> JsValue {
|
fn from(n: $n) -> JsValue {
|
||||||
JsValue::from_f64(n.into())
|
JsValue::from_f64(n.into())
|
||||||
|
Reference in New Issue
Block a user