Replace per-node measure functions with a single measure function + per-node context (#490)
1#[test]
2fn grid_min_content_flex_row() {
3 #[allow(unused_imports)]
4 use taffy::{prelude::*, tree::Layout, Taffy};
5 let mut taffy: Taffy<crate::TextMeasure> = Taffy::new();
6 let node00 = taffy
7 .new_leaf_with_context(
8 taffy::style::Style { ..Default::default() },
9 crate::TextMeasure {
10 text_content: "HH\u{200b}HH",
11 writing_mode: crate::WritingMode::Horizontal,
12 _aspect_ratio: None,
13 },
14 )
15 .unwrap();
16 let node01 = taffy
17 .new_leaf_with_context(
18 taffy::style::Style { ..Default::default() },
19 crate::TextMeasure {
20 text_content: "HH\u{200b}HH",
21 writing_mode: crate::WritingMode::Horizontal,
22 _aspect_ratio: None,
23 },
24 )
25 .unwrap();
26 let node02 = taffy
27 .new_leaf_with_context(
28 taffy::style::Style { ..Default::default() },
29 crate::TextMeasure {
30 text_content: "HH\u{200b}HH",
31 writing_mode: crate::WritingMode::Horizontal,
32 _aspect_ratio: None,
33 },
34 )
35 .unwrap();
36 let node0 = taffy
37 .new_with_children(
38 taffy::style::Style { display: taffy::style::Display::Flex, ..Default::default() },
39 &[node00, node01, node02],
40 )
41 .unwrap();
42 let node = taffy
43 .new_with_children(
44 taffy::style::Style {
45 display: taffy::style::Display::Grid,
46 grid_template_rows: vec![length(40f32)],
47 grid_template_columns: vec![min_content()],
48 ..Default::default()
49 },
50 &[node0],
51 )
52 .unwrap();
53 taffy.compute_layout_with_measure(node, taffy::geometry::Size::MAX_CONTENT, crate::test_measure_function).unwrap();
54 println!("\nComputed tree:");
55 taffy.print_tree(node);
56 println!();
57 let Layout { size, location, .. } = taffy.layout(node).unwrap();
58 assert_eq!(size.width, 60f32, "width of node {:?}. Expected {}. Actual {}", node, 60f32, size.width);
59 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node, 40f32, size.height);
60 assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x);
61 assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y);
62 let Layout { size, location, .. } = taffy.layout(node0).unwrap();
63 assert_eq!(size.width, 60f32, "width of node {:?}. Expected {}. Actual {}", node0, 60f32, size.width);
64 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node0, 40f32, size.height);
65 assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node0, 0f32, location.x);
66 assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node0, 0f32, location.y);
67 let Layout { size, location, .. } = taffy.layout(node00).unwrap();
68 assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node00, 20f32, size.width);
69 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node00, 40f32, size.height);
70 assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node00, 0f32, location.x);
71 assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node00, 0f32, location.y);
72 let Layout { size, location, .. } = taffy.layout(node01).unwrap();
73 assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node01, 20f32, size.width);
74 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node01, 40f32, size.height);
75 assert_eq!(location.x, 20f32, "x of node {:?}. Expected {}. Actual {}", node01, 20f32, location.x);
76 assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node01, 0f32, location.y);
77 let Layout { size, location, .. } = taffy.layout(node02).unwrap();
78 assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node02, 20f32, size.width);
79 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node02, 40f32, size.height);
80 assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node02, 40f32, location.x);
81 assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node02, 0f32, location.y);
82}