@@ -69,46 +69,38 @@ mod tests {
69
69
use crate :: ast_to_token_tree;
70
70
71
71
#[ test]
72
- fn test_invalid_parse ( ) {
73
- expect_err ( "invalid" , "expected subtree" ) ;
74
-
75
- is_valid ( "($i:ident) => ()" ) ;
76
- is_valid ( "($($i:ident)*) => ($_)" ) ;
77
- is_valid ( "($($true:ident)*) => ($true)" ) ;
78
- is_valid ( "($($false:ident)*) => ($false)" ) ;
79
-
80
- expect_err ( "$i:ident => ()" , "expected subtree" ) ;
81
- expect_err ( "($i:ident) ()" , "expected `=`" ) ;
82
- expect_err ( "($($i:ident)_) => ()" , "invalid repeat" ) ;
72
+ fn test_valid_arms ( ) {
73
+ fn check ( macro_body : & str ) {
74
+ let m = parse_macro_arm ( macro_body) ;
75
+ m. unwrap ( ) ;
76
+ }
83
77
84
- expect_err ( "($i) => ($i)" , "invalid macro definition" ) ;
85
- expect_err ( "($i:) => ($i)" , "invalid macro definition" ) ;
78
+ check ( "($i:ident) => ()" ) ;
79
+ check ( "($($i:ident)*) => ($_)" ) ;
80
+ check ( "($($true:ident)*) => ($true)" ) ;
81
+ check ( "($($false:ident)*) => ($false)" ) ;
86
82
}
87
83
88
- fn expect_err ( macro_body : & str , expected : & str ) {
89
- assert_eq ! (
90
- create_rules ( & format_macro ( macro_body ) ) ,
91
- Err ( ParseError :: Expected ( String :: from ( expected ) ) )
92
- ) ;
93
- }
84
+ # [ test ]
85
+ fn test_invalid_arms ( ) {
86
+ fn check ( macro_body : & str , err : & str ) {
87
+ let m = parse_macro_arm ( macro_body ) ;
88
+ assert_eq ! ( m , Err ( ParseError :: Expected ( String :: from ( err ) ) ) ) ;
89
+ }
94
90
95
- fn is_valid ( macro_body : & str ) {
96
- assert ! ( create_rules( & format_macro( macro_body) ) . is_ok( ) ) ;
97
- }
91
+ check ( "invalid" , "expected subtree" ) ;
92
+
93
+ check ( "$i:ident => ()" , "expected subtree" ) ;
94
+ check ( "($i:ident) ()" , "expected `=`" ) ;
95
+ check ( "($($i:ident)_) => ()" , "invalid repeat" ) ;
98
96
99
- fn format_macro ( macro_body : & str ) -> String {
100
- format ! (
101
- "
102
- macro_rules! foo {{
103
- {}
104
- }}
105
- " ,
106
- macro_body
107
- )
97
+ check ( "($i) => ($i)" , "invalid macro definition" ) ;
98
+ check ( "($i:) => ($i)" , "invalid macro definition" ) ;
108
99
}
109
100
110
- fn create_rules ( macro_definition : & str ) -> Result < crate :: MacroRules , ParseError > {
111
- let source_file = ast:: SourceFile :: parse ( macro_definition) . ok ( ) . unwrap ( ) ;
101
+ fn parse_macro_arm ( arm_definition : & str ) -> Result < crate :: MacroRules , ParseError > {
102
+ let macro_definition = format ! ( " macro_rules! m {{ {} }} " , arm_definition) ;
103
+ let source_file = ast:: SourceFile :: parse ( & macro_definition) . ok ( ) . unwrap ( ) ;
112
104
let macro_definition =
113
105
source_file. syntax ( ) . descendants ( ) . find_map ( ast:: MacroCall :: cast) . unwrap ( ) ;
114
106
0 commit comments