use crate::macros::string_newtype;
string_newtype! {
FontId, "FontId"
}
string_newtype! {
TemplateName, "TemplateName"
}
string_newtype! {
StyleName, "StyleName"
}
#[cfg(test)]
mod tests {
use super::*;
use crate::FoundationError;
#[test]
fn fontid_empty_is_error() {
let err = FontId::new("").unwrap_err();
match err {
FoundationError::EmptyIdentifier { ref item } => {
assert_eq!(item, "FontId");
}
other => panic!("unexpected error: {other}"),
}
}
#[test]
fn fontid_single_char() {
let f = FontId::new("A").unwrap();
assert_eq!(f.as_str(), "A");
}
#[test]
fn fontid_korean() {
let f = FontId::new("한컴바탕").unwrap();
assert_eq!(f.as_str(), "한컴바탕");
}
#[test]
fn fontid_special_chars() {
let f = FontId::new("D2Coding-Bold_Italic").unwrap();
assert_eq!(f.as_str(), "D2Coding-Bold_Italic");
}
#[test]
fn fontid_unicode_emoji() {
let f = FontId::new("Font\u{1F600}").unwrap();
assert!(f.as_str().contains('\u{1F600}'));
}
#[test]
fn fontid_long_name() {
let long = "x".repeat(10_000);
let f = FontId::new(long.clone()).unwrap();
assert_eq!(f.as_str().len(), 10_000);
}
#[test]
fn fontid_equality() {
let a = FontId::new("Arial").unwrap();
let b = FontId::new("Arial").unwrap();
let c = FontId::new("Batang").unwrap();
assert_eq!(a, b);
assert_ne!(a, c);
}
#[test]
fn fontid_hash_in_map() {
use std::collections::HashMap;
let mut map = HashMap::new();
let key = FontId::new("Batang").unwrap();
map.insert(key.clone(), 42);
assert_eq!(map[&key], 42);
}
#[test]
fn fontid_display() {
let f = FontId::new("Arial").unwrap();
assert_eq!(f.to_string(), "Arial");
}
#[test]
fn fontid_serde_roundtrip() {
let f = FontId::new("Batang").unwrap();
let json = serde_json::to_string(&f).unwrap();
assert_eq!(json, "\"Batang\"");
let back: FontId = serde_json::from_str(&json).unwrap();
assert_eq!(back, f);
}
#[test]
fn template_name_empty_is_error() {
assert!(TemplateName::new("").is_err());
}
#[test]
fn template_name_valid() {
let t = TemplateName::new("gov_proposal").unwrap();
assert_eq!(t.as_str(), "gov_proposal");
}
#[test]
fn style_name_empty_is_error() {
assert!(StyleName::new("").is_err());
}
#[test]
fn style_name_valid() {
let s = StyleName::new("heading1").unwrap();
assert_eq!(s.as_str(), "heading1");
}
#[test]
fn id_types_are_distinct() {
fn accept_font(_: &FontId) {}
fn accept_template(_: &TemplateName) {}
let f = FontId::new("x").unwrap();
let t = TemplateName::new("x").unwrap();
accept_font(&f);
accept_template(&t);
}
#[test]
fn fontid_as_ref() {
let f = FontId::new("test").unwrap();
let s: &str = f.as_ref();
assert_eq!(s, "test");
}
}