Skip to content

Commit 577eb32

Browse files
committed
Added tests (rustwasm#2806)
1 parent 0f754ed commit 577eb32

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

tests/wasm/js_keywords.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const wasm = require("wasm-bindgen-test.js");
2+
const assert = require("assert");
3+
4+
exports.js_keywords_compile = () => {
5+
assert.strictEqual(wasm._throw(1), 1);
6+
assert.strictEqual(wasm._class(1, 2), false);
7+
assert.strictEqual(wasm.classy(3), 3);
8+
let obj = new wasm.Class("class");
9+
assert.strictEqual(wasm.Class.void("string"), "string");
10+
assert.strictEqual(obj.catch, "class");
11+
assert.strictEqual(obj.instanceof("Class"), "class is instance of Class");
12+
};
13+
14+
exports.test_keyword_1_as_fn_name = (x) => {
15+
return wasm._throw(x);
16+
};
17+
18+
exports.test_keyword_2_as_fn_name = (x, y) => {
19+
return wasm._class(x, y);
20+
};
21+
22+
exports.test_keyword_as_fn_arg = (x) => {
23+
return wasm.classy(x);
24+
};

tests/wasm/js_keywords.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use wasm_bindgen::prelude::*;
2+
use wasm_bindgen_test::*;
3+
4+
#[wasm_bindgen(module = "tests/wasm/js_keywords.js")]
5+
extern "C" {
6+
fn js_keywords_compile();
7+
fn test_keyword_1_as_fn_name(x: u8) -> u8;
8+
fn test_keyword_2_as_fn_name(x: u8, y: u8) -> bool;
9+
fn test_keyword_as_fn_arg(x: u8) -> u8;
10+
}
11+
12+
#[wasm_bindgen]
13+
pub fn throw(class: u8) -> u8 {
14+
class
15+
}
16+
17+
#[wasm_bindgen(js_name = class)]
18+
pub fn fn_parsed_to_keyword(instanceof: u8, catch: u8) -> bool {
19+
instanceof > catch
20+
}
21+
22+
#[wasm_bindgen(js_name = classy)]
23+
pub fn arg_is_keyword(class: u8) -> u8 {
24+
class
25+
}
26+
27+
#[wasm_bindgen]
28+
struct Class {
29+
name: String,
30+
}
31+
#[wasm_bindgen]
32+
impl Class {
33+
#[wasm_bindgen(constructor)]
34+
pub fn new(void: String) -> Self {
35+
Class { name: void }
36+
}
37+
pub fn instanceof(&self, class: String) -> String {
38+
format!("{} is instance of {}", self.name.clone(), class)
39+
}
40+
#[wasm_bindgen(getter)]
41+
pub fn catch(&self) -> String {
42+
self.name.clone()
43+
}
44+
pub fn void(void: String) -> String {
45+
void
46+
}
47+
}
48+
49+
#[wasm_bindgen_test]
50+
fn compile() {
51+
js_keywords_compile();
52+
assert_eq!(test_keyword_1_as_fn_name(1), 1);
53+
assert_eq!(test_keyword_2_as_fn_name(1, 2), false);
54+
assert_eq!(test_keyword_as_fn_arg(1), 1);
55+
}

tests/wasm/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub mod futures;
2727
pub mod getters_and_setters;
2828
pub mod import_class;
2929
pub mod imports;
30+
pub mod js_keywords;
3031
pub mod js_objects;
3132
pub mod jscast;
3233
pub mod math;

0 commit comments

Comments
 (0)