Increases the maximum arity of AllOf() and AnyOf() to 10, by Marcus Börger.

This commit is contained in:
zhanyong.wan 2010-06-09 19:21:30 +00:00
parent b4140808f9
commit 02c1505ebf
4 changed files with 281 additions and 76 deletions

View File

@ -849,6 +849,190 @@ ElementsAreArray(const T (&array)[N]) {
return internal::ElementsAreArrayMatcher<T>(array, N);
}
// AllOf(m1, m2, ..., mk) matches any value that matches all of the given
// sub-matchers.
template <typename Matcher1, typename Matcher2>
inline internal::BothOfMatcher<Matcher1, Matcher2>
AllOf(Matcher1 m1, Matcher2 m2) {
return internal::BothOfMatcher<Matcher1, Matcher2>(m1, m2);
}
template <typename Matcher1, typename Matcher2, typename Matcher3>
inline internal::BothOfMatcher<Matcher1, internal::BothOfMatcher<Matcher2,
Matcher3> >
AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3) {
return AllOf(m1, AllOf(m2, m3));
}
template <typename Matcher1, typename Matcher2, typename Matcher3,
typename Matcher4>
inline internal::BothOfMatcher<Matcher1, internal::BothOfMatcher<Matcher2,
internal::BothOfMatcher<Matcher3, Matcher4> > >
AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4) {
return AllOf(m1, AllOf(m2, m3, m4));
}
template <typename Matcher1, typename Matcher2, typename Matcher3,
typename Matcher4, typename Matcher5>
inline internal::BothOfMatcher<Matcher1, internal::BothOfMatcher<Matcher2,
internal::BothOfMatcher<Matcher3, internal::BothOfMatcher<Matcher4,
Matcher5> > > >
AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5) {
return AllOf(m1, AllOf(m2, m3, m4, m5));
}
template <typename Matcher1, typename Matcher2, typename Matcher3,
typename Matcher4, typename Matcher5, typename Matcher6>
inline internal::BothOfMatcher<Matcher1, internal::BothOfMatcher<Matcher2,
internal::BothOfMatcher<Matcher3, internal::BothOfMatcher<Matcher4,
internal::BothOfMatcher<Matcher5, Matcher6> > > > >
AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5,
Matcher6 m6) {
return AllOf(m1, AllOf(m2, m3, m4, m5, m6));
}
template <typename Matcher1, typename Matcher2, typename Matcher3,
typename Matcher4, typename Matcher5, typename Matcher6, typename Matcher7>
inline internal::BothOfMatcher<Matcher1, internal::BothOfMatcher<Matcher2,
internal::BothOfMatcher<Matcher3, internal::BothOfMatcher<Matcher4,
internal::BothOfMatcher<Matcher5, internal::BothOfMatcher<Matcher6,
Matcher7> > > > > >
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 <typename Matcher1, typename Matcher2, typename Matcher3,
typename Matcher4, typename Matcher5, typename Matcher6, typename Matcher7,
typename Matcher8>
inline internal::BothOfMatcher<Matcher1, internal::BothOfMatcher<Matcher2,
internal::BothOfMatcher<Matcher3, internal::BothOfMatcher<Matcher4,
internal::BothOfMatcher<Matcher5, internal::BothOfMatcher<Matcher6,
internal::BothOfMatcher<Matcher7, Matcher8> > > > > > >
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 <typename Matcher1, typename Matcher2, typename Matcher3,
typename Matcher4, typename Matcher5, typename Matcher6, typename Matcher7,
typename Matcher8, typename Matcher9>
inline internal::BothOfMatcher<Matcher1, internal::BothOfMatcher<Matcher2,
internal::BothOfMatcher<Matcher3, internal::BothOfMatcher<Matcher4,
internal::BothOfMatcher<Matcher5, internal::BothOfMatcher<Matcher6,
internal::BothOfMatcher<Matcher7, internal::BothOfMatcher<Matcher8,
Matcher9> > > > > > > >
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 <typename Matcher1, typename Matcher2, typename Matcher3,
typename Matcher4, typename Matcher5, typename Matcher6, typename Matcher7,
typename Matcher8, typename Matcher9, typename Matcher10>
inline internal::BothOfMatcher<Matcher1, internal::BothOfMatcher<Matcher2,
internal::BothOfMatcher<Matcher3, internal::BothOfMatcher<Matcher4,
internal::BothOfMatcher<Matcher5, internal::BothOfMatcher<Matcher6,
internal::BothOfMatcher<Matcher7, internal::BothOfMatcher<Matcher8,
internal::BothOfMatcher<Matcher9, Matcher10> > > > > > > > >
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 <typename Matcher1, typename Matcher2>
inline internal::EitherOfMatcher<Matcher1, Matcher2>
AnyOf(Matcher1 m1, Matcher2 m2) {
return internal::EitherOfMatcher<Matcher1, Matcher2>(m1, m2);
}
template <typename Matcher1, typename Matcher2, typename Matcher3>
inline internal::EitherOfMatcher<Matcher1, internal::EitherOfMatcher<Matcher2,
Matcher3> >
AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3) {
return AnyOf(m1, AnyOf(m2, m3));
}
template <typename Matcher1, typename Matcher2, typename Matcher3,
typename Matcher4>
inline internal::EitherOfMatcher<Matcher1, internal::EitherOfMatcher<Matcher2,
internal::EitherOfMatcher<Matcher3, Matcher4> > >
AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4) {
return AnyOf(m1, AnyOf(m2, m3, m4));
}
template <typename Matcher1, typename Matcher2, typename Matcher3,
typename Matcher4, typename Matcher5>
inline internal::EitherOfMatcher<Matcher1, internal::EitherOfMatcher<Matcher2,
internal::EitherOfMatcher<Matcher3, internal::EitherOfMatcher<Matcher4,
Matcher5> > > >
AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5) {
return AnyOf(m1, AnyOf(m2, m3, m4, m5));
}
template <typename Matcher1, typename Matcher2, typename Matcher3,
typename Matcher4, typename Matcher5, typename Matcher6>
inline internal::EitherOfMatcher<Matcher1, internal::EitherOfMatcher<Matcher2,
internal::EitherOfMatcher<Matcher3, internal::EitherOfMatcher<Matcher4,
internal::EitherOfMatcher<Matcher5, Matcher6> > > > >
AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5,
Matcher6 m6) {
return AnyOf(m1, AnyOf(m2, m3, m4, m5, m6));
}
template <typename Matcher1, typename Matcher2, typename Matcher3,
typename Matcher4, typename Matcher5, typename Matcher6, typename Matcher7>
inline internal::EitherOfMatcher<Matcher1, internal::EitherOfMatcher<Matcher2,
internal::EitherOfMatcher<Matcher3, internal::EitherOfMatcher<Matcher4,
internal::EitherOfMatcher<Matcher5, internal::EitherOfMatcher<Matcher6,
Matcher7> > > > > >
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 <typename Matcher1, typename Matcher2, typename Matcher3,
typename Matcher4, typename Matcher5, typename Matcher6, typename Matcher7,
typename Matcher8>
inline internal::EitherOfMatcher<Matcher1, internal::EitherOfMatcher<Matcher2,
internal::EitherOfMatcher<Matcher3, internal::EitherOfMatcher<Matcher4,
internal::EitherOfMatcher<Matcher5, internal::EitherOfMatcher<Matcher6,
internal::EitherOfMatcher<Matcher7, Matcher8> > > > > > >
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 <typename Matcher1, typename Matcher2, typename Matcher3,
typename Matcher4, typename Matcher5, typename Matcher6, typename Matcher7,
typename Matcher8, typename Matcher9>
inline internal::EitherOfMatcher<Matcher1, internal::EitherOfMatcher<Matcher2,
internal::EitherOfMatcher<Matcher3, internal::EitherOfMatcher<Matcher4,
internal::EitherOfMatcher<Matcher5, internal::EitherOfMatcher<Matcher6,
internal::EitherOfMatcher<Matcher7, internal::EitherOfMatcher<Matcher8,
Matcher9> > > > > > > >
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 <typename Matcher1, typename Matcher2, typename Matcher3,
typename Matcher4, typename Matcher5, typename Matcher6, typename Matcher7,
typename Matcher8, typename Matcher9, typename Matcher10>
inline internal::EitherOfMatcher<Matcher1, internal::EitherOfMatcher<Matcher2,
internal::EitherOfMatcher<Matcher3, internal::EitherOfMatcher<Matcher4,
internal::EitherOfMatcher<Matcher5, internal::EitherOfMatcher<Matcher6,
internal::EitherOfMatcher<Matcher7, internal::EitherOfMatcher<Matcher8,
internal::EitherOfMatcher<Matcher9, Matcher10> > > > > > > > >
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

