|
16 | 16 | #include <react/renderer/core/RawPropsParser.h> |
17 | 17 | #include <react/renderer/core/ShadowNode.h> |
18 | 18 | #include <react/renderer/core/propsConversions.h> |
| 19 | +#include <string> |
| 20 | +#include <string_view> |
| 21 | +#include <utility> |
19 | 22 |
|
20 | 23 | using namespace facebook; |
21 | 24 | using namespace facebook::react; |
22 | 25 |
|
| 26 | +namespace { |
| 27 | + |
| 28 | +template <typename Name> |
| 29 | +concept CanLookUpProp = requires(const RawProps& props, Name&& name) { |
| 30 | + props.at(std::forward<Name>(name)); |
| 31 | +}; |
| 32 | + |
| 33 | +template <typename Name> |
| 34 | +concept CanConvertProp = requires( |
| 35 | + const PropsParserContext& context, const RawProps& props, Name&& name) { |
| 36 | + convertRawProp(context, props, std::forward<Name>(name), 0, {}); |
| 37 | +}; |
| 38 | + |
| 39 | +static_assert(CanLookUpProp<decltype("value")>); |
| 40 | +static_assert(CanConvertProp<decltype("value")>); |
| 41 | +static_assert(!CanLookUpProp<std::string>); |
| 42 | +static_assert(!CanConvertProp<std::string>); |
| 43 | +static_assert(!CanLookUpProp<const char*>); |
| 44 | +static_assert(!CanConvertProp<const char*>); |
| 45 | +static_assert(!CanLookUpProp<std::string_view>); |
| 46 | +static_assert(!CanConvertProp<std::string_view>); |
| 47 | + |
| 48 | +constexpr char unterminatedName[] = {'v', 'a', 'l', 'u', 'e'}; |
| 49 | +constexpr char paddedName[12] = "value"; |
| 50 | + |
| 51 | +struct PropsWithUnterminatedName : Props { |
| 52 | + PropsWithUnterminatedName( |
| 53 | + const PropsParserContext& context, |
| 54 | + const PropsWithUnterminatedName& source, |
| 55 | + const RawProps& rawProps) |
| 56 | + : value(convertRawProp( |
| 57 | + context, rawProps, unterminatedName, source.value, {})) {} |
| 58 | + |
| 59 | + PropsWithUnterminatedName() = default; |
| 60 | + int value{}; |
| 61 | +}; |
| 62 | + |
| 63 | +struct PropsWithComposedNames : Props { |
| 64 | + PropsWithComposedNames( |
| 65 | + const PropsParserContext&, |
| 66 | + const PropsWithComposedNames&, |
| 67 | + const RawProps& rawProps) { |
| 68 | + std::string name = "test"; |
| 69 | + name += "PropA"; |
| 70 | + if (const auto* rawValue = rawProps.atRuntime(name)) { |
| 71 | + first = static_cast<int>(*rawValue); |
| 72 | + } |
| 73 | + name.back() = 'B'; |
| 74 | + if (const auto* rawValue = rawProps.atRuntime(name)) { |
| 75 | + second = static_cast<int>(*rawValue); |
| 76 | + } |
| 77 | + if (const auto* rawValue = rawProps.at("Prop", "compat", "Value")) { |
| 78 | + composed = static_cast<int>(*rawValue); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + PropsWithComposedNames() = default; |
| 83 | + int first{}; |
| 84 | + int second{}; |
| 85 | + int composed{}; |
| 86 | +}; |
| 87 | + |
| 88 | +} // namespace |
| 89 | + |
| 90 | +TEST(RawPropsTest, looksUpAnUnterminatedName) { |
| 91 | + RawPropsParser parser; |
| 92 | + parser.prepare<PropsWithUnterminatedName>(); |
| 93 | + RawProps raw(folly::dynamic::object("value", 42)("valueExtra", 19)); |
| 94 | + raw.parse(parser); |
| 95 | + |
| 96 | + ContextContainer contextContainer; |
| 97 | + PropsParserContext context{-1, contextContainer}; |
| 98 | + EXPECT_EQ(PropsWithUnterminatedName(context, {}, raw).value, 42); |
| 99 | + const auto* value = raw.at(paddedName); |
| 100 | + ASSERT_NE(value, nullptr); |
| 101 | + EXPECT_EQ(static_cast<int>(*value), 42); |
| 102 | +} |
| 103 | + |
| 104 | +TEST(RawPropsTest, copiesComposedNamesWhilePreparing) { |
| 105 | + const auto parser = [] { |
| 106 | + RawPropsParser original; |
| 107 | + original.prepare<PropsWithComposedNames>(); |
| 108 | + return RawPropsParser(original); |
| 109 | + }(); |
| 110 | + RawProps raw(folly::dynamic::object("testPropA", 42)("testPropB", 19)( |
| 111 | + "compatPropValue", 7)); |
| 112 | + raw.parse(parser); |
| 113 | + |
| 114 | + ContextContainer contextContainer; |
| 115 | + PropsParserContext context{-1, contextContainer}; |
| 116 | + PropsWithComposedNames props(context, {}, raw); |
| 117 | + EXPECT_EQ(props.first, 42); |
| 118 | + EXPECT_EQ(props.second, 19); |
| 119 | + EXPECT_EQ(props.composed, 7); |
| 120 | + const auto* value = raw.at("compatPropValue", nullptr, nullptr); |
| 121 | + ASSERT_NE(value, nullptr); |
| 122 | + EXPECT_EQ(static_cast<int>(*value), 7); |
| 123 | +} |
| 124 | + |
23 | 125 | TEST(RawPropsTest, dynamicSourceIsBorrowed) { |
24 | 126 | const auto rawProps = RawProps(folly::dynamic::object("opacity", 0.5)); |
25 | 127 | const auto* dynamic = rawProps.getDynamic(); |
|
0 commit comments