Skip to content

Commit

Permalink
Add proptest tests.
Browse files Browse the repository at this point in the history
These can only really catch crashes in fm's code, but that's better than
nothing.
  • Loading branch information
ltratt committed May 20, 2024
1 parent 393dfb4 commit c1fc20b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ edition = "2021"

[dependencies]
regex = "1.8"

[dev-dependencies]
proptest = "1.0.0"
57 changes: 57 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ impl Error for FMatchError {
#[cfg(test)]
mod tests {
use super::*;
use proptest::proptest;

#[test]
fn defaults() {
Expand Down Expand Up @@ -1274,4 +1275,60 @@ Text (error at line 5):
assert!(!helper("a\na", "a\na "));
assert!(!helper("a\na ", "a\na"));
}

proptest! {
#[test]
fn proptest_basic(ptn in "[a-z ]{0,3}", text in "[a-z ]{0,3}") {
let helper = |ptn: &str, text: &str| -> bool {
FMBuilder::new(ptn)
.unwrap()
.build()
.unwrap()
.matches(text)
.is_ok()
};

helper(&ptn, &text);
}
}

#[test]
fn proptest_name_matcher() {
let ptn_re = Regex::new("\\$[0-9]+?\\b").unwrap();
let text_re = Regex::new("[a-z]+?\\b").unwrap();
proptest!(|(ptn in "([a-z ]|\\$[0-9]){0,10}", text in "[a-z ]{0,10}")| {
let helper = |ptn: &str, text: &str| -> bool {
FMBuilder::new(ptn)
.unwrap()
.name_matcher(ptn_re.clone(), text_re.clone())
.build()
.unwrap()
.matches(text)
.is_ok()
};

helper(&ptn, &text);
});
}

#[test]
fn proptest_name_matcher_ignore() {
let ptn_re = Regex::new("\\$[0-9]+?\\b").unwrap();
let ptn_ignore_re = Regex::new("\\$_\\b").unwrap();
let text_re = Regex::new("[a-z]+?\\b").unwrap();
proptest!(|(ptn in "([a-z ]|\\$[0-9_]){0,10}", text in "[a-z ]{0,10}")| {
let helper = |ptn: &str, text: &str| -> bool {
FMBuilder::new(ptn)
.unwrap()
.name_matcher(ptn_re.clone(), text_re.clone())
.name_matcher_ignore(ptn_ignore_re.clone(), text_re.clone())
.build()
.unwrap()
.matches(text)
.is_ok()
};

helper(&ptn, &text);
});
}
}

0 comments on commit c1fc20b

Please sign in to comment.