diff --git a/include/gmock/gmock-generated-matchers.h b/include/gmock/gmock-generated-matchers.h index 2cc5746e..cb610543 100644 --- a/include/gmock/gmock-generated-matchers.h +++ b/include/gmock/gmock-generated-matchers.h @@ -849,6 +849,190 @@ ElementsAreArray(const T (&array)[N]) { return internal::ElementsAreArrayMatcher(array, N); } +// AllOf(m1, m2, ..., mk) matches any value that matches all of the given +// sub-matchers. + +template +inline internal::BothOfMatcher +AllOf(Matcher1 m1, Matcher2 m2) { + return internal::BothOfMatcher(m1, m2); +} + +template +inline internal::BothOfMatcher > +AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3) { + return AllOf(m1, AllOf(m2, m3)); +} + +template +inline internal::BothOfMatcher > > +AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4) { + return AllOf(m1, AllOf(m2, m3, m4)); +} + +template +inline internal::BothOfMatcher > > > +AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5) { + return AllOf(m1, AllOf(m2, m3, m4, m5)); +} + +template +inline internal::BothOfMatcher > > > > +AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5, + Matcher6 m6) { + return AllOf(m1, AllOf(m2, m3, m4, m5, m6)); +} + +template +inline internal::BothOfMatcher > > > > > +AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5, + Matcher6 m6, Matcher7 m7) { + return AllOf(m1, AllOf(m2, m3, m4, m5, m6, m7)); +} + +template +inline internal::BothOfMatcher > > > > > > +AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5, + Matcher6 m6, Matcher7 m7, Matcher8 m8) { + return AllOf(m1, AllOf(m2, m3, m4, m5, m6, m7, m8)); +} + +template +inline internal::BothOfMatcher > > > > > > > +AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5, + Matcher6 m6, Matcher7 m7, Matcher8 m8, Matcher9 m9) { + return AllOf(m1, AllOf(m2, m3, m4, m5, m6, m7, m8, m9)); +} + +template +inline internal::BothOfMatcher > > > > > > > > +AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5, + Matcher6 m6, Matcher7 m7, Matcher8 m8, Matcher9 m9, Matcher10 m10) { + return AllOf(m1, AllOf(m2, m3, m4, m5, m6, m7, m8, m9, m10)); +} + +// AnyOf(m1, m2, ..., mk) matches any value that matches any of the given +// sub-matchers. + +template +inline internal::EitherOfMatcher +AnyOf(Matcher1 m1, Matcher2 m2) { + return internal::EitherOfMatcher(m1, m2); +} + +template +inline internal::EitherOfMatcher > +AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3) { + return AnyOf(m1, AnyOf(m2, m3)); +} + +template +inline internal::EitherOfMatcher > > +AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4) { + return AnyOf(m1, AnyOf(m2, m3, m4)); +} + +template +inline internal::EitherOfMatcher > > > +AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5) { + return AnyOf(m1, AnyOf(m2, m3, m4, m5)); +} + +template +inline internal::EitherOfMatcher > > > > +AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5, + Matcher6 m6) { + return AnyOf(m1, AnyOf(m2, m3, m4, m5, m6)); +} + +template +inline internal::EitherOfMatcher > > > > > +AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5, + Matcher6 m6, Matcher7 m7) { + return AnyOf(m1, AnyOf(m2, m3, m4, m5, m6, m7)); +} + +template +inline internal::EitherOfMatcher > > > > > > +AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5, + Matcher6 m6, Matcher7 m7, Matcher8 m8) { + return AnyOf(m1, AnyOf(m2, m3, m4, m5, m6, m7, m8)); +} + +template +inline internal::EitherOfMatcher > > > > > > > +AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5, + Matcher6 m6, Matcher7 m7, Matcher8 m8, Matcher9 m9) { + return AnyOf(m1, AnyOf(m2, m3, m4, m5, m6, m7, m8, m9)); +} + +template +inline internal::EitherOfMatcher > > > > > > > > +AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5, + Matcher6 m6, Matcher7 m7, Matcher8 m8, Matcher9 m9, Matcher10 m10) { + return AnyOf(m1, AnyOf(m2, m3, m4, m5, m6, m7, m8, m9, m10)); +} + } // namespace testing // The MATCHER* family of macros can be used in a namespace scope to diff --git a/include/gmock/gmock-generated-matchers.h.pump b/include/gmock/gmock-generated-matchers.h.pump index 9f19fb8b..02e3c980 100644 --- a/include/gmock/gmock-generated-matchers.h.pump +++ b/include/gmock/gmock-generated-matchers.h.pump @@ -302,6 +302,52 @@ ElementsAreArray(const T (&array)[N]) { return internal::ElementsAreArrayMatcher(array, N); } +// AllOf(m1, m2, ..., mk) matches any value that matches all of the given +// sub-matchers. + +$range i 2..n +$for i [[ +$range j 1..i +$range k 1..i-1 + +template <$for j, [[typename Matcher$j]]> +inline $for k[[internal::BothOfMatcher ]] + +AllOf($for j, [[Matcher$j m$j]]) { + +$if i == 2 [[ + return internal::BothOfMatcher(m1, m2); +]] $else [[ + return AllOf(m1, AllOf($for k, [[m$(k + 1)]])); +]] + +} + +]] + +// AnyOf(m1, m2, ..., mk) matches any value that matches any of the given +// sub-matchers. + +$range i 2..n +$for i [[ +$range j 1..i +$range k 1..i-1 + +template <$for j, [[typename Matcher$j]]> +inline $for k[[internal::EitherOfMatcher ]] + +AnyOf($for j, [[Matcher$j m$j]]) { + +$if i == 2 [[ + return internal::EitherOfMatcher(m1, m2); +]] $else [[ + return AnyOf(m1, AnyOf($for k, [[m$(k + 1)]])); +]] + +} + +]] + } // namespace testing $$ } // This Pump meta comment fixes auto-indentation in Emacs. It will not $$ // show up in the generated code. diff --git a/include/gmock/gmock-matchers.h b/include/gmock/gmock-matchers.h index 315e1f56..9f268d64 100644 --- a/include/gmock/gmock-matchers.h +++ b/include/gmock/gmock-matchers.h @@ -2889,82 +2889,6 @@ inline internal::NotMatcher Not(InnerMatcher m) { return internal::NotMatcher(m); } -// Creates a matcher that matches any value that matches all of the -// given matchers. -// -// For now we only support up to 5 matchers. Support for more -// matchers can be added as needed, or the user can use nested -// AllOf()s. -template -inline internal::BothOfMatcher -AllOf(Matcher1 m1, Matcher2 m2) { - return internal::BothOfMatcher(m1, m2); -} - -template -inline internal::BothOfMatcher > -AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3) { - return AllOf(m1, AllOf(m2, m3)); -} - -template -inline internal::BothOfMatcher > > -AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4) { - return AllOf(m1, AllOf(m2, m3, m4)); -} - -template -inline internal::BothOfMatcher > > > -AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5) { - return AllOf(m1, AllOf(m2, m3, m4, m5)); -} - -// Creates a matcher that matches any value that matches at least one -// of the given matchers. -// -// For now we only support up to 5 matchers. Support for more -// matchers can be added as needed, or the user can use nested -// AnyOf()s. -template -inline internal::EitherOfMatcher -AnyOf(Matcher1 m1, Matcher2 m2) { - return internal::EitherOfMatcher(m1, m2); -} - -template -inline internal::EitherOfMatcher > -AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3) { - return AnyOf(m1, AnyOf(m2, m3)); -} - -template -inline internal::EitherOfMatcher > > -AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4) { - return AnyOf(m1, AnyOf(m2, m3, m4)); -} - -template -inline internal::EitherOfMatcher > > > -AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5) { - return AnyOf(m1, AnyOf(m2, m3, m4, m5)); -} - // Returns a matcher that matches anything that satisfies the given // predicate. The predicate can be any unary function or functor // whose return type can be implicitly converted to bool. diff --git a/test/gmock-matchers_test.cc b/test/gmock-matchers_test.cc index dd2534f0..f7a49c34 100644 --- a/test/gmock-matchers_test.cc +++ b/test/gmock-matchers_test.cc @@ -1857,6 +1857,16 @@ TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) { Matcher m3 = Not(m); } +// Helper to allow easy testing of AllOf matchers with num parameters. +void AllOfMatches(int num, const Matcher& m) { + SCOPED_TRACE(Describe(m)); + EXPECT_TRUE(m.Matches(0)); + for (int i = 1; i <= num; ++i) { + EXPECT_FALSE(m.Matches(i)); + } + EXPECT_TRUE(m.Matches(num + 1)); +} + // Tests that AllOf(m1, ..., mn) matches any value that matches all of // the given matchers. TEST(AllOfTest, MatchesWhenAllMatch) { @@ -1884,6 +1894,23 @@ TEST(AllOfTest, MatchesWhenAllMatch) { EXPECT_TRUE(m.Matches(0)); EXPECT_TRUE(m.Matches(1)); EXPECT_FALSE(m.Matches(3)); + + // The following tests for varying number of sub-matchers. Due to the way + // the sub-matchers are handled it is enough to test every sub-matcher once + // with sub-matchers using the same matcher type. Varying matcher types are + // checked for above. + AllOfMatches(2, AllOf(Ne(1), Ne(2))); + AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3))); + AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4))); + AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5))); + AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6))); + AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7))); + AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), + Ne(8))); + AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), + Ne(8), Ne(9))); + AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), + Ne(9), Ne(10))); } // Tests that AllOf(m1, ..., mn) describes itself properly. @@ -2006,6 +2033,16 @@ TEST(AllOfTest, ExplainsResult) { EXPECT_EQ("which is 5 less than 20", Explain(m, 15)); } +// Helper to allow easy testing of AnyOf matchers with num parameters. +void AnyOfMatches(int num, const Matcher& m) { + SCOPED_TRACE(Describe(m)); + EXPECT_FALSE(m.Matches(0)); + for (int i = 1; i <= num; ++i) { + EXPECT_TRUE(m.Matches(i)); + } + EXPECT_FALSE(m.Matches(num + 1)); +} + // Tests that AnyOf(m1, ..., mn) matches any value that matches at // least one of the given matchers. TEST(AnyOfTest, MatchesWhenAnyMatches) { @@ -2033,6 +2070,20 @@ TEST(AnyOfTest, MatchesWhenAnyMatches) { EXPECT_TRUE(m.Matches(11)); EXPECT_TRUE(m.Matches(3)); EXPECT_FALSE(m.Matches(2)); + + // The following tests for varying number of sub-matchers. Due to the way + // the sub-matchers are handled it is enough to test every sub-matcher once + // with sub-matchers using the same matcher type. Varying matcher types are + // checked for above. + AnyOfMatches(2, AnyOf(1, 2)); + AnyOfMatches(3, AnyOf(1, 2, 3)); + AnyOfMatches(4, AnyOf(1, 2, 3, 4)); + AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5)); + AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6)); + AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7)); + AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8)); + AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9)); + AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); } // Tests that AnyOf(m1, ..., mn) describes itself properly.