tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 1111HTML 1039Markdown 12TOML 94541 Sutichaykuna

⎇ float-layout tests/generated/grid/grid_percent_items_width_and_margin.rs Rust · 117 siq'ikuna · 5.4 KB Ñawpa willay Pi imata llamkarqan ch'uya

Use tagged pointer to represent size/length values (#769) afa6437b0 · Nico Burns ·

1#[test]
2#[allow(non_snake_case)]
3fn grid_percent_items_width_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            size: taffy::geometry::Size { width: taffy::style::Dimension::from_percent(0.45f32), height: auto() },
10            margin: taffy::geometry::Rect {
11                left: percent(0.05f32),
12                right: percent(0.05f32),
13                top: percent(0.05f32),
14                bottom: percent(0.05f32),
15            },
16            padding: taffy::geometry::Rect {
17                left: length(3f32),
18                right: length(3f32),
19                top: length(3f32),
20                bottom: length(3f32),
21            },
22            ..Default::default()
23        })
24        .unwrap();
25    let node = taffy
26        .new_with_children(
27            taffy::style::Style {
28                display: taffy::style::Display::Grid,
29                size: taffy::geometry::Size { width: taffy::style::Dimension::from_length(200f32), height: auto() },
30                padding: taffy::geometry::Rect {
31                    left: length(3f32),
32                    right: length(3f32),
33                    top: length(3f32),
34                    bottom: length(3f32),
35                },
36                ..Default::default()
37            },
38            &[node0],
39        )
40        .unwrap();
41    taffy.compute_layout_with_measure(node, taffy::geometry::Size::MAX_CONTENT, crate::test_measure_function).unwrap();
42    println!("\nComputed tree:");
43    taffy.print_tree(node);
44    println!();
45    let layout = taffy.layout(node).unwrap();
46    let Layout { size, location, .. } = layout;
47    assert_eq!(size.width, 200f32, "width of node {:?}. Expected {}. Actual {}", node, 200f32, size.width);
48    assert_eq!(size.height, 31f32, "height of node {:?}. Expected {}. Actual {}", node, 31f32, size.height);
49    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x);
50    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y);
51    let layout = taffy.layout(node0).unwrap();
52    let Layout { size, location, .. } = layout;
53    assert_eq!(size.width, 87f32, "width of node {:?}. Expected {}. Actual {}", node0, 87f32, size.width);
54    assert_eq!(size.height, 6f32, "height of node {:?}. Expected {}. Actual {}", node0, 6f32, size.height);
55    assert_eq!(location.x, 13f32, "x of node {:?}. Expected {}. Actual {}", node0, 13f32, location.x);
56    assert_eq!(location.y, 13f32, "y of node {:?}. Expected {}. Actual {}", node0, 13f32, location.y);
57}
58
59#[test]
60#[allow(non_snake_case)]
61fn grid_percent_items_width_and_margin__content_box() {
62    #[allow(unused_imports)]
63    use taffy::{prelude::*, Layout};
64    let mut taffy = crate::new_test_tree();
65    let node0 = taffy
66        .new_leaf(taffy::style::Style {
67            box_sizing: taffy::style::BoxSizing::ContentBox,
68            size: taffy::geometry::Size { width: taffy::style::Dimension::from_percent(0.45f32), height: auto() },
69            margin: taffy::geometry::Rect {
70                left: percent(0.05f32),
71                right: percent(0.05f32),
72                top: percent(0.05f32),
73                bottom: percent(0.05f32),
74            },
75            padding: taffy::geometry::Rect {
76                left: length(3f32),
77                right: length(3f32),
78                top: length(3f32),
79                bottom: length(3f32),
80            },
81            ..Default::default()
82        })
83        .unwrap();
84    let node = taffy
85        .new_with_children(
86            taffy::style::Style {
87                display: taffy::style::Display::Grid,
88                box_sizing: taffy::style::BoxSizing::ContentBox,
89                size: taffy::geometry::Size { width: taffy::style::Dimension::from_length(200f32), height: auto() },
90                padding: taffy::geometry::Rect {
91                    left: length(3f32),
92                    right: length(3f32),
93                    top: length(3f32),
94                    bottom: length(3f32),
95                },
96                ..Default::default()
97            },
98            &[node0],
99        )
100        .unwrap();
101    taffy.compute_layout_with_measure(node, taffy::geometry::Size::MAX_CONTENT, crate::test_measure_function).unwrap();
102    println!("\nComputed tree:");
103    taffy.print_tree(node);
104    println!();
105    let layout = taffy.layout(node).unwrap();
106    let Layout { size, location, .. } = layout;
107    assert_eq!(size.width, 206f32, "width of node {:?}. Expected {}. Actual {}", node, 206f32, size.width);
108    assert_eq!(size.height, 32f32, "height of node {:?}. Expected {}. Actual {}", node, 32f32, size.height);
109    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x);
110    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y);
111    let layout = taffy.layout(node0).unwrap();
112    let Layout { size, location, .. } = layout;
113    assert_eq!(size.width, 96f32, "width of node {:?}. Expected {}. Actual {}", node0, 96f32, size.width);
114    assert_eq!(size.height, 6f32, "height of node {:?}. Expected {}. Actual {}", node0, 6f32, size.height);
115    assert_eq!(location.x, 13f32, "x of node {:?}. Expected {}. Actual {}", node0, 13f32, location.x);
116    assert_eq!(location.y, 13f32, "y of node {:?}. Expected {}. Actual {}", node0, 13f32, location.y);
117}