2018-03-29 11:47:44 +03:00
|
|
|
extern crate test_support;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn works() {
|
|
|
|
test_support::project()
|
|
|
|
.node(true)
|
|
|
|
.file("src/lib.rs", r#"
|
|
|
|
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
|
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
|
|
|
#[wasm_bindgen(module = "./test")]
|
|
|
|
extern {
|
|
|
|
fn hit();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn run() {
|
|
|
|
hit();
|
|
|
|
}
|
2018-03-29 13:12:54 +03:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct Foo {
|
|
|
|
contents: u32,
|
|
|
|
}
|
2018-03-30 00:49:36 +03:00
|
|
|
|
2018-03-29 13:12:54 +03:00
|
|
|
#[wasm_bindgen]
|
|
|
|
impl Foo {
|
|
|
|
pub fn new() -> Foo {
|
|
|
|
Foo::with_contents(0)
|
|
|
|
}
|
|
|
|
pub fn with_contents(a: u32) -> Foo {
|
|
|
|
Foo { contents: a }
|
|
|
|
}
|
|
|
|
pub fn add(&mut self, amt: u32) -> u32 {
|
|
|
|
self.contents += amt;
|
|
|
|
self.contents
|
|
|
|
}
|
|
|
|
}
|
2018-03-29 18:11:28 +03:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub enum Color {
|
|
|
|
Green,
|
|
|
|
Yellow,
|
|
|
|
Red,
|
|
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn cycle(color: Color) -> Color {
|
|
|
|
match color {
|
|
|
|
Color::Green => Color::Yellow,
|
|
|
|
Color::Yellow => Color::Red,
|
|
|
|
Color::Red => Color::Green,
|
|
|
|
}
|
|
|
|
}
|
2018-03-29 11:47:44 +03:00
|
|
|
"#)
|
|
|
|
.file("test.js", r#"
|
|
|
|
const assert = require('assert');
|
|
|
|
|
|
|
|
var called = false;
|
|
|
|
|
|
|
|
module.exports.hit = function() {
|
|
|
|
called = true;
|
|
|
|
};
|
|
|
|
|
2018-03-30 00:49:36 +03:00
|
|
|
const { run, Foo, Color, cycle } = require('./out');
|
|
|
|
|
2018-03-29 11:47:44 +03:00
|
|
|
module.exports.test = function() {
|
|
|
|
run();
|
|
|
|
assert.strictEqual(called, true);
|
2018-03-29 13:12:54 +03:00
|
|
|
|
|
|
|
var r = Foo.new();
|
|
|
|
assert.strictEqual(r.add(0), 0);
|
|
|
|
assert.strictEqual(r.add(1), 1);
|
2018-03-30 00:49:36 +03:00
|
|
|
assert.strictEqual(r.add(2), 3);
|
2018-03-29 13:12:54 +03:00
|
|
|
r.free();
|
|
|
|
|
|
|
|
var r2 = Foo.with_contents(10);
|
2018-03-30 00:49:36 +03:00
|
|
|
assert.strictEqual(r2.add(0), 10);
|
|
|
|
assert.strictEqual(r2.add(1), 11);
|
|
|
|
assert.strictEqual(r2.add(2), 13);
|
2018-03-29 13:12:54 +03:00
|
|
|
r2.free();
|
2018-03-29 18:11:28 +03:00
|
|
|
|
|
|
|
assert.strictEqual(Color.Green, 0);
|
|
|
|
assert.strictEqual(Color.Yellow, 1);
|
|
|
|
assert.strictEqual(Color.Red, 2);
|
|
|
|
assert.strictEqual(Object.keys(Color).length, 3);
|
2018-03-30 00:49:36 +03:00
|
|
|
assert.strictEqual(cycle(Color.Green), Color.Yellow);
|
2018-03-29 11:47:44 +03:00
|
|
|
};
|
2018-03-29 13:12:54 +03:00
|
|
|
|
2018-03-29 11:47:44 +03:00
|
|
|
"#)
|
|
|
|
.test();
|
|
|
|
}
|