1
1
use super :: project;
2
2
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
+
3
19
#[ test]
4
20
fn top_level_enum ( ) {
5
21
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
+ )
6
45
. file (
7
- "shape.webidl " ,
46
+ "src/lib.rs " ,
8
47
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
+ }
19
67
"# ,
20
68
)
69
+ . test ( ) ;
70
+ }
71
+
72
+ #[ test]
73
+ fn valid_enum_return ( ) {
74
+ project ( )
75
+ . file ( "shape.webidl" , SHAPE_IDL )
21
76
. file (
22
77
"shape.mjs" ,
23
78
r#"
@@ -33,6 +88,10 @@ fn top_level_enum() {
33
88
isCircle() {
34
89
return this.kind === 'circle';
35
90
}
91
+
92
+ getShape() {
93
+ return this.kind;
94
+ }
36
95
}
37
96
"# ,
38
97
)
@@ -55,8 +114,64 @@ fn top_level_enum() {
55
114
let square = Shape::new(ShapeType::Square).unwrap();
56
115
assert!(circle.is_circle());
57
116
assert!(!circle.is_square());
117
+ assert_eq!(circle.get_shape(), ShapeType::Circle);
58
118
assert!(square.is_square());
59
119
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
+ };
60
175
}
61
176
"# ,
62
177
)
0 commit comments