use core::str::FromStr as _;
use mediaframe::codec::VideoCodec as Foreign;
use super::VideoCodec;
#[test]
fn the_canonical_form_is_the_lowercase_slug() {
assert_eq!(VideoCodec::new("h264").as_str(), "h264");
assert_eq!(VideoCodec::new("H264").as_str(), "h264");
assert_eq!(VideoCodec::new("prores").as_str(), "prores");
assert_eq!(VideoCodec::new("AV1").as_str(), "av1");
}
#[test]
fn the_door_folds_every_spelling_of_one_codec() {
let canonical = VideoCodec::new("prores");
for sent in ["prores", "ProRes", "PRORES", "pRoReS"] {
assert_eq!(VideoCodec::new(sent), canonical, "`{sent}` is one value");
}
assert_eq!(VideoCodec::new("H264"), VideoCodec::new("h264"));
assert_eq!(VideoCodec::new("hEvC"), VideoCodec::new("hevc"));
}
#[test]
fn a_name_the_roster_does_not_carry_is_admitted_and_named_as_such() {
let known = VideoCodec::new("h264");
assert!(known.is_registered());
let unknown = VideoCodec::new("some_codec_9000");
assert!(!unknown.is_registered());
assert_eq!(
unknown.as_str(),
"some_codec_9000",
"the escape is LOSSLESS — the name a muxer wrote is the name that comes back",
);
assert_eq!(VideoCodec::new("SOME_CODEC_9000"), unknown);
}
#[test]
fn the_door_is_total_over_every_string() {
for sent in ["", " ", "!!!", "日本語", "not a codec at all", "\u{0}"] {
let codec = VideoCodec::new(sent);
assert!(!codec.is_registered(), "`{sent}` names no roster member");
assert_eq!(
codec,
VideoCodec::from_str(sent).expect("the parse is infallible"),
"`{sent}` reaches one value through both doors",
);
}
assert_eq!(VideoCodec::new("").as_str(), "");
}
#[test]
fn the_foreign_value_is_recanonicalised_on_the_way_in() {
let sideways = Foreign::other("h264");
assert_ne!(
sideways,
Foreign::H264,
"upstream holds these apart, which is the premise this test is about",
);
let crossed = VideoCodec::from(sideways);
assert_eq!(
crossed,
VideoCodec::new("h264"),
"the door re-runs the slug table, so only the named spelling exists in here",
);
assert!(crossed.is_registered());
assert_eq!(crossed.into_inner(), Foreign::H264);
let genuine = Foreign::other("some_codec_9000");
assert_eq!(
VideoCodec::from(genuine.clone()).into_inner(),
genuine,
"re-canonicalising is not the same as flattening — an unknown name stays itself",
);
}
#[test]
fn ordering_is_alphabetical_over_the_canonical_spelling() {
let mut codecs = [
VideoCodec::new("prores"),
VideoCodec::new("av1"),
VideoCodec::new("h264"),
VideoCodec::new("vp9"),
];
codecs.sort();
let order: Vec<&str> = codecs.iter().map(VideoCodec::as_str).collect();
assert_eq!(order, ["av1", "h264", "prores", "vp9"]);
assert!(VideoCodec::new("AV1") < VideoCodec::new("h264"));
}
#[test]
fn the_order_and_the_equality_agree_over_the_whole_value_space() {
let pairs = [
("h264", "h264"),
("H264", "h264"),
("h264", "av1"),
("some_codec_9000", "some_codec_9000"),
("some_codec_9000", "h264"),
("", ""),
("", "h264"),
];
for (left, right) in pairs {
let left = VideoCodec::new(left);
let right = VideoCodec::new(right);
assert_eq!(
left == right,
left.cmp(&right).is_eq(),
"`{left}` and `{right}` — equality and order must call the same pairs equal",
);
}
let sideways = VideoCodec::from(Foreign::other("h264"));
let named = VideoCodec::new("h264");
assert!(sideways == named && sideways.cmp(&named).is_eq());
}
#[test]
fn every_roster_member_is_a_fixpoint_of_the_door() {
assert!(
!Foreign::ROSTER.is_empty(),
"the vendored table is not empty"
);
for member in Foreign::ROSTER {
let citizen = VideoCodec::from(member.clone());
assert_eq!(
citizen.get(),
member,
"`{}` crosses into the citizen unchanged",
member.as_str(),
);
assert!(
citizen.is_registered(),
"`{}` is on the roster and must say so",
member.as_str(),
);
assert_eq!(
VideoCodec::new(citizen.as_str()),
citizen,
"`{}` is a fixpoint of the door",
member.as_str(),
);
assert_eq!(
VideoCodec::new(&member.as_str().to_ascii_uppercase()),
citizen,
"`{}` folds from its uppercase spelling",
member.as_str(),
);
}
}
#[test]
fn the_rendering_and_the_parse_are_inverse() {
for sent in ["h264", "H264", "av1", "some_codec_9000", ""] {
let codec = VideoCodec::new(sent);
let rendered = codec.to_string();
assert_eq!(VideoCodec::new(&rendered), codec, "`{sent}` round trips");
assert_eq!(rendered, codec.as_str(), "`Display` IS the canonical slug");
}
}
#[test]
fn the_debug_face_prints_the_slug() {
assert_eq!(format!("{:?}", VideoCodec::new("H264")), "\"h264\"");
assert_eq!(
format!("{:?}", VideoCodec::new("some_codec_9000")),
"\"some_codec_9000\"",
"the escape arm prints as its text and not as `Other(…)`",
);
}
#[test]
fn the_text_seat_converts_both_ways() {
let codec = VideoCodec::new("H264");
assert_eq!(AsRef::<str>::as_ref(&codec), "h264");
assert_eq!(String::from(codec.clone()), String::from("h264"));
assert_eq!(Foreign::from(codec.clone()), Foreign::H264);
assert_eq!(VideoCodec::from(Foreign::H264), codec);
}