#![expect(clippy::unwrap_used, reason = "tests: a panic is a test failure")]
mod common;
use common::fuzz_entry::wrap_script;
use laminapdf_model::{
Behaviour, CalculateFunction, DatePattern, Document, FieldBehaviour, FieldTrigger, NegativeStyle, NumberFormat,
OpenOptions, PercentFormat, RangeValidate, SeparatorStyle, SimpleCalculate, SpecialFormat,
};
use proptest::prelude::*;
fn read(script: &str) -> Behaviour {
let doc = Document::open(
wrap_script(&laminapdf_syntax::text::encode_text_string(script)),
OpenOptions::default(),
)
.unwrap();
let form = doc.form().unwrap();
doc.field_behaviour(form.field_named("a").unwrap()).unwrap()
}
fn behaviour_under(read: Behaviour, trigger: FieldTrigger) -> Option<FieldBehaviour> {
match trigger {
FieldTrigger::Keystroke => read.keystroke,
FieldTrigger::Format => read.format,
FieldTrigger::Validate => read.validate,
FieldTrigger::Calculate => read.calculate.map(FieldBehaviour::SimpleCalculate),
_ => None,
}
}
fn separator() -> impl Strategy<Value = SeparatorStyle> {
prop_oneof![
Just(SeparatorStyle::CommaDot),
Just(SeparatorStyle::Dot),
Just(SeparatorStyle::DotComma),
Just(SeparatorStyle::Comma),
Just(SeparatorStyle::ApostropheDot),
]
}
fn negative() -> impl Strategy<Value = NegativeStyle> {
prop_oneof![
Just(NegativeStyle::Minus),
Just(NegativeStyle::Red),
Just(NegativeStyle::Parentheses),
Just(NegativeStyle::RedParentheses),
]
}
fn text() -> impl Strategy<Value = String> {
prop::collection::vec(
prop_oneof![
4 => any::<char>(),
1 => prop::sample::select(vec!['\0', '\u{2028}', '\u{2029}', '\u{85}', '\u{202e}', '\u{feff}']),
1 => Just('"'),
1 => Just('\\'),
1 => Just('\n'),
1 => Just('\''),
],
0..12,
)
.prop_map(|chars| chars.into_iter().collect())
}
fn pattern() -> impl Strategy<Value = String> {
prop_oneof![
Just("mm/dd/yyyy".to_owned()),
Just("h:MM tt".to_owned()),
Just("HH:MM:ss".to_owned()),
text().prop_filter("a pattern is not empty", |p| !p.is_empty()),
]
}
fn bound() -> impl Strategy<Value = Option<f64>> {
prop::option::of(prop_oneof![
any::<f64>().prop_filter("finite", |n| n.is_finite()),
(-1000i32..1000).prop_map(f64::from),
(-1000i32..1000).prop_map(|n| f64::from(n) / 8.0),
])
}
fn behaviour() -> impl Strategy<Value = FieldBehaviour> {
prop_oneof![
(0u8..=20, separator(), negative(), text(), any::<bool>())
.prop_map(|(d, s, n, c, p)| FieldBehaviour::NumberFormat(NumberFormat::new(d, s, n, c, p))),
(0u8..=20, separator(), any::<bool>())
.prop_map(|(d, s, p)| FieldBehaviour::PercentFormat(PercentFormat::new(d, s, p))),
pattern().prop_map(|p| FieldBehaviour::DateFormat(DatePattern::new(p))),
pattern().prop_map(|p| FieldBehaviour::TimeFormat(DatePattern::new(p))),
prop_oneof![
Just(SpecialFormat::Zip),
Just(SpecialFormat::ZipPlus4),
Just(SpecialFormat::Phone),
Just(SpecialFormat::Ssn),
]
.prop_map(FieldBehaviour::SpecialFormat),
(bound(), bound()).prop_map(|(min, max)| FieldBehaviour::RangeValidate(RangeValidate::new(min, max))),
(
prop_oneof![
Just(CalculateFunction::Sum),
Just(CalculateFunction::Average),
Just(CalculateFunction::Product),
Just(CalculateFunction::Minimum),
Just(CalculateFunction::Maximum),
],
prop::collection::vec(text(), 1..6),
)
.prop_map(|(f, names)| FieldBehaviour::SimpleCalculate(SimpleCalculate::new(f, names))),
]
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(400))]
#[test]
fn every_behaviour_written_is_read_back_under_its_trigger_unchanged(b in behaviour()) {
let calls = b.helper_calls().unwrap();
prop_assert!(!calls.is_empty());
for (trigger, script) in &calls {
let again = behaviour_under(read(script), *trigger);
let expected = match (&b, trigger) {
(FieldBehaviour::PercentFormat(p), FieldTrigger::Keystroke) => {
FieldBehaviour::PercentFormat(PercentFormat::new(p.decimals, p.separator, false))
}
_ => b.clone(),
};
prop_assert_eq!(again, Some(expected), "{}", script);
}
}
#[test]
fn a_script_written_is_one_helper_call_with_no_line_break_outside_a_literal(b in behaviour()) {
for (_, script) in b.helper_calls().unwrap() {
prop_assert!(script.starts_with("AF") && script.ends_with(");"));
prop_assert!(!script.contains('\n') && !script.contains('\r'), "{}", script);
}
}
}