summaryrefslogtreecommitdiffstats
path: root/vendor/js-sys/tests/wasm/Iterator.rs
blob: f06c58b54448ac706c1040f9415092c02ef9d3bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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());
}