|
| 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 <NativeCxxModuleExample/NativeCxxModuleExample.h> |
| 9 | +#include <ReactCommon/TestCallInvoker.h> |
| 10 | +#include <ReactCommon/TurboModuleTestFixture.h> |
| 11 | +#include <gtest/gtest.h> |
| 12 | +#include <list> |
| 13 | +#include <memory> |
| 14 | +#include <optional> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +namespace facebook::react { |
| 18 | + |
| 19 | +class NativeCxxModuleExampleTests |
| 20 | + : public TurboModuleTestFixture<NativeCxxModuleExample> {}; |
| 21 | + |
| 22 | +TEST_F(NativeCxxModuleExampleTests, GetArrayReturnsCorrectValues) { |
| 23 | + std::vector<std::optional<facebook::react::ObjectStruct>> empty; |
| 24 | + EXPECT_EQ(module_->getArray(*runtime_, empty), empty); |
| 25 | + |
| 26 | + std::vector<std::optional<ObjectStruct>> withNull = {std::nullopt}; |
| 27 | + EXPECT_EQ(module_->getArray(*runtime_, withNull), withNull); |
| 28 | + |
| 29 | + std::vector<std::optional<ObjectStruct>> withObj = {ObjectStruct{1, "2"}}; |
| 30 | + auto result = module_->getArray(*runtime_, withObj); |
| 31 | + ASSERT_EQ(result.size(), 1); |
| 32 | + EXPECT_EQ(result[0]->a, 1); |
| 33 | + EXPECT_EQ(result[0]->b, "2"); |
| 34 | +} |
| 35 | + |
| 36 | +TEST_F(NativeCxxModuleExampleTests, GetBoolReturnsCorrectValues) { |
| 37 | + EXPECT_FALSE(module_->getBool(*runtime_, false)); |
| 38 | + EXPECT_TRUE(module_->getBool(*runtime_, true)); |
| 39 | +} |
| 40 | + |
| 41 | +TEST_F(NativeCxxModuleExampleTests, GetConstantsReturnsCorrectValues) { |
| 42 | + auto constants = module_->getConstants(*runtime_); |
| 43 | + EXPECT_TRUE(constants.const1); |
| 44 | + EXPECT_EQ(constants.const2, 69); |
| 45 | + EXPECT_EQ(constants.const3, "react-native"); |
| 46 | +} |
| 47 | + |
| 48 | +TEST_F(NativeCxxModuleExampleTests, GetCustomEnumReturnsCorrectValue) { |
| 49 | + EXPECT_EQ( |
| 50 | + module_->getCustomEnum(*runtime_, CustomEnumInt::A), CustomEnumInt::A); |
| 51 | +} |
| 52 | + |
| 53 | +TEST_F(NativeCxxModuleExampleTests, GetAndConsumeCustomHostObject) { |
| 54 | + auto hostObj = module_->getCustomHostObject(*runtime_); |
| 55 | + ASSERT_NE(hostObj, nullptr); |
| 56 | + EXPECT_EQ(module_->consumeCustomHostObject(*runtime_, hostObj), "answer42"); |
| 57 | +} |
| 58 | + |
| 59 | +TEST_F(NativeCxxModuleExampleTests, GetBinaryTreeNodeReturnsCorrectValues) { |
| 60 | + auto result = module_->getBinaryTreeNode( |
| 61 | + *runtime_, |
| 62 | + BinaryTreeNode{ |
| 63 | + .left = std::make_unique<BinaryTreeNode>( |
| 64 | + BinaryTreeNode{nullptr, 2, nullptr}), |
| 65 | + .value = 4, |
| 66 | + .right = std::make_unique<BinaryTreeNode>( |
| 67 | + BinaryTreeNode{nullptr, 6, nullptr})}); |
| 68 | + ASSERT_NE(result.left, nullptr); |
| 69 | + EXPECT_EQ(result.left->value, 2); |
| 70 | + EXPECT_EQ(result.value, 4); |
| 71 | + ASSERT_NE(result.right, nullptr); |
| 72 | + EXPECT_EQ(result.right->value, 6); |
| 73 | +} |
| 74 | + |
| 75 | +TEST_F(NativeCxxModuleExampleTests, GetGraphNodeReturnsCorrectValues) { |
| 76 | + GraphNode input{ |
| 77 | + .label = "root", |
| 78 | + .neighbors = std::vector<GraphNode>{ |
| 79 | + GraphNode{.label = "child1"}, GraphNode{.label = "child2"}}}; |
| 80 | + auto result = module_->getGraphNode(*runtime_, input); |
| 81 | + EXPECT_EQ(result.label, "root"); |
| 82 | + ASSERT_EQ(result.neighbors.value().size(), 4); |
| 83 | + EXPECT_EQ(result.neighbors.value()[0].label, "child1"); |
| 84 | + EXPECT_EQ(result.neighbors.value()[1].label, "child2"); |
| 85 | + EXPECT_EQ(result.neighbors.value()[2].label, "top"); |
| 86 | + EXPECT_EQ(result.neighbors.value()[3].label, "down"); |
| 87 | +} |
| 88 | + |
| 89 | +TEST_F(NativeCxxModuleExampleTests, GetNumEnumReturnsCorrectValues) { |
| 90 | + EXPECT_EQ( |
| 91 | + module_->getNumEnum(*runtime_, NativeCxxModuleExampleEnumInt::IA), |
| 92 | + NativeCxxModuleExampleEnumInt::IA); |
| 93 | + EXPECT_EQ( |
| 94 | + module_->getNumEnum(*runtime_, NativeCxxModuleExampleEnumInt::IB), |
| 95 | + NativeCxxModuleExampleEnumInt::IB); |
| 96 | +} |
| 97 | + |
| 98 | +TEST_F(NativeCxxModuleExampleTests, GetStrEnumReturnsCorrectValues) { |
| 99 | + EXPECT_EQ( |
| 100 | + module_->getStrEnum(*runtime_, NativeCxxModuleExampleEnumNone::NA), |
| 101 | + NativeCxxModuleExampleEnumStr::SB); |
| 102 | + EXPECT_EQ( |
| 103 | + module_->getStrEnum(*runtime_, NativeCxxModuleExampleEnumNone::NB), |
| 104 | + NativeCxxModuleExampleEnumStr::SB); |
| 105 | +} |
| 106 | + |
| 107 | +TEST_F(NativeCxxModuleExampleTests, GetMapReturnsCorrectValues) { |
| 108 | + std::map<std::string, std::optional<int32_t>> input = { |
| 109 | + {"a", 0}, {"b", std::nullopt}, {"c", 3}}; |
| 110 | + auto result = module_->getMap(*runtime_, input); |
| 111 | + EXPECT_EQ(result["a"], 0); |
| 112 | + EXPECT_EQ(result["b"], std::nullopt); |
| 113 | + EXPECT_EQ(result["c"], 3); |
| 114 | +} |
| 115 | + |
| 116 | +TEST_F(NativeCxxModuleExampleTests, GetNumberReturnsCorrectValues) { |
| 117 | + EXPECT_EQ(module_->getNumber(*runtime_, 0), 0); |
| 118 | + EXPECT_EQ(module_->getNumber(*runtime_, pow(2, 53)), pow(2, 53)); |
| 119 | +} |
| 120 | + |
| 121 | +TEST_F(NativeCxxModuleExampleTests, GetObjectReturnsCorrectValues) { |
| 122 | + ObjectStruct input1{2, "two"}; |
| 123 | + auto result1 = module_->getObject(*runtime_, input1); |
| 124 | + EXPECT_EQ(result1.a, 2); |
| 125 | + EXPECT_EQ(result1.b, "two"); |
| 126 | + ObjectStruct input2{4, "four", "seven"}; |
| 127 | + auto result2 = module_->getObject(*runtime_, input2); |
| 128 | + EXPECT_EQ(result2.a, 4); |
| 129 | + EXPECT_EQ(result2.b, "four"); |
| 130 | + EXPECT_EQ(result2.c, "seven"); |
| 131 | +} |
| 132 | + |
| 133 | +TEST_F(NativeCxxModuleExampleTests, GetSetReturnsCorrectValues) { |
| 134 | + std::set<float> input = {1, 2, 3, 3, 3}; |
| 135 | + auto result = module_->getSet(*runtime_, input); |
| 136 | + EXPECT_EQ(result.size(), 3); |
| 137 | + EXPECT_TRUE(result.count(1)); |
| 138 | + EXPECT_TRUE(result.count(2)); |
| 139 | + EXPECT_TRUE(result.count(3)); |
| 140 | +} |
| 141 | + |
| 142 | +TEST_F(NativeCxxModuleExampleTests, GetStringReturnsCorrectValues) { |
| 143 | + EXPECT_EQ(module_->getString(*runtime_, ""), ""); |
| 144 | + EXPECT_EQ(module_->getString(*runtime_, "string"), "string"); |
| 145 | +} |
| 146 | + |
| 147 | +TEST_F(NativeCxxModuleExampleTests, GetValueReturnsCorrectValues) { |
| 148 | + ObjectStruct z{4, "four", "seven"}; |
| 149 | + auto result = module_->getValue(*runtime_, 23, "forty-two", z); |
| 150 | + EXPECT_EQ(result.x, 23); |
| 151 | + EXPECT_EQ(result.y, "forty-two"); |
| 152 | + EXPECT_EQ(result.z.a, 4); |
| 153 | + EXPECT_EQ(result.z.b, "four"); |
| 154 | + EXPECT_EQ(result.z.c, "seven"); |
| 155 | +} |
| 156 | + |
| 157 | +TEST_F( |
| 158 | + NativeCxxModuleExampleTests, |
| 159 | + GetWithWithOptionalArgsReturnsCorrectValues) { |
| 160 | + EXPECT_EQ( |
| 161 | + module_->getWithWithOptionalArgs(*runtime_, std::nullopt), std::nullopt); |
| 162 | + EXPECT_EQ(module_->getWithWithOptionalArgs(*runtime_, true), true); |
| 163 | + EXPECT_EQ(module_->getWithWithOptionalArgs(*runtime_, false), false); |
| 164 | +} |
| 165 | + |
| 166 | +} // namespace facebook::react |
0 commit comments