2018-04-10 01:32:06 +03:00
|
|
|
use super::project;
|
2018-01-01 01:44:44 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn works() {
|
2018-04-10 01:32:06 +03:00
|
|
|
project()
|
2018-06-28 08:42:34 +03:00
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2018-03-15 00:33:53 +03:00
|
|
|
#![feature(proc_macro, wasm_custom_section)]
|
2018-01-01 01:44:44 +03:00
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
2018-02-08 03:41:33 +03:00
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn foo() -> JsValue {
|
2018-02-08 03:41:33 +03:00
|
|
|
JsValue::from("foo")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn bar(s: &str) -> JsValue {
|
2018-02-08 03:41:33 +03:00
|
|
|
JsValue::from(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn baz() -> JsValue {
|
2018-02-08 03:41:33 +03:00
|
|
|
JsValue::from(1.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn baz2(a: &JsValue, b: &JsValue) {
|
2018-02-08 03:41:33 +03:00
|
|
|
assert_eq!(a.as_f64(), Some(2.0));
|
|
|
|
assert_eq!(b.as_f64(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn js_null() -> JsValue {
|
2018-02-08 03:41:33 +03:00
|
|
|
JsValue::null()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn js_undefined() -> JsValue {
|
2018-02-08 03:41:33 +03:00
|
|
|
JsValue::undefined()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn test_is_null_undefined(
|
2018-02-08 03:41:33 +03:00
|
|
|
a: &JsValue,
|
|
|
|
b: &JsValue,
|
|
|
|
c: &JsValue,
|
|
|
|
) {
|
|
|
|
assert!(a.is_null());
|
|
|
|
assert!(!a.is_undefined());
|
|
|
|
|
|
|
|
assert!(!b.is_null());
|
|
|
|
assert!(b.is_undefined());
|
|
|
|
|
|
|
|
assert!(!c.is_null());
|
|
|
|
assert!(!c.is_undefined());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn get_true() -> JsValue {
|
2018-02-08 03:41:33 +03:00
|
|
|
JsValue::from(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn get_false() -> JsValue {
|
2018-02-08 03:41:33 +03:00
|
|
|
JsValue::from(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn test_bool(
|
2018-02-08 03:41:33 +03:00
|
|
|
a: &JsValue,
|
|
|
|
b: &JsValue,
|
|
|
|
c: &JsValue,
|
|
|
|
) {
|
|
|
|
assert_eq!(a.as_bool(), Some(true));
|
|
|
|
assert_eq!(b.as_bool(), Some(false));
|
|
|
|
assert_eq!(c.as_bool(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn mk_symbol() -> JsValue {
|
2018-02-08 03:41:33 +03:00
|
|
|
let a = JsValue::symbol(None);
|
|
|
|
assert!(a.is_symbol());
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn mk_symbol2(s: &str) -> JsValue {
|
2018-02-08 03:41:33 +03:00
|
|
|
let a = JsValue::symbol(Some(s));
|
|
|
|
assert!(a.is_symbol());
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn assert_symbols(a: &JsValue, b: &JsValue) {
|
2018-02-08 03:41:33 +03:00
|
|
|
assert!(a.is_symbol());
|
|
|
|
assert!(!b.is_symbol());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn acquire_string(a: &JsValue, b: &JsValue) {
|
2018-02-08 03:41:33 +03:00
|
|
|
assert_eq!(a.as_string().unwrap(), "foo");
|
|
|
|
assert_eq!(b.as_string(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn acquire_string2(a: &JsValue) -> String {
|
2018-02-08 03:41:33 +03:00
|
|
|
a.as_string().unwrap_or("wrong".to_string())
|
2018-01-01 01:44:44 +03:00
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file(
|
|
|
|
"test.ts",
|
|
|
|
r#"
|
2018-01-01 01:44:44 +03:00
|
|
|
import * as assert from "assert";
|
2018-01-30 08:20:38 +03:00
|
|
|
import * as wasm from "./out";
|
2018-01-01 01:44:44 +03:00
|
|
|
|
2018-01-30 08:20:38 +03:00
|
|
|
export function test() {
|
2018-01-01 01:44:44 +03:00
|
|
|
assert.strictEqual(wasm.foo(), 'foo');
|
|
|
|
assert.strictEqual(wasm.bar('a'), 'a');
|
2018-01-01 02:45:47 +03:00
|
|
|
assert.strictEqual(wasm.baz(), 1);
|
|
|
|
wasm.baz2(2, 'a');
|
|
|
|
|
|
|
|
assert.strictEqual(wasm.js_null(), null);
|
|
|
|
assert.strictEqual(wasm.js_undefined(), undefined);
|
|
|
|
|
|
|
|
wasm.test_is_null_undefined(null, undefined, 1.0);
|
|
|
|
|
|
|
|
assert.strictEqual(wasm.get_true(), true);
|
|
|
|
assert.strictEqual(wasm.get_false(), false);
|
|
|
|
wasm.test_bool(true, false, 1.0);
|
|
|
|
|
|
|
|
assert.strictEqual(typeof(wasm.mk_symbol()), 'symbol');
|
|
|
|
assert.strictEqual(typeof(wasm.mk_symbol2('a')), 'symbol');
|
2018-01-30 08:20:38 +03:00
|
|
|
assert.strictEqual((Symbol as any).keyFor(wasm.mk_symbol()), undefined);
|
|
|
|
assert.strictEqual((Symbol as any).keyFor(wasm.mk_symbol2('b')), undefined);
|
2018-01-01 02:45:47 +03:00
|
|
|
|
2018-01-30 08:20:38 +03:00
|
|
|
wasm.assert_symbols((Symbol as any)(), 'a');
|
2018-01-01 02:45:47 +03:00
|
|
|
wasm.acquire_string('foo', null)
|
|
|
|
assert.strictEqual(wasm.acquire_string2(''), '');
|
|
|
|
assert.strictEqual(wasm.acquire_string2('a'), 'a');
|
2018-01-01 01:44:44 +03:00
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
2018-01-01 01:44:44 +03:00
|
|
|
.test();
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:47:45 +03:00
|
|
|
#[test]
|
|
|
|
fn eq_works() {
|
|
|
|
project()
|
2018-06-28 08:42:34 +03:00
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2018-06-02 00:47:45 +03:00
|
|
|
#![feature(proc_macro, wasm_custom_section)]
|
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn test(a: &JsValue, b: &JsValue) -> bool {
|
|
|
|
a == b
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn test1(a: &JsValue) -> bool {
|
|
|
|
a == a
|
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file(
|
|
|
|
"test.ts",
|
|
|
|
r#"
|
2018-06-02 00:47:45 +03:00
|
|
|
import * as assert from "assert";
|
|
|
|
import * as wasm from "./out";
|
|
|
|
|
|
|
|
export function test() {
|
|
|
|
assert.strictEqual(wasm.test('a', 'a'), true);
|
|
|
|
assert.strictEqual(wasm.test('a', 'b'), false);
|
|
|
|
assert.strictEqual(wasm.test(NaN, NaN), false);
|
|
|
|
assert.strictEqual(wasm.test({a: 'a'}, {a: 'a'}), false);
|
|
|
|
assert.strictEqual(wasm.test1(NaN), false);
|
|
|
|
let x = {a: 'a'};
|
|
|
|
assert.strictEqual(wasm.test(x, x), true);
|
|
|
|
assert.strictEqual(wasm.test1(x), true);
|
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
2018-06-02 00:47:45 +03:00
|
|
|
.test();
|
|
|
|
}
|