diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/gmock-generated-matchers_test.cc | 39 | ||||
-rw-r--r-- | test/gmock-matchers_test.cc | 70 |
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; |