use crate::common::group;
use crate::common::result_with;
use dry4rust::cli::check_command::CheckCommand;
use dry4rust::cli::checking::check_thresholds::CheckThresholds;
use dry4rust::cli::cli_error::CliError;
use dry4rust::cli::cli_error::CliResult;
use dry4rust::cli::output_format::OutputFormat;
use dry4rust::config::Config;
fn checked(exact: usize, thresholds: &CheckThresholds) -> (CliResult, String) {
let groups = (0..exact)
.map(|n| group(0x11 + n as u64, &["a", "b"]))
.collect();
let result = result_with(groups, vec![], vec![], vec![]);
let config = Config::default();
let reporter = OutputFormat::Text.reporter(None);
let mut out = Vec::new();
let outcome = CheckCommand::new(&config, &result, reporter.as_ref(), thresholds).run(&mut out);
(outcome, String::from_utf8(out).expect("utf-8"))
}
#[test]
fn run_over_a_clean_result_passes_with_a_ceiling_of_zero() {
let thresholds = CheckThresholds {
max_exact: Some(0),
..CheckThresholds::default()
};
let (outcome, text) = checked(0, &thresholds);
assert!(outcome.is_ok());
assert!(text.contains("Check passed."), "{text}");
}
#[test]
fn run_with_a_ceiling_above_the_findings_passes() {
let thresholds = CheckThresholds {
max_exact: Some(9999),
..CheckThresholds::default()
};
let (outcome, _) = checked(2, &thresholds);
assert!(outcome.is_ok());
}
#[test]
fn run_with_a_zero_ceiling_fails_on_any_group() {
let thresholds = CheckThresholds {
max_exact: Some(0),
..CheckThresholds::default()
};
let (outcome, text) = checked(1, &thresholds);
assert!(matches!(outcome, Err(CliError::CheckFailed)));
assert!(text.contains("1 exact duplicate groups (max: 0)"), "{text}");
}
#[test]
fn run_without_thresholds_passes_however_much_it_finds() {
let (outcome, _) = checked(5, &CheckThresholds::default());
assert!(
outcome.is_ok(),
"an unset ceiling means the caller did not ask, not that it asked for zero"
);
}