Skip to content

Commit

Permalink
Add tests for explicit return out of {? } block
Browse files Browse the repository at this point in the history
Fixes #246
  • Loading branch information
kevinmehall committed Feb 15, 2021
1 parent ee74d96 commit d7a12a6
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tests/run-pass/conditional_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ peg::parser!( grammar parse() for str {
}
}

pub rule return_early() -> i32 = vs:$([_]+) {?
let v = vs.parse::<i32>().map_err(|_| "number")?;
if v > 100 {
return Err("smaller number");
}
Ok(v)
}
});

fn main() {
Expand All @@ -41,4 +48,8 @@ fn main() {
assert!(parse::xml("<a><b></b><c></c></a>").is_ok());
assert!(parse::xml("<a><b><c></b></c></a>").is_err());
assert!(parse::xml("<a><b></c><c></b></a>").is_err());

assert!(parse::return_early("a").unwrap_err().expected.tokens().any(|e| e == "number"));
assert!(parse::return_early("123").unwrap_err().expected.tokens().any(|e| e == "smaller number"));
assert_eq!(parse::return_early("99").unwrap(), 99);
}

0 comments on commit d7a12a6

Please sign in to comment.