use fonts::{CurrentTextStyle, UIFontFace, UIFontParser};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
fn font_path(rel: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../../fixtures/fonts")
.join(rel)
}
#[test]
fn test_get_romans_basic_functionality() {
let parser = UIFontParser::new();
let paths = vec![
font_path("Inter/Inter-VariableFont_opsz,wght.ttf"),
font_path("Inter/Inter-Italic-VariableFont_opsz,wght.ttf"),
];
if !paths.iter().all(|p| p.exists()) {
println!("Inter fonts not found, skipping test");
return;
}
let font_data: Vec<_> = paths.iter().map(|p| fs::read(p).unwrap()).collect();
let create_font_faces = || {
vec![
UIFontFace {
face_id: "Inter-VariableFont_opsz,wght.ttf".to_string(),
data: &font_data[0],
user_font_style_italic: Some(false), },
UIFontFace {
face_id: "Inter-Italic-VariableFont_opsz,wght.ttf".to_string(),
data: &font_data[1],
user_font_style_italic: Some(true), },
]
};
let roman_matches = parser
.get_romans(
Some("Inter".to_string()),
create_font_faces(),
None, None, )
.expect("Should analyze family successfully");
assert!(roman_matches.len() >= 1);
for mat in &roman_matches {
assert_eq!(mat.distance, 0.0);
assert!(mat.axis_diffs.is_none());
}
let current_style = CurrentTextStyle {
weight: Some(400),
width: Some(100),
slant: None,
custom_axes: HashMap::new(),
};
let roman_matches_with_style = parser
.get_romans(
Some("Inter".to_string()),
create_font_faces(),
Some(current_style),
None,
)
.expect("Should analyze family successfully");
assert!(roman_matches_with_style.len() >= 1);
for i in 1..roman_matches_with_style.len() {
assert!(
roman_matches_with_style[i - 1].distance <= roman_matches_with_style[i].distance,
"Results should be sorted by distance"
);
}
}
#[test]
fn test_get_romans_max_results() {
let parser = UIFontParser::new();
let paths = vec![
font_path("Inter/Inter-VariableFont_opsz,wght.ttf"),
font_path("Inter/Inter-Italic-VariableFont_opsz,wght.ttf"),
];
if !paths.iter().all(|p| p.exists()) {
println!("Inter fonts not found, skipping test");
return;
}
let font_data: Vec<_> = paths.iter().map(|p| fs::read(p).unwrap()).collect();
let font_faces = vec![
UIFontFace {
face_id: "Inter-VariableFont_opsz,wght.ttf".to_string(),
data: &font_data[0],
user_font_style_italic: Some(false), },
UIFontFace {
face_id: "Inter-Italic-VariableFont_opsz,wght.ttf".to_string(),
data: &font_data[1],
user_font_style_italic: Some(true), },
];
let current_style = CurrentTextStyle {
weight: Some(400),
width: Some(100),
slant: None,
custom_axes: HashMap::new(),
};
let roman_matches = parser
.get_romans(
Some("Inter".to_string()),
font_faces,
Some(current_style),
Some(1), )
.expect("Should analyze family successfully");
assert!(roman_matches.len() <= 1);
}
#[test]
fn test_get_romans_no_romans_available() {
let parser = UIFontParser::new();
let path = font_path("Inter/Inter-Italic-VariableFont_opsz,wght.ttf");
if !path.exists() {
println!("Inter Italic font not found, skipping test");
return;
}
let font_data = fs::read(&path).unwrap();
let font_faces = vec![UIFontFace {
face_id: "Inter-Italic-VariableFont_opsz,wght.ttf".to_string(),
data: &font_data,
user_font_style_italic: Some(true), }];
let current_style = CurrentTextStyle {
weight: Some(400),
width: Some(100),
slant: None,
custom_axes: HashMap::new(),
};
let roman_matches = parser
.get_romans(
Some("Inter".to_string()),
font_faces,
Some(current_style),
None,
)
.expect("Should analyze family successfully");
assert_eq!(roman_matches.len(), 0);
}
#[test]
fn test_get_romans_error_handling() {
let parser = UIFontParser::new();
let result = parser.get_romans(
Some("Inter".to_string()),
vec![], None,
None,
);
assert!(result.is_err());
let error_msg = result.unwrap_err();
println!("Error message: {}", error_msg);
assert!(!error_msg.is_empty());
}