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
0 commit comments