diff --git a/crates/ra_mbe/src/mbe_parser.rs b/crates/ra_mbe/src/mbe_parser.rs index 933d5154d126..957571835dc8 100644 --- a/crates/ra_mbe/src/mbe_parser.rs +++ b/crates/ra_mbe/src/mbe_parser.rs @@ -69,46 +69,38 @@ mod tests { use crate::ast_to_token_tree; #[test] - fn test_invalid_parse() { - expect_err("invalid", "expected subtree"); - - is_valid("($i:ident) => ()"); - is_valid("($($i:ident)*) => ($_)"); - is_valid("($($true:ident)*) => ($true)"); - is_valid("($($false:ident)*) => ($false)"); - - expect_err("$i:ident => ()", "expected subtree"); - expect_err("($i:ident) ()", "expected `=`"); - expect_err("($($i:ident)_) => ()", "invalid repeat"); + fn test_valid_arms() { + fn check(macro_body: &str) { + let m = parse_macro_arm(macro_body); + m.unwrap(); + } - expect_err("($i) => ($i)", "invalid macro definition"); - expect_err("($i:) => ($i)", "invalid macro definition"); + check("($i:ident) => ()"); + check("($($i:ident)*) => ($_)"); + check("($($true:ident)*) => ($true)"); + check("($($false:ident)*) => ($false)"); } - fn expect_err(macro_body: &str, expected: &str) { - assert_eq!( - create_rules(&format_macro(macro_body)), - Err(ParseError::Expected(String::from(expected))) - ); - } + #[test] + fn test_invalid_arms() { + fn check(macro_body: &str, err: &str) { + let m = parse_macro_arm(macro_body); + assert_eq!(m, Err(ParseError::Expected(String::from(err)))); + } - fn is_valid(macro_body: &str) { - assert!(create_rules(&format_macro(macro_body)).is_ok()); - } + check("invalid", "expected subtree"); + + check("$i:ident => ()", "expected subtree"); + check("($i:ident) ()", "expected `=`"); + check("($($i:ident)_) => ()", "invalid repeat"); - fn format_macro(macro_body: &str) -> String { - format!( - " - macro_rules! foo {{ - {} - }} -", - macro_body - ) + check("($i) => ($i)", "invalid macro definition"); + check("($i:) => ($i)", "invalid macro definition"); } - fn create_rules(macro_definition: &str) -> Result { - let source_file = ast::SourceFile::parse(macro_definition).ok().unwrap(); + fn parse_macro_arm(arm_definition: &str) -> Result { + let macro_definition = format!(" macro_rules! m {{ {} }} ", arm_definition); + let source_file = ast::SourceFile::parse(¯o_definition).ok().unwrap(); let macro_definition = source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();