| 1 |
use js_sys::*; |
| 2 |
use std::iter::FromIterator; |
| 3 |
use wasm_bindgen::prelude::*; |
| 4 |
use wasm_bindgen_test::*; |
| 5 |
|
| 6 |
macro_rules! js_array { |
| 7 |
($($e:expr),*) => ({ |
| 8 |
let __x = Array::new(); |
| 9 |
$(__x.push(&JsValue::from($e));)* |
| 10 |
__x |
| 11 |
}) |
| 12 |
} |
| 13 |
|
| 14 |
macro_rules! array { |
| 15 |
($($e:expr),*) => ({ |
| 16 |
let mut __x = Vec::new(); |
| 17 |
$(__x.push(JsValue::from($e));)* |
| 18 |
__x |
| 19 |
}) |
| 20 |
} |
| 21 |
|
| 22 |
fn to_rust(arr: &Array) -> Vec<JsValue> { |
| 23 |
let mut result = Vec::with_capacity(arr.length() as usize); |
| 24 |
arr.for_each(&mut |x, _, _| result.push(x)); |
| 25 |
result |
| 26 |
} |
| 27 |
|
| 28 |
#[wasm_bindgen_test] |
| 29 |
fn from_iter() { |
| 30 |
assert_eq!( |
| 31 |
to_rust( |
| 32 |
&vec![JsValue::from("a"), JsValue::from("b"), JsValue::from("c"),] |
| 33 |
.into_iter() |
| 34 |
.collect() |
| 35 |
), |
| 36 |
vec!["a", "b", "c"], |
| 37 |
); |
| 38 |
|
| 39 |
assert_eq!( |
| 40 |
to_rust( |
| 41 |
&vec![JsValue::from("a"), JsValue::from("b"), JsValue::from("c"),] |
| 42 |
.iter() |
| 43 |
.collect() |
| 44 |
), |
| 45 |
vec!["a", "b", "c"], |
| 46 |
); |
| 47 |
|
| 48 |
let array = js_array![1u32, 2u32, 3u32]; |
| 49 |
|
| 50 |
assert_eq!( |
| 51 |
to_rust(&vec![array.clone(),].into_iter().collect()), |
| 52 |
vec![JsValue::from(array.clone())], |
| 53 |
); |
| 54 |
|
| 55 |
assert_eq!( |
| 56 |
to_rust(&vec![array.clone(),].iter().collect()), |
| 57 |
vec![JsValue::from(array)], |
| 58 |
); |
| 59 |
|
| 60 |
assert_eq!( |
| 61 |
to_rust(&vec![5, 10, 20,].into_iter().map(JsValue::from).collect()), |
| 62 |
vec![5, 10, 20], |
| 63 |
); |
| 64 |
|
| 65 |
assert_eq!( |
| 66 |
to_rust(&Array::from_iter(&[ |
| 67 |
JsValue::from("a"), |
| 68 |
JsValue::from("b"), |
| 69 |
JsValue::from("c"), |
| 70 |
])), |
| 71 |
vec!["a", "b", "c"], |
| 72 |
); |
| 73 |
|
| 74 |
let v = vec!["a", "b", "c"]; |
| 75 |
|
| 76 |
assert_eq!( |
| 77 |
to_rust(&Array::from_iter(v.into_iter().map(|s| JsValue::from(s)))), |
| 78 |
vec!["a", "b", "c"], |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
#[wasm_bindgen_test] |
| 83 |
fn extend() { |
| 84 |
let mut array = array!["a", "b"]; |
| 85 |
array.extend(vec![JsValue::from("c"), JsValue::from("d")]); |
| 86 |
assert_eq!(array, array!["a", "b", "c", "d"]); |
| 87 |
} |
| 88 |
|
| 89 |
#[wasm_bindgen_test] |
| 90 |
fn to_vec() { |
| 91 |
let array = vec![JsValue::from("a"), JsValue::from("b"), JsValue::from("c")] |
| 92 |
.into_iter() |
| 93 |
.collect::<js_sys::Array>(); |
| 94 |
|
| 95 |
assert_eq!( |
| 96 |
array.to_vec(), |
| 97 |
vec![JsValue::from("a"), JsValue::from("b"), JsValue::from("c")] |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
#[wasm_bindgen_test] |
| 102 |
fn iter() { |
| 103 |
let array = vec![JsValue::from("a"), JsValue::from("b"), JsValue::from("c")] |
| 104 |
.into_iter() |
| 105 |
.collect::<js_sys::Array>(); |
| 106 |
|
| 107 |
assert_eq!( |
| 108 |
array.iter().collect::<Vec<JsValue>>(), |
| 109 |
vec![JsValue::from("a"), JsValue::from("b"), JsValue::from("c")] |
| 110 |
); |
| 111 |
|
| 112 |
let mut iter = array.iter(); |
| 113 |
|
| 114 |
assert_eq!(iter.size_hint(), (3, Some(3))); |
| 115 |
assert_eq!(iter.next(), Some(JsValue::from("a"))); |
| 116 |
|
| 117 |
assert_eq!(iter.size_hint(), (2, Some(2))); |
| 118 |
assert_eq!(iter.next_back(), Some(JsValue::from("c"))); |
| 119 |
|
| 120 |
assert_eq!(iter.size_hint(), (1, Some(1))); |
| 121 |
assert_eq!(iter.next_back(), Some(JsValue::from("b"))); |
| 122 |
|
| 123 |
assert_eq!(iter.size_hint(), (0, Some(0))); |
| 124 |
assert_eq!(iter.next(), None); |
| 125 |
|
| 126 |
assert_eq!(iter.size_hint(), (0, Some(0))); |
| 127 |
assert_eq!(iter.next_back(), None); |
| 128 |
|
| 129 |
let mut iter = array.iter(); |
| 130 |
|
| 131 |
assert_eq!(iter.size_hint(), (3, Some(3))); |
| 132 |
assert_eq!(iter.next(), Some(JsValue::from("a"))); |
| 133 |
|
| 134 |
assert_eq!(iter.size_hint(), (2, Some(2))); |
| 135 |
assert_eq!(iter.next(), Some(JsValue::from("b"))); |
| 136 |
|
| 137 |
assert_eq!(iter.size_hint(), (1, Some(1))); |
| 138 |
assert_eq!(iter.next(), Some(JsValue::from("c"))); |
| 139 |
|
| 140 |
assert_eq!(iter.size_hint(), (0, Some(0))); |
| 141 |
assert_eq!(iter.next(), None); |
| 142 |
|
| 143 |
let mut iter = array.iter(); |
| 144 |
|
| 145 |
assert_eq!(iter.size_hint(), (3, Some(3))); |
| 146 |
assert_eq!(iter.next_back(), Some(JsValue::from("c"))); |
| 147 |
|
| 148 |
assert_eq!(iter.size_hint(), (2, Some(2))); |
| 149 |
assert_eq!(iter.next_back(), Some(JsValue::from("b"))); |
| 150 |
|
| 151 |
assert_eq!(iter.size_hint(), (1, Some(1))); |
| 152 |
assert_eq!(iter.next_back(), Some(JsValue::from("a"))); |
| 153 |
|
| 154 |
assert_eq!(iter.size_hint(), (0, Some(0))); |
| 155 |
assert_eq!(iter.next_back(), None); |
| 156 |
} |
| 157 |
|
| 158 |
#[wasm_bindgen_test] |
| 159 |
fn new_with_length() { |
| 160 |
let array = Array::new_with_length(5); |
| 161 |
assert_eq!(array.length(), 5); |
| 162 |
assert_eq!(array.get(4), JsValue::undefined()); |
| 163 |
array.set(4, JsValue::from("a")); |
| 164 |
assert_eq!(array.get(4), "a"); |
| 165 |
assert_eq!(array.length(), 5); |
| 166 |
} |
| 167 |
|
| 168 |
#[wasm_bindgen_test] |
| 169 |
fn get() { |
| 170 |
let array = js_array!["a", "c", "x", "n"]; |
| 171 |
assert_eq!(array.length(), 4); |
| 172 |
assert_eq!(array.get(0), "a"); |
| 173 |
assert_eq!(array.get(3), "n"); |
| 174 |
assert_eq!(array.get(4), JsValue::undefined()); |
| 175 |
} |
| 176 |
|
| 177 |
#[wasm_bindgen_test] |
| 178 |
fn set() { |
| 179 |
let array = js_array!["a", "c", "x", "n"]; |
| 180 |
assert_eq!(array.length(), 4); |
| 181 |
assert_eq!(array.get(0), "a"); |
| 182 |
array.set(0, JsValue::from("b")); |
| 183 |
assert_eq!(array.get(0), "b"); |
| 184 |
|
| 185 |
assert_eq!(array.get(4), JsValue::undefined()); |
| 186 |
assert_eq!(array.length(), 4); |
| 187 |
array.set(4, JsValue::from("d")); |
| 188 |
assert_eq!(array.length(), 5); |
| 189 |
assert_eq!(array.get(4), "d"); |
| 190 |
|
| 191 |
assert_eq!(array.get(10), JsValue::undefined()); |
| 192 |
assert_eq!(array.length(), 5); |
| 193 |
array.set(10, JsValue::from("z")); |
| 194 |
assert_eq!(array.length(), 11); |
| 195 |
assert_eq!(array.get(10), "z"); |
| 196 |
assert_eq!(array.get(9), JsValue::undefined()); |
| 197 |
} |
| 198 |
|
| 199 |
#[wasm_bindgen_test] |
| 200 |
fn delete() { |
| 201 |
let array = js_array!["a", "c", "x", "n"]; |
| 202 |
assert_eq!(array.length(), 4); |
| 203 |
assert_eq!(array.get(0), "a"); |
| 204 |
array.delete(0); |
| 205 |
assert_eq!(array.get(0), JsValue::undefined()); |
| 206 |
} |
| 207 |
|
| 208 |
#[wasm_bindgen_test] |
| 209 |
fn filter() { |
| 210 |
let array = js_array!["a", "c", "x", "n"]; |
| 211 |
assert!(array.filter(&mut |x, _, _| x.as_f64().is_some()).length() == 0); |
| 212 |
|
| 213 |
let array = js_array![1, 2, 3, 4]; |
| 214 |
assert_eq!( |
| 215 |
array.filter(&mut |x, _, _| x.as_f64().is_some()).length(), |
| 216 |
4 |
| 217 |
); |
| 218 |
|
| 219 |
let array = js_array!["a", 1, "b", 2]; |
| 220 |
assert_eq!( |
| 221 |
array.filter(&mut |x, _, _| x.as_f64().is_some()).length(), |
| 222 |
2 |
| 223 |
); |
| 224 |
} |
| 225 |
|
| 226 |
#[wasm_bindgen_test] |
| 227 |
fn flat() { |
| 228 |
let array = js_array![ |
| 229 |
js_array!["a", "b", "c"], |
| 230 |
"d", |
| 231 |
js_array!["e", js_array!["f", "g"]] |
| 232 |
]; |
| 233 |
|
| 234 |
assert_eq!( |
| 235 |
to_rust(&array.flat(1).slice(0, 5)), |
| 236 |
vec!["a", "b", "c", "d", "e"] |
| 237 |
); |
| 238 |
|
| 239 |
assert_eq!(array.flat(1).length(), 6); |
| 240 |
|
| 241 |
assert_eq!( |
| 242 |
to_rust(&array.flat(2)), |
| 243 |
vec!["a", "b", "c", "d", "e", "f", "g"] |
| 244 |
); |
| 245 |
} |
| 246 |
|
| 247 |
#[wasm_bindgen_test] |
| 248 |
fn flat_map() { |
| 249 |
let array = js_array![1, 2, 3, 1]; |
| 250 |
|
| 251 |
assert_eq!( |
| 252 |
to_rust( |
| 253 |
&array.flat_map(&mut |val, _, _| match val.as_f64().map(|v| v as i32) { |
| 254 |
Some(1) => vec![JsString::from("x").into(), JsString::from("x").into()], |
| 255 |
Some(2) => vec![], |
| 256 |
Some(3) => vec![JsString::from("z").into()], |
| 257 |
_ => panic!("Unexpected conversion"), |
| 258 |
}) |
| 259 |
), |
| 260 |
vec!["x", "x", "z", "x", "x"] |
| 261 |
); |
| 262 |
} |
| 263 |
|
| 264 |
#[wasm_bindgen_test] |
| 265 |
fn index_of() { |
| 266 |
let chars = js_array!["a", "c", "x", "n"]; |
| 267 |
assert_eq!(chars.index_of(&"x".into(), 0), 2); |
| 268 |
assert_eq!(chars.index_of(&"z".into(), 0), -1); |
| 269 |
assert_eq!(chars.index_of(&"x".into(), -3), 2); |
| 270 |
assert_eq!(chars.index_of(&"z".into(), -2), -1); |
| 271 |
} |
| 272 |
|
| 273 |
#[wasm_bindgen_test] |
| 274 |
fn is_array() { |
| 275 |
assert!(Array::is_array(&Array::new().into())); |
| 276 |
assert!(Array::is_array(&js_array![1].into())); |
| 277 |
assert!(!Array::is_array(&JsValue::null())); |
| 278 |
assert!(!Array::is_array(&JsValue::undefined())); |
| 279 |
assert!(!Array::is_array(&10.into())); |
| 280 |
assert!(!Array::is_array(&"x".into())); |
| 281 |
assert!(!Array::is_array(&true.into())); |
| 282 |
assert!(!Array::is_array(&false.into())); |
| 283 |
} |
| 284 |
|
| 285 |
#[wasm_bindgen_test] |
| 286 |
fn sort() { |
| 287 |
let array = js_array![3, 1, 6, 2]; |
| 288 |
let sorted = array.sort(); |
| 289 |
assert_eq!(to_rust(&sorted), array![1, 2, 3, 6]); |
| 290 |
} |
| 291 |
|
| 292 |
#[wasm_bindgen_test] |
| 293 |
fn some() { |
| 294 |
let array = js_array!["z", 1, "y", 2]; |
| 295 |
assert!(array.some(&mut |e| e == JsValue::from(2))); |
| 296 |
assert!(array.some(&mut |e| e == JsValue::from("y"))); |
| 297 |
assert!(!array.some(&mut |e| e == JsValue::from("nope"))); |
| 298 |
} |
| 299 |
|
| 300 |
#[wasm_bindgen_test] |
| 301 |
fn last_index_of() { |
| 302 |
let characters = js_array!["a", "x", "c", "x", "n"]; |
| 303 |
assert_eq!(characters.last_index_of(&"x".into(), 5), 3); |
| 304 |
assert_eq!(characters.last_index_of(&"z".into(), 5), -1); |
| 305 |
assert_eq!(characters.last_index_of(&"x".into(), 2), 1); |
| 306 |
assert_eq!(characters.last_index_of(&"x".into(), 0), -1); |
| 307 |
} |
| 308 |
|
| 309 |
#[wasm_bindgen_test] |
| 310 |
fn join() { |
| 311 |
let characters = js_array!["a", "c", "x", "n"]; |
| 312 |
assert_eq!(String::from(characters.join(", ")), "a, c, x, n"); |
| 313 |
assert_eq!(String::from(characters.join("/")), "a/c/x/n"); |
| 314 |
} |
| 315 |
|
| 316 |
#[wasm_bindgen_test] |
| 317 |
fn slice() { |
| 318 |
let characters = js_array!["a", "c", "x", "n", 1, "8"]; |
| 319 |
let subset = characters.slice(1, 3); |
| 320 |
|
| 321 |
assert_eq!(to_rust(&subset), array!["c", "x"]); |
| 322 |
} |
| 323 |
|
| 324 |
#[wasm_bindgen_test] |
| 325 |
fn splice() { |
| 326 |
let characters = js_array!["a", "c", "x", "n", 1, "8"]; |
| 327 |
let removed = characters.splice(1, 3, &"b".into()); |
| 328 |
|
| 329 |
assert_eq!(to_rust(&removed), array!["c", "x", "n"]); |
| 330 |
assert_eq!(to_rust(&characters), array!["a", "b", 1, "8"]); |
| 331 |
} |
| 332 |
|
| 333 |
#[wasm_bindgen_test] |
| 334 |
fn fill() { |
| 335 |
let characters = js_array!["a", "c", "x", "n", 1, "8"]; |
| 336 |
let subset = characters.fill(&0.into(), 0, 3); |
| 337 |
|
| 338 |
assert_eq!(to_rust(&subset), array![0, 0, 0, "n", 1, "8"]); |
| 339 |
} |
| 340 |
|
| 341 |
#[wasm_bindgen_test] |
| 342 |
fn copy_within() { |
| 343 |
let characters = js_array![8, 5, 4, 3, 1, 2]; |
| 344 |
characters.copy_within(1, 4, 5); |
| 345 |
|
| 346 |
assert_eq!(to_rust(&characters)[1], JsValue::from(1)); |
| 347 |
|
| 348 |
// if negatives were used |
| 349 |
characters.copy_within(-1, -3, -2); |
| 350 |
assert_eq!(to_rust(&characters)[5], JsValue::from(3)); |
| 351 |
} |
| 352 |
|
| 353 |
#[wasm_bindgen_test] |
| 354 |
fn of() { |
| 355 |
let a = JsValue::from("a"); |
| 356 |
let b = JsValue::from("b"); |
| 357 |
let c = JsValue::from("c"); |
| 358 |
let arr = Array::of3(&a, &b, &c); |
| 359 |
let vec = to_rust(&arr); |
| 360 |
assert_eq!(vec.len(), 3); |
| 361 |
assert_eq!(vec[0], a); |
| 362 |
assert_eq!(vec[1], b); |
| 363 |
assert_eq!(vec[2], c); |
| 364 |
} |
| 365 |
|
| 366 |
#[wasm_bindgen_test] |
| 367 |
fn pop() { |
| 368 |
let characters = js_array![8, 5, 4, 3, 1, 2]; |
| 369 |
let item = characters.pop(); |
| 370 |
assert_eq!(item, JsValue::from(2)); |
| 371 |
assert_eq!(characters.length(), 5); |
| 372 |
} |
| 373 |
|
| 374 |
#[wasm_bindgen_test] |
| 375 |
fn push() { |
| 376 |
let characters = js_array![8, 5, 4, 3, 1, 2]; |
| 377 |
let length = characters.push(&"a".into()); |
| 378 |
assert_eq!(length, 7); |
| 379 |
assert_eq!(to_rust(&characters)[6], "a"); |
| 380 |
} |
| 381 |
|
| 382 |
#[wasm_bindgen_test] |
| 383 |
fn reverse() { |
| 384 |
let characters = js_array![8, 5, 4, 3, 1, 2]; |
| 385 |
let reversed = characters.reverse(); |
| 386 |
assert_eq!(to_rust(&reversed), array![2, 1, 3, 4, 5, 8]); |
| 387 |
} |
| 388 |
|
| 389 |
#[wasm_bindgen_test] |
| 390 |
fn shift() { |
| 391 |
let characters = js_array![8, 5, 4, 3, 1, 2]; |
| 392 |
let shiftedItem = characters.shift(); |
| 393 |
|
| 394 |
assert_eq!(shiftedItem, 8); |
| 395 |
assert_eq!(characters.length(), 5); |
| 396 |
} |
| 397 |
|
| 398 |
#[wasm_bindgen_test] |
| 399 |
fn unshift() { |
| 400 |
let characters = js_array![8, 5, 4, 3, 1, 2]; |
| 401 |
let length = characters.unshift(&"abba".into()); |
| 402 |
|
| 403 |
assert_eq!(length, 7); |
| 404 |
assert_eq!(to_rust(&characters)[0], "abba"); |
| 405 |
} |
| 406 |
|
| 407 |
#[wasm_bindgen_test] |
| 408 |
fn to_string() { |
| 409 |
let characters = js_array![8, 5, 4, 3, 1, 2]; |
| 410 |
assert_eq!(String::from(characters.to_string()), "8,5,4,3,1,2"); |
| 411 |
} |
| 412 |
|
| 413 |
#[wasm_bindgen_test] |
| 414 |
fn includes() { |
| 415 |
let characters = js_array![8, 5, 4, 3, 1, 2]; |
| 416 |
assert!(characters.includes(&2.into(), 0)); |
| 417 |
assert!(!characters.includes(&9.into(), 0)); |
| 418 |
assert!(!characters.includes(&3.into(), 4)); |
| 419 |
} |
| 420 |
|
| 421 |
#[wasm_bindgen_test] |
| 422 |
fn concat() { |
| 423 |
let arr1 = js_array![1, 2, 3]; |
| 424 |
let arr2 = js_array![4, 5, 6]; |
| 425 |
|
| 426 |
let new_array = arr1.concat(&arr2); |
| 427 |
assert_eq!(to_rust(&new_array), array![1, 2, 3, 4, 5, 6]); |
| 428 |
} |
| 429 |
|
| 430 |
#[wasm_bindgen_test] |
| 431 |
fn length() { |
| 432 |
let characters = js_array![8, 5, 4, 3, 1, 2]; |
| 433 |
assert_eq!(characters.length(), 6); |
| 434 |
assert_eq!(Array::new().length(), 0); |
| 435 |
} |
| 436 |
|
| 437 |
#[wasm_bindgen_test] |
| 438 |
fn every() { |
| 439 |
let even = js_array![2, 4, 6, 8]; |
| 440 |
assert!(even.every(&mut |x, _, _| x.as_f64().unwrap() % 2.0 == 0.0)); |
| 441 |
let odd = js_array![1, 3, 5, 7]; |
| 442 |
assert!(!odd.every(&mut |x, _, _| x.as_f64().unwrap() % 2.0 == 0.0)); |
| 443 |
let mixed = js_array![2, 3, 4, 5]; |
| 444 |
assert!(!mixed.every(&mut |x, _, _| x.as_f64().unwrap() % 2.0 == 0.0)); |
| 445 |
} |
| 446 |
|
| 447 |
#[wasm_bindgen_test] |
| 448 |
fn find() { |
| 449 |
let even = js_array![2, 4, 6, 8]; |
| 450 |
assert_eq!( |
| 451 |
even.find(&mut |x, _, _| x.as_f64().unwrap() % 2.0 == 0.0), |
| 452 |
2 |
| 453 |
); |
| 454 |
let odd = js_array![1, 3, 5, 7]; |
| 455 |
assert_eq!( |
| 456 |
odd.find(&mut |x, _, _| x.as_f64().unwrap() % 2.0 == 0.0), |
| 457 |
JsValue::undefined(), |
| 458 |
); |
| 459 |
let mixed = js_array![3, 5, 7, 10]; |
| 460 |
assert_eq!( |
| 461 |
mixed.find(&mut |x, _, _| x.as_f64().unwrap() % 2.0 == 0.0), |
| 462 |
10 |
| 463 |
); |
| 464 |
} |
| 465 |
|
| 466 |
#[wasm_bindgen_test] |
| 467 |
fn map() { |
| 468 |
let numbers = js_array![1, 4, 9]; |
| 469 |
let sqrt = numbers.map(&mut |x, _, _| x.as_f64().unwrap().sqrt().into()); |
| 470 |
assert_eq!(to_rust(&sqrt), array![1, 2, 3]); |
| 471 |
} |
| 472 |
|
| 473 |
#[wasm_bindgen_test] |
| 474 |
fn reduce() { |
| 475 |
let arr = js_array!["0", "1", "2", "3", "4"].reduce( |
| 476 |
&mut |ac, cr, _, _| { |
| 477 |
format!("{}{}", &ac.as_string().unwrap(), &cr.as_string().unwrap()).into() |
| 478 |
}, |
| 479 |
&"".into(), |
| 480 |
); |
| 481 |
assert_eq!(arr, "01234"); |
| 482 |
} |
| 483 |
|
| 484 |
#[wasm_bindgen_test] |
| 485 |
fn reduce_right() { |
| 486 |
let arr = js_array!["0", "1", "2", "3", "4"].reduce_right( |
| 487 |
&mut |ac, cr, _, _| { |
| 488 |
format!("{}{}", &ac.as_string().unwrap(), &cr.as_string().unwrap()).into() |
| 489 |
}, |
| 490 |
&"".into(), |
| 491 |
); |
| 492 |
assert_eq!(arr, "43210"); |
| 493 |
} |
| 494 |
|
| 495 |
#[wasm_bindgen_test] |
| 496 |
fn find_index() { |
| 497 |
let even = js_array![2, 4, 6, 8]; |
| 498 |
assert_eq!( |
| 499 |
even.find_index(&mut |e, _, _| e.as_f64().unwrap() % 2. == 0.), |
| 500 |
0 |
| 501 |
); |
| 502 |
let odd = js_array![1, 3, 5, 7]; |
| 503 |
assert_eq!( |
| 504 |
odd.find_index(&mut |e, _, _| e.as_f64().unwrap() % 2. == 0.), |
| 505 |
-1 |
| 506 |
); |
| 507 |
let mixed = js_array![3, 5, 7, 10]; |
| 508 |
assert_eq!( |
| 509 |
mixed.find_index(&mut |e, _, _| e.as_f64().unwrap() % 2. == 0.), |
| 510 |
3 |
| 511 |
); |
| 512 |
} |
| 513 |
|
| 514 |
#[wasm_bindgen_test] |
| 515 |
fn to_locale_string() { |
| 516 |
let output = js_array![1, "a", Date::new(&"21 Dec 1997 14:12:00 UTC".into())] |
| 517 |
.to_locale_string(&"en".into(), &JsValue::undefined()); |
| 518 |
assert!(String::from(output).len() > 0); |
| 519 |
} |
| 520 |
|
| 521 |
#[wasm_bindgen_test] |
| 522 |
fn for_each() { |
| 523 |
fn sum_indices_of_evens(array: &Array) -> u32 { |
| 524 |
let mut res = 0; |
| 525 |
array.for_each(&mut |elem: JsValue, i, _| match elem.as_f64() { |
| 526 |
Some(val) if val % 2. == 0. => res += i, |
| 527 |
_ => {} |
| 528 |
}); |
| 529 |
res |
| 530 |
} |
| 531 |
|
| 532 |
assert_eq!(sum_indices_of_evens(&js_array![2, 4, 6, 8]), 0 + 1 + 2 + 3); |
| 533 |
assert_eq!(sum_indices_of_evens(&js_array![1, 3, 5, 7]), 0); |
| 534 |
assert_eq!(sum_indices_of_evens(&js_array![3, 5, 7, 10]), 3); |
| 535 |
} |
| 536 |
|
| 537 |
#[wasm_bindgen_test] |
| 538 |
fn set_length() { |
| 539 |
let array = js_array![1, 2, 3, 4, 5]; |
| 540 |
array.set_length(3); |
| 541 |
assert_eq!( |
| 542 |
array.iter().collect::<Vec<_>>(), |
| 543 |
[1.0, 2.0, 3.0].map(|x| JsValue::from_f64(x)) |
| 544 |
); |
| 545 |
|
| 546 |
array.set_length(7); |
| 547 |
assert_eq!( |
| 548 |
array.iter().collect::<Vec<_>>(), |
| 549 |
[1.0, 2.0, 3.0] |
| 550 |
.iter() |
| 551 |
.copied() |
| 552 |
.map(|x| JsValue::from_f64(x)) |
| 553 |
.chain([JsValue::UNDEFINED; 4]) |
| 554 |
.collect::<Vec<_>>() |
| 555 |
); |
| 556 |
|
| 557 |
let mut calls = 0; |
| 558 |
array.for_each(&mut |_, _, _| calls += 1); |
| 559 |
// The later elements don't get filled with `undefined`, they get filled with |
| 560 |
// empty slots, which get skipped by `for_each`. |
| 561 |
assert_eq!(calls, 3); |
| 562 |
} |
| 563 |
|
| 564 |
#[wasm_bindgen_test] |
| 565 |
fn array_inheritance() { |
| 566 |
let array = Array::new(); |
| 567 |
assert!(array.is_instance_of::<Array>()); |
| 568 |
assert!(array.is_instance_of::<Object>()); |
| 569 |
let _: &Object = array.as_ref(); |
| 570 |
} |
| 571 |
|
| 572 |
#[wasm_bindgen(module = "tests/wasm/Array.js")] |
| 573 |
extern "C" { |
| 574 |
fn populate_array(arr: JsValue, start: JsValue, len: JsValue) -> JsValue; |
| 575 |
} |
| 576 |
|
| 577 |
fn test_array_view_mut_raw<ElemT: std::cmp::PartialEq + std::fmt::Debug, ArrT>( |
| 578 |
sut: unsafe fn(*mut ElemT, usize) -> ArrT, |
| 579 |
u8ToElem: fn(u8) -> ElemT, |
| 580 |
arrToJsValue: fn(ArrT) -> JsValue, |
| 581 |
) { |
| 582 |
let start: u8 = 10; |
| 583 |
let len: usize = 32; |
| 584 |
let end: u8 = start + len as u8; |
| 585 |
let mut buffer: Vec<ElemT> = Vec::new(); |
| 586 |
buffer.reserve(len); |
| 587 |
unsafe { |
| 588 |
let array: ArrT = sut(buffer.as_mut_ptr(), len); |
| 589 |
populate_array( |
| 590 |
arrToJsValue(array), |
| 591 |
JsValue::from(start), |
| 592 |
JsValue::from(len as u32), |
| 593 |
); |
| 594 |
buffer.set_len(len); |
| 595 |
} |
| 596 |
let expected: Vec<ElemT> = (start..end).map(u8ToElem).collect(); |
| 597 |
assert_eq!(buffer, expected) |
| 598 |
} |
| 599 |
|
| 600 |
#[wasm_bindgen_test] |
| 601 |
fn Int8Array_view_mut_raw() { |
| 602 |
fn u8Toi8_unsafe(x: u8) -> i8 { |
| 603 |
x as i8 |
| 604 |
} |
| 605 |
test_array_view_mut_raw( |
| 606 |
js_sys::Int8Array::view_mut_raw, |
| 607 |
u8Toi8_unsafe, |
| 608 |
JsValue::from, |
| 609 |
); |
| 610 |
} |
| 611 |
|
| 612 |
#[wasm_bindgen_test] |
| 613 |
fn Int16Array_view_mut_raw() { |
| 614 |
test_array_view_mut_raw(js_sys::Int16Array::view_mut_raw, i16::from, JsValue::from); |
| 615 |
} |
| 616 |
|
| 617 |
#[wasm_bindgen_test] |
| 618 |
fn Int32Array_view_mut_raw() { |
| 619 |
test_array_view_mut_raw(js_sys::Int32Array::view_mut_raw, i32::from, JsValue::from); |
| 620 |
} |
| 621 |
|
| 622 |
#[wasm_bindgen_test] |
| 623 |
fn BigInt64Array_view_mut_raw() { |
| 624 |
test_array_view_mut_raw( |
| 625 |
js_sys::BigInt64Array::view_mut_raw, |
| 626 |
i64::from, |
| 627 |
JsValue::from, |
| 628 |
); |
| 629 |
} |
| 630 |
|
| 631 |
#[wasm_bindgen_test] |
| 632 |
fn Uint8Array_view_mut_raw() { |
| 633 |
test_array_view_mut_raw(js_sys::Uint8Array::view_mut_raw, u8::from, JsValue::from); |
| 634 |
} |
| 635 |
|
| 636 |
#[wasm_bindgen_test] |
| 637 |
fn Uint8ClampedArray_view_mut_raw() { |
| 638 |
test_array_view_mut_raw( |
| 639 |
js_sys::Uint8ClampedArray::view_mut_raw, |
| 640 |
u8::from, |
| 641 |
JsValue::from, |
| 642 |
); |
| 643 |
} |
| 644 |
|
| 645 |
#[wasm_bindgen_test] |
| 646 |
fn Uint16Array_view_mut_raw() { |
| 647 |
test_array_view_mut_raw(js_sys::Uint16Array::view_mut_raw, u16::from, JsValue::from); |
| 648 |
} |
| 649 |
|
| 650 |
#[wasm_bindgen_test] |
| 651 |
fn Uint32Array_view_mut_raw() { |
| 652 |
test_array_view_mut_raw(js_sys::Uint32Array::view_mut_raw, u32::from, JsValue::from); |
| 653 |
} |
| 654 |
|
| 655 |
#[wasm_bindgen_test] |
| 656 |
fn BigUint64Array_view_mut_raw() { |
| 657 |
test_array_view_mut_raw( |
| 658 |
js_sys::BigUint64Array::view_mut_raw, |
| 659 |
u64::from, |
| 660 |
JsValue::from, |
| 661 |
); |
| 662 |
} |
| 663 |
|
| 664 |
#[wasm_bindgen_test] |
| 665 |
fn Float32Array_view_mut_raw() { |
| 666 |
test_array_view_mut_raw(js_sys::Float32Array::view_mut_raw, f32::from, JsValue::from); |
| 667 |
} |
| 668 |
|
| 669 |
#[wasm_bindgen_test] |
| 670 |
fn Float64Array_view_mut_raw() { |
| 671 |
test_array_view_mut_raw(js_sys::Float64Array::view_mut_raw, f64::from, JsValue::from); |
| 672 |
} |