Skip to content

Commit 22e7691

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Split value parsing tests (#48770)
Summary: Pull Request resolved: #48770 Splits CSSValueParserTest to be file per data type to better match new structure, before we introduce more complexity and tests for CSS colors. Changelog: [Internal] Reviewed By: joevilches Differential Revision: D68351049 fbshipit-source-id: 1a4218e49c8c8adb056fac1dd67f064a4f890775
1 parent 743de70 commit 22e7691

File tree

9 files changed

+399
-297
lines changed

9 files changed

+399
-297
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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/css/CSSAngle.h>
10+
#include <react/renderer/css/CSSValueParser.h>
11+
12+
namespace facebook::react {
13+
14+
TEST(CSSAngle, angle_values) {
15+
auto emptyValue = parseCSSProperty<CSSAngle>("");
16+
EXPECT_TRUE(std::holds_alternative<std::monostate>(emptyValue));
17+
18+
auto degreeValue = parseCSSProperty<CSSAngle>("10deg");
19+
EXPECT_TRUE(std::holds_alternative<CSSAngle>(degreeValue));
20+
EXPECT_EQ(std::get<CSSAngle>(degreeValue).degrees, 10.0f);
21+
22+
auto spongebobCaseValue = parseCSSProperty<CSSAngle>("20dEg");
23+
EXPECT_TRUE(std::holds_alternative<CSSAngle>(spongebobCaseValue));
24+
EXPECT_EQ(std::get<CSSAngle>(spongebobCaseValue).degrees, 20.0f);
25+
26+
auto radianValue = parseCSSProperty<CSSAngle>("10rad");
27+
EXPECT_TRUE(std::holds_alternative<CSSAngle>(radianValue));
28+
ASSERT_NEAR(std::get<CSSAngle>(radianValue).degrees, 572.958f, 0.001f);
29+
30+
auto negativeRadianValue = parseCSSProperty<CSSAngle>("-10rad");
31+
EXPECT_TRUE(std::holds_alternative<CSSAngle>(negativeRadianValue));
32+
ASSERT_NEAR(
33+
std::get<CSSAngle>(negativeRadianValue).degrees, -572.958f, 0.001f);
34+
35+
auto gradianValue = parseCSSProperty<CSSAngle>("10grad");
36+
EXPECT_TRUE(std::holds_alternative<CSSAngle>(gradianValue));
37+
ASSERT_NEAR(std::get<CSSAngle>(gradianValue).degrees, 9.0f, 0.001f);
38+
39+
auto turnValue = parseCSSProperty<CSSAngle>(".25turn");
40+
EXPECT_TRUE(std::holds_alternative<CSSAngle>(turnValue));
41+
EXPECT_EQ(std::get<CSSAngle>(turnValue).degrees, 90.0f);
42+
}
43+
44+
} // namespace facebook::react
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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/css/CSSColor.h>
10+
#include <react/renderer/css/CSSValueParser.h>
11+
12+
namespace facebook::react {
13+
14+
TEST(CSSColor, hex_color_values) {
15+
auto emptyValue = parseCSSProperty<CSSColor>("");
16+
EXPECT_TRUE(std::holds_alternative<std::monostate>(emptyValue));
17+
18+
auto hex3DigitColorValue = parseCSSProperty<CSSColor>("#fff");
19+
EXPECT_TRUE(std::holds_alternative<CSSColor>(hex3DigitColorValue));
20+
EXPECT_EQ(std::get<CSSColor>(hex3DigitColorValue).r, 255);
21+
EXPECT_EQ(std::get<CSSColor>(hex3DigitColorValue).g, 255);
22+
EXPECT_EQ(std::get<CSSColor>(hex3DigitColorValue).b, 255);
23+
EXPECT_EQ(std::get<CSSColor>(hex3DigitColorValue).a, 255);
24+
25+
auto hex4DigitColorValue = parseCSSProperty<CSSColor>("#ffff");
26+
EXPECT_TRUE(std::holds_alternative<CSSColor>(hex4DigitColorValue));
27+
EXPECT_EQ(std::get<CSSColor>(hex4DigitColorValue).r, 255);
28+
EXPECT_EQ(std::get<CSSColor>(hex4DigitColorValue).g, 255);
29+
EXPECT_EQ(std::get<CSSColor>(hex4DigitColorValue).b, 255);
30+
EXPECT_EQ(std::get<CSSColor>(hex4DigitColorValue).a, 255);
31+
32+
auto hex6DigitColorValue = parseCSSProperty<CSSColor>("#ffffff");
33+
EXPECT_TRUE(std::holds_alternative<CSSColor>(hex6DigitColorValue));
34+
EXPECT_EQ(std::get<CSSColor>(hex6DigitColorValue).r, 255);
35+
EXPECT_EQ(std::get<CSSColor>(hex6DigitColorValue).g, 255);
36+
EXPECT_EQ(std::get<CSSColor>(hex6DigitColorValue).b, 255);
37+
EXPECT_EQ(std::get<CSSColor>(hex6DigitColorValue).a, 255);
38+
39+
auto hex8DigitColorValue = parseCSSProperty<CSSColor>("#ffffffff");
40+
EXPECT_TRUE(std::holds_alternative<CSSColor>(hex8DigitColorValue));
41+
EXPECT_EQ(std::get<CSSColor>(hex8DigitColorValue).r, 255);
42+
EXPECT_EQ(std::get<CSSColor>(hex8DigitColorValue).g, 255);
43+
EXPECT_EQ(std::get<CSSColor>(hex8DigitColorValue).b, 255);
44+
EXPECT_EQ(std::get<CSSColor>(hex8DigitColorValue).a, 255);
45+
46+
auto hexMixedCaseColorValue = parseCSSProperty<CSSColor>("#FFCc99");
47+
EXPECT_TRUE(std::holds_alternative<CSSColor>(hexMixedCaseColorValue));
48+
EXPECT_EQ(std::get<CSSColor>(hexMixedCaseColorValue).r, 255);
49+
EXPECT_EQ(std::get<CSSColor>(hexMixedCaseColorValue).g, 204);
50+
EXPECT_EQ(std::get<CSSColor>(hexMixedCaseColorValue).b, 153);
51+
EXPECT_EQ(std::get<CSSColor>(hexMixedCaseColorValue).a, 255);
52+
53+
auto hexDigitOnlyColorValue = parseCSSProperty<CSSColor>("#369");
54+
EXPECT_TRUE(std::holds_alternative<CSSColor>(hexDigitOnlyColorValue));
55+
EXPECT_EQ(std::get<CSSColor>(hexDigitOnlyColorValue).r, 51);
56+
EXPECT_EQ(std::get<CSSColor>(hexDigitOnlyColorValue).g, 102);
57+
EXPECT_EQ(std::get<CSSColor>(hexDigitOnlyColorValue).b, 153);
58+
EXPECT_EQ(std::get<CSSColor>(hexDigitOnlyColorValue).a, 255);
59+
60+
auto hexAlphaTestValue = parseCSSProperty<CSSColor>("#FFFFFFCC");
61+
EXPECT_TRUE(std::holds_alternative<CSSColor>(hexAlphaTestValue));
62+
EXPECT_EQ(std::get<CSSColor>(hexAlphaTestValue).r, 255);
63+
EXPECT_EQ(std::get<CSSColor>(hexAlphaTestValue).g, 255);
64+
EXPECT_EQ(std::get<CSSColor>(hexAlphaTestValue).b, 255);
65+
EXPECT_EQ(std::get<CSSColor>(hexAlphaTestValue).a, 204);
66+
}
67+
68+
TEST(CSSColor, named_colors) {
69+
auto invalidNamedColorTestValue = parseCSSProperty<CSSColor>("redd");
70+
EXPECT_TRUE(
71+
std::holds_alternative<std::monostate>(invalidNamedColorTestValue));
72+
73+
auto namedColorTestValue1 = parseCSSProperty<CSSColor>("red");
74+
EXPECT_TRUE(std::holds_alternative<CSSColor>(namedColorTestValue1));
75+
EXPECT_EQ(std::get<CSSColor>(namedColorTestValue1).r, 255);
76+
EXPECT_EQ(std::get<CSSColor>(namedColorTestValue1).g, 0);
77+
EXPECT_EQ(std::get<CSSColor>(namedColorTestValue1).b, 0);
78+
EXPECT_EQ(std::get<CSSColor>(namedColorTestValue1).a, 255);
79+
80+
auto namedColorTestValue2 = parseCSSProperty<CSSColor>("cornsilk");
81+
EXPECT_TRUE(std::holds_alternative<CSSColor>(namedColorTestValue2));
82+
EXPECT_EQ(std::get<CSSColor>(namedColorTestValue2).r, 255);
83+
EXPECT_EQ(std::get<CSSColor>(namedColorTestValue2).g, 248);
84+
EXPECT_EQ(std::get<CSSColor>(namedColorTestValue2).b, 220);
85+
EXPECT_EQ(std::get<CSSColor>(namedColorTestValue2).a, 255);
86+
87+
auto namedColorMixedCaseTestValue = parseCSSProperty<CSSColor>("sPrINgGrEEn");
88+
EXPECT_TRUE(std::holds_alternative<CSSColor>(namedColorMixedCaseTestValue));
89+
EXPECT_EQ(std::get<CSSColor>(namedColorMixedCaseTestValue).r, 0);
90+
EXPECT_EQ(std::get<CSSColor>(namedColorMixedCaseTestValue).g, 255);
91+
EXPECT_EQ(std::get<CSSColor>(namedColorMixedCaseTestValue).b, 127);
92+
EXPECT_EQ(std::get<CSSColor>(namedColorMixedCaseTestValue).a, 255);
93+
94+
auto transparentColor = parseCSSProperty<CSSColor>("transparent");
95+
EXPECT_TRUE(std::holds_alternative<CSSColor>(transparentColor));
96+
EXPECT_EQ(std::get<CSSColor>(transparentColor).r, 0);
97+
EXPECT_EQ(std::get<CSSColor>(transparentColor).g, 0);
98+
EXPECT_EQ(std::get<CSSColor>(transparentColor).b, 0);
99+
EXPECT_EQ(std::get<CSSColor>(transparentColor).a, 0);
100+
}
101+
102+
} // namespace facebook::react
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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/css/CSSKeyword.h>
10+
#include <react/renderer/css/CSSValueParser.h>
11+
12+
namespace facebook::react {
13+
14+
TEST(CSSKeyword, keyword_values) {
15+
auto emptyValue = parseCSSProperty<CSSKeyword>("");
16+
EXPECT_TRUE(std::holds_alternative<std::monostate>(emptyValue));
17+
18+
auto inheritValue = parseCSSProperty<>("inherit");
19+
EXPECT_TRUE(std::holds_alternative<CSSWideKeyword>(inheritValue));
20+
EXPECT_EQ(std::get<CSSWideKeyword>(inheritValue), CSSWideKeyword::Inherit);
21+
22+
auto autoValue = parseCSSProperty<CSSKeyword>("auto");
23+
EXPECT_TRUE(std::holds_alternative<CSSKeyword>(autoValue));
24+
EXPECT_EQ(std::get<CSSKeyword>(autoValue), CSSKeyword::Auto);
25+
26+
auto autoCapsValue = parseCSSProperty<CSSKeyword>("AuTO");
27+
EXPECT_TRUE(std::holds_alternative<CSSKeyword>(autoCapsValue));
28+
EXPECT_EQ(std::get<CSSKeyword>(autoCapsValue), CSSKeyword::Auto);
29+
30+
auto autoDisallowedValue = parseCSSProperty<>("auto");
31+
EXPECT_TRUE(std::holds_alternative<std::monostate>(autoDisallowedValue));
32+
33+
auto whitespaceValue = parseCSSProperty<CSSKeyword>(" flex-start ");
34+
EXPECT_TRUE(std::holds_alternative<CSSKeyword>(whitespaceValue));
35+
EXPECT_EQ(std::get<CSSKeyword>(whitespaceValue), CSSKeyword::FlexStart);
36+
37+
auto badIdentValue = parseCSSProperty<CSSKeyword>("bad");
38+
EXPECT_TRUE(std::holds_alternative<std::monostate>(badIdentValue));
39+
40+
auto pxValue = parseCSSProperty<>("20px");
41+
EXPECT_TRUE(std::holds_alternative<std::monostate>(pxValue));
42+
43+
auto multiValue = parseCSSProperty<>("auto flex-start");
44+
EXPECT_TRUE(std::holds_alternative<std::monostate>(multiValue));
45+
}
46+
47+
TEST(CSSKeyword, parse_constexpr) {
48+
[[maybe_unused]] constexpr auto rowValue =
49+
parseCSSProperty<CSSKeyword>("row");
50+
}
51+
52+
} // namespace facebook::react
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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/css/CSSLength.h>
10+
#include <react/renderer/css/CSSPercentage.h>
11+
#include <react/renderer/css/CSSValueParser.h>
12+
13+
namespace facebook::react {
14+
15+
TEST(CSSLengthPercentage, length_percentage_values) {
16+
auto emptyValue = parseCSSProperty<CSSLength, CSSPercentage>("");
17+
EXPECT_TRUE(std::holds_alternative<std::monostate>(emptyValue));
18+
19+
auto autoValue =
20+
parseCSSProperty<CSSKeyword, CSSLength, CSSPercentage>("auto");
21+
EXPECT_TRUE(std::holds_alternative<CSSKeyword>(autoValue));
22+
EXPECT_EQ(std::get<CSSKeyword>(autoValue), CSSKeyword::Auto);
23+
24+
auto pxValue = parseCSSProperty<CSSLength, CSSPercentage>("20px");
25+
EXPECT_TRUE(std::holds_alternative<CSSLength>(pxValue));
26+
EXPECT_EQ(std::get<CSSLength>(pxValue).value, 20.0f);
27+
EXPECT_EQ(std::get<CSSLength>(pxValue).unit, CSSLengthUnit::Px);
28+
29+
auto pctValue = parseCSSProperty<CSSLength, CSSPercentage>("-40%");
30+
EXPECT_TRUE(std::holds_alternative<CSSPercentage>(pctValue));
31+
EXPECT_EQ(std::get<CSSPercentage>(pctValue).value, -40.0f);
32+
}
33+
34+
} // namespace facebook::react
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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/css/CSSLength.h>
10+
#include <react/renderer/css/CSSValueParser.h>
11+
12+
namespace facebook::react {
13+
14+
TEST(CSSLength, length_values) {
15+
auto emptyValue = parseCSSProperty<CSSLength>("");
16+
EXPECT_TRUE(std::holds_alternative<std::monostate>(emptyValue));
17+
18+
auto autoValue = parseCSSProperty<CSSKeyword, CSSLength>("auto");
19+
EXPECT_TRUE(std::holds_alternative<CSSKeyword>(autoValue));
20+
EXPECT_EQ(std::get<CSSKeyword>(autoValue), CSSKeyword::Auto);
21+
22+
auto pxValue = parseCSSProperty<CSSLength>("20px");
23+
EXPECT_TRUE(std::holds_alternative<CSSLength>(pxValue));
24+
EXPECT_EQ(std::get<CSSLength>(pxValue).value, 20.0f);
25+
EXPECT_EQ(std::get<CSSLength>(pxValue).unit, CSSLengthUnit::Px);
26+
27+
auto capsValue = parseCSSProperty<CSSLength>("50PX");
28+
EXPECT_TRUE(std::holds_alternative<CSSLength>(capsValue));
29+
EXPECT_EQ(std::get<CSSLength>(capsValue).value, 50.0f);
30+
EXPECT_EQ(std::get<CSSLength>(capsValue).unit, CSSLengthUnit::Px);
31+
32+
auto cmValue = parseCSSProperty<CSSLength>("453cm");
33+
EXPECT_TRUE(std::holds_alternative<CSSLength>(cmValue));
34+
EXPECT_TRUE(std::get<CSSLength>(cmValue).value == 453.0f);
35+
EXPECT_EQ(std::get<CSSLength>(cmValue).unit, CSSLengthUnit::Cm);
36+
37+
auto unitlessZeroValue = parseCSSProperty<CSSLength>("0");
38+
EXPECT_TRUE(std::holds_alternative<CSSLength>(unitlessZeroValue));
39+
EXPECT_EQ(std::get<CSSLength>(unitlessZeroValue).value, 0.0f);
40+
EXPECT_EQ(std::get<CSSLength>(unitlessZeroValue).unit, CSSLengthUnit::Px);
41+
42+
auto unitlessNonzeroValue = parseCSSProperty<CSSLength>("123");
43+
EXPECT_TRUE(std::holds_alternative<std::monostate>(unitlessNonzeroValue));
44+
45+
auto pctValue = parseCSSProperty<CSSLength>("-40%");
46+
EXPECT_TRUE(std::holds_alternative<std::monostate>(pctValue));
47+
}
48+
49+
TEST(CSSLength, parse_constexpr) {
50+
[[maybe_unused]] constexpr auto pxValue = parseCSSProperty<CSSLength>("2px");
51+
}
52+
53+
} // namespace facebook::react
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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/css/CSSLength.h>
10+
#include <react/renderer/css/CSSNumber.h>
11+
#include <react/renderer/css/CSSValueParser.h>
12+
13+
namespace facebook::react {
14+
15+
TEST(CSSNumberLength, number_length_values) {
16+
auto unitlessZeroValue = parseCSSProperty<CSSNumber, CSSLength>("0");
17+
EXPECT_TRUE(std::holds_alternative<CSSNumber>(unitlessZeroValue));
18+
EXPECT_EQ(std::get<CSSNumber>(unitlessZeroValue).value, 0.0f);
19+
}
20+
21+
} // namespace facebook::react
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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/css/CSSNumber.h>
10+
#include <react/renderer/css/CSSValueParser.h>
11+
12+
namespace facebook::react {
13+
14+
TEST(CSSNumber, number_values) {
15+
auto emptyValue = parseCSSProperty<CSSNumber>("");
16+
EXPECT_TRUE(std::holds_alternative<std::monostate>(emptyValue));
17+
18+
auto pxValue = parseCSSProperty<CSSNumber>("20px");
19+
EXPECT_TRUE(std::holds_alternative<std::monostate>(pxValue));
20+
21+
auto numberValue = parseCSSProperty<CSSNumber>("123.456");
22+
EXPECT_TRUE(std::holds_alternative<CSSNumber>(numberValue));
23+
EXPECT_EQ(std::get<CSSNumber>(numberValue).value, 123.456f);
24+
25+
auto exponentValue = parseCSSProperty<CSSNumber>("-1.5E3");
26+
EXPECT_TRUE(std::holds_alternative<CSSNumber>(exponentValue));
27+
EXPECT_EQ(std::get<CSSNumber>(exponentValue).value, -1.5E3f);
28+
}
29+
30+
} // namespace facebook::react

0 commit comments

Comments
 (0)