mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-24 10:11:34 +00:00
webidl: initial enum support
Add enum support to the WebIDL interface generator.
This commit is contained in:
64
crates/webidl/tests/all/enums.rs
Normal file
64
crates/webidl/tests/all/enums.rs
Normal file
@ -0,0 +1,64 @@
|
||||
use super::project;
|
||||
|
||||
#[test]
|
||||
fn top_level_enum() {
|
||||
project()
|
||||
.file(
|
||||
"shape.webidl",
|
||||
r#"
|
||||
enum ShapeType { "circle", "square" };
|
||||
|
||||
[Constructor(ShapeType kind)]
|
||||
interface Shape {
|
||||
[Pure]
|
||||
boolean isSquare();
|
||||
|
||||
[Pure]
|
||||
boolean isCircle();
|
||||
};
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"shape.mjs",
|
||||
r#"
|
||||
export class Shape {
|
||||
constructor(kind) {
|
||||
this.kind = kind;
|
||||
}
|
||||
|
||||
isSquare() {
|
||||
return this.kind === 'square';
|
||||
}
|
||||
|
||||
isCircle() {
|
||||
return this.kind === 'circle';
|
||||
}
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
pub mod shape;
|
||||
|
||||
use shape::{Shape, ShapeType};
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn test() {
|
||||
let circle = Shape::new(ShapeType::Circle);
|
||||
let square = Shape::new(ShapeType::Square);
|
||||
assert!(circle.is_circle());
|
||||
assert!(!circle.is_square());
|
||||
assert!(square.is_square());
|
||||
assert!(!square.is_circle());
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test();
|
||||
}
|
@ -2,3 +2,4 @@ extern crate wasm_bindgen_test_project_builder as project_builder;
|
||||
use project_builder::project;
|
||||
|
||||
mod simple;
|
||||
mod enums;
|
||||
|
Reference in New Issue
Block a user