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>);
js_renamed_enum(b: RenamedEnum)12     fn js_renamed_enum(b: RenamedEnum);
13 }
14 
15 #[wasm_bindgen]
16 #[derive(PartialEq, Debug)]
17 pub enum Color {
18     Green,
19     Yellow,
20     Red,
21 }
22 
23 pub mod inner {
24     use wasm_bindgen::prelude::*;
25 
26     #[wasm_bindgen]
27     pub enum ColorWithCustomValues {
28         Green = 21,
29         Yellow = 34,
30         Red = 2,
31     }
32 }
33 
34 #[wasm_bindgen(js_name = JsRenamedEnum)]
35 #[derive(Copy, Clone)]
36 pub enum RenamedEnum {
37     A = 10,
38     B = 20,
39 }
40 
41 #[wasm_bindgen]
enum_cycle(color: Color) -> Color42 pub fn enum_cycle(color: Color) -> Color {
43     match color {
44         Color::Green => Color::Yellow,
45         Color::Yellow => Color::Red,
46         Color::Red => Color::Green,
47     }
48 }
49 
50 #[wasm_bindgen]
enum_with_custom_values_cycle(color: ColorWithCustomValues) -> ColorWithCustomValues51 pub fn enum_with_custom_values_cycle(color: ColorWithCustomValues) -> ColorWithCustomValues {
52     match color {
53         ColorWithCustomValues::Green => ColorWithCustomValues::Yellow,
54         ColorWithCustomValues::Yellow => ColorWithCustomValues::Red,
55         ColorWithCustomValues::Red => ColorWithCustomValues::Green,
56     }
57 }
58 
59 #[wasm_bindgen_test]
c_style_enum()60 fn c_style_enum() {
61     js_c_style_enum();
62 }
63 
64 #[wasm_bindgen_test]
c_style_enum_with_custom_values()65 fn c_style_enum_with_custom_values() {
66     js_c_style_enum_with_custom_values();
67 }
68 
69 #[wasm_bindgen]
handle_optional_enums(x: Option<Color>) -> Option<Color>70 pub fn handle_optional_enums(x: Option<Color>) -> Option<Color> {
71     x
72 }
73 
74 #[wasm_bindgen_test]
test_optional_enums()75 fn test_optional_enums() {
76     use self::Color::*;
77 
78     assert_eq!(js_handle_optional_enums(None), None);
79     assert_eq!(js_handle_optional_enums(Some(Green)), Some(Green));
80     assert_eq!(js_handle_optional_enums(Some(Yellow)), Some(Yellow));
81     assert_eq!(js_handle_optional_enums(Some(Red)), Some(Red));
82 }
83 
84 #[wasm_bindgen_test]
test_optional_enum_values()85 fn test_optional_enum_values() {
86     use self::Color::*;
87 
88     js_expect_enum(Green, Some(Green));
89     js_expect_enum(Yellow, Some(Yellow));
90     js_expect_enum(Red, Some(Red));
91     js_expect_enum_none(None);
92 }
93 
94 #[wasm_bindgen_test]
test_renamed_enum()95 fn test_renamed_enum() {
96     js_renamed_enum(RenamedEnum::B);
97 }
98