1 use self::inner::ColorWithCustomValues;
2 use wasm_bindgen::prelude::*;
3 use wasm_bindgen_test::*;
4
5 #[wasm_bindgen(module = "tests/wasm/enums.js")]
6 extern "C" {
js_c_style_enum()7 fn js_c_style_enum();
js_c_style_enum_with_custom_values()8 fn js_c_style_enum_with_custom_values();
js_handle_optional_enums(x: Option<Color>) -> Option<Color>9 fn js_handle_optional_enums(x: Option<Color>) -> Option<Color>;
js_expect_enum(x: Color, y: Option<Color>)10 fn js_expect_enum(x: Color, y: Option<Color>);
js_expect_enum_none(x: Option<Color>)11 fn js_expect_enum_none(x: Option<Color>);
12 }
13
14 #[wasm_bindgen]
15 #[derive(PartialEq, Debug)]
16 pub enum Color {
17 Green,
18 Yellow,
19 Red,
20 }
21
22 pub mod inner {
23 use wasm_bindgen::prelude::*;
24
25 #[wasm_bindgen]
26 pub enum ColorWithCustomValues {
27 Green = 21,
28 Yellow = 34,
29 Red = 2,
30 }
31 }
32
33 #[wasm_bindgen]
enum_cycle(color: Color) -> Color34 pub fn enum_cycle(color: Color) -> Color {
35 match color {
36 Color::Green => Color::Yellow,
37 Color::Yellow => Color::Red,
38 Color::Red => Color::Green,
39 }
40 }
41
42 #[wasm_bindgen]
enum_with_custom_values_cycle(color: ColorWithCustomValues) -> ColorWithCustomValues43 pub fn enum_with_custom_values_cycle(color: ColorWithCustomValues) -> ColorWithCustomValues {
44 match color {
45 ColorWithCustomValues::Green => ColorWithCustomValues::Yellow,
46 ColorWithCustomValues::Yellow => ColorWithCustomValues::Red,
47 ColorWithCustomValues::Red => ColorWithCustomValues::Green,
48 }
49 }
50
51 #[wasm_bindgen_test]
c_style_enum()52 fn c_style_enum() {
53 js_c_style_enum();
54 }
55
56 #[wasm_bindgen_test]
c_style_enum_with_custom_values()57 fn c_style_enum_with_custom_values() {
58 js_c_style_enum_with_custom_values();
59 }
60
61 #[wasm_bindgen]
handle_optional_enums(x: Option<Color>) -> Option<Color>62 pub fn handle_optional_enums(x: Option<Color>) -> Option<Color> {
63 x
64 }
65
66 #[wasm_bindgen_test]
test_optional_enums()67 fn test_optional_enums() {
68 use self::Color::*;
69
70 assert_eq!(js_handle_optional_enums(None), None);
71 assert_eq!(js_handle_optional_enums(Some(Green)), Some(Green));
72 assert_eq!(js_handle_optional_enums(Some(Yellow)), Some(Yellow));
73 assert_eq!(js_handle_optional_enums(Some(Red)), Some(Red));
74 }
75
76 #[wasm_bindgen_test]
test_optional_enum_values()77 fn test_optional_enum_values() {
78 use self::Color::*;
79
80 js_expect_enum(Green, Some(Green));
81 js_expect_enum(Yellow, Some(Yellow));
82 js_expect_enum(Red, Some(Red));
83 js_expect_enum_none(None);
84 }
85