summaryrefslogtreecommitdiffstats
path: root/vendor/js-sys/tests/wasm/Generator.rs
blob: 690bb22605c1420ac9e6b36d4202e8fae18f1295 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use js_sys::*;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen_test::*;

#[wasm_bindgen(module = "tests/wasm/Generator.js")]
extern "C" {
    fn one_two_generator() -> Generator;
    fn dummy_generator() -> Generator;
    fn broken_generator() -> Generator;

    type GeneratorResult;

    #[wasm_bindgen(method, getter, structural)]
    fn value(this: &GeneratorResult) -> JsValue;
    #[wasm_bindgen(method, getter, structural)]
    fn done(this: &GeneratorResult) -> bool;

}

#[wasm_bindgen_test]
fn return_() {
    let gen = one_two_generator();
    gen.next(&JsValue::undefined()).unwrap();

    let res = GeneratorResult::from(gen.return_(&42.into()));
    assert_eq!(res.value(), 42);
    assert!(res.done());

    let next = GeneratorResult::from(gen.next(&JsValue::undefined()).unwrap());
    assert!(next.value().is_undefined());
    assert!(next.done());
}

#[wasm_bindgen_test]
fn next() {
    let gen = dummy_generator();

    let result = GeneratorResult::from(gen.next(&JsValue::undefined()).unwrap());
    assert!(!result.done());
    assert_eq!(result.value(), "2 * 2");

    let result = GeneratorResult::from(gen.next(&4.into()).unwrap());
    assert!(result.done());
    assert_eq!(result.value(), true);

    assert!(broken_generator().next(&3.into()).is_err());
}

#[wasm_bindgen_test]
fn throw() {
    let gen = one_two_generator();
    gen.next(&JsValue::undefined()).unwrap();

    assert!(gen.throw(&Error::new("something went wrong")).is_err());
    let next = GeneratorResult::from(gen.next(&JsValue::undefined()).unwrap());
    assert!(next.value().is_undefined());
    assert!(next.done());
}

#[wasm_bindgen_test]
fn generator_inheritance() {
    let gen = dummy_generator();

    assert!(gen.is_instance_of::<Object>());
}