use super::super::assoc::*;
use crate::item_kind::ItemKind;
fn segs(parts: &[&str]) -> Vec<String> {
parts.iter().map(|s| (*s).to_string()).collect()
}
#[test]
fn every_family_declares_a_primary_prefix() {
for family in [
AssocAnchorKind::Method,
AssocAnchorKind::Type,
AssocAnchorKind::Constant,
] {
let prefixes = family.anchor_prefixes();
assert!(!prefixes.is_empty(), "{family:?} declares no prefix");
assert_eq!(family.primary_prefix(), prefixes[0], "{family:?}");
assert!(
prefixes.iter().all(|p| p.ends_with('.')),
"{family:?} prefixes={prefixes:?}"
);
}
}
#[test]
fn method_family_probes_both_flavours_in_order() {
assert_eq!(
AssocAnchorKind::Method.anchor_prefixes(),
&["method.", "tymethod."]
);
assert_eq!(
AssocAnchorKind::Type.anchor_prefixes(),
&["associatedtype."]
);
assert_eq!(
AssocAnchorKind::Constant.anchor_prefixes(),
&["associatedconstant."]
);
}
#[test]
fn variant_and_field_route_through_their_own_families() {
assert_eq!(
associated_item_path(ItemKind::Variant, &segs(&["Option", "Some"])),
Some(AssocAnchorKind::Variant)
);
assert_eq!(
associated_item_path(ItemKind::StructField, &segs(&["Range", "start"])),
Some(AssocAnchorKind::StructField)
);
assert_eq!(AssocAnchorKind::Variant.primary_prefix(), "variant.");
assert_eq!(
AssocAnchorKind::StructField.primary_prefix(),
"structfield."
);
assert_eq!(
AssocAnchorKind::Variant.parent_kind_probe(),
&[ItemKind::Enum]
);
assert_eq!(
AssocAnchorKind::StructField.parent_kind_probe(),
&[ItemKind::Struct, ItemKind::Union]
);
}
#[test]
fn member_only_kinds_are_flagged_regardless_of_path_shape() {
assert_eq!(
member_only_family(ItemKind::Variant),
Some(AssocAnchorKind::Variant)
);
assert_eq!(
member_only_family(ItemKind::StructField),
Some(AssocAnchorKind::StructField)
);
for k in [
ItemKind::Fn,
ItemKind::Type,
ItemKind::Constant,
ItemKind::Struct,
] {
assert_eq!(member_only_family(k), None, "{k:?}");
}
}
#[test]
fn fragment_recovers_the_two_new_families() {
assert_eq!(
assoc_from_fragment("variant.Some"),
Some((AssocAnchorKind::Variant, "Some"))
);
assert_eq!(
assoc_from_fragment("structfield.start"),
Some((AssocAnchorKind::StructField, "start"))
);
}
#[test]
fn dry_run_names_are_the_live_probe_order_verbatim() {
for family in [
AssocAnchorKind::Method,
AssocAnchorKind::Type,
AssocAnchorKind::Constant,
] {
let names: Vec<&str> = family.parent_kind_probe_names().collect();
assert_eq!(
names.len(),
family.parent_kind_probe().len(),
"{family:?} dropped a kind"
);
assert!(!names.is_empty(), "{family:?} has no parent to probe");
for (name, kind) in names.iter().zip(family.parent_kind_probe()) {
assert_eq!(*name, kind.as_str(), "{family:?} order broke at {name}");
}
}
}
#[test]
fn probe_order_leads_with_the_kind_that_hosts_the_family() {
assert_eq!(
AssocAnchorKind::Type.parent_kind_probe()[0],
ItemKind::Trait
);
assert_eq!(
AssocAnchorKind::Method.parent_kind_probe()[0],
ItemKind::Struct
);
assert_eq!(
AssocAnchorKind::Constant.parent_kind_probe()[0],
ItemKind::Struct
);
for family in [
AssocAnchorKind::Method,
AssocAnchorKind::Type,
AssocAnchorKind::Constant,
] {
assert_eq!(family.parent_kind_probe().len(), 5, "{family:?}");
}
}
#[test]
fn detects_each_family_from_kind_and_leaf_case() {
assert_eq!(
associated_item_path(ItemKind::Fn, &segs(&["Runtime", "new"])),
Some(AssocAnchorKind::Method)
);
assert_eq!(
associated_item_path(ItemKind::Type, &segs(&["Iterator", "Item"])),
Some(AssocAnchorKind::Type)
);
assert_eq!(
associated_item_path(ItemKind::Constant, &segs(&["Duration", "MAX"])),
Some(AssocAnchorKind::Constant)
);
}
#[test]
fn lowercase_parent_keeps_free_item_path() {
assert_eq!(
associated_item_path(ItemKind::Constant, &segs(&["u32", "MAX"])),
None
);
assert_eq!(
associated_item_path(ItemKind::Constant, &segs(&["f32", "EPSILON"])),
None
);
assert_eq!(
associated_item_path(ItemKind::Type, &segs(&["io", "Result"])),
None
);
}
#[test]
fn rejects_shapes_outside_the_family() {
assert_eq!(associated_item_path(ItemKind::Fn, &segs(&["spawn"])), None);
assert_eq!(
associated_item_path(ItemKind::Struct, &segs(&["Runtime", "Builder"])),
None
);
assert_eq!(
associated_item_path(ItemKind::Static, &segs(&["Thing", "VALUE"])),
None
);
assert_eq!(
associated_item_path(ItemKind::Type, &segs(&["Iterator", ""])),
None
);
}
#[test]
fn case_mismatch_still_routes_to_parent_anchor() {
assert_eq!(
associated_item_path(ItemKind::Type, &segs(&["Iterator", "item"])),
Some(AssocAnchorKind::Type)
);
assert_eq!(
associated_item_path(ItemKind::Constant, &segs(&["Duration", "max"])),
Some(AssocAnchorKind::Constant)
);
assert_eq!(
associated_item_path(ItemKind::Fn, &segs(&["Iterator", "Item"])),
Some(AssocAnchorKind::Method)
);
}
#[test]
fn fragment_names_its_own_family() {
assert_eq!(
assoc_from_fragment("tymethod.next"),
Some((AssocAnchorKind::Method, "next"))
);
assert_eq!(
assoc_from_fragment("method.map"),
Some((AssocAnchorKind::Method, "map"))
);
assert_eq!(
assoc_from_fragment("associatedtype.Item"),
Some((AssocAnchorKind::Type, "Item"))
);
assert_eq!(
assoc_from_fragment("associatedconstant.MAX"),
Some((AssocAnchorKind::Constant, "MAX"))
);
assert_eq!(assoc_from_fragment("required-methods"), None);
assert_eq!(assoc_from_fragment("implementations"), None);
assert_eq!(assoc_from_fragment("associatedtype."), None);
assert_eq!(assoc_from_fragment("variant."), None);
}
#[test]
fn anchor_ids_follow_probe_order() {
assert_eq!(
AssocAnchorKind::Method.anchor_ids("next"),
vec!["method.next".to_string(), "tymethod.next".to_string()]
);
assert_eq!(
AssocAnchorKind::Type.anchor_ids("Item"),
vec!["associatedtype.Item".to_string()]
);
}
#[test]
fn strip_rejects_empty_and_foreign_ids() {
let prefixes = AssocAnchorKind::Type.anchor_prefixes();
assert_eq!(
strip_assoc_anchor_prefix("associatedtype.Item", prefixes),
Some("Item")
);
assert_eq!(strip_assoc_anchor_prefix("associatedtype.", prefixes), None);
assert_eq!(strip_assoc_anchor_prefix("method.next", prefixes), None);
}