View File

@ -302,6 +302,52 @@ ElementsAreArray(const T (&array)[N]) {
return internal::ElementsAreArrayMatcher<T>(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<Matcher$k, ]]Matcher$i[[]]$for k [[> ]]
AllOf($for j, [[Matcher$j m$j]]) {
$if i == 2 [[
return internal::BothOfMatcher<Matcher1, Matcher2>(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<Matcher$k, ]]Matcher$i[[]]$for k [[> ]]
AnyOf($for j, [[Matcher$j m$j]]) {
$if i == 2 [[
return internal::EitherOfMatcher<Matcher1, Matcher2>(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.

View File

@ -2889,82 +2889,6 @@ inline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) {
return internal::NotMatcher<InnerMatcher>(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 <typename Matcher1, typename Matcher2>
inline internal::BothOfMatcher<Matcher1, Matcher2>
AllOf(Matcher1 m1, Matcher2 m2) {
return internal::BothOfMatcher<Matcher1, Matcher2>(m1, m2);
}
template <typename Matcher1, typename Matcher2, typename Matcher3>
inline internal::BothOfMatcher<Matcher1,
internal::BothOfMatcher<Matcher2, Matcher3> >
AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3) {
return AllOf(m1, AllOf(m2, m3));
}
template <typename Matcher1, typename Matcher2, typename Matcher3,
typename Matcher4>
inline internal::BothOfMatcher<Matcher1,
internal::BothOfMatcher<Matcher2,
internal::BothOfMatcher<Matcher3, Matcher4> > >
AllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4) {
return AllOf(m1, AllOf(m2, m3, m4));
}
template <typename Matcher1, typename Matcher2, typename Matcher3,
typename Matcher4, typename Matcher5>
inline internal::BothOfMatcher<Matcher1,
internal::BothOfMatcher<Matcher2,
internal::BothOfMatcher<Matcher3,
internal::BothOfMatcher<Matcher4, Matcher5> > > >
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 <typename Matcher1, typename Matcher2>
inline internal::EitherOfMatcher<Matcher1, Matcher2>
AnyOf(Matcher1 m1, Matcher2 m2) {
return internal::EitherOfMatcher<Matcher1, Matcher2>(m1, m2);
}
template <typename Matcher1, typename Matcher2, typename Matcher3>
inline internal::EitherOfMatcher<Matcher1,
internal::EitherOfMatcher<Matcher2, Matcher3> >
AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3) {
return AnyOf(m1, AnyOf(m2, m3));
}
template <typename Matcher1, typename Matcher2, typename Matcher3,
typename Matcher4>
inline internal::EitherOfMatcher<Matcher1,
internal::EitherOfMatcher<Matcher2,
internal::EitherOfMatcher<Matcher3, Matcher4> > >
AnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4) {
return AnyOf(m1, AnyOf(m2, m3, m4));
}
template <typename Matcher1, typename Matcher2, typename Matcher3,
typename Matcher4, typename Matcher5>
inline internal::EitherOfMatcher<Matcher1,
internal::EitherOfMatcher<Matcher2,
internal::EitherOfMatcher<Matcher3,
internal::EitherOfMatcher<Matcher4, Matcher5> > > >
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.

View File

@ -1857,6 +1857,16 @@ TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
Matcher<int&> m3 = Not(m);
}
// Helper to allow easy testing of AllOf matchers with num parameters.
void AllOfMatches(int num, const Matcher<int>& 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<int>& 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.