use crate::audit::{BASIS_POINTS, DensityScope, RuleAggregate, ScoreDimension};
use crate::policy::UNMEASURED_NOISE_BASIS_POINTS;
use crate::report::Severity;
use super::scored;
fn ranked(id: &str, contribution: u64, noise: Option<u16>) -> RuleAggregate {
RuleAggregate {
id: id.to_owned(),
effective_severity: Severity::Warning,
category: None,
dimension: Some(ScoreDimension::Reliability),
scope: DensityScope::Workspace,
tier: None,
occurrences: 1,
numerator: 1,
noise,
contribution,
}
}
#[test]
fn a_rule_the_corpus_measured_wrong_yields_the_lead_to_a_quieter_one() {
let score = scored(&[
("clippy::indexing_slicing", "reliability", Severity::Warning, 60),
(
"rust_doctor::cargo::duplicate_major_versions",
"dependencies",
Severity::Warning,
2,
),
]);
assert_eq!(
score.projected_rule_ids.first().map(String::as_str),
Some("rust_doctor::cargo::duplicate_major_versions"),
"the rule measured wrong on forty sites does not lead over the one measured right"
);
}
#[test]
fn an_unmeasured_rule_is_ranked_at_half_its_contribution() {
let unmeasured = ranked("rust_doctor::repo::tracked_secret_file", 1_000_000, None);
assert_eq!(unmeasured.expected_repair_value(), 500_000);
assert_eq!(UNMEASURED_NOISE_BASIS_POINTS, 5000);
let quiet = ranked("clippy::rc_buffer", 1_000_000, Some(2000));
assert!(
quiet.expected_repair_value() > unmeasured.expected_repair_value(),
"a rule measured quiet ranks above one nobody has measured"
);
}
#[test]
fn the_same_rate_on_more_sites_moves_further_from_the_default() {
let five = ranked("clippy::panic", 1_000_000, Some(8571));
let forty = ranked("clippy::indexing_slicing", 1_000_000, Some(9762));
let unmeasured = ranked("rust_doctor::repo::tracked_secret_file", 1_000_000, None);
assert!(forty.expected_repair_value() < five.expected_repair_value());
assert!(five.expected_repair_value() < unmeasured.expected_repair_value());
}
#[test]
fn a_rule_measured_wrong_on_every_site_still_ranks_above_nothing() {
let worst = ranked("clippy::panic", 1_000_000, Some(8571));
assert!(worst.expected_repair_value() > 0);
assert_eq!(
worst.expected_repair_value(),
1_000_000 * (BASIS_POINTS - 8571) / BASIS_POINTS
);
let score = scored(&[
("clippy::indexing_slicing", "reliability", Severity::Warning, 60),
("clippy::string_slice", "reliability", Severity::Warning, 12),
]);
assert_eq!(
score.projected_rule_ids,
vec![
"clippy::indexing_slicing".to_owned(),
"clippy::string_slice".to_owned()
],
"two rules measured wrong everywhere are ordered rather than tied away"
);
assert!(score.withheld_rule_ids.is_empty());
}
#[test]
fn the_rules_the_ranking_dropped_are_published_loudest_first() {
let noisy = ranked("clippy::indexing_slicing", 40, Some(9762));
let quieter = ranked("clippy::string_slice", 30, Some(9762));
assert_eq!(noisy.expected_repair_value(), 0);
assert_eq!(quieter.expected_repair_value(), 0);
assert!(noisy.contribution() > quieter.contribution());
let rules = [quieter, noisy];
let (projected, withheld) = crate::audit::rank_repairs(&rules);
assert!(projected.is_empty());
assert_eq!(
withheld
.iter()
.map(|rule| rule.id.as_str())
.collect::<Vec<_>>(),
vec!["clippy::indexing_slicing", "clippy::string_slice"]
);
}
#[test]
fn the_rate_orders_the_advice_and_never_moves_the_score() {
let noisy = scored(&[(
"clippy::indexing_slicing",
"reliability",
Severity::Warning,
12,
)]);
let quiet = scored(&[("clippy::unreachable", "reliability", Severity::Warning, 12)]);
assert_eq!(noisy.value, quiet.value);
assert_eq!(noisy.dimensions, quiet.dimensions);
assert_eq!(
ranked("clippy::indexing_slicing", 1_000_000, Some(9762)).expected_repair_value(),
23_800
);
assert_eq!(
ranked("clippy::unreachable", 1_000_000, Some(7143)).expected_repair_value(),
285_700
);
}