Skip to content

Commit 4eef8bd

Browse files
committed
cleanup tests
1 parent abeb51c commit 4eef8bd

File tree

1 file changed

+25
-33
lines changed

1 file changed

+25
-33
lines changed

crates/ra_mbe/src/mbe_parser.rs

+25-33
Original file line numberDiff line numberDiff line change
@@ -69,46 +69,38 @@ mod tests {
6969
use crate::ast_to_token_tree;
7070

7171
#[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+
}
8377

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)");
8682
}
8783

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+
}
9490

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");
9896

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");
10899
}
109100

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();
112104
let macro_definition =
113105
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
114106

0 commit comments

Comments
 (0)