|
33 | 33 |
|
34 | 34 | #include <functional>
|
35 | 35 | #include <memory>
|
| 36 | +#include <optional> |
36 | 37 | #include <string>
|
37 | 38 | #include <tuple>
|
38 | 39 | #include <vector>
|
@@ -2396,6 +2397,74 @@ TEST(ExplainMatchResultTest, AllOf_True_True_2) {
|
2396 | 2397 | EXPECT_EQ("is >= 2, and is <= 3", Explain(m, 2));
|
2397 | 2398 | }
|
2398 | 2399 |
|
| 2400 | +// A matcher that records whether the listener was interested. |
| 2401 | +template <typename T> |
| 2402 | +class CountingMatcher : public MatcherInterface<T> { |
| 2403 | + public: |
| 2404 | + explicit CountingMatcher(const Matcher<T>& base_matcher, |
| 2405 | + std::vector<bool>* listener_interested) |
| 2406 | + : base_matcher_(base_matcher), |
| 2407 | + listener_interested_(listener_interested) {} |
| 2408 | + |
| 2409 | + bool MatchAndExplain(T x, MatchResultListener* listener) const override { |
| 2410 | + listener_interested_->push_back(listener->IsInterested()); |
| 2411 | + return base_matcher_.MatchAndExplain(x, listener); |
| 2412 | + } |
| 2413 | + |
| 2414 | + void DescribeTo(ostream* os) const override { base_matcher_.DescribeTo(os); } |
| 2415 | + |
| 2416 | + private: |
| 2417 | + Matcher<T> base_matcher_; |
| 2418 | + std::vector<bool>* listener_interested_; |
| 2419 | +}; |
| 2420 | + |
| 2421 | +TEST(AllOfTest, DoesNotFormatChildMatchersWhenNotInterested) { |
| 2422 | + std::vector<bool> listener_interested; |
| 2423 | + Matcher<int> matcher = |
| 2424 | + MakeMatcher(new CountingMatcher<int>(Eq(1), &listener_interested)); |
| 2425 | + EXPECT_TRUE(matcher.Matches(1)); |
| 2426 | + EXPECT_THAT(listener_interested, ElementsAre(false)); |
| 2427 | + listener_interested.clear(); |
| 2428 | + Matcher<int> all_of_matcher = AllOf(matcher, matcher); |
| 2429 | + EXPECT_TRUE(all_of_matcher.Matches(1)); |
| 2430 | + EXPECT_THAT(listener_interested, ElementsAre(false, false)); |
| 2431 | + listener_interested.clear(); |
| 2432 | + EXPECT_FALSE(all_of_matcher.Matches(0)); |
| 2433 | + EXPECT_THAT(listener_interested, ElementsAre(false)); |
| 2434 | +} |
| 2435 | + |
| 2436 | +TEST(AnyOfTest, DoesNotFormatChildMatchersWhenNotInterested) { |
| 2437 | + std::vector<bool> listener_interested; |
| 2438 | + Matcher<int> matcher = |
| 2439 | + MakeMatcher(new CountingMatcher<int>(Eq(1), &listener_interested)); |
| 2440 | + EXPECT_TRUE(matcher.Matches(1)); |
| 2441 | + EXPECT_THAT(listener_interested, ElementsAre(false)); |
| 2442 | + listener_interested.clear(); |
| 2443 | + Matcher<int> any_of_matcher = AnyOf(matcher, matcher); |
| 2444 | + EXPECT_TRUE(any_of_matcher.Matches(1)); |
| 2445 | + EXPECT_THAT(listener_interested, ElementsAre(false)); |
| 2446 | + listener_interested.clear(); |
| 2447 | + EXPECT_FALSE(any_of_matcher.Matches(0)); |
| 2448 | + EXPECT_THAT(listener_interested, ElementsAre(false, false)); |
| 2449 | +} |
| 2450 | + |
| 2451 | +TEST(OptionalTest, DoesNotFormatChildMatcherWhenNotInterested) { |
| 2452 | + std::vector<bool> listener_interested; |
| 2453 | + Matcher<int> matcher = |
| 2454 | + MakeMatcher(new CountingMatcher<int>(Eq(1), &listener_interested)); |
| 2455 | + EXPECT_TRUE(matcher.Matches(1)); |
| 2456 | + EXPECT_THAT(listener_interested, ElementsAre(false)); |
| 2457 | + listener_interested.clear(); |
| 2458 | + Matcher<std::optional<int>> optional_matcher = Optional(matcher); |
| 2459 | + EXPECT_FALSE(optional_matcher.Matches(std::nullopt)); |
| 2460 | + EXPECT_THAT(listener_interested, ElementsAre()); |
| 2461 | + EXPECT_TRUE(optional_matcher.Matches(1)); |
| 2462 | + EXPECT_THAT(listener_interested, ElementsAre(false)); |
| 2463 | + listener_interested.clear(); |
| 2464 | + EXPECT_FALSE(matcher.Matches(0)); |
| 2465 | + EXPECT_THAT(listener_interested, ElementsAre(false)); |
| 2466 | +} |
| 2467 | + |
2399 | 2468 | INSTANTIATE_GTEST_MATCHER_TEST_P(ExplainmatcherResultTest);
|
2400 | 2469 |
|
2401 | 2470 | TEST_P(ExplainmatcherResultTestP, MonomorphicMatcher) {
|
|
0 commit comments