use fonts::{CurrentTextStyle, UIFontParser};
use std::collections::HashMap;
#[test]
fn verify_priority_rule_explicit_properties_always_win() {
let parser = UIFontParser::new();
let mut custom_axes = HashMap::new();
custom_axes.insert("wght".to_string(), 500.0);
let current_style = CurrentTextStyle {
weight: Some(400), width: None,
slant: None,
custom_axes,
};
assert_eq!(
parser.get_current_axis_value(¤t_style, "wght"),
Some(400.0),
"FAILURE: Explicit weight=400 should take priority over custom_axes[\"wght\"]=500"
);
let mut custom_axes = HashMap::new();
custom_axes.insert("wdth".to_string(), 120.0);
let current_style = CurrentTextStyle {
weight: None,
width: Some(100), slant: None,
custom_axes,
};
assert_eq!(
parser.get_current_axis_value(¤t_style, "wdth"),
Some(100.0),
"FAILURE: Explicit width=100 should take priority over custom_axes[\"wdth\"]=120"
);
let mut custom_axes = HashMap::new();
custom_axes.insert("slnt".to_string(), 5.0);
let current_style = CurrentTextStyle {
weight: None,
width: None,
slant: Some(15.0), custom_axes,
};
assert_eq!(
parser.get_current_axis_value(¤t_style, "slnt"),
Some(15.0),
"FAILURE: Explicit slant=15.0 should take priority over custom_axes[\"slnt\"]=5.0"
);
}
#[test]
fn verify_fallback_rule_custom_axes_when_explicit_is_none() {
let parser = UIFontParser::new();
let mut custom_axes = HashMap::new();
custom_axes.insert("wght".to_string(), 500.0);
let current_style = CurrentTextStyle {
weight: None, width: None,
slant: None,
custom_axes,
};
assert_eq!(
parser.get_current_axis_value(¤t_style, "wght"),
Some(500.0),
"FAILURE: Should fallback to custom_axes[\"wght\"]=500 when weight=None"
);
let mut custom_axes = HashMap::new();
custom_axes.insert("wdth".to_string(), 120.0);
let current_style = CurrentTextStyle {
weight: None,
width: None, slant: None,
custom_axes,
};
assert_eq!(
parser.get_current_axis_value(¤t_style, "wdth"),
Some(120.0),
"FAILURE: Should fallback to custom_axes[\"wdth\"]=120 when width=None"
);
let mut custom_axes = HashMap::new();
custom_axes.insert("slnt".to_string(), 5.0);
let current_style = CurrentTextStyle {
weight: None,
width: None,
slant: None, custom_axes,
};
assert_eq!(
parser.get_current_axis_value(¤t_style, "slnt"),
Some(5.0),
"FAILURE: Should fallback to custom_axes[\"slnt\"]=5.0 when slant=None"
);
}
#[test]
fn verify_custom_axes_only_for_other_axes() {
let parser = UIFontParser::new();
let mut custom_axes = HashMap::new();
custom_axes.insert("opsz".to_string(), 14.0);
let current_style = CurrentTextStyle {
weight: None,
width: None,
slant: None,
custom_axes,
};
assert_eq!(
parser.get_current_axis_value(¤t_style, "opsz"),
Some(14.0),
"FAILURE: Should use custom_axes[\"opsz\"]=14.0 for axes without explicit properties"
);
let mut custom_axes = HashMap::new();
custom_axes.insert("GRAD".to_string(), 0.5);
let current_style = CurrentTextStyle {
weight: None,
width: None,
slant: None,
custom_axes,
};
assert_eq!(
parser.get_current_axis_value(¤t_style, "GRAD"),
Some(0.5),
"FAILURE: Should use custom_axes[\"GRAD\"]=0.5 for axes without explicit properties"
);
}
#[test]
fn verify_none_values_return_none() {
let parser = UIFontParser::new();
let current_style = CurrentTextStyle {
weight: None,
width: None,
slant: None,
custom_axes: HashMap::new(),
};
assert_eq!(
parser.get_current_axis_value(¤t_style, "wght"),
None,
"FAILURE: Should return None when weight=None and no custom_axes[\"wght\"]"
);
assert_eq!(
parser.get_current_axis_value(¤t_style, "wdth"),
None,
"FAILURE: Should return None when width=None and no custom_axes[\"wdth\"]"
);
assert_eq!(
parser.get_current_axis_value(¤t_style, "slnt"),
None,
"FAILURE: Should return None when slant=None and no custom_axes[\"slnt\"]"
);
assert_eq!(
parser.get_current_axis_value(¤t_style, "ital"),
None,
"FAILURE: Should return None for italic axis (special handling)"
);
assert_eq!(
parser.get_current_axis_value(¤t_style, "opsz"),
None,
"FAILURE: Should return None when no custom_axes[\"opsz\"]"
);
}
#[test]
fn verify_mixed_scenarios_priority_and_fallback() {
let parser = UIFontParser::new();
let mut custom_axes = HashMap::new();
custom_axes.insert("wght".to_string(), 500.0); custom_axes.insert("wdth".to_string(), 120.0); custom_axes.insert("slnt".to_string(), 5.0); custom_axes.insert("opsz".to_string(), 14.0);
let current_style = CurrentTextStyle {
weight: Some(400), width: None, slant: Some(15.0), custom_axes,
};
assert_eq!(
parser.get_current_axis_value(¤t_style, "wght"),
Some(400.0),
"FAILURE: weight=400 should take priority over custom_axes[\"wght\"]=500"
);
assert_eq!(
parser.get_current_axis_value(¤t_style, "wdth"),
Some(120.0),
"FAILURE: width=None should fallback to custom_axes[\"wdth\"]=120"
);
assert_eq!(
parser.get_current_axis_value(¤t_style, "slnt"),
Some(15.0),
"FAILURE: slant=15.0 should take priority over custom_axes[\"slnt\"]=5.0"
);
assert_eq!(
parser.get_current_axis_value(¤t_style, "opsz"),
Some(14.0),
"FAILURE: Should use custom_axes[\"opsz\"]=14.0 for axes without explicit properties"
);
}
#[test]
fn verify_edge_case_zero_values() {
let parser = UIFontParser::new();
let mut custom_axes = HashMap::new();
custom_axes.insert("wght".to_string(), 500.0);
custom_axes.insert("wdth".to_string(), 120.0);
custom_axes.insert("slnt".to_string(), 5.0);
let current_style = CurrentTextStyle {
weight: Some(0), width: Some(0), slant: Some(0.0), custom_axes,
};
assert_eq!(
parser.get_current_axis_value(¤t_style, "wght"),
Some(0.0),
"FAILURE: weight=0 should take priority over custom_axes[\"wght\"]=500"
);
assert_eq!(
parser.get_current_axis_value(¤t_style, "wdth"),
Some(0.0),
"FAILURE: width=0 should take priority over custom_axes[\"wdth\"]=120"
);
assert_eq!(
parser.get_current_axis_value(¤t_style, "slnt"),
Some(0.0),
"FAILURE: slant=0.0 should take priority over custom_axes[\"slnt\"]=5.0"
);
}
#[test]
fn verify_edge_case_negative_values() {
let parser = UIFontParser::new();
let mut custom_axes = HashMap::new();
custom_axes.insert("slnt".to_string(), 5.0);
let current_style = CurrentTextStyle {
weight: None,
width: None,
slant: Some(-15.0), custom_axes,
};
assert_eq!(
parser.get_current_axis_value(¤t_style, "slnt"),
Some(-15.0),
"FAILURE: slant=-15.0 should take priority over custom_axes[\"slnt\"]=5.0"
);
}
#[test]
fn verify_consistency_for_round_trip_testing() {
let parser = UIFontParser::new();
let mut custom_axes = HashMap::new();
custom_axes.insert("wght".to_string(), 500.0);
custom_axes.insert("wdth".to_string(), 120.0);
custom_axes.insert("slnt".to_string(), 5.0);
custom_axes.insert("opsz".to_string(), 14.0);
let current_style = CurrentTextStyle {
weight: Some(400), width: None, slant: Some(15.0), custom_axes,
};
let resolved_values = [
(
"wght",
parser.get_current_axis_value(¤t_style, "wght"),
),
(
"wdth",
parser.get_current_axis_value(¤t_style, "wdth"),
),
(
"slnt",
parser.get_current_axis_value(¤t_style, "slnt"),
),
(
"opsz",
parser.get_current_axis_value(¤t_style, "opsz"),
),
(
"ital",
parser.get_current_axis_value(¤t_style, "ital"),
),
];
let expected_values = [
("wght", Some(400.0)), ("wdth", Some(120.0)), ("slnt", Some(15.0)), ("opsz", Some(14.0)), ("ital", None), ];
for ((axis, actual), (expected_axis, expected)) in
resolved_values.iter().zip(expected_values.iter())
{
assert_eq!(
axis, expected_axis,
"FAILURE: Axis mismatch in consistency test"
);
assert_eq!(
actual, expected,
"FAILURE: {} resolution inconsistent: got {:?}, expected {:?}",
axis, actual, expected
);
}
}