Skip to content

Commit 77fd289

Browse files
committed
fix: add test coverage for dynamic style values
1 parent 034e746 commit 77fd289

3 files changed

Lines changed: 408 additions & 0 deletions

File tree

tests/StyleDynamicValueTest.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include <gtest/gtest.h>
9+
10+
#include <yoga/node/Node.h>
11+
#include <yoga/style/StyleLength.h>
12+
#include <yoga/style/StyleSizeLength.h>
13+
14+
namespace facebook::yoga {
15+
16+
static YGValue _resolveToHalfReference(
17+
YGNodeConstRef /*node*/,
18+
YGValueDynamicID /*id*/,
19+
YGValueDynamicContext context) {
20+
return YGValue{context.referenceLength * 0.5f, YGUnitPoint};
21+
}
22+
23+
static YGValue _resolveToZero(
24+
YGNodeConstRef /*node*/,
25+
YGValueDynamicID /*id*/,
26+
YGValueDynamicContext /*context*/) {
27+
return YGValue{0, YGUnitPoint};
28+
}
29+
30+
TEST(StyleLength, dynamic_resolve_returns_undefined_without_node) {
31+
const auto resolved =
32+
StyleLength::dynamic(_resolveToHalfReference, 1).resolve(100.0f, nullptr);
33+
34+
ASSERT_TRUE(resolved.isUndefined());
35+
}
36+
37+
TEST(StyleLength, dynamic_resolve_returns_undefined_without_callback) {
38+
const Node node{};
39+
const auto resolved = StyleLength::dynamic(nullptr, 1).resolve(100.0f, &node);
40+
41+
ASSERT_TRUE(resolved.isUndefined());
42+
}
43+
44+
TEST(StyleLength, dynamic_resolve_calls_callback) {
45+
const Node node{};
46+
const auto resolved =
47+
StyleLength::dynamic(_resolveToHalfReference, 1).resolve(80.0f, &node);
48+
49+
ASSERT_TRUE(resolved.isDefined());
50+
ASSERT_FLOAT_EQ(resolved.unwrap(), 40.0f);
51+
}
52+
53+
TEST(StyleSizeLength, dynamic_resolve_calls_callback) {
54+
const Node node{};
55+
const auto resolved = StyleSizeLength::dynamic(_resolveToHalfReference, 1)
56+
.resolve(80.0f, &node);
57+
58+
ASSERT_TRUE(resolved.isDefined());
59+
ASSERT_FLOAT_EQ(resolved.unwrap(), 40.0f);
60+
}
61+
62+
TEST(StyleLength, dynamic_equality) {
63+
const auto a = StyleLength::dynamic(_resolveToHalfReference, 1);
64+
const auto b = StyleLength::dynamic(_resolveToHalfReference, 1);
65+
const auto differentId = StyleLength::dynamic(_resolveToHalfReference, 2);
66+
const auto differentCallback = StyleLength::dynamic(_resolveToZero, 1);
67+
const auto points = StyleLength::points(50);
68+
69+
ASSERT_TRUE(a == b);
70+
ASSERT_FALSE(a == differentId);
71+
ASSERT_FALSE(a == differentCallback);
72+
ASSERT_FALSE(a == points);
73+
ASSERT_TRUE(a.inexactEquals(b));
74+
ASSERT_FALSE(a.inexactEquals(differentId));
75+
ASSERT_FALSE(a.inexactEquals(differentCallback));
76+
}
77+
78+
TEST(StyleLength, dynamic_ygvalue_conversion) {
79+
const auto length = StyleLength::dynamic(_resolveToHalfReference, 1);
80+
const YGValue ygValue = static_cast<YGValue>(length);
81+
82+
ASSERT_EQ(ygValue.unit, YGUnitDynamic);
83+
ASSERT_TRUE(YGFloatIsUndefined(ygValue.value));
84+
}
85+
86+
} // namespace facebook::yoga

tests/StyleValuePoolTest.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
namespace facebook::yoga {
1212

13+
static YGValue
14+
dynamicValueCallback(YGNodeConstRef, YGValueDynamicID, YGValueDynamicContext) {
15+
return YGValue{0.0f, YGUnitPoint};
16+
}
17+
1318
TEST(StyleValuePool, undefined_at_init) {
1419
StyleValuePool pool;
1520
StyleValueHandle handle;
@@ -143,4 +148,24 @@ TEST(StyleValuePool, store_keywords) {
143148
EXPECT_EQ(pool.getSize(handleStretch), StyleSizeLength::ofStretch());
144149
}
145150

151+
TEST(StyleValuePool, stores_and_recovers_dynamic) {
152+
StyleValuePool pool;
153+
StyleValueHandle lengthHandle;
154+
StyleValueHandle sizeHandle;
155+
156+
pool.store(lengthHandle, StyleLength::dynamic(dynamicValueCallback, 11));
157+
pool.store(sizeHandle, StyleSizeLength::dynamic(dynamicValueCallback, 12));
158+
159+
const auto restoredLength = pool.getLength(lengthHandle);
160+
const auto restoredSize = pool.getSize(sizeHandle);
161+
162+
EXPECT_TRUE(restoredLength.isDynamic());
163+
EXPECT_EQ(restoredLength.callback(), dynamicValueCallback);
164+
EXPECT_EQ(restoredLength.callbackId(), 11);
165+
166+
EXPECT_TRUE(restoredSize.isDynamic());
167+
EXPECT_EQ(restoredSize.callback(), dynamicValueCallback);
168+
EXPECT_EQ(restoredSize.callbackId(), 12);
169+
}
170+
146171
} // namespace facebook::yoga

0 commit comments

Comments
 (0)