File tree 2 files changed +31
-0
lines changed
src/tools/compiletest/src
2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,10 @@ macro_rules! string_enum {
51
51
}
52
52
}
53
53
54
+ // Make the macro visible outside of this module, for tests.
55
+ #[ cfg( test) ]
56
+ pub ( crate ) use string_enum;
57
+
54
58
string_enum ! {
55
59
#[ derive( Clone , Copy , PartialEq , Debug ) ]
56
60
pub enum Mode {
Original file line number Diff line number Diff line change @@ -66,3 +66,30 @@ fn is_test_test() {
66
66
assert ! ( !is_test( & OsString :: from( "#a_dog_gif" ) ) ) ;
67
67
assert ! ( !is_test( & OsString :: from( "~a_temp_file" ) ) ) ;
68
68
}
69
+
70
+ #[ test]
71
+ fn string_enums ( ) {
72
+ // These imports are needed for the macro-generated code
73
+ use std:: fmt;
74
+ use std:: str:: FromStr ;
75
+
76
+ crate :: common:: string_enum! {
77
+ #[ derive( Clone , Copy , Debug , PartialEq ) ]
78
+ enum Animal {
79
+ Cat => "meow" ,
80
+ Dog => "woof" ,
81
+ }
82
+ }
83
+
84
+ // General assertions, mostly to silence the dead code warnings
85
+ assert_eq ! ( Animal :: VARIANTS . len( ) , 2 ) ;
86
+ assert_eq ! ( Animal :: STR_VARIANTS . len( ) , 2 ) ;
87
+
88
+ // Correct string conversions
89
+ assert_eq ! ( Animal :: Cat , "meow" . parse( ) . unwrap( ) ) ;
90
+ assert_eq ! ( Animal :: Dog , "woof" . parse( ) . unwrap( ) ) ;
91
+
92
+ // Invalid conversions
93
+ let animal = "nya" . parse :: < Animal > ( ) ;
94
+ assert ! ( matches!( animal, Err ( ( ) ) ) ) ;
95
+ }
You can’t perform that action at this time.
0 commit comments