wasm-bindgen/crates/js-sys/tests/wasm/Iterator.rs
Alex Crichton 7ecf4aae87 cargo +nightly fmt --all
Rustfmt all the things!
2018-09-26 08:26:00 -07:00

49 lines
1.2 KiB
Rust

use js_sys::*;
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
#[wasm_bindgen(module = "tests/wasm/Iterator.js")]
extern "C" {
fn get_iterable() -> JsValue;
fn get_not_iterable() -> JsValue;
fn get_symbol_iterator_throws() -> JsValue;
fn get_symbol_iterator_not_function() -> JsValue;
fn get_symbol_iterator_returns_not_object() -> JsValue;
fn get_symbol_iterator_returns_object_without_next() -> JsValue;
}
#[wasm_bindgen_test]
fn try_iter_handles_iteration_protocol() {
assert_eq!(
try_iter(&get_iterable())
.unwrap()
.unwrap()
.map(|x| x.unwrap().as_string().unwrap())
.collect::<Vec<_>>(),
vec!["one", "two", "three"]
);
assert!(try_iter(&get_not_iterable()).unwrap().is_none());
assert!(try_iter(&get_symbol_iterator_throws()).is_err());
assert!(
try_iter(&get_symbol_iterator_not_function())
.unwrap()
.is_none()
);
assert!(
try_iter(&get_symbol_iterator_returns_not_object())
.unwrap()
.is_none()
);
assert!(
try_iter(&get_symbol_iterator_returns_object_without_next())
.unwrap()
.is_none()
);
}