use super::{Claimed, Lane, LaneIdent, Lanes, lane_collisions, lane_for};
use crate::{Slot, SlotName};
static ROSTER: &[Lane] = &[
Lane::of(LaneIdent::new("en")),
Lane::of(LaneIdent::new("ja")),
Lane::of(LaneIdent::new("zh")).accepting(&[
LaneIdent::new("zh_hans"),
LaneIdent::new("zh_hant"),
LaneIdent::new("cmn"),
]),
];
struct Unfanned;
impl Slot for Unfanned {
type Of = ();
const SLOT: &'static SlotName = SlotName::new("unfanned");
const FIELD: &'static str = "body";
const ENGINE: &'static str = "lexical";
}
impl Lanes for Unfanned {}
struct Fanned;
impl Slot for Fanned {
type Of = ();
const SLOT: &'static SlotName = SlotName::new("fanned");
const FIELD: &'static str = "body";
const ENGINE: &'static str = "lexical";
}
impl Lanes for Fanned {
const LANES: &'static [Lane] = ROSTER;
}
#[test]
fn a_slot_that_named_no_lanes_has_the_undetermined_one() {
assert_eq!(Unfanned::LANES.len(), 1);
assert_eq!(Unfanned::LANES[0].key(), LaneIdent::UND);
assert!(Unfanned::LANES[0].accepts().is_empty());
assert_eq!(Fanned::LANES.len(), 3);
}
#[test]
fn a_lane_carries_its_key_and_its_aliases() {
let plain = Lane::of(LaneIdent::new("en"));
assert_eq!(plain.key(), LaneIdent::new("en"));
assert!(plain.accepts().is_empty());
const ACCEPTS: &[&LaneIdent] = &[LaneIdent::new("en_gb")];
let folded = plain.accepting(ACCEPTS);
assert_eq!(folded.key(), LaneIdent::new("en"));
assert_eq!(folded.accepts(), &[LaneIdent::new("en_gb")]);
}
#[test]
fn the_gate_accepts_broadly() {
for (tag, key) in [
("en", "en"),
("EN", "en"),
("En", "en"),
("en-US", "en"),
("en-GB", "en"),
("en-US-posix", "en"),
("ja-JP", "ja"),
("zh-Hant", "zh"),
("zh-Hant-TW", "zh"),
("zh_hans", "zh"),
("ZH-HANS-CN", "zh"),
("zh-cmn-Hans", "zh"),
("cmn", "zh"),
] {
let lane = lane_for(tag, ROSTER).unwrap_or_else(|error| panic!("{tag}: {error}"));
assert_eq!(&**lane.key(), key, "at {tag}");
}
}
#[test]
fn the_gate_emits_one_canonical_key() {
let one = lane_for("zh-Hant-TW", ROSTER).expect("a declared alias");
let other = lane_for("cmn", ROSTER).expect("a declared alias");
let third = lane_for("ZH", ROSTER).expect("the key itself");
assert_eq!(one.key(), other.key());
assert_eq!(other.key(), third.key());
assert_eq!(&**one.key(), "zh");
}
#[test]
fn an_unmatched_tag_is_refused_by_name() {
for tag in ["sw", "de", "und", "qaa"] {
assert!(lane_for(tag, ROSTER).is_err(), "{tag} should be refused");
}
assert_eq!(
&**lane_for("zh-Latn", ROSTER).expect("`zh` is declared").key(),
"zh"
);
let refusal = lane_for("sw-KE", ROSTER).expect_err("no lane answers Swahili");
let sentence = refusal.to_string();
assert_eq!(refusal.tag(), "sw-KE");
assert_eq!(refusal.lanes(), ROSTER);
assert!(sentence.contains("sw-KE"), "{sentence}");
assert!(sentence.contains("`en`"), "{sentence}");
assert!(sentence.contains("`ja`"), "{sentence}");
assert!(sentence.contains("`zh`"), "{sentence}");
assert!(sentence.contains("zh_hant"), "{sentence}");
assert!(sentence.contains("cmn"), "{sentence}");
}
#[test]
fn a_tag_that_is_not_a_tag_is_refused() {
for tag in ["", "x-private", "i-klingon", "!!", "1"] {
let refusal = lane_for(tag, ROSTER).expect_err("not a language tag");
assert_eq!(refusal.tag(), tag);
}
}
#[test]
fn an_empty_roster_refuses_everything() {
static NONE: &[Lane] = &[];
let refusal = lane_for("en", NONE).expect_err("there is nothing to reach");
assert!(refusal.to_string().contains("no lanes at all"), "{refusal}");
}
#[test]
fn a_key_wins_over_another_lanes_alias() {
static SPLIT: &[Lane] = &[
Lane::of(LaneIdent::new("zh")).accepting(&[LaneIdent::new("zh_hant")]),
Lane::of(LaneIdent::new("zh-Hant")),
];
assert_eq!(&**lane_for("zh-Hant", SPLIT).unwrap().key(), "zh-Hant");
assert_eq!(&**lane_for("zh", SPLIT).unwrap().key(), "zh");
assert!(lane_collisions(SPLIT).is_err());
}
#[test]
fn a_roster_with_no_two_claims_on_one_word_passes() {
assert!(lane_collisions(ROSTER).is_ok());
assert!(lane_collisions(Unfanned::LANES).is_ok());
assert!(lane_collisions(&[]).is_ok());
}
#[test]
fn the_audit_names_both_claimants() {
static TWO_KEYS: &[Lane] = &[
Lane::of(LaneIdent::new("zh_hant")),
Lane::of(LaneIdent::new("ZH-HANT")),
];
let collision = lane_collisions(TWO_KEYS).expect_err("one word, two lanes");
assert_eq!(collision.word(), Some("zh_hant"));
assert_eq!(&**collision.first(), "zh_hant");
assert_eq!(&**collision.second(), "ZH-HANT");
assert!(collision.to_string().contains("cannot name two lanes"));
static KEY_AND_ALIAS: &[Lane] = &[
Lane::of(LaneIdent::new("zh")).accepting(&[LaneIdent::new("cmn")]),
Lane::of(LaneIdent::new("cmn")),
];
assert!(lane_collisions(KEY_AND_ALIAS).is_err());
static TWO_ALIASES: &[Lane] = &[
Lane::of(LaneIdent::new("zh")).accepting(&[LaneIdent::new("yue")]),
Lane::of(LaneIdent::new("ja")).accepting(&[LaneIdent::new("YUE")]),
];
assert!(lane_collisions(TWO_ALIASES).is_err());
static SELF_CLAIM: &[Lane] =
&[Lane::of(LaneIdent::new("zh")).accepting(&[LaneIdent::new("cmn"), LaneIdent::new("CMN")])];
let own = lane_collisions(SELF_CLAIM).expect_err("one lane, one word, twice");
assert_eq!(own.word(), Some("cmn"));
assert_eq!(own.first(), own.second());
static ECHO: &[Lane] = &[Lane::of(LaneIdent::new("zh")).accepting(&[LaneIdent::new("ZH")])];
assert!(lane_collisions(ECHO).is_err());
}
#[test]
fn distinct_words_never_collide() {
static WIDE: &[Lane] = &[
Lane::of(LaneIdent::new("en")),
Lane::of(LaneIdent::new("ja")),
Lane::of(LaneIdent::new("zh")).accepting(&[LaneIdent::new("zh_hans"), LaneIdent::new("cmn")]),
Lane::of(LaneIdent::new("ko")),
Lane::of(LaneIdent::UND),
];
assert!(lane_collisions(WIDE).is_ok());
}
static SIX_STEP: &[Lane] = &[
Lane::of(LaneIdent::new("en")),
Lane::of(LaneIdent::new("zh")),
Lane::of(LaneIdent::new("sr")),
Lane::of(LaneIdent::new("other")).catch_all(),
];
fn reached(tag: &str, lanes: &'static [Lane]) -> Option<&'static str> {
lane_for(tag, lanes).ok().map(|lane| &**lane.key())
}
#[test]
fn a_script_bearing_tag_degrades_to_its_language_key() {
assert_eq!(reached("zh-Hant", SIX_STEP), Some("zh"));
assert_eq!(reached("zh-Hans-CN", SIX_STEP), Some("zh"));
assert_eq!(reached("en-Latn", SIX_STEP), Some("en"));
assert_eq!(reached("en-Latn-US", SIX_STEP), Some("en"));
assert_eq!(reached("sr-Latn", SIX_STEP), Some("sr"));
assert_eq!(reached("sr-Cyrl-RS", SIX_STEP), Some("sr"));
}
#[test]
fn an_exact_key_outranks_the_language_only_step() {
static SPLIT: &[Lane] = &[
Lane::of(LaneIdent::new("zh")),
Lane::of(LaneIdent::new("zh_hant")),
];
assert_eq!(reached("zh-Hant", SPLIT), Some("zh_hant"));
assert_eq!(reached("zh-Hant-TW", SPLIT), Some("zh_hant"));
assert_eq!(reached("zh", SPLIT), Some("zh"));
assert_eq!(reached("zh-Hans", SPLIT), Some("zh"));
}
#[test]
fn an_exact_alias_outranks_a_language_only_key() {
static DECLARED_FIRST: &[Lane] = &[
Lane::of(LaneIdent::new("zh")),
Lane::of(LaneIdent::new("cn")).accepting(&[LaneIdent::new("zh_hant")]),
];
assert_eq!(
reached("zh-Hant", DECLARED_FIRST),
Some("cn"),
"an exact alias is a declaration about this tag; `zh` is only its language"
);
assert_eq!(reached("zh-Hans", DECLARED_FIRST), Some("zh"));
assert_eq!(reached("zh", DECLARED_FIRST), Some("zh"));
}
#[test]
fn the_catch_all_takes_only_what_no_lane_has_a_home_for() {
assert_eq!(reached("sw", SIX_STEP), Some("other"));
assert_eq!(reached("sw-KE", SIX_STEP), Some("other"));
assert_eq!(reached("beng", SIX_STEP), Some("other"));
assert_eq!(reached("zzzz", SIX_STEP), Some("other"));
assert_eq!(reached("en-GB", SIX_STEP), Some("en"));
assert_eq!(reached("zh-Hant", SIX_STEP), Some("zh"));
}
#[test]
fn the_catch_all_catches_what_is_not_a_tag_at_all() {
for stranger in ["", "x-private", "i-klingon", "!!", "1", "toolongsubtag"] {
assert_eq!(
reached(stranger, SIX_STEP),
Some("other"),
"at {stranger:?}"
);
}
}
#[test]
fn without_a_catch_all_a_stranger_is_still_refused() {
static NO_CATCH_ALL: &[Lane] = &[
Lane::of(LaneIdent::new("en")),
Lane::of(LaneIdent::new("zh")),
];
let refusal = lane_for("sw-KE", NO_CATCH_ALL).expect_err("no home, no catch-all");
let sentence = refusal.to_string();
assert!(sentence.contains("sw-KE"), "{sentence}");
assert!(sentence.contains("`en`"), "{sentence}");
assert!(
sentence.ends_with(", and no catch-all is declared"),
"{sentence}"
);
assert!(lane_for("x-private", NO_CATCH_ALL).is_err());
assert_eq!(reached("zh-Hant", NO_CATCH_ALL), Some("zh"));
}
#[test]
fn a_second_catch_all_is_refused_by_the_audit() {
static TWO: &[Lane] = &[
Lane::of(LaneIdent::new("en")).catch_all(),
Lane::of(LaneIdent::new("zh")),
Lane::of(LaneIdent::new("other")).catch_all(),
];
let refusal = lane_collisions(TWO).expect_err("two catch-alls");
assert_eq!(refusal.claimed(), &Claimed::CatchAll);
assert_eq!(
refusal.word(),
None,
"what was claimed is a role, not a word"
);
assert_eq!(&**refusal.first(), "en");
assert_eq!(&**refusal.second(), "other");
assert!(refusal.to_string().contains("both declared the catch-all"));
assert!(lane_collisions(SIX_STEP).is_ok());
assert!(lane_collisions(ROSTER).is_ok());
}
#[test]
fn a_catch_all_is_still_a_lane() {
let lane = Lane::of(LaneIdent::new("other")).catch_all();
assert!(lane.is_catch_all());
assert_eq!(lane.key(), LaneIdent::new("other"));
assert!(!Lane::of(LaneIdent::new("en")).is_catch_all());
assert_eq!(reached("other", SIX_STEP), Some("other"));
}