use super::{KNOWN_ATTRIBUTE_NAMES, bare_name, closest_known_name, optimal_string_alignment};
#[test]
fn bare_name_strips_the_sigil_and_any_argument_list() {
assert_eq!(bare_name("@readonly"), "readonly");
assert_eq!(bare_name("@length(min: 1, max: 200)"), "length");
assert_eq!(
bare_name("@relation(fields: [a], references: [b])"),
"relation"
);
assert_eq!(bare_name("readonly"), "readonly");
}
#[test]
fn a_transposition_is_one_edit() {
assert_eq!(optimal_string_alignment("raedonly", "readonly"), 1);
assert_eq!(optimal_string_alignment("readonly", "readonly"), 0);
}
#[test]
fn the_ticket_typo_suggests_the_attribute_it_meant() {
assert_eq!(closest_known_name("raedonly"), Some("readonly"));
}
#[test]
fn a_name_that_resembles_nothing_is_left_alone() {
assert_eq!(closest_known_name("totallyBogusAttribute"), None);
assert_eq!(closest_known_name("whatever"), None);
}
#[test]
fn very_short_names_never_produce_a_suggestion() {
assert_eq!(closest_known_name("ix"), None);
assert_eq!(closest_known_name("q"), None);
}
#[test]
fn a_case_only_difference_is_suggested() {
assert_eq!(closest_known_name("ReadOnly"), Some("readonly"));
assert_eq!(closest_known_name("SERVER_ONLY"), Some("server_only"));
}
#[test]
fn the_length_floor_takes_precedence_over_case_only_detection() {
assert_eq!(closest_known_name("Id"), None);
}
#[test]
fn every_known_name_is_zero_distance_from_itself() {
for known in KNOWN_ATTRIBUTE_NAMES {
assert_eq!(
optimal_string_alignment(known, known),
0,
"{known} should be identical to itself"
);
}
}
#[test]
fn the_known_set_has_no_duplicates() {
let mut sorted = KNOWN_ATTRIBUTE_NAMES.to_vec();
sorted.sort_unstable();
let mut deduped = sorted.clone();
deduped.dedup();
assert_eq!(
sorted, deduped,
"KNOWN_ATTRIBUTE_NAMES contains a duplicate"
);
}
#[test]
fn no_two_known_names_are_within_suggestion_distance() {
for (index, left) in KNOWN_ATTRIBUTE_NAMES.iter().enumerate() {
for right in &KNOWN_ATTRIBUTE_NAMES[index + 1..] {
let distance = optimal_string_alignment(left, right);
let limit = super::max_distance_for(left).max(super::max_distance_for(right));
assert!(
distance > limit,
"`@{left}` and `@{right}` are only {distance} edit(s) apart, within the \
suggestion threshold of {limit} — a typo of one would be suggested as the \
other. Rename one, or reconsider the threshold."
);
}
}
}