Skip to content

Commit

Permalink
cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Sep 17, 2019
1 parent abeb51c commit 4eef8bd
Showing 1 changed file with 25 additions and 33 deletions.
58 changes: 25 additions & 33 deletions crates/ra_mbe/src/mbe_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<crate::MacroRules, ParseError> {
let source_file = ast::SourceFile::parse(macro_definition).ok().unwrap();
fn parse_macro_arm(arm_definition: &str) -> Result<crate::MacroRules, ParseError> {
let macro_definition = format!(" macro_rules! m {{ {} }} ", arm_definition);
let source_file = ast::SourceFile::parse(&macro_definition).ok().unwrap();
let macro_definition =
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();

Expand Down

0 comments on commit 4eef8bd

Please sign in to comment.