|
| 1 | +// |
| 2 | +// |
| 3 | +// Tencent is pleased to support the open source community by making tRPC available. |
| 4 | +// |
| 5 | +// Copyright (C) 2023 THL A29 Limited, a Tencent company. |
| 6 | +// All rights reserved. |
| 7 | +// |
| 8 | +// If you have downloaded a copy of the tRPC source code from Tencent, |
| 9 | +// please note that tRPC source code is licensed under the Apache 2.0 License, |
| 10 | +// A copy of the Apache 2.0 License is included in this file. |
| 11 | +// |
| 12 | +// |
| 13 | + |
| 14 | +#include <any> |
| 15 | +#include <map> |
| 16 | +#include <string> |
| 17 | + |
| 18 | +#include "gtest/gtest.h" |
| 19 | +#include "gmock/gmock.h" |
| 20 | +#include "trpc/config/testing/mock_config.h" |
| 21 | + |
| 22 | +namespace trpc::testing { |
| 23 | + |
| 24 | +class AnyEqMatcher : public ::testing::MatcherInterface<const std::any&> { |
| 25 | + public: |
| 26 | + explicit AnyEqMatcher(const std::any& expected) : expected_(expected) {} |
| 27 | + |
| 28 | + bool MatchAndExplain(const std::any& actual, ::testing::MatchResultListener* listener) const override { |
| 29 | + if (!actual.has_value() && !expected_.has_value()) { |
| 30 | + return true; |
| 31 | + } |
| 32 | + |
| 33 | + if (actual.type() != expected_.type()) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + if (actual.type() == typeid(int)) { |
| 38 | + return std::any_cast<int>(actual) == std::any_cast<int>(expected_); |
| 39 | + } else if (actual.type() == typeid(std::string)) { |
| 40 | + return std::any_cast<std::string>(actual) == std::any_cast<std::string>(expected_); |
| 41 | + } |
| 42 | + |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + void DescribeTo(::std::ostream* os) const override { |
| 47 | + *os << "is equal to the expected value"; |
| 48 | + } |
| 49 | + |
| 50 | + void DescribeNegationTo(::std::ostream* os) const override { |
| 51 | + *os << "is not equal to the expected value"; |
| 52 | + } |
| 53 | + |
| 54 | + private: |
| 55 | + const std::any expected_; |
| 56 | +}; |
| 57 | + |
| 58 | +inline ::testing::Matcher<const std::any&> AnyEq(const std::any& expected) { |
| 59 | + return ::testing::MakeMatcher(new AnyEqMatcher(expected)); |
| 60 | +} |
| 61 | + |
| 62 | +class TestConfig : public ::testing::Test { |
| 63 | + protected: |
| 64 | + void SetUp() override { |
| 65 | + mock_config_ = MakeRefCounted<MockConfig>(); |
| 66 | + ASSERT_TRUE(mock_config_ != nullptr); |
| 67 | + } |
| 68 | + |
| 69 | + RefPtr<MockConfig> mock_config_; |
| 70 | +}; |
| 71 | + |
| 72 | +TEST_F(TestConfig, PullFileConfig) { |
| 73 | + std::string config_name = "test_config"; |
| 74 | + std::string config; |
| 75 | + std::any params; |
| 76 | + EXPECT_CALL(*mock_config_, PullFileConfig(config_name, &config, AnyEq(params))) |
| 77 | + .WillOnce(::testing::Return(true)); |
| 78 | + bool result = mock_config_->PullFileConfig(config_name, &config, params); |
| 79 | + |
| 80 | + ASSERT_TRUE(result); |
| 81 | +} |
| 82 | + |
| 83 | +TEST_F(TestConfig, PullKVConfig) { |
| 84 | + std::string config_name = "test_config"; |
| 85 | + std::string key = "test_key"; |
| 86 | + std::string config; |
| 87 | + std::any params; |
| 88 | + EXPECT_CALL(*mock_config_, PullKVConfig(config_name, key, &config, AnyEq(params))) |
| 89 | + .WillOnce(::testing::Return(true)); |
| 90 | + bool result = mock_config_->PullKVConfig(config_name, key, &config, params); |
| 91 | + |
| 92 | + ASSERT_TRUE(result); |
| 93 | +} |
| 94 | + |
| 95 | +TEST_F(TestConfig, PullMultiKVConfig) { |
| 96 | + std::string config_name = "test_config"; |
| 97 | + std::map<std::string, std::string> config; |
| 98 | + std::any params; |
| 99 | + EXPECT_CALL(*mock_config_, PullMultiKVConfig(config_name, &config, AnyEq(params))) |
| 100 | + .WillOnce(::testing::Return(true)); |
| 101 | + bool result = mock_config_->PullMultiKVConfig(config_name, &config, params); |
| 102 | + |
| 103 | + ASSERT_TRUE(result); |
| 104 | +} |
| 105 | + |
| 106 | +} // namespace trpc::testing |
0 commit comments