Skip to content

Commit e97a7e4

Browse files
committed
test(framework): support data-expect-margin-* for computed_style assertions
TestCtx now reads data-expect-margin-top/right/bottom/left (alongside legacy expect_margin_*) and asserts against computed_style().margin. This enables code-constructed tests that assert computed margins (e.g. css_margin.rs margin_root_empty_block) to migrate to HTML.
1 parent 03a122b commit e97a7e4

1 file changed

Lines changed: 59 additions & 1 deletion

File tree

  • float-pigment-forest/tests

float-pigment-forest/tests/mod.rs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub use float_pigment_forest::Len;
1212
use float_pigment_forest::{layout::LayoutPosition, node::Length, *};
1313
use float_pigment_layout::{
1414
DefLength, LayoutGridAuto, LayoutGridTemplate, LayoutTrackListItem, LayoutTrackSize,
15+
LayoutTreeNode,
1516
};
1617
use float_pigment_mlp::{
1718
context::{Context, Parse},
@@ -68,6 +69,15 @@ impl PartialEq<LayoutPosition> for PartialLayoutPosition {
6869
true
6970
}
7071
}
72+
73+
#[derive(Debug, Default)]
74+
pub struct PartialComputedMargin {
75+
pub top: Option<f32>,
76+
pub right: Option<f32>,
77+
pub bottom: Option<f32>,
78+
pub left: Option<f32>,
79+
}
80+
7181
type NodeId = usize;
7282
type PaintPos = FxHashMap<*const Node, (LayoutPosition, Color)>;
7383

@@ -89,6 +99,7 @@ pub struct TestCtx {
8999
pub root: Option<NodePtr>,
90100
pub layout_pos: FxHashMap<*const Node, LayoutPosition>,
91101
pub expect_layout_pos: FxHashMap<*const Node, PartialLayoutPosition>,
102+
pub expect_computed_margin: FxHashMap<*const Node, PartialComputedMargin>,
92103
pub paint_pos: PaintPos,
93104
}
94105

@@ -227,6 +238,7 @@ impl TestCtx {
227238
root: None,
228239
layout_pos: FxHashMap::default(),
229240
expect_layout_pos: FxHashMap::default(),
241+
expect_computed_margin: FxHashMap::default(),
230242
paint_pos: FxHashMap::default(),
231243
}
232244
}
@@ -317,10 +329,26 @@ impl TestCtx {
317329
pub fn assert(&mut self) {
318330
self.expect_layout_pos.iter().for_each(|(id, expect_pos)| {
319331
if let Some(layout_pos) = self.layout_pos.get(id) {
320-
// println!("layout_pos {:?}, expect_pos {:?}", layout_pos, expect_pos);
321332
assert_eq!(expect_pos, layout_pos);
322333
}
323334
});
335+
self.expect_computed_margin.iter().for_each(|(id, expect_m)| {
336+
unsafe {
337+
let cs = (*(*id)).layout_node().computed_style();
338+
if let Some(v) = expect_m.top {
339+
assert_eq!(cs.margin.top.to_f32().round(), v);
340+
}
341+
if let Some(v) = expect_m.right {
342+
assert_eq!(cs.margin.right.to_f32().round(), v);
343+
}
344+
if let Some(v) = expect_m.bottom {
345+
assert_eq!(cs.margin.bottom.to_f32().round(), v);
346+
}
347+
if let Some(v) = expect_m.left {
348+
assert_eq!(cs.margin.left.to_f32().round(), v);
349+
}
350+
}
351+
});
324352
}
325353
#[cfg(target_os = "macos")]
326354
pub fn render(&self) {
@@ -389,6 +417,7 @@ impl TestCtx {
389417
}
390418
}
391419
self.set_expect_layout_pos(node, e.attributes());
420+
self.set_expect_computed_margin(node, e.attributes());
392421

393422
if is_measure_text_slot(e.tag()) {
394423
let text_len = e
@@ -454,6 +483,35 @@ impl TestCtx {
454483
self.expect_layout_pos.insert(node_ptr, pos);
455484
}
456485

486+
pub fn set_expect_computed_margin(&mut self, node_ptr: *const Node, attrs: &Attribute) {
487+
let mut m = PartialComputedMargin::default();
488+
if let Some(v) = attrs
489+
.get("data-expect-margin-top")
490+
.or_else(|| attrs.get("expect_margin_top"))
491+
{
492+
m.top = Some(v.parse::<f32>().unwrap());
493+
}
494+
if let Some(v) = attrs
495+
.get("data-expect-margin-right")
496+
.or_else(|| attrs.get("expect_margin_right"))
497+
{
498+
m.right = Some(v.parse::<f32>().unwrap());
499+
}
500+
if let Some(v) = attrs
501+
.get("data-expect-margin-bottom")
502+
.or_else(|| attrs.get("expect_margin_bottom"))
503+
{
504+
m.bottom = Some(v.parse::<f32>().unwrap());
505+
}
506+
if let Some(v) = attrs
507+
.get("data-expect-margin-left")
508+
.or_else(|| attrs.get("expect_margin_left"))
509+
{
510+
m.left = Some(v.parse::<f32>().unwrap());
511+
}
512+
self.expect_computed_margin.insert(node_ptr, m);
513+
}
514+
457515
// style
458516
pub unsafe fn set_style(
459517
node: &Node,

0 commit comments

Comments
 (0)