|
| 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 <set> |
| 11 | +#include <string_view> |
| 12 | + |
| 13 | +#include <yoga/enums/Align.h> |
| 14 | +#include <yoga/enums/BoxSizing.h> |
| 15 | +#include <yoga/enums/Dimension.h> |
| 16 | +#include <yoga/enums/Direction.h> |
| 17 | +#include <yoga/enums/Display.h> |
| 18 | +#include <yoga/enums/Edge.h> |
| 19 | +#include <yoga/enums/Errata.h> |
| 20 | +#include <yoga/enums/ExperimentalFeature.h> |
| 21 | +#include <yoga/enums/FlexDirection.h> |
| 22 | +#include <yoga/enums/GridTrackType.h> |
| 23 | +#include <yoga/enums/Gutter.h> |
| 24 | +#include <yoga/enums/Justify.h> |
| 25 | +#include <yoga/enums/LogLevel.h> |
| 26 | +#include <yoga/enums/MeasureMode.h> |
| 27 | +#include <yoga/enums/NodeType.h> |
| 28 | +#include <yoga/enums/Overflow.h> |
| 29 | +#include <yoga/enums/PositionType.h> |
| 30 | +#include <yoga/enums/Unit.h> |
| 31 | +#include <yoga/enums/Wrap.h> |
| 32 | +#include <yoga/enums/YogaEnums.h> |
| 33 | + |
| 34 | +namespace facebook::yoga { |
| 35 | +namespace { |
| 36 | + |
| 37 | +// The scoped enums and the YG*ToString() switches are emitted from separate |
| 38 | +// generator outputs, so ordinalCount<T>() is an independent source of truth for |
| 39 | +// how many values each switch must handle. |
| 40 | +template <HasOrdinality EnumT> |
| 41 | +void expectNamesForEveryOrdinal() { |
| 42 | + std::set<std::string_view> names; |
| 43 | + for (EnumT value : ordinals<EnumT>()) { |
| 44 | + SCOPED_TRACE(to_underlying(value)); |
| 45 | + const char* name = toString(value); |
| 46 | + EXPECT_STRNE("unknown", name); |
| 47 | + EXPECT_STRNE("", name); |
| 48 | + EXPECT_TRUE(names.insert(std::string_view{name}).second) |
| 49 | + << "duplicate name: " << name; |
| 50 | + } |
| 51 | + EXPECT_EQ(ordinalCount<EnumT>(), static_cast<int32_t>(names.size())); |
| 52 | +} |
| 53 | + |
| 54 | +} // namespace |
| 55 | + |
| 56 | +TEST(YGEnumsTest, testToStringNamesEveryOrdinalDistinctly) { |
| 57 | + expectNamesForEveryOrdinal<Align>(); |
| 58 | + expectNamesForEveryOrdinal<BoxSizing>(); |
| 59 | + expectNamesForEveryOrdinal<Dimension>(); |
| 60 | + expectNamesForEveryOrdinal<Direction>(); |
| 61 | + expectNamesForEveryOrdinal<Display>(); |
| 62 | + expectNamesForEveryOrdinal<Edge>(); |
| 63 | + expectNamesForEveryOrdinal<ExperimentalFeature>(); |
| 64 | + expectNamesForEveryOrdinal<FlexDirection>(); |
| 65 | + expectNamesForEveryOrdinal<GridTrackType>(); |
| 66 | + expectNamesForEveryOrdinal<Gutter>(); |
| 67 | + expectNamesForEveryOrdinal<Justify>(); |
| 68 | + expectNamesForEveryOrdinal<LogLevel>(); |
| 69 | + expectNamesForEveryOrdinal<MeasureMode>(); |
| 70 | + expectNamesForEveryOrdinal<NodeType>(); |
| 71 | + expectNamesForEveryOrdinal<Overflow>(); |
| 72 | + expectNamesForEveryOrdinal<PositionType>(); |
| 73 | + expectNamesForEveryOrdinal<Unit>(); |
| 74 | + expectNamesForEveryOrdinal<Wrap>(); |
| 75 | +} |
| 76 | + |
| 77 | +// YGErrata is a bit mask rather than a sequence, so only its declared constants |
| 78 | +// have names; a combination of flags is not one of the flags it contains and |
| 79 | +// must not be reported as such. |
| 80 | +TEST(YGEnumsTest, testErrataToStringNamesConstantsButNotCombinations) { |
| 81 | + std::set<std::string_view> names; |
| 82 | + for (Errata errata : |
| 83 | + {Errata::None, |
| 84 | + Errata::StretchFlexBasis, |
| 85 | + Errata::AbsolutePositionWithoutInsetsExcludesPadding, |
| 86 | + Errata::AbsolutePercentAgainstInnerSize, |
| 87 | + Errata::MinSizeUndefinedInsteadOfAuto, |
| 88 | + Errata::FlexFirstPassUsesRunningTotals, |
| 89 | + Errata::All, |
| 90 | + Errata::Classic}) { |
| 91 | + SCOPED_TRACE(to_underlying(errata)); |
| 92 | + const char* name = toString(errata); |
| 93 | + EXPECT_STRNE("unknown", name); |
| 94 | + EXPECT_TRUE(names.insert(std::string_view{name}).second) |
| 95 | + << "duplicate name: " << name; |
| 96 | + } |
| 97 | + |
| 98 | + const Errata combination = |
| 99 | + Errata::StretchFlexBasis | Errata::MinSizeUndefinedInsteadOfAuto; |
| 100 | + EXPECT_STREQ("unknown", toString(combination)); |
| 101 | +} |
| 102 | + |
| 103 | +} // namespace facebook::yoga |
0 commit comments