Skip to content

Commit

Permalink
add property tests for RelativeDuration parser and formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
intarga committed Apr 21, 2024
1 parent a9d49e1 commit 1f25739
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ version = "0.2.6"
[dev-dependencies]
criterion = "0.3"
chrono-tz = "0.8.3"
proptest = "1.4.0"

[[bench]]
name = "delta"
Expand Down
26 changes: 26 additions & 0 deletions src/relative_duration/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ impl RelativeDuration {

#[cfg(test)]
mod tests {
use proptest::prelude::*;

use super::*;

#[test]
Expand Down Expand Up @@ -333,4 +335,28 @@ mod tests {
.iter()
.for_each(|(input, expected)| assert_eq!(input.to_iso_8601(), *expected))
}

proptest! {
#[test]
fn proptest_format_and_back(
months in prop::num::i32::ANY,
secs in (i64::MIN/1000)..(i64::MAX/1000),
nanos in 0u32..1_000_000_000
) {
let d = RelativeDuration::months(months).with_duration(Duration::new(secs, nanos).unwrap());
prop_assert_eq!(d, RelativeDuration::from_iso_8601(&(d.to_iso_8601())).unwrap());
}

#[test]
fn proptest_parse_and_back(
s in r"P(?:[1-9][0-9]{0,7}Y)?(?:(?:[1-9]|1[0-1])M)?(?:(?:[1-9]|[1-2][0-9])D)?(?:T(?:(?:[1-9]|1[0-9]|2[0-3])H)(?:(?:[1-9]|[1-5][0-9])M)(?:(?:(?:[1-9]|[1-5][0-9])|(?:(?:[0-9]|[1-5][0-9])\.[0-9]{0,8}[1-9]))S))?",
) {
prop_assert_eq!(s.clone(), RelativeDuration::from_iso_8601(&s).unwrap().to_iso_8601());
}

#[test]
fn proptest_parse_doesnt_panic(s in r"//PC*") {
let _ = RelativeDuration::from_iso_8601(&s);
}
}
}

0 comments on commit 1f25739

Please sign in to comment.