Skip to content

Commit 7fa1b61

Browse files
DevmateUtgenCppGeneralFBOrg Botfacebook-github-bot
authored andcommitted
xplat/js/react-native-github/packages/react-native/ReactCommon/react/renderer/attributedstring/ParagraphAttributes.cpp
Reviewed By: cortinico Differential Revision: D110867849
1 parent 7edcc28 commit 7fa1b61

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
#include <react/renderer/attributedstring/ParagraphAttributes.h>
10+
11+
namespace facebook::react {
12+
13+
// The three Float fields default to NaN, and NaN != NaN under IEEE-754.
14+
// operator== must special-case NaN via floatEquality so two freshly
15+
// default-constructed ParagraphAttributes compare equal.
16+
TEST(
17+
ParagraphAttributesTest,
18+
testOperatorEqualsDefaultConstructedInstancesAreEqual) {
19+
ParagraphAttributes a{};
20+
ParagraphAttributes b{};
21+
22+
EXPECT_TRUE(a == b);
23+
}
24+
25+
// operator== compares Float fields with an epsilon tolerance (0.005) rather
26+
// than an exact ==. Differences below the epsilon must still compare equal;
27+
// differences well above the epsilon must compare unequal.
28+
TEST(
29+
ParagraphAttributesTest,
30+
testOperatorEqualsFloatFieldsUseEpsilonComparison) {
31+
ParagraphAttributes a{};
32+
a.minimumFontSize = 12.0f;
33+
a.maximumFontSize = 48.0f;
34+
a.minimumFontScale = 0.5f;
35+
auto b = a;
36+
37+
b.minimumFontSize = a.minimumFontSize + 0.001f;
38+
b.maximumFontSize = a.maximumFontSize + 0.001f;
39+
b.minimumFontScale = a.minimumFontScale + 0.001f;
40+
EXPECT_TRUE(a == b);
41+
42+
b = a;
43+
b.minimumFontSize = a.minimumFontSize + 1.0f;
44+
EXPECT_FALSE(a == b);
45+
}
46+
47+
// floatEquality returns true only when *both* operands are NaN or when
48+
// *neither* is. A NaN-vs-finite mismatch in any of the three float fields
49+
// must therefore make the instances unequal, even though both operands are
50+
// "invalid" font sizes.
51+
TEST(
52+
ParagraphAttributesTest,
53+
testOperatorEqualsNaNVsFiniteFloatComparesUnequal) {
54+
ParagraphAttributes withNaN{};
55+
ParagraphAttributes withFinite{};
56+
withFinite.minimumFontSize = 12.0f;
57+
58+
EXPECT_FALSE(withNaN == withFinite);
59+
}
60+
61+
// textAlignVertical is a std::optional; operator== must treat "unset" and
62+
// "set" as distinct, independent of the wrapped value.
63+
TEST(
64+
ParagraphAttributesTest,
65+
testOperatorEqualsHandlesOptionalTextAlignVerticalSetVsUnset) {
66+
ParagraphAttributes unset{};
67+
ParagraphAttributes set{};
68+
set.textAlignVertical = TextAlignmentVertical::Auto;
69+
70+
EXPECT_FALSE(unset == set);
71+
}
72+
73+
} // namespace facebook::react

0 commit comments

Comments
 (0)