2018-07-03 23:42:49 +03:00
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
|
|
|
use project;
|
|
|
|
|
2018-07-03 23:47:10 +03:00
|
|
|
#[test]
|
2018-07-04 01:22:56 +03:00
|
|
|
fn return_() {
|
2018-07-03 23:47:10 +03:00
|
|
|
project()
|
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2018-07-19 16:57:18 +03:00
|
|
|
#![feature(use_extern_macros)]
|
2018-07-03 23:47:10 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
extern crate wasm_bindgen;
|
2018-07-19 22:30:58 +03:00
|
|
|
extern crate js_sys;
|
2018-07-05 06:37:09 +03:00
|
|
|
use wasm_bindgen::prelude::*;
|
2018-07-03 23:47:10 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
#[wasm_bindgen]
|
2018-07-19 22:30:58 +03:00
|
|
|
pub fn gen_return(this: &js_sys::Generator, value: &JsValue) -> JsValue {
|
2018-07-05 06:37:09 +03:00
|
|
|
this.return_(value)
|
|
|
|
}
|
|
|
|
"#,
|
2018-07-03 23:47:10 +03:00
|
|
|
)
|
|
|
|
.file(
|
2018-07-05 06:37:09 +03:00
|
|
|
"test.js",
|
2018-07-03 23:47:10 +03:00
|
|
|
r#"
|
2018-07-05 06:37:09 +03:00
|
|
|
import * as assert from "assert";
|
|
|
|
import * as wasm from "./out";
|
2018-07-03 23:47:10 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
export function test() {
|
|
|
|
function* generator() {
|
|
|
|
yield 1;
|
|
|
|
yield 2;
|
|
|
|
}
|
2018-07-03 23:47:10 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
const gen = generator();
|
|
|
|
gen.next();
|
2018-07-03 23:47:10 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
const res = wasm.gen_return(gen, 42);
|
|
|
|
assert.deepEqual(res, { value: 42, done: true });
|
2018-07-03 23:47:10 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
const next = gen.next();
|
|
|
|
assert.deepEqual(next, { value: undefined, done: true });
|
|
|
|
}
|
|
|
|
"#,
|
2018-07-03 23:47:10 +03:00
|
|
|
)
|
|
|
|
.test()
|
|
|
|
}
|
|
|
|
|
2018-07-03 23:42:49 +03:00
|
|
|
#[test]
|
|
|
|
fn next() {
|
|
|
|
project()
|
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2018-07-19 16:57:18 +03:00
|
|
|
#![feature(use_extern_macros)]
|
2018-07-05 06:37:09 +03:00
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
2018-07-19 22:30:58 +03:00
|
|
|
extern crate js_sys;
|
2018-07-05 06:37:09 +03:00
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-07-19 22:30:58 +03:00
|
|
|
pub fn next(this: &js_sys::Generator, value: &JsValue) -> JsValue {
|
2018-07-05 06:37:09 +03:00
|
|
|
this.next(value)
|
|
|
|
.ok()
|
|
|
|
.expect("generator throws an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-07-19 22:30:58 +03:00
|
|
|
pub fn next_throws_error(this: &js_sys::Generator, value: &JsValue) -> bool {
|
2018-07-05 06:37:09 +03:00
|
|
|
this.next(value).is_err()
|
|
|
|
}
|
|
|
|
"#,
|
2018-07-03 23:42:49 +03:00
|
|
|
)
|
|
|
|
.file(
|
2018-07-05 06:37:09 +03:00
|
|
|
"test.js",
|
2018-07-03 23:42:49 +03:00
|
|
|
r#"
|
2018-07-05 06:37:09 +03:00
|
|
|
import * as assert from "assert";
|
|
|
|
import * as wasm from "./out";
|
2018-07-03 23:42:49 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
export function test() {
|
|
|
|
function* generator() {
|
|
|
|
const reply = yield '2 * 2';
|
2018-07-03 23:42:49 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
return reply === 4;
|
|
|
|
}
|
2018-07-03 23:42:49 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
const gen = generator();
|
2018-07-03 23:42:49 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
const q = wasm.next(gen, undefined);
|
|
|
|
assert.deepEqual(q, { value: '2 * 2', done: false });
|
2018-07-03 23:42:49 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
const a = wasm.next(gen, 4);
|
|
|
|
assert.deepEqual(a, { value: true, done: true });
|
2018-07-04 01:22:56 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
function* brokenGenerator() {
|
|
|
|
throw new Error('Something went wrong');
|
|
|
|
yield 1;
|
|
|
|
}
|
2018-07-04 01:22:56 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
assert.ok(wasm.next_throws_error(brokenGenerator(), undefined));
|
|
|
|
}
|
|
|
|
"#,
|
2018-07-03 23:42:49 +03:00
|
|
|
)
|
|
|
|
.test()
|
|
|
|
}
|
2018-07-03 23:48:54 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn throw() {
|
|
|
|
project()
|
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2018-07-19 16:57:18 +03:00
|
|
|
#![feature(use_extern_macros)]
|
2018-07-03 23:48:54 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
extern crate wasm_bindgen;
|
2018-07-19 22:30:58 +03:00
|
|
|
extern crate js_sys;
|
2018-07-05 06:37:09 +03:00
|
|
|
use wasm_bindgen::prelude::*;
|
2018-07-03 23:48:54 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
#[wasm_bindgen]
|
2018-07-19 22:30:58 +03:00
|
|
|
pub fn gen_throws_error(this: &js_sys::Generator, error: &js_sys::Error) -> bool {
|
2018-07-05 06:37:09 +03:00
|
|
|
this.throw(error).is_err()
|
|
|
|
}
|
|
|
|
"#,
|
2018-07-03 23:48:54 +03:00
|
|
|
)
|
|
|
|
.file(
|
2018-07-05 06:37:09 +03:00
|
|
|
"test.js",
|
2018-07-03 23:48:54 +03:00
|
|
|
r#"
|
2018-07-05 06:37:09 +03:00
|
|
|
import * as assert from "assert";
|
|
|
|
import * as wasm from "./out";
|
2018-07-03 23:48:54 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
export function test() {
|
|
|
|
function* generator() {
|
|
|
|
yield 1;
|
|
|
|
yield 2;
|
|
|
|
}
|
2018-07-03 23:48:54 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
const gen = generator();
|
|
|
|
gen.next();
|
2018-07-03 23:48:54 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
assert.ok(wasm.gen_throws_error(gen, new Error('Something went wrong')));
|
|
|
|
assert.deepEqual(gen.next(), { value: undefined, done: true });
|
|
|
|
}
|
|
|
|
"#,
|
2018-07-03 23:48:54 +03:00
|
|
|
)
|
|
|
|
.test()
|
|
|
|
}
|