Skip to content

Commit

Permalink
Add support to assert_parse for testing parse failures.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimblandy committed Dec 11, 2016
1 parent 39cfb0b commit 785eafa
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/testing.rs
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
Expand All @@ -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.

Copy link
@fitzgen

fitzgen Dec 11, 2016

Member

Sounds like a bug.

This comment has been minimized.

Copy link
@fitzgen

fitzgen Dec 11, 2016

Member

rust-lang-deprecated/error-chain#95 was closed. Reading the comments, it seems justified, but not satisfying. Maybe we should manually impl it ourselves in the error mod.

This comment has been minimized.

Copy link
@jimblandy

jimblandy Dec 12, 2016

Author Collaborator

Manually implementing PartialEq on an enum is... a total pain in the neck, isn't it? Is there anything other than std::mem::discriminant or a big dumb match statement?

Anyway, as it turned out, the match statement wasn't so bad.

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);
}
}
}
}

0 comments on commit 785eafa

Please sign in to comment.