2017-12-15 06:31:01 +03:00
|
|
|
extern crate test_support;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add() {
|
|
|
|
test_support::project()
|
|
|
|
.file("src/lib.rs", r#"
|
|
|
|
#![feature(proc_macro)]
|
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
|
|
|
wasm_bindgen! {
|
|
|
|
pub fn add(a: u32, b: u32) -> u32 {
|
|
|
|
a + b
|
|
|
|
}
|
2017-12-15 07:07:26 +03:00
|
|
|
|
|
|
|
pub fn add3(a: u32) -> u32 {
|
|
|
|
a + 3
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get2() -> u32 {
|
|
|
|
2
|
|
|
|
}
|
2017-12-15 06:31:01 +03:00
|
|
|
}
|
|
|
|
"#)
|
2017-12-20 06:06:48 +03:00
|
|
|
.file("test.ts", r#"
|
2017-12-15 06:31:01 +03:00
|
|
|
import * as assert from "assert";
|
2017-12-20 06:06:48 +03:00
|
|
|
import { Exports, Imports } from "./out";
|
|
|
|
|
|
|
|
export const imports: Imports = {};
|
2017-12-15 06:31:01 +03:00
|
|
|
|
2017-12-20 06:06:48 +03:00
|
|
|
export function test(wasm: Exports) {
|
2017-12-15 08:55:21 +03:00
|
|
|
assert.strictEqual(wasm.add(1, 2), 3);
|
|
|
|
assert.strictEqual(wasm.add(2, 3), 5);
|
|
|
|
assert.strictEqual(wasm.add3(2), 5);
|
|
|
|
assert.strictEqual(wasm.get2(), 2);
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.test();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string_arguments() {
|
|
|
|
test_support::project()
|
|
|
|
.file("src/lib.rs", r#"
|
|
|
|
#![feature(proc_macro)]
|
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
|
|
|
wasm_bindgen! {
|
|
|
|
pub fn assert_foo_and_bar(a: &str, b: &str) {
|
|
|
|
assert_eq!(a, "foo2");
|
|
|
|
assert_eq!(b, "bar");
|
|
|
|
}
|
2017-12-19 03:13:37 +03:00
|
|
|
|
|
|
|
pub fn assert_foo(a: &str) {
|
|
|
|
assert_eq!(a, "foo");
|
|
|
|
}
|
2017-12-15 08:55:21 +03:00
|
|
|
}
|
|
|
|
"#)
|
2017-12-20 06:06:48 +03:00
|
|
|
.file("test.ts", r#"
|
|
|
|
import { Exports, Imports } from "./out";
|
|
|
|
|
|
|
|
export const imports: Imports = {};
|
|
|
|
|
|
|
|
export function test(wasm: Exports) {
|
2017-12-15 08:55:21 +03:00
|
|
|
wasm.assert_foo("foo");
|
|
|
|
wasm.assert_foo_and_bar("foo2", "bar");
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.test();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn return_a_string() {
|
|
|
|
test_support::project()
|
|
|
|
.file("src/lib.rs", r#"
|
|
|
|
#![feature(proc_macro)]
|
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
|
|
|
wasm_bindgen! {
|
|
|
|
pub fn clone(a: &str) -> String {
|
|
|
|
a.to_string()
|
|
|
|
}
|
2017-12-15 08:56:40 +03:00
|
|
|
}
|
2017-12-15 08:55:21 +03:00
|
|
|
|
2017-12-15 08:56:40 +03:00
|
|
|
wasm_bindgen! {
|
2017-12-15 08:55:21 +03:00
|
|
|
pub fn concat(a: &str, b: &str, c: i8) -> String {
|
|
|
|
format!("{} {} {}", a, b, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#)
|
2017-12-20 06:06:48 +03:00
|
|
|
.file("test.ts", r#"
|
2017-12-15 08:55:21 +03:00
|
|
|
import * as assert from "assert";
|
2017-12-20 06:06:48 +03:00
|
|
|
import { Exports, Imports } from "./out";
|
2017-12-15 08:55:21 +03:00
|
|
|
|
2017-12-20 06:06:48 +03:00
|
|
|
export const imports: Imports = {};
|
|
|
|
|
|
|
|
export function test(wasm: Exports) {
|
2017-12-15 08:55:21 +03:00
|
|
|
assert.strictEqual(wasm.clone("foo"), "foo");
|
|
|
|
assert.strictEqual(wasm.clone("another"), "another");
|
|
|
|
assert.strictEqual(wasm.concat("a", "b", 3), "a b 3");
|
|
|
|
assert.strictEqual(wasm.concat("c", "d", -2), "c d -2");
|
2017-12-15 06:31:01 +03:00
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.test();
|
|
|
|
}
|
2017-12-19 01:44:09 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn exceptions() {
|
|
|
|
test_support::project()
|
|
|
|
.file("src/lib.rs", r#"
|
|
|
|
#![feature(proc_macro)]
|
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
|
|
|
wasm_bindgen! {
|
|
|
|
pub fn foo(_a: u32) {}
|
|
|
|
pub fn bar(_a: &str) {}
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.file("test.js", r#"
|
2017-12-20 06:06:48 +03:00
|
|
|
var assert = require("assert");
|
2017-12-19 01:44:09 +03:00
|
|
|
|
2017-12-20 06:06:48 +03:00
|
|
|
exports.imports = {};
|
|
|
|
exports.test = function(wasm) {
|
2017-12-19 01:44:09 +03:00
|
|
|
assert.throws(() => wasm.foo('a'), /expected a number argument/);
|
|
|
|
assert.throws(() => wasm.bar(3), /expected a string argument/);
|
2017-12-20 06:06:48 +03:00
|
|
|
};
|
|
|
|
"#)
|
|
|
|
.file("test.d.ts", r#"
|
|
|
|
import { Exports, Imports } from "./out";
|
|
|
|
|
|
|
|
export const imports: Imports;
|
|
|
|
|
|
|
|
export function test(wasm: Exports): void;
|
2017-12-19 01:44:09 +03:00
|
|
|
"#)
|
|
|
|
.test();
|
|
|
|
}
|