File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed
src/tools/compiletest/src Expand file tree Collapse file tree 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 {
5151 }
5252}
5353
54+ // Make the macro visible outside of this module, for tests.
55+ #[ cfg( test) ]
56+ pub ( crate ) use string_enum;
57+
5458string_enum ! {
5559 #[ derive( Clone , Copy , PartialEq , Debug ) ]
5660 pub enum Mode {
Original file line number Diff line number Diff line change @@ -66,3 +66,30 @@ fn is_test_test() {
6666 assert ! ( !is_test( & OsString :: from( "#a_dog_gif" ) ) ) ;
6767 assert ! ( !is_test( & OsString :: from( "~a_temp_file" ) ) ) ;
6868}
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