From 22e7691473a0e895385e03743186aaa32add6731 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Fri, 17 Jan 2025 15:44:48 -0800 Subject: [PATCH] Split value parsing tests (#48770) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/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 --- .../react/renderer/css/tests/CSSAngleTest.cpp | 44 +++ .../react/renderer/css/tests/CSSColorTest.cpp | 102 ++++++ .../renderer/css/tests/CSSKeywordTest.cpp | 52 +++ .../css/tests/CSSLengthPercentageTest.cpp | 34 ++ .../renderer/css/tests/CSSLengthTest.cpp | 53 ++++ .../css/tests/CSSNumberLengthTest.cpp | 21 ++ .../renderer/css/tests/CSSNumberTest.cpp | 30 ++ .../react/renderer/css/tests/CSSRatioTest.cpp | 63 ++++ .../renderer/css/tests/CSSValueParserTest.cpp | 297 ------------------ 9 files changed, 399 insertions(+), 297 deletions(-) create mode 100644 packages/react-native/ReactCommon/react/renderer/css/tests/CSSAngleTest.cpp create mode 100644 packages/react-native/ReactCommon/react/renderer/css/tests/CSSColorTest.cpp create mode 100644 packages/react-native/ReactCommon/react/renderer/css/tests/CSSKeywordTest.cpp create mode 100644 packages/react-native/ReactCommon/react/renderer/css/tests/CSSLengthPercentageTest.cpp create mode 100644 packages/react-native/ReactCommon/react/renderer/css/tests/CSSLengthTest.cpp create mode 100644 packages/react-native/ReactCommon/react/renderer/css/tests/CSSNumberLengthTest.cpp create mode 100644 packages/react-native/ReactCommon/react/renderer/css/tests/CSSNumberTest.cpp create mode 100644 packages/react-native/ReactCommon/react/renderer/css/tests/CSSRatioTest.cpp delete mode 100644 packages/react-native/ReactCommon/react/renderer/css/tests/CSSValueParserTest.cpp diff --git a/packages/react-native/ReactCommon/react/renderer/css/tests/CSSAngleTest.cpp b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSAngleTest.cpp new file mode 100644 index 00000000000000..114093bd42abd4 --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSAngleTest.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include +#include +#include + +namespace facebook::react { + +TEST(CSSAngle, angle_values) { + auto emptyValue = parseCSSProperty(""); + EXPECT_TRUE(std::holds_alternative(emptyValue)); + + auto degreeValue = parseCSSProperty("10deg"); + EXPECT_TRUE(std::holds_alternative(degreeValue)); + EXPECT_EQ(std::get(degreeValue).degrees, 10.0f); + + auto spongebobCaseValue = parseCSSProperty("20dEg"); + EXPECT_TRUE(std::holds_alternative(spongebobCaseValue)); + EXPECT_EQ(std::get(spongebobCaseValue).degrees, 20.0f); + + auto radianValue = parseCSSProperty("10rad"); + EXPECT_TRUE(std::holds_alternative(radianValue)); + ASSERT_NEAR(std::get(radianValue).degrees, 572.958f, 0.001f); + + auto negativeRadianValue = parseCSSProperty("-10rad"); + EXPECT_TRUE(std::holds_alternative(negativeRadianValue)); + ASSERT_NEAR( + std::get(negativeRadianValue).degrees, -572.958f, 0.001f); + + auto gradianValue = parseCSSProperty("10grad"); + EXPECT_TRUE(std::holds_alternative(gradianValue)); + ASSERT_NEAR(std::get(gradianValue).degrees, 9.0f, 0.001f); + + auto turnValue = parseCSSProperty(".25turn"); + EXPECT_TRUE(std::holds_alternative(turnValue)); + EXPECT_EQ(std::get(turnValue).degrees, 90.0f); +} + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/css/tests/CSSColorTest.cpp b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSColorTest.cpp new file mode 100644 index 00000000000000..3286ce59a56c1f --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSColorTest.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include +#include +#include + +namespace facebook::react { + +TEST(CSSColor, hex_color_values) { + auto emptyValue = parseCSSProperty(""); + EXPECT_TRUE(std::holds_alternative(emptyValue)); + + auto hex3DigitColorValue = parseCSSProperty("#fff"); + EXPECT_TRUE(std::holds_alternative(hex3DigitColorValue)); + EXPECT_EQ(std::get(hex3DigitColorValue).r, 255); + EXPECT_EQ(std::get(hex3DigitColorValue).g, 255); + EXPECT_EQ(std::get(hex3DigitColorValue).b, 255); + EXPECT_EQ(std::get(hex3DigitColorValue).a, 255); + + auto hex4DigitColorValue = parseCSSProperty("#ffff"); + EXPECT_TRUE(std::holds_alternative(hex4DigitColorValue)); + EXPECT_EQ(std::get(hex4DigitColorValue).r, 255); + EXPECT_EQ(std::get(hex4DigitColorValue).g, 255); + EXPECT_EQ(std::get(hex4DigitColorValue).b, 255); + EXPECT_EQ(std::get(hex4DigitColorValue).a, 255); + + auto hex6DigitColorValue = parseCSSProperty("#ffffff"); + EXPECT_TRUE(std::holds_alternative(hex6DigitColorValue)); + EXPECT_EQ(std::get(hex6DigitColorValue).r, 255); + EXPECT_EQ(std::get(hex6DigitColorValue).g, 255); + EXPECT_EQ(std::get(hex6DigitColorValue).b, 255); + EXPECT_EQ(std::get(hex6DigitColorValue).a, 255); + + auto hex8DigitColorValue = parseCSSProperty("#ffffffff"); + EXPECT_TRUE(std::holds_alternative(hex8DigitColorValue)); + EXPECT_EQ(std::get(hex8DigitColorValue).r, 255); + EXPECT_EQ(std::get(hex8DigitColorValue).g, 255); + EXPECT_EQ(std::get(hex8DigitColorValue).b, 255); + EXPECT_EQ(std::get(hex8DigitColorValue).a, 255); + + auto hexMixedCaseColorValue = parseCSSProperty("#FFCc99"); + EXPECT_TRUE(std::holds_alternative(hexMixedCaseColorValue)); + EXPECT_EQ(std::get(hexMixedCaseColorValue).r, 255); + EXPECT_EQ(std::get(hexMixedCaseColorValue).g, 204); + EXPECT_EQ(std::get(hexMixedCaseColorValue).b, 153); + EXPECT_EQ(std::get(hexMixedCaseColorValue).a, 255); + + auto hexDigitOnlyColorValue = parseCSSProperty("#369"); + EXPECT_TRUE(std::holds_alternative(hexDigitOnlyColorValue)); + EXPECT_EQ(std::get(hexDigitOnlyColorValue).r, 51); + EXPECT_EQ(std::get(hexDigitOnlyColorValue).g, 102); + EXPECT_EQ(std::get(hexDigitOnlyColorValue).b, 153); + EXPECT_EQ(std::get(hexDigitOnlyColorValue).a, 255); + + auto hexAlphaTestValue = parseCSSProperty("#FFFFFFCC"); + EXPECT_TRUE(std::holds_alternative(hexAlphaTestValue)); + EXPECT_EQ(std::get(hexAlphaTestValue).r, 255); + EXPECT_EQ(std::get(hexAlphaTestValue).g, 255); + EXPECT_EQ(std::get(hexAlphaTestValue).b, 255); + EXPECT_EQ(std::get(hexAlphaTestValue).a, 204); +} + +TEST(CSSColor, named_colors) { + auto invalidNamedColorTestValue = parseCSSProperty("redd"); + EXPECT_TRUE( + std::holds_alternative(invalidNamedColorTestValue)); + + auto namedColorTestValue1 = parseCSSProperty("red"); + EXPECT_TRUE(std::holds_alternative(namedColorTestValue1)); + EXPECT_EQ(std::get(namedColorTestValue1).r, 255); + EXPECT_EQ(std::get(namedColorTestValue1).g, 0); + EXPECT_EQ(std::get(namedColorTestValue1).b, 0); + EXPECT_EQ(std::get(namedColorTestValue1).a, 255); + + auto namedColorTestValue2 = parseCSSProperty("cornsilk"); + EXPECT_TRUE(std::holds_alternative(namedColorTestValue2)); + EXPECT_EQ(std::get(namedColorTestValue2).r, 255); + EXPECT_EQ(std::get(namedColorTestValue2).g, 248); + EXPECT_EQ(std::get(namedColorTestValue2).b, 220); + EXPECT_EQ(std::get(namedColorTestValue2).a, 255); + + auto namedColorMixedCaseTestValue = parseCSSProperty("sPrINgGrEEn"); + EXPECT_TRUE(std::holds_alternative(namedColorMixedCaseTestValue)); + EXPECT_EQ(std::get(namedColorMixedCaseTestValue).r, 0); + EXPECT_EQ(std::get(namedColorMixedCaseTestValue).g, 255); + EXPECT_EQ(std::get(namedColorMixedCaseTestValue).b, 127); + EXPECT_EQ(std::get(namedColorMixedCaseTestValue).a, 255); + + auto transparentColor = parseCSSProperty("transparent"); + EXPECT_TRUE(std::holds_alternative(transparentColor)); + EXPECT_EQ(std::get(transparentColor).r, 0); + EXPECT_EQ(std::get(transparentColor).g, 0); + EXPECT_EQ(std::get(transparentColor).b, 0); + EXPECT_EQ(std::get(transparentColor).a, 0); +} + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/css/tests/CSSKeywordTest.cpp b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSKeywordTest.cpp new file mode 100644 index 00000000000000..6fea75c367e93b --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSKeywordTest.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include +#include +#include + +namespace facebook::react { + +TEST(CSSKeyword, keyword_values) { + auto emptyValue = parseCSSProperty(""); + EXPECT_TRUE(std::holds_alternative(emptyValue)); + + auto inheritValue = parseCSSProperty<>("inherit"); + EXPECT_TRUE(std::holds_alternative(inheritValue)); + EXPECT_EQ(std::get(inheritValue), CSSWideKeyword::Inherit); + + auto autoValue = parseCSSProperty("auto"); + EXPECT_TRUE(std::holds_alternative(autoValue)); + EXPECT_EQ(std::get(autoValue), CSSKeyword::Auto); + + auto autoCapsValue = parseCSSProperty("AuTO"); + EXPECT_TRUE(std::holds_alternative(autoCapsValue)); + EXPECT_EQ(std::get(autoCapsValue), CSSKeyword::Auto); + + auto autoDisallowedValue = parseCSSProperty<>("auto"); + EXPECT_TRUE(std::holds_alternative(autoDisallowedValue)); + + auto whitespaceValue = parseCSSProperty(" flex-start "); + EXPECT_TRUE(std::holds_alternative(whitespaceValue)); + EXPECT_EQ(std::get(whitespaceValue), CSSKeyword::FlexStart); + + auto badIdentValue = parseCSSProperty("bad"); + EXPECT_TRUE(std::holds_alternative(badIdentValue)); + + auto pxValue = parseCSSProperty<>("20px"); + EXPECT_TRUE(std::holds_alternative(pxValue)); + + auto multiValue = parseCSSProperty<>("auto flex-start"); + EXPECT_TRUE(std::holds_alternative(multiValue)); +} + +TEST(CSSKeyword, parse_constexpr) { + [[maybe_unused]] constexpr auto rowValue = + parseCSSProperty("row"); +} + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/css/tests/CSSLengthPercentageTest.cpp b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSLengthPercentageTest.cpp new file mode 100644 index 00000000000000..c01f1a41df09d0 --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSLengthPercentageTest.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include +#include +#include +#include + +namespace facebook::react { + +TEST(CSSLengthPercentage, length_percentage_values) { + auto emptyValue = parseCSSProperty(""); + EXPECT_TRUE(std::holds_alternative(emptyValue)); + + auto autoValue = + parseCSSProperty("auto"); + EXPECT_TRUE(std::holds_alternative(autoValue)); + EXPECT_EQ(std::get(autoValue), CSSKeyword::Auto); + + auto pxValue = parseCSSProperty("20px"); + EXPECT_TRUE(std::holds_alternative(pxValue)); + EXPECT_EQ(std::get(pxValue).value, 20.0f); + EXPECT_EQ(std::get(pxValue).unit, CSSLengthUnit::Px); + + auto pctValue = parseCSSProperty("-40%"); + EXPECT_TRUE(std::holds_alternative(pctValue)); + EXPECT_EQ(std::get(pctValue).value, -40.0f); +} + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/css/tests/CSSLengthTest.cpp b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSLengthTest.cpp new file mode 100644 index 00000000000000..c991ba5197b568 --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSLengthTest.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include +#include +#include + +namespace facebook::react { + +TEST(CSSLength, length_values) { + auto emptyValue = parseCSSProperty(""); + EXPECT_TRUE(std::holds_alternative(emptyValue)); + + auto autoValue = parseCSSProperty("auto"); + EXPECT_TRUE(std::holds_alternative(autoValue)); + EXPECT_EQ(std::get(autoValue), CSSKeyword::Auto); + + auto pxValue = parseCSSProperty("20px"); + EXPECT_TRUE(std::holds_alternative(pxValue)); + EXPECT_EQ(std::get(pxValue).value, 20.0f); + EXPECT_EQ(std::get(pxValue).unit, CSSLengthUnit::Px); + + auto capsValue = parseCSSProperty("50PX"); + EXPECT_TRUE(std::holds_alternative(capsValue)); + EXPECT_EQ(std::get(capsValue).value, 50.0f); + EXPECT_EQ(std::get(capsValue).unit, CSSLengthUnit::Px); + + auto cmValue = parseCSSProperty("453cm"); + EXPECT_TRUE(std::holds_alternative(cmValue)); + EXPECT_TRUE(std::get(cmValue).value == 453.0f); + EXPECT_EQ(std::get(cmValue).unit, CSSLengthUnit::Cm); + + auto unitlessZeroValue = parseCSSProperty("0"); + EXPECT_TRUE(std::holds_alternative(unitlessZeroValue)); + EXPECT_EQ(std::get(unitlessZeroValue).value, 0.0f); + EXPECT_EQ(std::get(unitlessZeroValue).unit, CSSLengthUnit::Px); + + auto unitlessNonzeroValue = parseCSSProperty("123"); + EXPECT_TRUE(std::holds_alternative(unitlessNonzeroValue)); + + auto pctValue = parseCSSProperty("-40%"); + EXPECT_TRUE(std::holds_alternative(pctValue)); +} + +TEST(CSSLength, parse_constexpr) { + [[maybe_unused]] constexpr auto pxValue = parseCSSProperty("2px"); +} + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/css/tests/CSSNumberLengthTest.cpp b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSNumberLengthTest.cpp new file mode 100644 index 00000000000000..43a472aa9e5067 --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSNumberLengthTest.cpp @@ -0,0 +1,21 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include +#include +#include +#include + +namespace facebook::react { + +TEST(CSSNumberLength, number_length_values) { + auto unitlessZeroValue = parseCSSProperty("0"); + EXPECT_TRUE(std::holds_alternative(unitlessZeroValue)); + EXPECT_EQ(std::get(unitlessZeroValue).value, 0.0f); +} + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/css/tests/CSSNumberTest.cpp b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSNumberTest.cpp new file mode 100644 index 00000000000000..5aaa764ebf8b10 --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSNumberTest.cpp @@ -0,0 +1,30 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include +#include +#include + +namespace facebook::react { + +TEST(CSSNumber, number_values) { + auto emptyValue = parseCSSProperty(""); + EXPECT_TRUE(std::holds_alternative(emptyValue)); + + auto pxValue = parseCSSProperty("20px"); + EXPECT_TRUE(std::holds_alternative(pxValue)); + + auto numberValue = parseCSSProperty("123.456"); + EXPECT_TRUE(std::holds_alternative(numberValue)); + EXPECT_EQ(std::get(numberValue).value, 123.456f); + + auto exponentValue = parseCSSProperty("-1.5E3"); + EXPECT_TRUE(std::holds_alternative(exponentValue)); + EXPECT_EQ(std::get(exponentValue).value, -1.5E3f); +} + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/css/tests/CSSRatioTest.cpp b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSRatioTest.cpp new file mode 100644 index 00000000000000..28039ee361e8eb --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSRatioTest.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include +#include +#include + +namespace facebook::react { + +TEST(CSSRatio, ratio_values) { + auto emptyValue = parseCSSProperty(""); + EXPECT_TRUE(std::holds_alternative(emptyValue)); + + auto validRatio = parseCSSProperty("16/9"); + EXPECT_TRUE(std::holds_alternative(validRatio)); + EXPECT_EQ(std::get(validRatio).numerator, 16.0f); + EXPECT_EQ(std::get(validRatio).denominator, 9.0f); + + auto validRatioWithWhitespace = parseCSSProperty("16 / 9"); + EXPECT_TRUE(std::holds_alternative(validRatioWithWhitespace)); + EXPECT_EQ(std::get(validRatioWithWhitespace).numerator, 16.0f); + EXPECT_EQ(std::get(validRatioWithWhitespace).denominator, 9.0f); + + auto singleNumberRatio = parseCSSProperty("16"); + EXPECT_TRUE(std::holds_alternative(singleNumberRatio)); + EXPECT_EQ(std::get(singleNumberRatio).numerator, 16.0f); + EXPECT_EQ(std::get(singleNumberRatio).denominator, 1.0f); + + auto fractionalNumber = parseCSSProperty("16.5"); + EXPECT_TRUE(std::holds_alternative(fractionalNumber)); + EXPECT_EQ(std::get(fractionalNumber).numerator, 16.5f); + EXPECT_EQ(std::get(fractionalNumber).denominator, 1.0f); + + auto negativeNumber = parseCSSProperty("-16"); + EXPECT_TRUE(std::holds_alternative(negativeNumber)); + + auto missingDenominator = parseCSSProperty("16/"); + EXPECT_TRUE(std::holds_alternative(missingDenominator)); + + auto negativeNumerator = parseCSSProperty("-16/9"); + EXPECT_TRUE(std::holds_alternative(negativeNumerator)); + + auto negativeDenominator = parseCSSProperty("16/-9"); + EXPECT_TRUE(std::holds_alternative(negativeDenominator)); + + auto fractionalNumerator = parseCSSProperty("16.5/9"); + EXPECT_TRUE(std::holds_alternative(fractionalNumerator)); + EXPECT_EQ(std::get(fractionalNumerator).numerator, 16.5f); + EXPECT_EQ(std::get(fractionalNumerator).denominator, 9.0f); + + auto fractionalDenominator = parseCSSProperty("16/9.5"); + EXPECT_TRUE(std::holds_alternative(fractionalDenominator)); + EXPECT_EQ(std::get(fractionalDenominator).numerator, 16.0f); + + auto degenerateRatio = parseCSSProperty("0"); + EXPECT_TRUE(std::holds_alternative(degenerateRatio)); +} + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/css/tests/CSSValueParserTest.cpp b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSValueParserTest.cpp deleted file mode 100644 index a229b5b9ad7d20..00000000000000 --- a/packages/react-native/ReactCommon/react/renderer/css/tests/CSSValueParserTest.cpp +++ /dev/null @@ -1,297 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace facebook::react { - -TEST(CSSValueParser, keyword_values) { - auto emptyValue = parseCSSProperty(""); - EXPECT_TRUE(std::holds_alternative(emptyValue)); - - auto inheritValue = parseCSSProperty<>("inherit"); - EXPECT_TRUE(std::holds_alternative(inheritValue)); - EXPECT_EQ(std::get(inheritValue), CSSWideKeyword::Inherit); - - auto autoValue = parseCSSProperty("auto"); - EXPECT_TRUE(std::holds_alternative(autoValue)); - EXPECT_EQ(std::get(autoValue), CSSKeyword::Auto); - - auto autoCapsValue = parseCSSProperty("AuTO"); - EXPECT_TRUE(std::holds_alternative(autoCapsValue)); - EXPECT_EQ(std::get(autoCapsValue), CSSKeyword::Auto); - - auto autoDisallowedValue = parseCSSProperty<>("auto"); - EXPECT_TRUE(std::holds_alternative(autoDisallowedValue)); - - auto whitespaceValue = parseCSSProperty(" flex-start "); - EXPECT_TRUE(std::holds_alternative(whitespaceValue)); - EXPECT_EQ(std::get(whitespaceValue), CSSKeyword::FlexStart); - - auto badIdentValue = parseCSSProperty("bad"); - EXPECT_TRUE(std::holds_alternative(badIdentValue)); - - auto pxValue = parseCSSProperty<>("20px"); - EXPECT_TRUE(std::holds_alternative(pxValue)); - - auto multiValue = parseCSSProperty<>("auto flex-start"); - EXPECT_TRUE(std::holds_alternative(multiValue)); -} - -TEST(CSSValueParser, length_values) { - auto emptyValue = parseCSSProperty(""); - EXPECT_TRUE(std::holds_alternative(emptyValue)); - - auto autoValue = parseCSSProperty("auto"); - EXPECT_TRUE(std::holds_alternative(autoValue)); - EXPECT_EQ(std::get(autoValue), CSSKeyword::Auto); - - auto pxValue = parseCSSProperty("20px"); - EXPECT_TRUE(std::holds_alternative(pxValue)); - EXPECT_EQ(std::get(pxValue).value, 20.0f); - EXPECT_EQ(std::get(pxValue).unit, CSSLengthUnit::Px); - - auto capsValue = parseCSSProperty("50PX"); - EXPECT_TRUE(std::holds_alternative(capsValue)); - EXPECT_EQ(std::get(capsValue).value, 50.0f); - EXPECT_EQ(std::get(capsValue).unit, CSSLengthUnit::Px); - - auto cmValue = parseCSSProperty("453cm"); - EXPECT_TRUE(std::holds_alternative(cmValue)); - EXPECT_TRUE(std::get(cmValue).value == 453.0f); - EXPECT_EQ(std::get(cmValue).unit, CSSLengthUnit::Cm); - - auto unitlessZeroValue = parseCSSProperty("0"); - EXPECT_TRUE(std::holds_alternative(unitlessZeroValue)); - EXPECT_EQ(std::get(unitlessZeroValue).value, 0.0f); - EXPECT_EQ(std::get(unitlessZeroValue).unit, CSSLengthUnit::Px); - - auto unitlessNonzeroValue = parseCSSProperty("123"); - EXPECT_TRUE(std::holds_alternative(unitlessNonzeroValue)); - - auto pctValue = parseCSSProperty("-40%"); - EXPECT_TRUE(std::holds_alternative(pctValue)); -} - -TEST(CSSValueParser, length_percentage_values) { - auto emptyValue = parseCSSProperty(""); - EXPECT_TRUE(std::holds_alternative(emptyValue)); - - auto autoValue = - parseCSSProperty("auto"); - EXPECT_TRUE(std::holds_alternative(autoValue)); - EXPECT_EQ(std::get(autoValue), CSSKeyword::Auto); - - auto pxValue = parseCSSProperty("20px"); - EXPECT_TRUE(std::holds_alternative(pxValue)); - EXPECT_EQ(std::get(pxValue).value, 20.0f); - EXPECT_EQ(std::get(pxValue).unit, CSSLengthUnit::Px); - - auto pctValue = parseCSSProperty("-40%"); - EXPECT_TRUE(std::holds_alternative(pctValue)); - EXPECT_EQ(std::get(pctValue).value, -40.0f); -} - -TEST(CSSValueParser, number_values) { - auto emptyValue = parseCSSProperty(""); - EXPECT_TRUE(std::holds_alternative(emptyValue)); - - auto pxValue = parseCSSProperty("20px"); - EXPECT_TRUE(std::holds_alternative(pxValue)); - - auto numberValue = parseCSSProperty("123.456"); - EXPECT_TRUE(std::holds_alternative(numberValue)); - EXPECT_EQ(std::get(numberValue).value, 123.456f); - - auto unitlessZeroValue = parseCSSProperty("0"); - EXPECT_TRUE(std::holds_alternative(unitlessZeroValue)); - EXPECT_EQ(std::get(unitlessZeroValue).value, 0.0f); -} - -TEST(CSSValueParser, ratio_values) { - auto emptyValue = parseCSSProperty(""); - EXPECT_TRUE(std::holds_alternative(emptyValue)); - - auto validRatio = parseCSSProperty("16/9"); - EXPECT_TRUE(std::holds_alternative(validRatio)); - EXPECT_EQ(std::get(validRatio).numerator, 16.0f); - EXPECT_EQ(std::get(validRatio).denominator, 9.0f); - - auto validRatioWithWhitespace = parseCSSProperty("16 / 9"); - EXPECT_TRUE(std::holds_alternative(validRatioWithWhitespace)); - EXPECT_EQ(std::get(validRatioWithWhitespace).numerator, 16.0f); - EXPECT_EQ(std::get(validRatioWithWhitespace).denominator, 9.0f); - - auto singleNumberRatio = parseCSSProperty("16"); - EXPECT_TRUE(std::holds_alternative(singleNumberRatio)); - EXPECT_EQ(std::get(singleNumberRatio).numerator, 16.0f); - EXPECT_EQ(std::get(singleNumberRatio).denominator, 1.0f); - - auto fractionalNumber = parseCSSProperty("16.5"); - EXPECT_TRUE(std::holds_alternative(fractionalNumber)); - EXPECT_EQ(std::get(fractionalNumber).numerator, 16.5f); - EXPECT_EQ(std::get(fractionalNumber).denominator, 1.0f); - - auto negativeNumber = parseCSSProperty("-16"); - EXPECT_TRUE(std::holds_alternative(negativeNumber)); - - auto missingDenominator = parseCSSProperty("16/"); - EXPECT_TRUE(std::holds_alternative(missingDenominator)); - - auto negativeNumerator = parseCSSProperty("-16/9"); - EXPECT_TRUE(std::holds_alternative(negativeNumerator)); - - auto negativeDenominator = parseCSSProperty("16/-9"); - EXPECT_TRUE(std::holds_alternative(negativeDenominator)); - - auto fractionalNumerator = parseCSSProperty("16.5/9"); - EXPECT_TRUE(std::holds_alternative(fractionalNumerator)); - EXPECT_EQ(std::get(fractionalNumerator).numerator, 16.5f); - EXPECT_EQ(std::get(fractionalNumerator).denominator, 9.0f); - - auto fractionalDenominator = parseCSSProperty("16/9.5"); - EXPECT_TRUE(std::holds_alternative(fractionalDenominator)); - EXPECT_EQ(std::get(fractionalDenominator).numerator, 16.0f); - - auto degenerateRatio = parseCSSProperty("0"); - EXPECT_TRUE(std::holds_alternative(degenerateRatio)); -} - -TEST(CSSValueParser, angle_values) { - auto emptyValue = parseCSSProperty(""); - EXPECT_TRUE(std::holds_alternative(emptyValue)); - - auto degreeValue = parseCSSProperty("10deg"); - EXPECT_TRUE(std::holds_alternative(degreeValue)); - EXPECT_EQ(std::get(degreeValue).degrees, 10.0f); - - auto spongebobCaseValue = parseCSSProperty("20dEg"); - EXPECT_TRUE(std::holds_alternative(spongebobCaseValue)); - EXPECT_EQ(std::get(spongebobCaseValue).degrees, 20.0f); - - auto radianValue = parseCSSProperty("10rad"); - EXPECT_TRUE(std::holds_alternative(radianValue)); - ASSERT_NEAR(std::get(radianValue).degrees, 572.958f, 0.001f); - - auto negativeRadianValue = parseCSSProperty("-10rad"); - EXPECT_TRUE(std::holds_alternative(negativeRadianValue)); - ASSERT_NEAR( - std::get(negativeRadianValue).degrees, -572.958f, 0.001f); - - auto gradianValue = parseCSSProperty("10grad"); - EXPECT_TRUE(std::holds_alternative(gradianValue)); - ASSERT_NEAR(std::get(gradianValue).degrees, 9.0f, 0.001f); - - auto turnValue = parseCSSProperty(".25turn"); - EXPECT_TRUE(std::holds_alternative(turnValue)); - EXPECT_EQ(std::get(turnValue).degrees, 90.0f); -} - -TEST(CSSValueParser, parse_constexpr) { - [[maybe_unused]] constexpr auto rowValue = - parseCSSProperty("row"); - - [[maybe_unused]] constexpr auto pxValue = parseCSSProperty("2px"); -} - -TEST(CSSValueParser, hex_color_values) { - auto emptyValue = parseCSSProperty(""); - EXPECT_TRUE(std::holds_alternative(emptyValue)); - - auto hex3DigitColorValue = parseCSSProperty("#fff"); - EXPECT_TRUE(std::holds_alternative(hex3DigitColorValue)); - EXPECT_EQ(std::get(hex3DigitColorValue).r, 255); - EXPECT_EQ(std::get(hex3DigitColorValue).g, 255); - EXPECT_EQ(std::get(hex3DigitColorValue).b, 255); - EXPECT_EQ(std::get(hex3DigitColorValue).a, 255); - - auto hex4DigitColorValue = parseCSSProperty("#ffff"); - EXPECT_TRUE(std::holds_alternative(hex4DigitColorValue)); - EXPECT_EQ(std::get(hex4DigitColorValue).r, 255); - EXPECT_EQ(std::get(hex4DigitColorValue).g, 255); - EXPECT_EQ(std::get(hex4DigitColorValue).b, 255); - EXPECT_EQ(std::get(hex4DigitColorValue).a, 255); - - auto hex6DigitColorValue = parseCSSProperty("#ffffff"); - EXPECT_TRUE(std::holds_alternative(hex6DigitColorValue)); - EXPECT_EQ(std::get(hex6DigitColorValue).r, 255); - EXPECT_EQ(std::get(hex6DigitColorValue).g, 255); - EXPECT_EQ(std::get(hex6DigitColorValue).b, 255); - EXPECT_EQ(std::get(hex6DigitColorValue).a, 255); - - auto hex8DigitColorValue = parseCSSProperty("#ffffffff"); - EXPECT_TRUE(std::holds_alternative(hex8DigitColorValue)); - EXPECT_EQ(std::get(hex8DigitColorValue).r, 255); - EXPECT_EQ(std::get(hex8DigitColorValue).g, 255); - EXPECT_EQ(std::get(hex8DigitColorValue).b, 255); - EXPECT_EQ(std::get(hex8DigitColorValue).a, 255); - - auto hexMixedCaseColorValue = parseCSSProperty("#FFCc99"); - EXPECT_TRUE(std::holds_alternative(hexMixedCaseColorValue)); - EXPECT_EQ(std::get(hexMixedCaseColorValue).r, 255); - EXPECT_EQ(std::get(hexMixedCaseColorValue).g, 204); - EXPECT_EQ(std::get(hexMixedCaseColorValue).b, 153); - EXPECT_EQ(std::get(hexMixedCaseColorValue).a, 255); - - auto hexDigitOnlyColorValue = parseCSSProperty("#369"); - EXPECT_TRUE(std::holds_alternative(hexDigitOnlyColorValue)); - EXPECT_EQ(std::get(hexDigitOnlyColorValue).r, 51); - EXPECT_EQ(std::get(hexDigitOnlyColorValue).g, 102); - EXPECT_EQ(std::get(hexDigitOnlyColorValue).b, 153); - EXPECT_EQ(std::get(hexDigitOnlyColorValue).a, 255); - - auto hexAlphaTestValue = parseCSSProperty("#FFFFFFCC"); - EXPECT_TRUE(std::holds_alternative(hexAlphaTestValue)); - EXPECT_EQ(std::get(hexAlphaTestValue).r, 255); - EXPECT_EQ(std::get(hexAlphaTestValue).g, 255); - EXPECT_EQ(std::get(hexAlphaTestValue).b, 255); - EXPECT_EQ(std::get(hexAlphaTestValue).a, 204); -} - -TEST(CSSValueParser, named_colors) { - auto invalidNamedColorTestValue = parseCSSProperty("redd"); - EXPECT_TRUE( - std::holds_alternative(invalidNamedColorTestValue)); - - auto namedColorTestValue1 = parseCSSProperty("red"); - EXPECT_TRUE(std::holds_alternative(namedColorTestValue1)); - EXPECT_EQ(std::get(namedColorTestValue1).r, 255); - EXPECT_EQ(std::get(namedColorTestValue1).g, 0); - EXPECT_EQ(std::get(namedColorTestValue1).b, 0); - EXPECT_EQ(std::get(namedColorTestValue1).a, 255); - - auto namedColorTestValue2 = parseCSSProperty("cornsilk"); - EXPECT_TRUE(std::holds_alternative(namedColorTestValue2)); - EXPECT_EQ(std::get(namedColorTestValue2).r, 255); - EXPECT_EQ(std::get(namedColorTestValue2).g, 248); - EXPECT_EQ(std::get(namedColorTestValue2).b, 220); - EXPECT_EQ(std::get(namedColorTestValue2).a, 255); - - auto namedColorMixedCaseTestValue = parseCSSProperty("sPrINgGrEEn"); - EXPECT_TRUE(std::holds_alternative(namedColorMixedCaseTestValue)); - EXPECT_EQ(std::get(namedColorMixedCaseTestValue).r, 0); - EXPECT_EQ(std::get(namedColorMixedCaseTestValue).g, 255); - EXPECT_EQ(std::get(namedColorMixedCaseTestValue).b, 127); - EXPECT_EQ(std::get(namedColorMixedCaseTestValue).a, 255); - - auto transparentColor = parseCSSProperty("transparent"); - EXPECT_TRUE(std::holds_alternative(transparentColor)); - EXPECT_EQ(std::get(transparentColor).r, 0); - EXPECT_EQ(std::get(transparentColor).g, 0); - EXPECT_EQ(std::get(transparentColor).b, 0); - EXPECT_EQ(std::get(transparentColor).a, 0); -} - -} // namespace facebook::react