Skip to content

Commit

Permalink
Fixed testing macro bug.
Browse files Browse the repository at this point in the history
Fixed a bug where the remaining tokens in the iterator were not
checked.
  • Loading branch information
dragostis committed Jun 28, 2017
1 parent 8e45ba8 commit 65ffafa
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions pest/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
#[doc(hidden)]
#[macro_export]
macro_rules! consumes_to {
( $_rules:ident, $tokens:expr, [] ) => {
let rest: Vec<_> = $tokens.map(|r| r.unwrap()).collect();

assert!(rest.is_empty(), format!("expected end of stream, but found {:?}", rest));
};
( $_rules:ident, $tokens:expr, [] ) => ();
( $rules:ident, $tokens:expr, [ $name:ident ( $start:expr, $end:expr ) ] ) => {
let expected = format!("expected Start {{ rule: {:?}, pos: Position {{ pos: {} }} }}",
$rules::$name, $start);
Expand Down Expand Up @@ -200,11 +196,18 @@ macro_rules! parses_to {
( parser: $parser:ident, input: $string:expr, rule: $rules:tt :: $rule:tt,
tokens: [ $( $names:ident $calls:tt ),* ] ) => {

let input = $crate::inputs::StringInput::new($string.to_owned());
let mut tokens = $parser::parse($rules::$rule,
::std::rc::Rc::new(input)).unwrap().into_iter();
#[allow(unused_mut)]
{
let input = $crate::inputs::StringInput::new($string.to_owned());
let mut tokens = $parser::parse($rules::$rule,
::std::rc::Rc::new(input)).unwrap().into_iter();

consumes_to!($rules, &mut tokens, [ $( $names $calls ),* ]);

let rest: Vec<_> = tokens.collect();

consumes_to!($rules, tokens, [ $( $names $calls ),* ]);
assert!(rest.is_empty(), format!("expected end of stream, but found {:?}", rest));
}
};
}

Expand Down Expand Up @@ -257,4 +260,30 @@ mod tests {
]
};
}

#[test]
#[should_panic]
fn missing_end() {
parses_to! {
parser: AbcParser,
input: "abcde",
rule: Rule::a,
tokens: [
a(0, 3, [
b(1, 2)
])
]
};
}

#[test]
#[should_panic]
fn empty() {
parses_to! {
parser: AbcParser,
input: "abcde",
rule: Rule::a,
tokens: []
};
}
}

0 comments on commit 65ffafa

Please sign in to comment.