tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 707HTML 660Markdown 12TOML 62344 Definitions

⎇ 0.7.x tests/generated/grid/grid_absolute_with_padding_and_margin.rs Rust · 280 lines · 17.5 KB History Who touched what raw

Add `taffy_test_helpers` crate (#755) 2a3e8476b · Nico Burns ·

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