aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorzhanyong.wan <zhanyong.wan@8415998a-534a-0410-bf83-d39667b30386>2013-08-08 18:41:51 +0000
committerzhanyong.wan <zhanyong.wan@8415998a-534a-0410-bf83-d39667b30386>2013-08-08 18:41:51 +0000
commit1cc1d4bcecdac906250f863bef2636b278eb679d (patch)
treea51411ad22ce343439b323c7cf4e8718f341deef /test
parent5579c1a8b1591d4932495b8cb3cc61f3edca2555 (diff)
downloadgoogletest-1cc1d4bcecdac906250f863bef2636b278eb679d.tar.gz
googletest-1cc1d4bcecdac906250f863bef2636b278eb679d.tar.bz2
googletest-1cc1d4bcecdac906250f863bef2636b278eb679d.zip
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.
Diffstat (limited to 'test')
-rw-r--r--test/gmock-generated-matchers_test.cc39
-rw-r--r--test/gmock-matchers_test.cc70
2 files changed, 108 insertions, 1 deletions
diff --git a/test/gmock-generated-matchers_test.cc b/test/gmock-generated-matchers_test.cc
index e43781b6..dba74ecb 100644
--- a/test/gmock-generated-matchers_test.cc
+++ b/test/gmock-generated-matchers_test.cc
@@ -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) };
diff --git a/test/gmock-matchers_test.cc b/test/gmock-matchers_test.cc
index ae97c9e0..4644f91f 100644
--- a/test/gmock-matchers_test.cc
+++ b/test/gmock-matchers_test.cc
@@ -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;