#include "envoy/matcher/matcher.h" #include "common/matcher/field_matcher.h" #include "common/matcher/matcher.h" #include "test/common/matcher/test_utility.h" #include "gtest/gtest.h" namespace Envoy { namespace Matcher { class FieldMatcherTest : public testing::Test { public: std::vector> createMatchers(std::vector> values) { std::vector> matchers; matchers.reserve(values.size()); for (const auto& v : values) { matchers.emplace_back(std::make_unique>( std::make_unique(DataInputGetResult{v.second, absl::nullopt}), std::make_unique(v.first))); } return matchers; } std::vector> createMatchers(std::vector values) { std::vector> new_values; new_values.reserve(values.size()); for (const auto v : values) { new_values.emplace_back(v, DataInputGetResult::DataAvailability::AllDataAvailable); } return createMatchers(new_values); } }; TEST_F(FieldMatcherTest, SingleFieldMatcher) { EXPECT_EQ( createSingleMatcher("foo", [](auto v) { return v == "foo"; })->match(TestData()).match_state_, MatchState::MatchComplete); EXPECT_EQ(createSingleMatcher( absl::nullopt, [](auto v) { return v == "foo"; }, DataInputGetResult::DataAvailability::NotAvailable) ->match(TestData()) .match_state_, MatchState::UnableToMatch); EXPECT_EQ(createSingleMatcher( "fo", [](auto v) { return v == "foo"; }, DataInputGetResult::DataAvailability::MoreDataMightBeAvailable) ->match(TestData()) .match_state_, MatchState::UnableToMatch); EXPECT_TRUE( createSingleMatcher("foo", [](auto v) { return v == "foo"; })->match(TestData()).result()); EXPECT_FALSE( createSingleMatcher("foo", [](auto v) { return v != "foo"; })->match(TestData()).result()); EXPECT_TRUE(createSingleMatcher(absl::nullopt, [](auto v) { return v == absl::nullopt; }) ->match(TestData()) .result()); EXPECT_FALSE(createSingleMatcher(absl::nullopt, [](auto v) { return v == "foo"; }) ->match(TestData()) .result()); } TEST_F(FieldMatcherTest, AnyMatcher) { EXPECT_TRUE(AnyFieldMatcher(createMatchers({true, false})).match(TestData()).result()); EXPECT_TRUE(AnyFieldMatcher(createMatchers({true, true})).match(TestData()).result()); EXPECT_FALSE( AnyFieldMatcher(createMatchers({false, false})).match(TestData()).result()); EXPECT_EQ(AnyFieldMatcher( createMatchers( {std::make_pair(false, DataInputGetResult::DataAvailability::MoreDataMightBeAvailable), std::make_pair(true, DataInputGetResult::DataAvailability::AllDataAvailable)})) .match(TestData()) .match_state_, MatchState::MatchComplete); EXPECT_EQ( AnyFieldMatcher( createMatchers( {std::make_pair(false, DataInputGetResult::DataAvailability::MoreDataMightBeAvailable), std::make_pair(false, DataInputGetResult::DataAvailability::AllDataAvailable)})) .match(TestData()) .match_state_, MatchState::UnableToMatch); } TEST_F(FieldMatcherTest, AllMatcher) { EXPECT_FALSE(AllFieldMatcher(createMatchers({true, false})).match(TestData()).result()); EXPECT_TRUE(AllFieldMatcher(createMatchers({true, true})).match(TestData()).result()); EXPECT_FALSE( AllFieldMatcher(createMatchers({false, false})).match(TestData()).result()); EXPECT_EQ( AllFieldMatcher( createMatchers( {std::make_pair(false, DataInputGetResult::DataAvailability::MoreDataMightBeAvailable), std::make_pair(false, DataInputGetResult::DataAvailability::AllDataAvailable)})) .match(TestData()) .match_state_, MatchState::UnableToMatch); } } // namespace Matcher } // namespace Envoy