summaryrefslogtreecommitdiffstats
path: root/googlemock/test/gmock-matchers_test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'googlemock/test/gmock-matchers_test.cc')
-rw-r--r--googlemock/test/gmock-matchers_test.cc174
1 files changed, 105 insertions, 69 deletions
diff --git a/googlemock/test/gmock-matchers_test.cc b/googlemock/test/gmock-matchers_test.cc
index c1589a4..a35942d 100644
--- a/googlemock/test/gmock-matchers_test.cc
+++ b/googlemock/test/gmock-matchers_test.cc
@@ -46,6 +46,7 @@
#include <string.h>
#include <time.h>
#include <deque>
+#include <forward_list>
#include <functional>
#include <iostream>
#include <iterator>
@@ -56,22 +57,16 @@
#include <set>
#include <sstream>
#include <string>
+#include <type_traits>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "gtest/gtest-spi.h"
-#if GTEST_HAS_STD_FORWARD_LIST_
-# include <forward_list> // NOLINT
-#endif
-
-#if GTEST_LANG_CXX11
-# include <type_traits>
-#endif
-
namespace testing {
namespace gmock_matchers_test {
+namespace {
using std::greater;
using std::less;
@@ -158,6 +153,19 @@ using testing::internal::StreamMatchResultListener;
using testing::internal::string;
using testing::internal::Strings;
+// Helper for testing container-valued matchers in mock method context. It is
+// important to test matchers in this context, since it requires additional type
+// deduction beyond what EXPECT_THAT does, thus making it more restrictive.
+struct ContainerHelper {
+ MOCK_METHOD1(Call, void(std::vector<std::unique_ptr<int>>));
+};
+
+std::vector<std::unique_ptr<int>> MakeUniquePtrs(const std::vector<int>& ints) {
+ std::vector<std::unique_ptr<int>> pointers;
+ for (int i : ints) pointers.emplace_back(new int(i));
+ return pointers;
+}
+
// For testing ExplainMatchResultTo().
class GreaterThanMatcher : public MatcherInterface<int> {
public:
@@ -1159,30 +1167,18 @@ TEST(IsNullTest, MatchesNullPointer) {
EXPECT_TRUE(m2.Matches(p2));
EXPECT_FALSE(m2.Matches("hi"));
-#if !GTEST_OS_SYMBIAN
- // Nokia's Symbian compiler generates:
- // gmock-matchers.h: ambiguous access to overloaded function
- // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
- // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
- // MatcherInterface<void *> *)'
- // gmock-matchers.h: (point of instantiation: 'testing::
- // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
- // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc
Matcher<void*> m3 = IsNull();
void* p3 = nullptr;
EXPECT_TRUE(m3.Matches(p3));
EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
-#endif
}
-#if GTEST_LANG_CXX11
TEST(IsNullTest, StdFunction) {
const Matcher<std::function<void()>> m = IsNull();
EXPECT_TRUE(m.Matches(std::function<void()>()));
EXPECT_FALSE(m.Matches([]{}));
}
-#endif // GTEST_LANG_CXX11
// Tests that IsNull() describes itself properly.
TEST(IsNullTest, CanDescribeSelf) {
@@ -1223,14 +1219,12 @@ TEST(NotNullTest, ReferenceToConstLinkedPtr) {
EXPECT_TRUE(m.Matches(non_null_p));
}
-#if GTEST_LANG_CXX11
TEST(NotNullTest, StdFunction) {
const Matcher<std::function<void()>> m = NotNull();
EXPECT_TRUE(m.Matches([]{}));
EXPECT_FALSE(m.Matches(std::function<void()>()));
}
-#endif // GTEST_LANG_CXX11
// Tests that NotNull() describes itself properly.
TEST(NotNullTest, CanDescribeSelf) {
@@ -1513,7 +1507,6 @@ TEST(KeyTest, MatchesCorrectly) {
EXPECT_THAT(p, Not(Key(Lt(25))));
}
-#if GTEST_LANG_CXX11
template <size_t I>
struct Tag {};
@@ -1540,7 +1533,6 @@ TEST(PairTest, MatchesPairWithGetCorrectly) {
std::vector<PairWithGet> v = {{11, "Foo"}, {29, "gMockIsBestMock"}};
EXPECT_THAT(v, Contains(Key(29)));
}
-#endif // GTEST_LANG_CXX11
TEST(KeyTest, SafelyCastsInnerMatcher) {
Matcher<int> is_positive = Gt(0);
@@ -1679,7 +1671,12 @@ TEST(PairTest, InsideContainsUsingMap) {
EXPECT_THAT(container, Not(Contains(Pair(3, _))));
}
-#if GTEST_LANG_CXX11
+TEST(ContainsTest, WorksWithMoveOnly) {
+ ContainerHelper helper;
+ EXPECT_CALL(helper, Call(Contains(Pointee(2))));
+ helper.Call(MakeUniquePtrs({1, 2}));
+}
+
TEST(PairTest, UseGetInsteadOfMembers) {
PairWithGet pair{7, "ABC"};
EXPECT_THAT(pair, Pair(7, "ABC"));
@@ -1689,7 +1686,6 @@ TEST(PairTest, UseGetInsteadOfMembers) {
std::vector<PairWithGet> v = {{11, "Foo"}, {29, "gMockIsBestMock"}};
EXPECT_THAT(v, ElementsAre(Pair(11, string("Foo")), Pair(Ge(10), Not(""))));
}
-#endif // GTEST_LANG_CXX11
// Tests StartsWith(s).
@@ -2660,7 +2656,6 @@ static void AnyOfMatches(int num, const Matcher<int>& m) {
EXPECT_FALSE(m.Matches(num + 1));
}
-#if GTEST_LANG_CXX11
static void AnyOfStringMatches(int num, const Matcher<std::string>& m) {
SCOPED_TRACE(Describe(m));
EXPECT_FALSE(m.Matches(std::to_string(0)));
@@ -2670,7 +2665,6 @@ static void AnyOfStringMatches(int num, const Matcher<std::string>& m) {
}
EXPECT_FALSE(m.Matches(std::to_string(num + 1)));
}
-#endif
// Tests that AnyOf(m1, ..., mn) matches any value that matches at
// least one of the given matchers.
@@ -2715,7 +2709,6 @@ TEST(AnyOfTest, MatchesWhenAnyMatches) {
AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
}
-#if GTEST_LANG_CXX11
// Tests the variadic version of the AnyOfMatcher.
TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) {
// Also make sure AnyOf is defined in the right namespace and does not depend
@@ -2764,7 +2757,6 @@ TEST(ElementsAreTest, HugeMatcherUnordered) {
Eq(3), Eq(9), Eq(12), Eq(11), Ne(122)));
}
-#endif // GTEST_LANG_CXX11
// Tests that AnyOf(m1, ..., mn) describes itself properly.
TEST(AnyOfTest, CanDescribeSelf) {
@@ -3166,20 +3158,8 @@ TEST(MatcherAssertionTest, WorksForByRefArguments) {
"Actual: 0" + OfType("int") + ", which is located @");
}
-#if !GTEST_OS_SYMBIAN
// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
// monomorphic.
-
-// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
-// Symbian compiler: it tries to compile
-// template<T, U> class MatcherCastImpl { ...
-// virtual bool MatchAndExplain(T x, ...) const {
-// return source_matcher_.MatchAndExplain(static_cast<U>(x), ...);
-// with U == string and T == const char*
-// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
-// the compiler silently crashes with no output.
-// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
-// the code compiles but the converted string is bogus.
TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
Matcher<const char*> starts_with_he = StartsWith("he");
ASSERT_THAT("hello", starts_with_he);
@@ -3197,7 +3177,6 @@ TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
"Expected: is > 5\n"
" Actual: 5" + OfType("int"));
}
-#endif // !GTEST_OS_SYMBIAN
// Tests floating-point matchers.
template <typename RawType>
@@ -4135,9 +4114,7 @@ class AClass {
// A getter that returns a reference to const.
const std::string& s() const { return s_; }
-#if GTEST_LANG_CXX11
const std::string& s_ref() const & { return s_; }
-#endif
void set_s(const std::string& new_s) { s_ = new_s; }
@@ -4194,7 +4171,6 @@ TEST(PropertyTest, WorksForReferenceToConstProperty) {
EXPECT_FALSE(m_with_name.Matches(a));
}
-#if GTEST_LANG_CXX11
// Tests that Property(&Foo::property, ...) works when property() is
// ref-qualified.
TEST(PropertyTest, WorksForRefQualifiedProperty) {
@@ -4211,7 +4187,6 @@ TEST(PropertyTest, WorksForRefQualifiedProperty) {
EXPECT_FALSE(m.Matches(a));
EXPECT_FALSE(m_with_name.Matches(a));
}
-#endif
// Tests that Property(&Foo::property, ...) works when property()
// returns a reference to non-const.
@@ -4574,7 +4549,6 @@ TEST(ResultOfTest, WorksForPolymorphicFunctors) {
EXPECT_FALSE(matcher_string.Matches("shrt"));
}
-#if GTEST_LANG_CXX11
TEST(ResultOfTest, WorksForPolymorphicFunctorsIgnoringResultType) {
Matcher<int*> matcher = ResultOf(PolymorphicFunctor(), "good ptr");
@@ -4589,7 +4563,6 @@ TEST(ResultOfTest, WorksForLambdas) {
EXPECT_TRUE(matcher.Matches(3));
EXPECT_FALSE(matcher.Matches(1));
}
-#endif
const int* ReferencingFunction(const int& n) { return &n; }
@@ -4752,6 +4725,12 @@ TEST(IsEmptyTest, ExplainsResult) {
EXPECT_EQ("whose size is 1", Explain(m, container));
}
+TEST(IsEmptyTest, WorksWithMoveOnly) {
+ ContainerHelper helper;
+ EXPECT_CALL(helper, Call(IsEmpty()));
+ helper.Call({});
+}
+
TEST(IsTrueTest, IsTrueIsFalse) {
EXPECT_THAT(true, IsTrue());
EXPECT_THAT(false, IsFalse());
@@ -4774,7 +4753,6 @@ TEST(IsTrueTest, IsTrueIsFalse) {
EXPECT_THAT(&a, Not(IsFalse()));
EXPECT_THAT(false, Not(IsTrue()));
EXPECT_THAT(true, Not(IsFalse()));
-#if GTEST_LANG_CXX11
EXPECT_THAT(std::true_type(), IsTrue());
EXPECT_THAT(std::true_type(), Not(IsFalse()));
EXPECT_THAT(std::false_type(), IsFalse());
@@ -4787,7 +4765,6 @@ TEST(IsTrueTest, IsTrueIsFalse) {
EXPECT_THAT(null_unique, IsFalse());
EXPECT_THAT(nonnull_unique, IsTrue());
EXPECT_THAT(nonnull_unique, Not(IsFalse()));
-#endif // GTEST_LANG_CXX11
}
TEST(SizeIsTest, ImplementsSizeIs) {
@@ -4822,6 +4799,12 @@ TEST(SizeIsTest, WorksWithReferences) {
EXPECT_THAT(container, m);
}
+TEST(SizeIsTest, WorksWithMoveOnly) {
+ ContainerHelper helper;
+ EXPECT_CALL(helper, Call(SizeIs(3)));
+ helper.Call(MakeUniquePtrs({1, 2, 3}));
+}
+
// SizeIs should work for any type that provides a size() member function.
// For example, a size_type member type should not need to be provided.
struct MinimalistCustomType {
@@ -5281,7 +5264,6 @@ TEST(StreamlikeTest, Iteration) {
}
}
-#if GTEST_HAS_STD_FORWARD_LIST_
TEST(BeginEndDistanceIsTest, WorksWithForwardList) {
std::forward_list<int> container;
EXPECT_THAT(container, BeginEndDistanceIs(0));
@@ -5293,7 +5275,6 @@ TEST(BeginEndDistanceIsTest, WorksWithForwardList) {
EXPECT_THAT(container, Not(BeginEndDistanceIs(0)));
EXPECT_THAT(container, BeginEndDistanceIs(2));
}
-#endif // GTEST_HAS_STD_FORWARD_LIST_
TEST(BeginEndDistanceIsTest, WorksWithNonStdList) {
const int a[5] = {1, 2, 3, 4, 5};
@@ -5308,6 +5289,12 @@ TEST(BeginEndDistanceIsTest, CanDescribeSelf) {
DescribeNegation(m));
}
+TEST(BeginEndDistanceIsTest, WorksWithMoveOnly) {
+ ContainerHelper helper;
+ EXPECT_CALL(helper, Call(BeginEndDistanceIs(2)));
+ helper.Call(MakeUniquePtrs({1, 2}));
+}
+
TEST(BeginEndDistanceIsTest, ExplainsResult) {
Matcher<vector<int> > m1 = BeginEndDistanceIs(2);
Matcher<vector<int> > m2 = BeginEndDistanceIs(Lt(2));
@@ -5469,13 +5456,19 @@ TEST(IsSupersetOfTest, MatchAndExplain) {
" - element #2 is matched by matcher #0"));
}
-#if GTEST_HAS_STD_INITIALIZER_LIST_
TEST(IsSupersetOfTest, WorksForRhsInitializerList) {
const int numbers[] = {1, 3, 6, 2, 4, 5};
EXPECT_THAT(numbers, IsSupersetOf({1, 2}));
EXPECT_THAT(numbers, Not(IsSupersetOf({3, 0})));
}
-#endif
+
+TEST(IsSupersetOfTest, WorksWithMoveOnly) {
+ ContainerHelper helper;
+ EXPECT_CALL(helper, Call(IsSupersetOf({Pointee(1)})));
+ helper.Call(MakeUniquePtrs({1, 2}));
+ EXPECT_CALL(helper, Call(Not(IsSupersetOf({Pointee(1), Pointee(2)}))));
+ helper.Call(MakeUniquePtrs({2}));
+}
TEST(IsSubsetOfTest, WorksForNativeArray) {
const int subset[] = {1, 4};
@@ -5591,13 +5584,19 @@ TEST(IsSubsetOfTest, MatchAndExplain) {
" - element #1 is matched by matcher #2"));
}
-#if GTEST_HAS_STD_INITIALIZER_LIST_
TEST(IsSubsetOfTest, WorksForRhsInitializerList) {
const int numbers[] = {1, 2, 3};
EXPECT_THAT(numbers, IsSubsetOf({1, 2, 3, 4}));
EXPECT_THAT(numbers, Not(IsSubsetOf({1, 2})));
}
-#endif
+
+TEST(IsSubsetOfTest, WorksWithMoveOnly) {
+ ContainerHelper helper;
+ EXPECT_CALL(helper, Call(IsSubsetOf({Pointee(1), Pointee(2)})));
+ helper.Call(MakeUniquePtrs({1}));
+ EXPECT_CALL(helper, Call(Not(IsSubsetOf({Pointee(1)}))));
+ helper.Call(MakeUniquePtrs({2}));
+}
// Tests using ElementsAre() and ElementsAreArray() with stream-like
// "containers".
@@ -5632,6 +5631,15 @@ TEST(ElementsAreTest, WorksWithUncopyable) {
EXPECT_THAT(objs, ElementsAre(UncopyableIs(-3), Truly(ValueIsPositive)));
}
+TEST(ElementsAreTest, WorksWithMoveOnly) {
+ ContainerHelper helper;
+ EXPECT_CALL(helper, Call(ElementsAre(Pointee(1), Pointee(2))));
+ helper.Call(MakeUniquePtrs({1, 2}));
+
+ EXPECT_CALL(helper, Call(ElementsAreArray({Pointee(3), Pointee(4)})));
+ helper.Call(MakeUniquePtrs({3, 4}));
+}
+
TEST(ElementsAreTest, TakesStlContainer) {
const int actual[] = {3, 1, 2};
@@ -5699,7 +5707,6 @@ TEST(UnorderedElementsAreArrayTest, TakesStlContainer) {
EXPECT_THAT(actual, Not(UnorderedElementsAreArray(expected)));
}
-#if GTEST_HAS_STD_INITIALIZER_LIST_
TEST(UnorderedElementsAreArrayTest, TakesInitializerList) {
const int a[5] = {2, 1, 4, 5, 3};
@@ -5733,7 +5740,13 @@ TEST(UnorderedElementsAreArrayTest,
{Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)})));
}
-#endif // GTEST_HAS_STD_INITIALIZER_LIST_
+
+TEST(UnorderedElementsAreArrayTest, WorksWithMoveOnly) {
+ ContainerHelper helper;
+ EXPECT_CALL(helper,
+ Call(UnorderedElementsAreArray({Pointee(1), Pointee(2)})));
+ helper.Call(MakeUniquePtrs({2, 1}));
+}
class UnorderedElementsAreTest : public testing::Test {
protected:
@@ -5782,6 +5795,12 @@ TEST_F(UnorderedElementsAreTest, WorksForStreamlike) {
EXPECT_THAT(s, Not(UnorderedElementsAre(2, 2, 3, 4, 5)));
}
+TEST_F(UnorderedElementsAreTest, WorksWithMoveOnly) {
+ ContainerHelper helper;
+ EXPECT_CALL(helper, Call(UnorderedElementsAre(Pointee(1), Pointee(2))));
+ helper.Call(MakeUniquePtrs({2, 1}));
+}
+
// One naive implementation of the matcher runs in O(N!) time, which is too
// slow for many real-world inputs. This test shows that our matcher can match
// 100 inputs very quickly (a few milliseconds). An O(100!) is 10^158
@@ -6332,6 +6351,12 @@ TEST(EachTest, WorksForNativeArrayAsTuple) {
EXPECT_THAT(std::make_tuple(pointer, 2), Not(Each(Gt(1))));
}
+TEST(EachTest, WorksWithMoveOnly) {
+ ContainerHelper helper;
+ EXPECT_CALL(helper, Call(Each(Pointee(Gt(0)))));
+ helper.Call(MakeUniquePtrs({1, 2}));
+}
+
// For testing Pointwise().
class IsHalfOfMatcher {
public:
@@ -6419,7 +6444,6 @@ TEST(PointwiseTest, WorksForVectorOfBool) {
EXPECT_THAT(lhs, Not(Pointwise(Eq(), rhs)));
}
-#if GTEST_HAS_STD_INITIALIZER_LIST_
TEST(PointwiseTest, WorksForRhsInitializerList) {
const vector<int> lhs{2, 4, 6};
@@ -6427,7 +6451,6 @@ TEST(PointwiseTest, WorksForRhsInitializerList) {
EXPECT_THAT(lhs, Not(Pointwise(Lt(), {3, 3, 7})));
}
-#endif // GTEST_HAS_STD_INITIALIZER_LIST_
TEST(PointwiseTest, RejectsWrongSize) {
const double lhs[2] = {1, 2};
@@ -6470,6 +6493,17 @@ TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs));
}
+MATCHER(PointeeEquals, "Points to an equal value") {
+ return ExplainMatchResult(::testing::Pointee(::testing::get<1>(arg)),
+ ::testing::get<0>(arg), result_listener);
+}
+
+TEST(PointwiseTest, WorksWithMoveOnly) {
+ ContainerHelper helper;
+ EXPECT_CALL(helper, Call(Pointwise(PointeeEquals(), std::vector<int>{1, 2})));
+ helper.Call(MakeUniquePtrs({1, 2}));
+}
+
TEST(UnorderedPointwiseTest, DescribesSelf) {
vector<int> rhs;
rhs.push_back(1);
@@ -6530,7 +6564,6 @@ TEST(UnorderedPointwiseTest, WorksForRhsNativeArray) {
EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), rhs)));
}
-#if GTEST_HAS_STD_INITIALIZER_LIST_
TEST(UnorderedPointwiseTest, WorksForRhsInitializerList) {
const vector<int> lhs{2, 4, 6};
@@ -6538,7 +6571,6 @@ TEST(UnorderedPointwiseTest, WorksForRhsInitializerList) {
EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), {1, 1, 7})));
}
-#endif // GTEST_HAS_STD_INITIALIZER_LIST_
TEST(UnorderedPointwiseTest, RejectsWrongSize) {
const double lhs[2] = {1, 2};
@@ -6584,6 +6616,13 @@ TEST(UnorderedPointwiseTest, AllowsMonomorphicInnerMatcher) {
EXPECT_THAT(lhs, UnorderedPointwise(m2, rhs));
}
+TEST(UnorderedPointwiseTest, WorksWithMoveOnly) {
+ ContainerHelper helper;
+ EXPECT_CALL(helper, Call(UnorderedPointwise(PointeeEquals(),
+ std::vector<int>{1, 2})));
+ helper.Call(MakeUniquePtrs({2, 1}));
+}
+
// Sample optional type implementation with minimal requirements for use with
// Optional matcher.
class SampleOptionalInt {
@@ -6724,7 +6763,6 @@ TEST(AnyWithTest, TestBadCastType) {
EXPECT_FALSE(m.Matches(SampleAnyType(1)));
}
-#if GTEST_LANG_CXX11
TEST(AnyWithTest, TestUseInContainers) {
std::vector<SampleAnyType> a;
a.emplace_back(1);
@@ -6741,7 +6779,6 @@ TEST(AnyWithTest, TestUseInContainers) {
AnyWith<std::string>("merhaba"),
AnyWith<std::string>("salut")}));
}
-#endif // GTEST_LANG_CXX11
TEST(AnyWithTest, TestCompare) {
EXPECT_THAT(SampleAnyType(1), AnyWith<int>(Gt(0)));
}
@@ -6976,8 +7013,7 @@ TEST_F(PredicateFormatterFromMatcherTest, NoShortCircuitOnFailure) {
EXPECT_FALSE(result); // Implicit cast to bool.
std::string expect =
"Value of: dummy-name\nExpected: [DescribeTo]\n"
- " Actual: 1" +
- OfType(kMatcherType) + ", [MatchAndExplain]";
+ " Actual: 1, [MatchAndExplain]";
EXPECT_EQ(expect, result.message());
}
@@ -6988,11 +7024,11 @@ TEST_F(PredicateFormatterFromMatcherTest, DetectsFlakyShortCircuit) {
"Value of: dummy-name\nExpected: [DescribeTo]\n"
" The matcher failed on the initial attempt; but passed when rerun to "
"generate the explanation.\n"
- " Actual: 2" +
- OfType(kMatcherType) + ", [MatchAndExplain]";
+ " Actual: 2, [MatchAndExplain]";
EXPECT_EQ(expect, result.message());
}
+} // namespace
} // namespace gmock_matchers_test
} // namespace testing