Skip to content

Commit e01f16c

Browse files
committed
add more tests for WebIDL enums
1 parent 8a9831c commit e01f16c

File tree

1 file changed

+126
-11
lines changed

1 file changed

+126
-11
lines changed

crates/webidl/tests/all/enums.rs

Lines changed: 126 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,78 @@
11
use super::project;
22

3+
static SHAPE_IDL: &'static str = r#"
4+
enum ShapeType { "circle", "square" };
5+
6+
[Constructor(ShapeType kind)]
7+
interface Shape {
8+
[Pure]
9+
boolean isSquare();
10+
11+
[Pure]
12+
boolean isCircle();
13+
14+
[Pure]
15+
ShapeType getShape();
16+
};
17+
"#;
18+
319
#[test]
420
fn top_level_enum() {
521
project()
22+
.file("shape.webidl", SHAPE_IDL)
23+
.file(
24+
"shape.mjs",
25+
r#"
26+
export class Shape {
27+
constructor(kind) {
28+
this.kind = kind;
29+
}
30+
31+
isSquare() {
32+
return this.kind === 'square';
33+
}
34+
35+
isCircle() {
36+
return this.kind === 'circle';
37+
}
38+
39+
getShape() {
40+
return this.kind;
41+
}
42+
}
43+
"#,
44+
)
645
.file(
7-
"shape.webidl",
46+
"src/lib.rs",
847
r#"
9-
enum ShapeType { "circle", "square" };
10-
11-
[Constructor(ShapeType kind)]
12-
interface Shape {
13-
[Pure]
14-
boolean isSquare();
15-
16-
[Pure]
17-
boolean isCircle();
18-
};
48+
#![feature(use_extern_macros)]
49+
50+
extern crate wasm_bindgen;
51+
52+
use wasm_bindgen::prelude::*;
53+
54+
pub mod shape;
55+
56+
use shape::{Shape, ShapeType};
57+
58+
#[wasm_bindgen]
59+
pub fn test() {
60+
let circle = Shape::new(ShapeType::Circle).unwrap();
61+
let square = Shape::new(ShapeType::Square).unwrap();
62+
assert!(circle.is_circle());
63+
assert!(!circle.is_square());
64+
assert!(square.is_square());
65+
assert!(!square.is_circle());
66+
}
1967
"#,
2068
)
69+
.test();
70+
}
71+
72+
#[test]
73+
fn valid_enum_return() {
74+
project()
75+
.file("shape.webidl", SHAPE_IDL)
2176
.file(
2277
"shape.mjs",
2378
r#"
@@ -33,6 +88,10 @@ fn top_level_enum() {
3388
isCircle() {
3489
return this.kind === 'circle';
3590
}
91+
92+
getShape() {
93+
return this.kind;
94+
}
3695
}
3796
"#,
3897
)
@@ -55,8 +114,64 @@ fn top_level_enum() {
55114
let square = Shape::new(ShapeType::Square).unwrap();
56115
assert!(circle.is_circle());
57116
assert!(!circle.is_square());
117+
assert_eq!(circle.get_shape(), ShapeType::Circle);
58118
assert!(square.is_square());
59119
assert!(!square.is_circle());
120+
assert_eq!(square.get_shape(), ShapeType::Square);
121+
}
122+
"#,
123+
)
124+
.test();
125+
}
126+
127+
#[test]
128+
fn invalid_enum_return() {
129+
project()
130+
.file("shape.webidl", SHAPE_IDL)
131+
.file(
132+
"shape.mjs",
133+
r#"
134+
export class Shape {
135+
constructor(kind) {
136+
this.kind = 'triangle'; // <-- invalid ShapeType
137+
}
138+
139+
isSquare() {
140+
return this.kind === 'square';
141+
}
142+
143+
isCircle() {
144+
return this.kind === 'circle';
145+
}
146+
147+
getShape() {
148+
return this.kind;
149+
}
150+
}
151+
"#,
152+
)
153+
.file(
154+
"src/lib.rs",
155+
r#"
156+
#![feature(use_extern_macros)]
157+
158+
extern crate wasm_bindgen;
159+
160+
use wasm_bindgen::prelude::*;
161+
162+
pub mod shape;
163+
164+
use shape::{Shape, ShapeType};
165+
166+
#[wasm_bindgen]
167+
pub fn test() {
168+
let actually_a_triangle = Shape::new(ShapeType::Circle).unwrap();
169+
assert!(!actually_a_triangle.is_circle());
170+
assert!(!actually_a_triangle.is_square());
171+
match actually_a_triangle.get_shape() {
172+
ShapeType::Circle | ShapeType::Square => assert!(false),
173+
_ => {} // Success
174+
};
60175
}
61176
"#,
62177
)

0 commit comments

Comments
 (0)