77 lines
2.0 KiB
Rust
77 lines
2.0 KiB
Rust
use domain::{
|
|
AlignItems, ContainerNode, Direction, JustifyContent, Layout, LayoutChild, LayoutNode,
|
|
LayoutValidationError, Sizing, WidgetId,
|
|
};
|
|
use std::collections::BTreeSet;
|
|
|
|
fn leaf(id: WidgetId) -> LayoutChild {
|
|
LayoutChild {
|
|
sizing: Sizing::Flex(1),
|
|
node: LayoutNode::Leaf(id),
|
|
}
|
|
}
|
|
|
|
fn row(children: Vec<LayoutChild>) -> LayoutNode {
|
|
LayoutNode::Container(ContainerNode {
|
|
direction: Direction::Row,
|
|
gap: 0,
|
|
padding: 0,
|
|
justify_content: JustifyContent::Start,
|
|
align_items: AlignItems::Stretch,
|
|
children,
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn validate_returns_empty_when_all_widgets_exist() {
|
|
let layout = Layout {
|
|
root: row(vec![leaf(1), leaf(2)]),
|
|
};
|
|
let known = BTreeSet::from([1, 2]);
|
|
assert!(layout.validate(&known).is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn validate_reports_unknown_widget_ids() {
|
|
let layout = Layout {
|
|
root: row(vec![leaf(1), leaf(99)]),
|
|
};
|
|
let known = BTreeSet::from([1]);
|
|
let errors = layout.validate(&known);
|
|
assert_eq!(errors, vec![LayoutValidationError::UnknownWidget(99)]);
|
|
}
|
|
|
|
#[test]
|
|
fn validate_reports_empty_container() {
|
|
let layout = Layout { root: row(vec![]) };
|
|
let known = BTreeSet::new();
|
|
let errors = layout.validate(&known);
|
|
assert_eq!(errors, vec![LayoutValidationError::EmptyContainer]);
|
|
}
|
|
|
|
#[test]
|
|
fn validate_checks_nested_containers() {
|
|
let inner = LayoutChild {
|
|
sizing: Sizing::Flex(1),
|
|
node: row(vec![leaf(1), leaf(42)]),
|
|
};
|
|
let layout = Layout {
|
|
root: row(vec![inner, leaf(2)]),
|
|
};
|
|
let known = BTreeSet::from([1, 2]);
|
|
let errors = layout.validate(&known);
|
|
assert_eq!(errors, vec![LayoutValidationError::UnknownWidget(42)]);
|
|
}
|
|
|
|
#[test]
|
|
fn widget_ids_collects_all_leaf_ids() {
|
|
let inner = LayoutChild {
|
|
sizing: Sizing::Flex(1),
|
|
node: row(vec![leaf(3)]),
|
|
};
|
|
let layout = Layout {
|
|
root: row(vec![leaf(1), inner, leaf(2)]),
|
|
};
|
|
assert_eq!(layout.widget_ids(), BTreeSet::from([1, 2, 3]));
|
|
}
|