Makes some container matchers accept initializer lists in C++11 mode and work with stream-like containers that don't have size() or empty(); exposes StringMatchResultListener for defining composite matchers.

This commit is contained in:
zhanyong.wan
2013-08-08 18:41:51 +00:00
parent 5579c1a8b1
commit 1cc1d4bcec
4 changed files with 214 additions and 54 deletions

View File

@@ -64,6 +64,7 @@ using testing::ElementsAreArray;
using testing::Eq;
using testing::Ge;
using testing::Gt;
using testing::Le;
using testing::Lt;
using testing::MakeMatcher;
using testing::Matcher;
@@ -632,6 +633,44 @@ TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
}
#if GTEST_LANG_CXX11
TEST(ElementsAreArrayTest, TakesInitializerList) {
const int a[5] = { 1, 2, 3, 4, 5 };
EXPECT_THAT(a, ElementsAreArray({ 1, 2, 3, 4, 5 }));
EXPECT_THAT(a, Not(ElementsAreArray({ 1, 2, 3, 5, 4 })));
EXPECT_THAT(a, Not(ElementsAreArray({ 1, 2, 3, 4, 6 })));
}
TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) {
const string a[5] = { "a", "b", "c", "d", "e" };
EXPECT_THAT(a, ElementsAreArray({ "a", "b", "c", "d", "e" }));
EXPECT_THAT(a, Not(ElementsAreArray({ "a", "b", "c", "e", "d" })));
EXPECT_THAT(a, Not(ElementsAreArray({ "a", "b", "c", "d", "ef" })));
}
TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
const int a[5] = { 1, 2, 3, 4, 5 };
EXPECT_THAT(a, ElementsAreArray(
{ Eq(1), Eq(2), Eq(3), Eq(4), Eq(5) }));
EXPECT_THAT(a, Not(ElementsAreArray(
{ Eq(1), Eq(2), Eq(3), Eq(4), Eq(6) })));
}
TEST(ElementsAreArrayTest,
TakesInitializerListOfDifferentTypedMatchers) {
const int a[5] = { 1, 2, 3, 4, 5 };
// The compiler cannot infer the type of the initializer list if its
// elements have different types. We must explicitly specify the
// unified element type in this case.
EXPECT_THAT(a, ElementsAreArray<Matcher<int> >(
{ Eq(1), Ne(-2), Ge(3), Le(4), Eq(5) }));
EXPECT_THAT(a, Not(ElementsAreArray<Matcher<int> >(
{ Eq(1), Ne(-2), Ge(3), Le(4), Eq(6) })));
}
#endif // GTEST_LANG_CXX11
TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {
const int a[] = { 1, 2, 3 };
const Matcher<int> kMatchers[] = { Eq(1), Eq(2), Eq(3) };

View File

@@ -124,6 +124,7 @@ using testing::Ref;
using testing::ResultOf;
using testing::SizeIs;
using testing::StartsWith;
using testing::StringMatchResultListener;
using testing::StrCaseEq;
using testing::StrCaseNe;
using testing::StrEq;
@@ -145,7 +146,6 @@ using testing::internal::JoinAsTuple;
using testing::internal::MatchMatrix;
using testing::internal::RE;
using testing::internal::StreamMatchResultListener;
using testing::internal::StringMatchResultListener;
using testing::internal::Strings;
using testing::internal::linked_ptr;
using testing::internal::scoped_ptr;
@@ -222,6 +222,12 @@ TEST(MatchResultListenerTest, StreamingWorks) {
listener << "hi" << 5;
EXPECT_EQ("hi5", listener.str());
listener.Clear();
EXPECT_EQ("", listener.str());
listener << 42;
EXPECT_EQ("42", listener.str());
// Streaming shouldn't crash when the underlying ostream is NULL.
DummyMatchResultListener dummy;
dummy << "hi" << 5;
@@ -4443,6 +4449,32 @@ TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) {
EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
}
// Tests using ElementsAre() and ElementsAreArray() with stream-like
// "containers".
TEST(ElemensAreStreamTest, WorksForStreamlike) {
const int a[5] = { 1, 2, 3, 4, 5 };
Streamlike<int> s(a, a + GMOCK_ARRAY_SIZE_(a));
EXPECT_THAT(s, ElementsAre(1, 2, 3, 4, 5));
EXPECT_THAT(s, Not(ElementsAre(2, 1, 4, 5, 3)));
}
TEST(ElemensAreArrayStreamTest, WorksForStreamlike) {
const int a[5] = { 1, 2, 3, 4, 5 };
Streamlike<int> s(a, a + GMOCK_ARRAY_SIZE_(a));
vector<int> expected;
expected.push_back(1);
expected.push_back(2);
expected.push_back(3);
expected.push_back(4);
expected.push_back(5);
EXPECT_THAT(s, ElementsAreArray(expected));
expected[3] = 0;
EXPECT_THAT(s, Not(ElementsAreArray(expected)));
}
// Tests for UnorderedElementsAreArray()
TEST(UnorderedElementsAreArrayTest, SucceedsWhenExpected) {
@@ -4484,6 +4516,42 @@ TEST(UnorderedElementsAreArrayTest, WorksForStreamlike) {
EXPECT_THAT(s, Not(UnorderedElementsAreArray(expected)));
}
#if GTEST_LANG_CXX11
TEST(UnorderedElementsAreArrayTest, TakesInitializerList) {
const int a[5] = { 2, 1, 4, 5, 3 };
EXPECT_THAT(a, UnorderedElementsAreArray({ 1, 2, 3, 4, 5 }));
EXPECT_THAT(a, Not(UnorderedElementsAreArray({ 1, 2, 3, 4, 6 })));
}
TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfCStrings) {
const string a[5] = { "a", "b", "c", "d", "e" };
EXPECT_THAT(a, UnorderedElementsAreArray({ "a", "b", "c", "d", "e" }));
EXPECT_THAT(a, Not(UnorderedElementsAreArray({ "a", "b", "c", "d", "ef" })));
}
TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
const int a[5] = { 2, 1, 4, 5, 3 };
EXPECT_THAT(a, UnorderedElementsAreArray(
{ Eq(1), Eq(2), Eq(3), Eq(4), Eq(5) }));
EXPECT_THAT(a, Not(UnorderedElementsAreArray(
{ Eq(1), Eq(2), Eq(3), Eq(4), Eq(6) })));
}
TEST(UnorderedElementsAreArrayTest,
TakesInitializerListOfDifferentTypedMatchers) {
const int a[5] = { 2, 1, 4, 5, 3 };
// The compiler cannot infer the type of the initializer list if its
// elements have different types. We must explicitly specify the
// unified element type in this case.
EXPECT_THAT(a, UnorderedElementsAreArray<Matcher<int> >(
{ Eq(1), Ne(-2), Ge(3), Le(4), Eq(5) }));
EXPECT_THAT(a, Not(UnorderedElementsAreArray<Matcher<int> >(
{ Eq(1), Ne(-2), Ge(3), Le(4), Eq(6) })));
}
#endif // GTEST_LANG_CXX11
class UnorderedElementsAreTest : public testing::Test {
protected:
typedef std::vector<int> IntVec;