mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-16 02:00:51 +00:00
Add test for enums
This commit is contained in:
parent
b78343a551
commit
3a270b6510
@ -1054,8 +1054,15 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
||||
passed_args.push_str(arg);
|
||||
};
|
||||
match *arg {
|
||||
shared::TYPE_ENUM | shared::TYPE_NUMBER => {
|
||||
// TODO: TS for Enum
|
||||
shared::TYPE_ENUM => {
|
||||
dst_ts.push_str(&format!(": {}", "any"));
|
||||
if self.cx.config.debug {
|
||||
self.cx.expose_assert_num();
|
||||
arg_conversions.push_str(&format!("_assertNum({});\n", name));
|
||||
}
|
||||
pass(&name)
|
||||
}
|
||||
shared::TYPE_NUMBER => {
|
||||
dst_ts.push_str(": number");
|
||||
if self.cx.config.debug {
|
||||
self.cx.expose_assert_num();
|
||||
@ -1160,6 +1167,10 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
||||
dst_ts.push_str(": void");
|
||||
format!("return ret;")
|
||||
}
|
||||
Some(shared::TYPE_ENUM) => {
|
||||
dst_ts.push_str(": any");
|
||||
format!("return ret;")
|
||||
}
|
||||
Some(shared::TYPE_NUMBER) => {
|
||||
dst_ts.push_str(": number");
|
||||
format!("return ret;")
|
||||
@ -1439,6 +1450,14 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
||||
self.cx.globals.push_str(&format!("export const {} = {{", enum_.name));
|
||||
self.cx.globals.push_str(&variants);
|
||||
self.cx.globals.push_str("}\n");
|
||||
self.cx.typescript.push_str(&format!("export enum {} {{", enum_.name));
|
||||
|
||||
variants.clear();
|
||||
for variant in enum_.variants.iter() {
|
||||
variants.push_str(&format!("{},", variant.name));
|
||||
}
|
||||
self.cx.typescript.push_str(&variants);
|
||||
self.cx.typescript.push_str("}\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
44
tests/enums.rs
Normal file
44
tests/enums.rs
Normal file
@ -0,0 +1,44 @@
|
||||
extern crate test_support;
|
||||
|
||||
#[test]
|
||||
fn c_style_enum() {
|
||||
test_support::project()
|
||||
.file("src/lib.rs", r#"
|
||||
#![feature(proc_macro)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub enum Color {
|
||||
Green,
|
||||
Yellow,
|
||||
Red,
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[wasm_bindgen]
|
||||
pub extern fn cycle(color: Color) -> Color {
|
||||
match color {
|
||||
Color::Green => Color::Yellow,
|
||||
Color::Yellow => Color::Red,
|
||||
Color::Red => Color::Green,
|
||||
}
|
||||
}
|
||||
"#)
|
||||
.file("test.ts", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
assert.strictEqual(wasm.Color.Green, 0);
|
||||
assert.strictEqual(wasm.Color.Yellow, 1);
|
||||
assert.strictEqual(wasm.Color.Red, 2);
|
||||
assert.strictEqual(Object.keys(wasm.Color).length, 3);
|
||||
|
||||
assert.strictEqual(wasm.cycle(wasm.Color.Green), wasm.Color.Yellow);
|
||||
}
|
||||
"#)
|
||||
.test();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user