use super::{ID, InterfaceArm, InterfaceColumn, InterfaceKind, KIND, UnknownKind};
use crate::FieldName;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum MediaKind {
Video,
Audio,
}
impl InterfaceKind for MediaKind {
const INTERFACE: &'static str = "Media";
const ALL: &'static [Self] = &[Self::Video, Self::Audio];
fn as_str(self) -> &'static str {
match self {
Self::Video => "video",
Self::Audio => "audio",
}
}
}
#[test]
fn a_word_round_trips_through_the_vocabulary_it_names() {
for kind in MediaKind::ALL.iter().copied() {
assert_eq!(MediaKind::parse(kind.as_str()), Ok(kind));
}
}
#[test]
fn a_word_no_member_carries_is_refused_by_name() {
let refused = MediaKind::parse("caption").expect_err("no member is called that");
assert_eq!(refused.interface(), "Media");
assert_eq!(refused.word(), "caption");
assert_eq!(
refused.to_string(),
"`Media` has no member called `caption`"
);
assert_eq!(refused, UnknownKind::new("Media", "caption"));
}
#[test]
fn every_discriminant_is_a_word_no_collation_can_reorder() {
for kind in MediaKind::ALL.iter().copied() {
let word = kind.as_str();
assert!(
word
.bytes()
.all(|byte| byte.is_ascii_lowercase() || byte == b'_'),
"`{word}` carries a character a collation may fold"
);
}
}
#[test]
fn a_synthesized_column_wears_the_fence_and_a_real_one_does_not() {
assert_eq!(KIND.as_str(), "__ig_kind__");
assert!(
KIND.as_str().starts_with("__") && KIND.as_str().ends_with("__"),
"the fence is `__` at BOTH ends, which is what the derive refuses a column for"
);
assert_eq!(ID.as_str(), "id");
}
#[test]
fn an_arm_carries_what_each_member_calls_the_shared_column() {
const COLUMNS: &[InterfaceColumn] = &[
InterfaceColumn::new(FieldName::new("title"), FieldName::new("title")),
InterfaceColumn::new(FieldName::new("created_at"), FieldName::new("imported_at")),
];
const ARM: InterfaceArm<MediaKind> = InterfaceArm::new(MediaKind::Audio, COLUMNS);
assert_eq!(ARM.kind(), MediaKind::Audio);
assert_eq!(ARM.columns().len(), 2);
assert_eq!(ARM.columns()[0].field().as_str(), "title");
assert_eq!(ARM.columns()[0].column().as_str(), "title");
assert_eq!(ARM.columns()[1].field().as_str(), "created_at");
assert_eq!(ARM.columns()[1].column().as_str(), "imported_at");
}