padding.ui.tests.cpp
| #include <planet/ostream.hpp>
#include <planet/ui/padding.hpp>
#include <felspar/test.hpp>
namespace {
|
The CSS shorthand rules that the side defaults implement
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 | constexpr planet::ui::padding every_side{2};
static_assert(every_side.top == 2);
static_assert(every_side.right == 2);
static_assert(every_side.bottom == 2);
static_assert(every_side.left == 2);
constexpr planet::ui::padding vertical_horizontal{2, 3};
static_assert(vertical_horizontal.top == 2);
static_assert(vertical_horizontal.right == 3);
static_assert(vertical_horizontal.bottom == 2);
static_assert(vertical_horizontal.left == 3);
constexpr planet::ui::padding top_horizontal_bottom{2, 3, 4};
static_assert(top_horizontal_bottom.top == 2);
static_assert(top_horizontal_bottom.right == 3);
static_assert(top_horizontal_bottom.bottom == 4);
static_assert(top_horizontal_bottom.left == 3);
constexpr planet::ui::padding each_side{2, 3, 4, 5};
static_assert(each_side.top == 2);
static_assert(each_side.right == 3);
static_assert(each_side.bottom == 4);
static_assert(each_side.left == 5);
|
The mirroring the named vertical/horizontal call sites rely on
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 | constexpr planet::ui::padding named{.top = 2, .right = 3};
static_assert(named.bottom == named.top);
static_assert(named.left == named.right);
auto const rm = felspar::testsuite(
"padding.ui/remove_from",
[](auto check) {
constexpr planet::ui::padding p{.top = 2, .right = 3};
auto const r = p.remove_from(
planet::affine::rectangle2d{
{10, 20}, planet::affine::extents2d{100, 60}});
check(r.top_left) == planet::affine::point2d{13, 22};
check(r.extents) == planet::affine::extents2d{94, 56};
},
[](auto check) {
constexpr planet::ui::padding p{2, 3, 4, 5};
check(p.remove_from(planet::affine::extents2d{100, 60}))
== planet::affine::extents2d{92, 54};
});
auto const at = felspar::testsuite(
"padding.ui/add_to",
[](auto check) {
constexpr planet::ui::padding p{2, 3, 4, 5};
check(p.add_to(planet::affine::extents2d{92, 54}))
== planet::affine::extents2d{100, 60};
},
[](auto check) {
|
Adding back what was removed gives the rectangle again
66
67
68
69
70
71
72
73
74
75
76
77
78
79 | constexpr planet::ui::padding p{2, 3, 4, 5};
constexpr planet::affine::rectangle2d r{
{10, 20}, planet::affine::extents2d{100, 60}};
check(p.add_to(p.remove_from(r)).top_left) == r.top_left;
check(p.add_to(p.remove_from(r)).extents) == r.extents;
});
constexpr planet::ui::padding::constrained_type space{
{100, 20, 200}, {60, 10, 120}};
auto const rl = felspar::testsuite(
"padding.ui/reflow",
[](auto check) {
|
The lambda lays out in the space left by the padding
81
82
83
84
85
86
87
88
89
90
91
92
93
94 | constexpr planet::ui::padding p{.top = 2, .right = 3};
p.reflow(
{.screen = space}, space,
[&](auto const &, auto const &inner) {
check(inner.width.value()) == 94;
check(inner.width.min()) == 14;
check(inner.width.max()) == 194;
check(inner.height.value()) == 56;
check(inner.height.min()) == 6;
check(inner.height.max()) == 116;
return inner;
});
},
[](auto check) {
|
A lambda that fills what it is offered is given back ex
96
97
98
99
100
101
102
103
104
105
106
107 | constexpr planet::ui::padding p{.top = 2, .right = 3};
auto const outer = p.reflow(
{.screen = space}, space,
[](auto const &, auto const &inner) { return inner; });
check(outer.width.value()) == space.width.value();
check(outer.width.min()) == space.width.min();
check(outer.width.max()) == space.width.max();
check(outer.height.value()) == space.height.value();
check(outer.height.min()) == space.height.min();
check(outer.height.max()) == space.height.max();
},
[](auto check) {
|
The content's own minimum comes back out padded
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 | constexpr planet::ui::padding p{.top = 2, .right = 3};
auto const outer = p.reflow(
{.screen = space}, space,
[](auto const &, auto const &) {
return planet::ui::padding::constrained_type{
{40, 30, 194}, {25, 20, 116}};
});
check(outer.width.value()) == 46;
check(outer.width.min()) == 36;
check(outer.height.value()) == 29;
check(outer.height.min()) == 24;
});
constexpr planet::affine::rectangle2d area{
{10, 20}, planet::affine::extents2d{100, 60}};
auto const op = felspar::testsuite(
"padding.ui/move_to_with_outer_padding",
[](auto check) {
|
The lambda lays out inside the padding, and what it lays out
in is all that is claimed — the ring is nobody's
133
134
135
136
137
138
139
140
141
142
143
144
145
146 | constexpr planet::ui::padding p{.top = 2, .right = 3};
auto const used = p.move_to_with_outer_padding(
{.screen = space}, area,
[&](auto const &, auto const &inner) {
check(inner.top_left)
== planet::affine::point2d{13, 22};
check(inner.extents)
== planet::affine::extents2d{94, 56};
return inner;
});
check(used.top_left) == planet::affine::point2d{13, 22};
check(used.extents) == planet::affine::extents2d{94, 56};
},
[](auto check) {
|
Content that uses less keeps only what it used
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163 | constexpr planet::ui::padding p{.top = 2, .right = 3};
auto const used = p.move_to_with_outer_padding(
{.screen = space}, area,
[](auto const &, auto const &inner) {
return planet::affine::rectangle2d{
inner.top_left,
planet::affine::extents2d{10, 10}};
});
check(used.top_left) == planet::affine::point2d{13, 22};
check(used.extents) == planet::affine::extents2d{10, 10};
});
auto const im = felspar::testsuite(
"padding.ui/move_to_with_inner_margin",
[](auto check) {
|
A lambda that fills what it is offered gets back r, the
margin being part of what the element covers
168
169
170
171
172
173
174
175 | constexpr planet::ui::padding p{.top = 2, .right = 3};
auto const used = p.move_to_with_inner_margin(
{.screen = space}, area,
[](auto const &, auto const &inner) { return inner; });
check(used.top_left) == area.top_left;
check(used.extents) == area.extents;
},
[](auto check) {
|
Content that uses less still covers its margin around it
177
178
179
180
181
182
183
184
185
186
187
188
189
190 | constexpr planet::ui::padding p{.top = 2, .right = 3};
auto const used = p.move_to_with_inner_margin(
{.screen = space}, area,
[](auto const &, auto const &inner) {
return planet::affine::rectangle2d{
inner.top_left,
planet::affine::extents2d{10, 10}};
});
check(used.top_left) == planet::affine::point2d{10, 20};
check(used.extents) == planet::affine::extents2d{16, 14};
});
}
|