-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to assert_parse for testing parse failures.
- Loading branch information
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
/// Try to parse something, and check the result. For example: | ||
/// | ||
/// assert_parse!(OperatorName: "quokka" => Ok(OperatorName::Question, "okka")); | ||
/// assert_parse!(OperatorName: "quokka" => Err(ErrorKind::UnexpectedEnd))); | ||
/// | ||
/// This asserts that calling `OperatorName::parse` on the text `"qu"` | ||
/// succeeds, producing the value `OperatorName::Question`, and leaving the | ||
|
@@ -25,5 +26,29 @@ macro_rules! assert_parse { | |
} | ||
} | ||
} | ||
}; | ||
|
||
($nonterm:ty : $input:expr => Err($ex_error:pat)) => { | ||
let input = $input as &[u8]; | ||
let input_printable = String::from_utf8_lossy(input).into_owned(); | ||
match <$nonterm>::parse(IndexStr::from(input)) { | ||
Err(err) => { | ||
// error_chain doesn't derive PartialEq for ErrorKind, | ||
// so we're stuck with this, which is ridiculous | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
fitzgen
Member
|
||
match *err.kind() { | ||
$ex_error => { }, | ||
_ => { | ||
panic!("Parsing {:?} as {} should fail with {},\n\ | ||
failed with {:?} instead", | ||
input_printable, stringify!($nonterm), stringify!($ex_error), err.kind()); | ||
} | ||
} | ||
} | ||
Ok((value, tail)) => { | ||
panic!("Parsing {:?} as {} should fail with {},\n\ | ||
but succeeded with value {:?}, tail {:?}", | ||
input_printable, stringify!($nonterm), stringify!($ex_error), value, tail); | ||
} | ||
} | ||
} | ||
} |
Sounds like a bug.