use super::Style;
#[derive(Debug, Clone, Default)]
pub struct ComputedStyle {
pub style: Style,
}
impl ComputedStyle {
pub fn new() -> Self {
Self::default()
}
pub fn compute(style: Style, parent: Option<&ComputedStyle>) -> Self {
let computed_style = match parent {
Some(parent_computed) => style.with_inheritance(&parent_computed.style),
None => style,
};
Self {
style: computed_style,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_computed_style_new() {
let computed = ComputedStyle::new();
assert_eq!(computed.style.layout.gap, 0);
assert_eq!(computed.style.spacing.padding.top, 0);
}
#[test]
fn test_computed_style_compute_no_parent() {
let style = Style::default();
let computed = ComputedStyle::compute(style.clone(), None);
assert_eq!(computed.style.layout.gap, style.layout.gap);
}
#[test]
fn test_computed_style_compute_with_parent() {
let parent_style = Style::default();
let parent = ComputedStyle {
style: parent_style,
};
let child_style = Style::default();
let computed = ComputedStyle::compute(child_style.clone(), Some(&parent));
assert_eq!(computed.style.layout.gap, parent.style.layout.gap);
}
#[test]
fn test_computed_style_default() {
let computed = ComputedStyle::default();
assert_eq!(computed.style.layout.gap, 0);
}
#[test]
fn test_computed_style_preserves_values() {
let mut style = Style::default();
style.layout.gap = 10;
style.spacing.padding.top = 5;
let computed = ComputedStyle::compute(style, None);
assert_eq!(computed.style.layout.gap, 10);
assert_eq!(computed.style.spacing.padding.top, 5);
}
#[test]
fn test_computed_style_clones_style() {
let style = Style::default();
let computed = ComputedStyle::compute(style.clone(), None);
assert_eq!(computed.style.layout.gap, style.layout.gap);
}
}