Mercurial > lbo > hg > rex
changeset 47:34980229be32
Add first few tests to general test suite
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Thu, 18 Jul 2019 17:40:44 +0200 |
parents | cfc2c894ef2c |
children | 5e245f1851d7 |
files | src/lib.rs src/matching.rs src/tests.rs |
diffstat | 3 files changed, 27 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/lib.rs Thu Jul 18 17:31:14 2019 +0200 +++ b/src/lib.rs Thu Jul 18 17:40:44 2019 +0200 @@ -7,6 +7,8 @@ mod repr; mod state; +mod tests; + /// Compiles a parsed regular expression into the internal state graph and matches s against it. /// Returns whether the string matched as well as a list of submatches. The first submatch is the /// entire matched string. A submatch is a tuple of (start, end), where end is the index of the
--- a/src/matching.rs Thu Jul 18 17:31:14 2019 +0200 +++ b/src/matching.rs Thu Jul 18 17:40:44 2019 +0200 @@ -65,6 +65,9 @@ /// do_match starts the matching process. It tries to match the supplied compiled regex against the /// supplied string. If it fails, it skips ahead and tries later in the string (i.e., if the regex /// isn't anchored, it will do a full-text match). +/// +/// The boolean component is true if the match succeeded. The Vec contains tuples of (start, +/// one-past-end) for each submatch, starting with the implicit whole match. pub fn do_match(ws: WrappedState, s: &str) -> (bool, Vec<(usize, usize)>) { let mut ms = MatchState::new(s, ws); let (mut i, len) = (0, s.len());
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tests.rs Thu Jul 18 17:40:44 2019 +0200 @@ -0,0 +1,22 @@ +#![cfg(test)] + +//! A general test suite aiming for wide coverage of positive and negative matches. + +use crate::{compile, matching, parse}; + +fn match_re(re: &str, s: &str) -> (bool, Vec<(usize, usize)>) { + let parsed = parse::parse(re).unwrap(); + let ready = compile::start_compile(&parsed); + matching::do_match(ready, s) +} + +#[test] +fn test_simple_repeat() { + assert!(match_re("a+", "aaa").0); + assert!(match_re("aaa+", "aaa").0); + assert!(match_re("aa(a+)", "aaa").0); + assert_eq!(vec![(0, 3), (2, 3)], match_re("aa(a+)", "aaa").1); + assert_eq!(vec![(0, 3)], match_re("aaa+", "aaabcde").1); + assert!(!match_re("a+", "").0); + assert!(!match_re("aa+$", "aaabc").0); +}