use super::*;
use ::std::string::ToString;
#[test]
fn every_named_variant_round_trips() {
for slug in [
"jpeg", "png", "heif", "heic", "avif", "tiff", "webp", "gif", "bmp", "dng", "cr2", "cr3",
"nef", "nrw", "arw", "orf", "rw2", "raf", "pef", "srw", "rwl", "iiq", "3fr", "x3f", "mrw",
"gpr",
] {
let v: Format = slug.parse().unwrap();
assert!(!v.is_other(), "`{slug}` should be a named variant");
assert_eq!(v.as_str(), slug);
}
}
#[test]
fn unknown_slug_lands_in_other() {
let v: Format = "weird_image".parse().unwrap();
assert!(v.is_other());
assert_eq!(v.as_str(), "weird_image");
assert_eq!(v.to_string(), "weird_image");
}
#[test]
fn display_matches_as_str() {
assert_eq!(Format::Jpeg.to_string(), "jpeg");
assert_eq!(Format::Threefr.to_string(), "3fr");
assert_eq!(Format::Other(SmolStr::new("custom")).to_string(), "custom");
}
#[test]
fn is_variant_predicates() {
assert!(Format::Jpeg.is_jpeg());
assert!(!Format::Png.is_jpeg());
assert!(Format::Threefr.is_threefr());
assert!(Format::Other(SmolStr::new("x")).is_other());
assert!(Format::Cr2.is_cr2());
assert!(!Format::Cr3.is_cr2());
assert!(Format::Cr3.is_cr3());
assert!(Format::Rw2.is_rw2());
assert!(Format::X3f.is_x3f());
}
#[test]
fn unwrap_other_borrowed_view() {
let v = Format::Other(SmolStr::new("custom"));
assert_eq!(v.unwrap_other_ref().as_str(), "custom");
assert!(v.try_unwrap_other_ref().is_ok());
let named = Format::Jpeg;
assert!(named.try_unwrap_other_ref().is_err());
}
#[test]
fn as_extension_matches_disk_form() {
for (variant, ext) in [
(Format::Jpeg, "jpg"),
(Format::Png, "png"),
(Format::Heif, "heif"),
(Format::Heic, "heic"),
(Format::Avif, "avif"),
(Format::Tiff, "tif"),
(Format::Webp, "webp"),
(Format::Gif, "gif"),
(Format::Bmp, "bmp"),
(Format::Dng, "dng"),
(Format::Cr2, "cr2"),
(Format::Cr3, "cr3"),
(Format::Nef, "nef"),
(Format::Nrw, "nrw"),
(Format::Arw, "arw"),
(Format::Orf, "orf"),
(Format::Rw2, "rw2"),
(Format::Raf, "raf"),
(Format::Pef, "pef"),
(Format::Srw, "srw"),
(Format::Rwl, "rwl"),
(Format::Iiq, "iiq"),
(Format::Threefr, "3fr"),
(Format::X3f, "x3f"),
(Format::Mrw, "mrw"),
(Format::Gpr, "gpr"),
] {
assert_eq!(variant.as_extension(), ext, "{variant:?}");
}
assert_eq!(Format::Other(SmolStr::new("weird")).as_extension(), "");
}
#[test]
fn extensions_are_canonical_first_and_every_alias_parses() {
for v in Format::ROSTER {
let exts = v.extensions();
assert!(!exts.is_empty(), "{v:?}: extensions() is empty");
assert_eq!(
exts[0],
v.as_extension(),
"{v:?}: extensions()[0] must be as_extension()"
);
for ext in exts {
let parsed: Format = ext.parse().unwrap();
assert_eq!(&parsed, v, "extension `{ext}` did not parse back to {v:?}");
let shouted: Format = ext.to_ascii_uppercase().parse().unwrap();
assert_eq!(
&shouted, v,
"extension `{ext}` (uppercased) did not parse back to {v:?}"
);
}
}
assert_eq!(
Format::Other(SmolStr::new("weird")).extensions(),
&[] as &[&str]
);
assert_eq!(Format::Jpeg.extensions(), &["jpg", "jpeg", "jpe"]);
assert_eq!(Format::Heif.extensions(), &["heif"]);
assert_eq!(Format::Heic.extensions(), &["heic"]);
assert!(!Format::Heif.extensions().contains(&"hif"));
assert!(!Format::Heic.extensions().contains(&"hif"));
assert_eq!(Format::Tiff.extensions(), &["tif", "tiff"]);
assert_eq!(Format::Bmp.extensions(), &["bmp", "dib"]);
assert_eq!(Format::Orf.extensions(), &["orf", "ori"]);
}
#[test]
fn r6_heic_routes_to_its_own_variant_and_the_heif_route_is_gone_for_heic() {
for ext in ["heic", "HEIC"] {
let v: Format = ext.parse().unwrap();
assert_eq!(v, Format::Heic, "`{ext}` must route to Heic");
assert_ne!(v, Format::Heif, "`{ext}` must NOT route to Heif");
}
assert_eq!("heif".parse::<Format>().unwrap(), Format::Heif);
assert_eq!("HEIF".parse::<Format>().unwrap(), Format::Heif);
}
#[test]
fn r8_hif_routes_to_other_not_heic_or_heif() {
for ext in ["hif", "HIF", "Hif"] {
let v: Format = ext.parse().unwrap();
assert_eq!(
v,
Format::other("hif"),
"`{ext}` must route to the open escape, carrying its own name"
);
assert!(v.is_other(), "`{ext}` must NOT resolve to a named variant");
assert_ne!(v, Format::Heic, "`{ext}` must NOT route to Heic");
assert_ne!(v, Format::Heif, "`{ext}` must NOT route to Heif");
}
}
#[test]
fn format_slugs_are_lowercase_canonical_and_fold() {
const SLUGS: &[&str] = &[
"jpeg", "png", "heif", "heic", "avif", "tiff", "webp", "gif", "bmp", "dng", "cr2", "cr3",
"nef", "nrw", "arw", "orf", "rw2", "raf", "pef", "srw", "rwl", "iiq", "3fr", "x3f", "mrw",
"gpr",
];
for (i, slug) in SLUGS.iter().enumerate() {
assert!(
!slug.bytes().any(|b| b.is_ascii_uppercase()),
"slug {slug:?} is not lowercase-canonical"
);
for prior in &SLUGS[..i] {
assert!(
!prior.eq_ignore_ascii_case(slug),
"two variants fold onto {slug:?}"
);
}
let v: Format = slug.parse().unwrap();
assert!(!v.is_other(), "`{slug}` should be a named variant");
assert_eq!(v.as_str(), *slug, "`{slug}` is not its own canonical form");
}
assert_eq!("jpg", "JPG".parse::<Format>().unwrap().as_extension());
assert_eq!("cr2", "CR2".parse::<Format>().unwrap().as_str());
const ALIASES: &[&str] = &["jpg", "jpe", "heic", "tif", "dib", "ori"];
for alias in ALIASES {
assert!(!alias.bytes().any(|b| b.is_ascii_uppercase()));
let v: Format = alias.parse().unwrap();
assert!(!v.is_other(), "alias `{alias}` should be a named variant");
let shouted: Format = alias.to_ascii_uppercase().parse().unwrap();
assert_eq!(v, shouted, "alias `{alias}` does not fold by case");
}
let escaped: Format = "WEIRD_X".parse().unwrap();
assert!(escaped.is_other());
assert_eq!(escaped.as_str(), "weird_x");
assert_eq!(Format::other("WEIRD_X"), escaped);
}
#[test]
fn rosters_are_well_formed() {
crate::roster_tests::check(Format::ROSTER, "Format", Format::as_str);
}