tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 998HTML 936Markdown 12TOML 83048 Definitions

⎇ 0.4.x tests/generated/grid/grid_basic.rs Rust · 274 lines · 11.8 KB History Who touched what raw

Rename Taffy struct to TaffyTree (#580) 17235ffa2 · Nico Burns ·

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