use core::{borrow::Borrow, hash::BuildHasher};
use std::collections::HashMap;
use super::{SlotName, is_identifier};
use crate::FieldName;
#[test]
fn the_charset_admits_a_bare_identifier_and_nothing_else() {
assert!(is_identifier(b"captions"));
assert!(is_identifier(b"_captions"));
assert!(is_identifier(b"captions_v2"));
assert!(is_identifier(b"c9"));
assert!(!is_identifier(b""));
assert!(!is_identifier(b"9captions"));
assert!(!is_identifier(b"captions v2"));
assert!(!is_identifier(b"captions-v2"));
assert!(!is_identifier(b"."));
assert!(!is_identifier(b".."));
assert!(!is_identifier(b"a/b"));
assert!(!is_identifier(b"a\\b"));
assert!(!is_identifier(b"a\0b"));
assert!(!is_identifier("ключ".as_bytes()));
}
#[test]
fn a_slot_name_is_const_constructible() {
const CAPTIONS: &SlotName = SlotName::new("captions");
assert_eq!(CAPTIONS.as_ref(), "captions");
}
#[test]
#[should_panic = "a slot name must be non-empty ASCII"]
fn a_malformed_slot_name_is_refused() {
let _ = SlotName::new("captions v2");
}
#[test]
fn a_field_name_admits_what_a_slot_name_refuses() {
assert_eq!(FieldName::new("").len(), 0);
assert_eq!(FieldName::new("a\"b").len(), 3);
assert_eq!(FieldName::new("ключ").len(), 8);
assert!(!is_identifier(b""));
assert!(!is_identifier(b"a\"b"));
assert!(!is_identifier("ключ".as_bytes()));
}
#[test]
fn a_slot_name_and_its_borrowed_str_agree() {
let slot = SlotName::new("captions");
let borrowed: &str = slot.borrow();
let indexes = HashMap::from([("captions", 0usize), ("transcripts", 1)]);
assert_eq!(indexes.get(borrowed), Some(&0));
assert_eq!(
indexes.get(Borrow::<str>::borrow(SlotName::new("transcripts"))),
Some(&1)
);
let hasher = indexes.hasher();
assert_eq!(hasher.hash_one(slot), hasher.hash_one(borrowed));
assert_eq!(slot, SlotName::new("captions"));
assert_eq!(
slot.cmp(SlotName::new("transcripts")),
borrowed.cmp("transcripts")
);
}