2018-09-26 18:26:00 +03:00
|
|
|
use js_sys::*;
|
2018-07-20 22:56:32 +03:00
|
|
|
use wasm_bindgen::prelude::*;
|
2018-08-18 17:11:07 +03:00
|
|
|
use wasm_bindgen::JsCast;
|
2018-09-26 18:26:00 +03:00
|
|
|
use wasm_bindgen_test::*;
|
2018-07-20 22:56:32 +03:00
|
|
|
|
2018-08-06 20:55:04 +03:00
|
|
|
#[wasm_bindgen(module = "tests/wasm/Generator.js")]
|
2018-09-26 18:26:00 +03:00
|
|
|
extern "C" {
|
2018-07-20 22:56:32 +03:00
|
|
|
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();
|
|
|
|
|
2018-07-26 02:50:30 +03:00
|
|
|
assert!(gen.throw(&Error::new("something went wrong")).is_err());
|
2018-07-20 22:56:32 +03:00
|
|
|
let next = GeneratorResult::from(gen.next(&JsValue::undefined()).unwrap());
|
|
|
|
assert!(next.value().is_undefined());
|
|
|
|
assert!(next.done());
|
|
|
|
}
|
2018-08-18 17:11:07 +03:00
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn generator_inheritance() {
|
|
|
|
let gen = dummy_generator();
|
2018-09-26 18:26:00 +03:00
|
|
|
|
2018-08-18 17:11:07 +03:00
|
|
|
assert!(gen.is_instance_of::<Object>());
|
|
|
|
}
|