Add `taffy_test_helpers` crate (#755)
1#[test]
2#[allow(non_snake_case)]
3fn grid_absolute_with_padding__border_box() {
4 #[allow(unused_imports)]
5 use taffy::{prelude::*, Layout};
6 let mut taffy = crate::new_test_tree();
7 let node0 = taffy
8 .new_leaf(taffy::style::Style {
9 position: taffy::style::Position::Absolute,
10 inset: taffy::geometry::Rect {
11 left: auto(),
12 right: taffy::style::LengthPercentageAuto::Length(0f32),
13 top: taffy::style::LengthPercentageAuto::Length(0f32),
14 bottom: auto(),
15 },
16 ..Default::default()
17 })
18 .unwrap();
19 let node1 = taffy
20 .new_leaf(taffy::style::Style {
21 position: taffy::style::Position::Absolute,
22 inset: taffy::geometry::Rect {
23 left: taffy::style::LengthPercentageAuto::Length(10f32),
24 right: auto(),
25 top: auto(),
26 bottom: taffy::style::LengthPercentageAuto::Length(10f32),
27 },
28 ..Default::default()
29 })
30 .unwrap();
31 let node2 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap();
32 let node3 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap();
33 let node4 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap();
34 let node5 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap();
35 let node6 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap();
36 let node7 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap();
37 let node8 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap();
38 let node = taffy
39 .new_with_children(
40 taffy::style::Style {
41 display: taffy::style::Display::Grid,
42 grid_template_rows: vec![length(40f32), length(40f32), length(40f32)],
43 grid_template_columns: vec![length(40f32), length(40f32), length(40f32)],
44 padding: taffy::geometry::Rect {
45 left: taffy::style::LengthPercentage::Length(40f32),
46 right: taffy::style::LengthPercentage::Length(20f32),
47 top: taffy::style::LengthPercentage::Length(10f32),
48 bottom: taffy::style::LengthPercentage::Length(30f32),
49 },
50 ..Default::default()
51 },
52 &[node0, node1, node2, node3, node4, node5, node6, node7, node8],
53 )
54 .unwrap();
55 taffy.compute_layout_with_measure(node, taffy::geometry::Size::MAX_CONTENT, crate::test_measure_function).unwrap();
56 println!("\nComputed tree:");
57 taffy.print_tree(node);
58 println!();
59 let layout = taffy.layout(node).unwrap();
60 let Layout { size, location, .. } = layout;
61 assert_eq!(size.width, 180f32, "width of node {:?}. Expected {}. Actual {}", node, 180f32, size.width);
62 assert_eq!(size.height, 160f32, "height of node {:?}. Expected {}. Actual {}", node, 160f32, size.height);
63 assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x);
64 assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y);
65 let layout = taffy.layout(node0).unwrap();
66 let Layout { size, location, .. } = layout;
67 assert_eq!(size.width, 0f32, "width of node {:?}. Expected {}. Actual {}", node0, 0f32, size.width);
68 assert_eq!(size.height, 0f32, "height of node {:?}. Expected {}. Actual {}", node0, 0f32, size.height);
69 assert_eq!(location.x, 180f32, "x of node {:?}. Expected {}. Actual {}", node0, 180f32, location.x);
70 assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node0, 0f32, location.y);
71 let layout = taffy.layout(node1).unwrap();
72 let Layout { size, location, .. } = layout;
73 assert_eq!(size.width, 0f32, "width of node {:?}. Expected {}. Actual {}", node1, 0f32, size.width);
74 assert_eq!(size.height, 0f32, "height of node {:?}. Expected {}. Actual {}", node1, 0f32, size.height);
75 assert_eq!(location.x, 10f32, "x of node {:?}. Expected {}. Actual {}", node1, 10f32, location.x);
76 assert_eq!(location.y, 150f32, "y of node {:?}. Expected {}. Actual {}", node1, 150f32, location.y);
77 let layout = taffy.layout(node2).unwrap();
78 let Layout { size, location, .. } = layout;
79 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node2, 40f32, size.width);
80 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node2, 40f32, size.height);
81 assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node2, 40f32, location.x);
82 assert_eq!(location.y, 10f32, "y of node {:?}. Expected {}. Actual {}", node2, 10f32, location.y);
83 let layout = taffy.layout(node3).unwrap();
84 let Layout { size, location, .. } = layout;
85 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node3, 40f32, size.width);
86 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node3, 40f32, size.height);
87 assert_eq!(location.x, 80f32, "x of node {:?}. Expected {}. Actual {}", node3, 80f32, location.x);
88 assert_eq!(location.y, 10f32, "y of node {:?}. Expected {}. Actual {}", node3, 10f32, location.y);
89 let layout = taffy.layout(node4).unwrap();
90 let Layout { size, location, .. } = layout;
91 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node4, 40f32, size.width);
92 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node4, 40f32, size.height);
93 assert_eq!(location.x, 120f32, "x of node {:?}. Expected {}. Actual {}", node4, 120f32, location.x);
94 assert_eq!(location.y, 10f32, "y of node {:?}. Expected {}. Actual {}", node4, 10f32, location.y);
95 let layout = taffy.layout(node5).unwrap();
96 let Layout { size, location, .. } = layout;
97 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node5, 40f32, size.width);
98 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node5, 40f32, size.height);
99 assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node5, 40f32, location.x);
100 assert_eq!(location.y, 50f32, "y of node {:?}. Expected {}. Actual {}", node5, 50f32, location.y);
101 let layout = taffy.layout(node6).unwrap();
102 let Layout { size, location, .. } = layout;
103 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node6, 40f32, size.width);
104 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node6, 40f32, size.height);
105 assert_eq!(location.x, 80f32, "x of node {:?}. Expected {}. Actual {}", node6, 80f32, location.x);
106 assert_eq!(location.y, 50f32, "y of node {:?}. Expected {}. Actual {}", node6, 50f32, location.y);
107 let layout = taffy.layout(node7).unwrap();
108 let Layout { size, location, .. } = layout;
109 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node7, 40f32, size.width);
110 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node7, 40f32, size.height);
111 assert_eq!(location.x, 120f32, "x of node {:?}. Expected {}. Actual {}", node7, 120f32, location.x);
112 assert_eq!(location.y, 50f32, "y of node {:?}. Expected {}. Actual {}", node7, 50f32, location.y);
113 let layout = taffy.layout(node8).unwrap();
114 let Layout { size, location, .. } = layout;
115 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node8, 40f32, size.width);
116 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node8, 40f32, size.height);
117 assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node8, 40f32, location.x);
118 assert_eq!(location.y, 90f32, "y of node {:?}. Expected {}. Actual {}", node8, 90f32, location.y);
119}
120
121#[test]
122#[allow(non_snake_case)]
123fn grid_absolute_with_padding__content_box() {
124 #[allow(unused_imports)]
125 use taffy::{prelude::*, Layout};
126 let mut taffy = crate::new_test_tree();
127 let node0 = taffy
128 .new_leaf(taffy::style::Style {
129 box_sizing: taffy::style::BoxSizing::ContentBox,
130 position: taffy::style::Position::Absolute,
131 inset: taffy::geometry::Rect {
132 left: auto(),
133 right: taffy::style::LengthPercentageAuto::Length(0f32),
134 top: taffy::style::LengthPercentageAuto::Length(0f32),
135 bottom: auto(),
136 },
137 ..Default::default()
138 })
139 .unwrap();
140 let node1 = taffy
141 .new_leaf(taffy::style::Style {
142 box_sizing: taffy::style::BoxSizing::ContentBox,
143 position: taffy::style::Position::Absolute,
144 inset: taffy::geometry::Rect {
145 left: taffy::style::LengthPercentageAuto::Length(10f32),
146 right: auto(),
147 top: auto(),
148 bottom: taffy::style::LengthPercentageAuto::Length(10f32),
149 },
150 ..Default::default()
151 })
152 .unwrap();
153 let node2 = taffy
154 .new_leaf(taffy::style::Style { box_sizing: taffy::style::BoxSizing::ContentBox, ..Default::default() })
155 .unwrap();
156 let node3 = taffy
157 .new_leaf(taffy::style::Style { box_sizing: taffy::style::BoxSizing::ContentBox, ..Default::default() })
158 .unwrap();
159 let node4 = taffy
160 .new_leaf(taffy::style::Style { box_sizing: taffy::style::BoxSizing::ContentBox, ..Default::default() })
161 .unwrap();
162 let node5 = taffy
163 .new_leaf(taffy::style::Style { box_sizing: taffy::style::BoxSizing::ContentBox, ..Default::default() })
164 .unwrap();
165 let node6 = taffy
166 .new_leaf(taffy::style::Style { box_sizing: taffy::style::BoxSizing::ContentBox, ..Default::default() })
167 .unwrap();
168 let node7 = taffy
169 .new_leaf(taffy::style::Style { box_sizing: taffy::style::BoxSizing::ContentBox, ..Default::default() })
170 .unwrap();
171 let node8 = taffy
172 .new_leaf(taffy::style::Style { box_sizing: taffy::style::BoxSizing::ContentBox, ..Default::default() })
173 .unwrap();
174 let node = taffy
175 .new_with_children(
176 taffy::style::Style {
177 display: taffy::style::Display::Grid,
178 box_sizing: taffy::style::BoxSizing::ContentBox,
179 grid_template_rows: vec![length(40f32), length(40f32), length(40f32)],
180 grid_template_columns: vec![length(40f32), length(40f32), length(40f32)],
181 padding: taffy::geometry::Rect {
182 left: taffy::style::LengthPercentage::Length(40f32),
183 right: taffy::style::LengthPercentage::Length(20f32),
184 top: taffy::style::LengthPercentage::Length(10f32),
185 bottom: taffy::style::LengthPercentage::Length(30f32),
186 },
187 ..Default::default()
188 },
189 &[node0, node1, node2, node3, node4, node5, node6, node7, node8],
190 )
191 .unwrap();
192 taffy.compute_layout_with_measure(node, taffy::geometry::Size::MAX_CONTENT, crate::test_measure_function).unwrap();
193 println!("\nComputed tree:");
194 taffy.print_tree(node);
195 println!();
196 let layout = taffy.layout(node).unwrap();
197 let Layout { size, location, .. } = layout;
198 assert_eq!(size.width, 180f32, "width of node {:?}. Expected {}. Actual {}", node, 180f32, size.width);
199 assert_eq!(size.height, 160f32, "height of node {:?}. Expected {}. Actual {}", node, 160f32, size.height);
200 assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x);
201 assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y);
202 let layout = taffy.layout(node0).unwrap();
203 let Layout { size, location, .. } = layout;
204 assert_eq!(size.width, 0f32, "width of node {:?}. Expected {}. Actual {}", node0, 0f32, size.width);
205 assert_eq!(size.height, 0f32, "height of node {:?}. Expected {}. Actual {}", node0, 0f32, size.height);
206 assert_eq!(location.x, 180f32, "x of node {:?}. Expected {}. Actual {}", node0, 180f32, location.x);
207 assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node0, 0f32, location.y);
208 let layout = taffy.layout(node1).unwrap();
209 let Layout { size, location, .. } = layout;
210 assert_eq!(size.width, 0f32, "width of node {:?}. Expected {}. Actual {}", node1, 0f32, size.width);
211 assert_eq!(size.height, 0f32, "height of node {:?}. Expected {}. Actual {}", node1, 0f32, size.height);
212 assert_eq!(location.x, 10f32, "x of node {:?}. Expected {}. Actual {}", node1, 10f32, location.x);
213 assert_eq!(location.y, 150f32, "y of node {:?}. Expected {}. Actual {}", node1, 150f32, location.y);
214 let layout = taffy.layout(node2).unwrap();
215 let Layout { size, location, .. } = layout;
216 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node2, 40f32, size.width);
217 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node2, 40f32, size.height);
218 assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node2, 40f32, location.x);
219 assert_eq!(location.y, 10f32, "y of node {:?}. Expected {}. Actual {}", node2, 10f32, location.y);
220 let layout = taffy.layout(node3).unwrap();
221 let Layout { size, location, .. } = layout;
222 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node3, 40f32, size.width);
223 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node3, 40f32, size.height);
224 assert_eq!(location.x, 80f32, "x of node {:?}. Expected {}. Actual {}", node3, 80f32, location.x);
225 assert_eq!(location.y, 10f32, "y of node {:?}. Expected {}. Actual {}", node3, 10f32, location.y);
226 let layout = taffy.layout(node4).unwrap();
227 let Layout { size, location, .. } = layout;
228 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node4, 40f32, size.width);
229 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node4, 40f32, size.height);
230 assert_eq!(location.x, 120f32, "x of node {:?}. Expected {}. Actual {}", node4, 120f32, location.x);
231 assert_eq!(location.y, 10f32, "y of node {:?}. Expected {}. Actual {}", node4, 10f32, location.y);
232 let layout = taffy.layout(node5).unwrap();
233 let Layout { size, location, .. } = layout;
234 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node5, 40f32, size.width);
235 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node5, 40f32, size.height);
236 assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node5, 40f32, location.x);
237 assert_eq!(location.y, 50f32, "y of node {:?}. Expected {}. Actual {}", node5, 50f32, location.y);
238 let layout = taffy.layout(node6).unwrap();
239 let Layout { size, location, .. } = layout;
240 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node6, 40f32, size.width);
241 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node6, 40f32, size.height);
242 assert_eq!(location.x, 80f32, "x of node {:?}. Expected {}. Actual {}", node6, 80f32, location.x);
243 assert_eq!(location.y, 50f32, "y of node {:?}. Expected {}. Actual {}", node6, 50f32, location.y);
244 let layout = taffy.layout(node7).unwrap();
245 let Layout { size, location, .. } = layout;
246 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node7, 40f32, size.width);
247 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node7, 40f32, size.height);
248 assert_eq!(location.x, 120f32, "x of node {:?}. Expected {}. Actual {}", node7, 120f32, location.x);
249 assert_eq!(location.y, 50f32, "y of node {:?}. Expected {}. Actual {}", node7, 50f32, location.y);
250 let layout = taffy.layout(node8).unwrap();
251 let Layout { size, location, .. } = layout;
252 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node8, 40f32, size.width);
253 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node8, 40f32, size.height);
254 assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node8, 40f32, location.x);
255 assert_eq!(location.y, 90f32, "y of node {:?}. Expected {}. Actual {}", node8, 90f32, location.y);
256}