use proptest::prelude::*;
use pactum_annotations::{
check_charset, check_email, check_phone_e164, check_phone_locale, check_rfc3339, check_slug,
check_url, check_uuid, check_ymd, number_i64, ClampNum, NumericValue,
};
fn days_in_month(year: i32, month: u32) -> u32 {
match month {
1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
4 | 6 | 9 | 11 => 30,
2 => {
if (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 {
29
} else {
28
}
}
_ => 0,
}
}
proptest! {
#[test]
fn string_validators_never_panic_and_enforce_invariants(s in "\\PC*") {
let _ = check_ymd("f", &s);
let _ = check_rfc3339("f", &s);
let _ = check_email("f", &s);
let _ = check_phone_e164("f", &s);
let _ = check_phone_locale("f", &s, "US");
let _ = check_uuid("f", &s);
let _ = check_url("f", &s, false);
let _ = check_url("f", &s, true);
let _ = check_slug("f", &s);
let _ = check_charset("f", &s, "a-z0-9_-");
let has_edge_ws = s.starts_with(char::is_whitespace) || s.ends_with(char::is_whitespace);
if has_edge_ws || !s.is_ascii() {
prop_assert!(check_ymd("f", &s).is_err());
prop_assert!(check_rfc3339("f", &s).is_err());
prop_assert!(check_phone_e164("f", &s).is_err());
}
if has_edge_ws || s.matches('@').count() != 1 {
prop_assert!(check_email("f", &s).is_err());
}
}
#[test]
fn rfc3339_accepts_valid_generated_timestamps_and_rejects_emoji(
year in 1900i32..=2100,
month in 1u32..=12,
day_seed in 1u32..=31,
hour in 0u32..=23,
minute in 0u32..=59,
second in 0u32..=60,
frac in proptest::option::of(0u32..=999_999),
tz_sign in prop_oneof![Just('+'), Just('-')],
tz_h in 0u32..=23,
tz_m in 0u32..=59,
use_z in any::<bool>(),
) {
let day = ((day_seed - 1) % days_in_month(year, month)) + 1;
let ymd = format!("{year:04}-{month:02}-{day:02}");
prop_assert!(check_ymd("d", &ymd).is_ok());
let frac_part = match frac {
Some(f) => format!(".{f:06}"),
None => String::new(),
};
let tz_part = if use_z {
"Z".to_string()
} else {
format!("{tz_sign}{tz_h:02}:{tz_m:02}")
};
let ts = format!("{ymd}T{hour:02}:{minute:02}:{second:02}{frac_part}{tz_part}");
prop_assert!(check_rfc3339("ts", &ts).is_ok(), "failed on {ts}");
let with_emoji = format!("{ymd}T{hour:02}:{minute:02}:🔥Z");
prop_assert!(check_rfc3339("ts", &with_emoji).is_err());
}
#[test]
fn numeric_bounds_and_clamp_never_panic(
vi in any::<i64>(),
vu in any::<u64>(),
vf in any::<f64>(),
min in any::<f64>(),
max in any::<f64>(),
gt in any::<f64>(),
lt in any::<f64>(),
mult in any::<Option<i64>>(),
) {
let _ = vi.check_bounds("i", Some(min), Some(max), Some(gt), Some(lt), mult);
let _ = vu.check_bounds("u", Some(min), Some(max), Some(gt), Some(lt), mult);
let _ = vf.check_bounds("f", Some(min), Some(max), Some(gt), Some(lt), mult);
let _ = number_i64("ni", vi, Some(i64::MIN / 2), Some(i64::MAX / 2), None, None, mult);
let mut ci = vi;
ci.clamp_in_place(min, max);
let mut cu = vu;
cu.clamp_in_place(min, max);
let mut cf = vf;
cf.clamp_in_place(min, max);
if cf.is_finite() && min.is_finite() && max.is_finite() && min <= max {
prop_assert!(cf >= min && cf <= max);
}
}
#[test]
fn i64_bounds_above_2_pow_53_preserve_precision(offset in 1i64..10_000) {
let base = 1i64 << 53;
let val = base + offset;
prop_assert!(val.check_bounds("v", None, None, Some(base as f64), None, None).is_ok());
prop_assert!(number_i64("v", val, None, None, Some(base), None, None).is_ok());
prop_assert!(number_i64("v", base, None, None, Some(base), None, None).is_err());
}
}