summaryrefslogtreecommitdiffstats
path: root/testing/gmock/test
diff options
context:
space:
mode:
Diffstat (limited to 'testing/gmock/test')
-rw-r--r--testing/gmock/test/gmock-generated-matchers_test.cc70
-rw-r--r--testing/gmock/test/gmock-internal-utils_test.cc56
-rw-r--r--testing/gmock/test/gmock-matchers_test.cc369
-rw-r--r--testing/gmock/test/gmock-nice-strict_test.cc26
-rw-r--r--testing/gmock/test/gmock-printers_test.cc20
-rw-r--r--testing/gmock/test/gmock-spec-builders_test.cc186
-rw-r--r--testing/gmock/test/gmock_all_test.cc49
-rw-r--r--testing/gmock/test/gmock_link_test.h4
8 files changed, 556 insertions, 224 deletions
diff --git a/testing/gmock/test/gmock-generated-matchers_test.cc b/testing/gmock/test/gmock-generated-matchers_test.cc
index 4141305..db2ffb2 100644
--- a/testing/gmock/test/gmock-generated-matchers_test.cc
+++ b/testing/gmock/test/gmock-generated-matchers_test.cc
@@ -68,6 +68,7 @@ using testing::Lt;
using testing::MakeMatcher;
using testing::Matcher;
using testing::MatcherInterface;
+using testing::MatchResultListener;
using testing::Ne;
using testing::Not;
using testing::Pointee;
@@ -136,6 +137,16 @@ TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
EXPECT_THAT(t, Not(Args<2, 1>(Lt())));
}
+// The MATCHER*() macros trigger warning C4100 (unreferenced formal
+// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
+// the macro definition, as the warnings are generated when the macro
+// is expanded and macro expansion cannot contain #pragma. Therefore
+// we suppress them here.
+#ifdef _MSC_VER
+#pragma warning(push)
+#pragma warning(disable:4100)
+#endif
+
MATCHER(SumIsZero, "") {
return get<0>(arg) + get<1>(arg) + get<2>(arg) == 0;
}
@@ -167,9 +178,7 @@ TEST(ArgsTest, CanMatchTupleByReference) {
// Validates that arg is printed as str.
MATCHER_P(PrintsAs, str, "") {
- typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(arg_type)) RawTuple;
- return
- testing::internal::UniversalPrinter<RawTuple>::PrintToString(arg) == str;
+ return testing::PrintToString(arg) == str;
}
TEST(ArgsTest, AcceptsTenTemplateArgs) {
@@ -207,21 +216,22 @@ class GreaterThanMatcher : public MatcherInterface<int> {
public:
explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
- virtual bool Matches(int lhs) const { return lhs > rhs_; }
-
virtual void DescribeTo(::std::ostream* os) const {
*os << "is greater than " << rhs_;
}
- virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const {
+ virtual bool MatchAndExplain(int lhs,
+ MatchResultListener* listener) const {
const int diff = lhs - rhs_;
if (diff > 0) {
- *os << "is " << diff << " more than " << rhs_;
+ *listener << "is " << diff << " more than " << rhs_;
} else if (diff == 0) {
- *os << "is the same as " << rhs_;
+ *listener << "is the same as " << rhs_;
} else {
- *os << "is " << -diff << " less than " << rhs_;
+ *listener << "is " << -diff << " less than " << rhs_;
}
+
+ return lhs > rhs_;
}
private:
@@ -553,6 +563,44 @@ TEST(MatcherMacroTest, Works) {
EXPECT_EQ("", Explain(m, 7));
}
+// Tests explaining match result in a MATCHER* macro.
+
+MATCHER(IsEven2, "is even") {
+ if ((arg % 2) == 0) {
+ // Verifies that we can stream to result_listener, a listener
+ // supplied by the MATCHER macro implicitly.
+ *result_listener << "OK";
+ return true;
+ } else {
+ *result_listener << "% 2 == " << (arg % 2);
+ return false;
+ }
+}
+
+MATCHER_P2(EqSumOf, x, y, "") {
+ if (arg == (x + y)) {
+ *result_listener << "OK";
+ return true;
+ } else {
+ // Verifies that we can stream to the underlying stream of
+ // result_listener.
+ if (result_listener->stream() != NULL) {
+ *result_listener->stream() << "diff == " << (x + y - arg);
+ }
+ return false;
+ }
+}
+
+TEST(MatcherMacroTest, CanExplainMatchResult) {
+ const Matcher<int> m1 = IsEven2();
+ EXPECT_EQ("OK", Explain(m1, 4));
+ EXPECT_EQ("% 2 == 1", Explain(m1, 5));
+
+ const Matcher<int> m2 = EqSumOf(1, 2);
+ EXPECT_EQ("OK", Explain(m2, 3));
+ EXPECT_EQ("diff == -1", Explain(m2, 4));
+}
+
// Tests that the description string supplied to MATCHER() must be
// valid.
@@ -1052,4 +1100,8 @@ TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
EXPECT_THAT(a, Contains(Not(Contains(5))));
}
+#ifdef _MSC_VER
+#pragma warning(pop)
+#endif
+
} // namespace
diff --git a/testing/gmock/test/gmock-internal-utils_test.cc b/testing/gmock/test/gmock-internal-utils_test.cc
index 7dd8311..fc5d9e5 100644
--- a/testing/gmock/test/gmock-internal-utils_test.cc
+++ b/testing/gmock/test/gmock-internal-utils_test.cc
@@ -538,8 +538,7 @@ TEST_F(LogIsVisibleTest, WorksWhenVerbosityIsWarning) {
EXPECT_TRUE(LogIsVisible(WARNING));
}
-// TODO(wan@google.com): find a way to re-enable these tests.
-#if 0
+#if GTEST_HAS_STREAM_REDIRECTION_
// Tests the Log() function.
@@ -549,16 +548,16 @@ void TestLogWithSeverity(const string& verbosity, LogSeverity severity,
bool should_print) {
const string old_flag = GMOCK_FLAG(verbose);
GMOCK_FLAG(verbose) = verbosity;
- CaptureTestStdout();
+ CaptureStdout();
Log(severity, "Test log.\n", 0);
if (should_print) {
- EXPECT_PRED2(RE::FullMatch,
- GetCapturedTestStdout(),
- severity == WARNING ?
- "\nGMOCK WARNING:\nTest log\\.\nStack trace:\n[\\s\\S]*" :
- "\nTest log\\.\nStack trace:\n[\\s\\S]*");
+ EXPECT_THAT(GetCapturedStdout().c_str(),
+ ContainsRegex(
+ severity == WARNING ?
+ "^\nGMOCK WARNING:\nTest log\\.\nStack trace:\n" :
+ "^\nTest log\\.\nStack trace:\n"));
} else {
- EXPECT_EQ("", GetCapturedTestStdout());
+ EXPECT_STREQ("", GetCapturedStdout().c_str());
}
GMOCK_FLAG(verbose) = old_flag;
}
@@ -567,18 +566,18 @@ void TestLogWithSeverity(const string& verbosity, LogSeverity severity,
// Log() doesn't include the stack trace in the output.
TEST(LogTest, NoStackTraceWhenStackFramesToSkipIsNegative) {
GMOCK_FLAG(verbose) = kInfoVerbosity;
- CaptureTestStdout();
+ CaptureStdout();
Log(INFO, "Test log.\n", -1);
- EXPECT_EQ("\nTest log.\n", GetCapturedTestStdout());
+ EXPECT_STREQ("\nTest log.\n", GetCapturedStdout().c_str());
}
// Tests that in opt mode, a positive stack_frames_to_skip argument is
// treated as 0.
TEST(LogTest, NoSkippingStackFrameInOptMode) {
- CaptureTestStdout();
+ CaptureStdout();
Log(WARNING, "Test log.\n", 100);
- const string log = GetCapturedTestStdout();
-#ifdef NDEBUG
+ const String log = GetCapturedStdout();
+#if defined(NDEBUG) && GTEST_GOOGLE3_MODE_
// In opt mode, no stack frame should be skipped.
EXPECT_THAT(log, ContainsRegex("\nGMOCK WARNING:\n"
"Test log\\.\n"
@@ -586,10 +585,10 @@ TEST(LogTest, NoSkippingStackFrameInOptMode) {
".+"));
#else
// In dbg mode, the stack frames should be skipped.
- EXPECT_EQ("\nGMOCK WARNING:\n"
- "Test log.\n"
- "Stack trace:\n", log);
-#endif // NDEBUG
+ EXPECT_STREQ("\nGMOCK WARNING:\n"
+ "Test log.\n"
+ "Stack trace:\n", log.c_str());
+#endif
}
// Tests that all logs are printed when the value of the
@@ -620,7 +619,7 @@ TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsInvalid) {
TestLogWithSeverity("invalid", WARNING, true);
}
-#endif // 0
+#endif // GTEST_HAS_STREAM_REDIRECTION_
TEST(TypeTraitsTest, true_type) {
EXPECT_TRUE(true_type::value);
@@ -657,18 +656,17 @@ TEST(TypeTraitsTest, remove_reference) {
EXPECT_TRUE((type_equals<double*, remove_reference<double*>::type>::value));
}
-// TODO(wan@google.com): find a way to re-enable these tests.
-#if 0
+#if GTEST_HAS_STREAM_REDIRECTION_
// Verifies that Log() behaves correctly for the given verbosity level
// and log severity.
-string GrabOutput(void(*logger)(), const char* verbosity) {
+String GrabOutput(void(*logger)(), const char* verbosity) {
const string saved_flag = GMOCK_FLAG(verbose);
GMOCK_FLAG(verbose) = verbosity;
- CaptureTestStdout();
+ CaptureStdout();
logger();
GMOCK_FLAG(verbose) = saved_flag;
- return GetCapturedTestStdout();
+ return GetCapturedStdout();
}
class DummyMock {
@@ -692,13 +690,13 @@ TEST(ExpectCallTest, LogsWhenVerbosityIsInfo) {
// Verifies that EXPECT_CALL doesn't log
// if the --gmock_verbose flag is set to "warning".
TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsWarning) {
- EXPECT_EQ("", GrabOutput(ExpectCallLogger, kWarningVerbosity));
+ EXPECT_STREQ("", GrabOutput(ExpectCallLogger, kWarningVerbosity).c_str());
}
// Verifies that EXPECT_CALL doesn't log
// if the --gmock_verbose flag is set to "error".
TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsError) {
- EXPECT_EQ("", GrabOutput(ExpectCallLogger, kErrorVerbosity));
+ EXPECT_STREQ("", GrabOutput(ExpectCallLogger, kErrorVerbosity).c_str());
}
void OnCallLogger() {
@@ -715,13 +713,13 @@ TEST(OnCallTest, LogsWhenVerbosityIsInfo) {
// Verifies that ON_CALL doesn't log
// if the --gmock_verbose flag is set to "warning".
TEST(OnCallTest, DoesNotLogWhenVerbosityIsWarning) {
- EXPECT_EQ("", GrabOutput(OnCallLogger, kWarningVerbosity));
+ EXPECT_STREQ("", GrabOutput(OnCallLogger, kWarningVerbosity).c_str());
}
// Verifies that ON_CALL doesn't log if
// the --gmock_verbose flag is set to "error".
TEST(OnCallTest, DoesNotLogWhenVerbosityIsError) {
- EXPECT_EQ("", GrabOutput(OnCallLogger, kErrorVerbosity));
+ EXPECT_STREQ("", GrabOutput(OnCallLogger, kErrorVerbosity).c_str());
}
void OnCallAnyArgumentLogger() {
@@ -735,7 +733,7 @@ TEST(OnCallTest, LogsAnythingArgument) {
HasSubstr("ON_CALL(mock, TestMethodArg(_)"));
}
-#endif // 0
+#endif // GTEST_HAS_STREAM_REDIRECTION_
// Tests ArrayEq().
diff --git a/testing/gmock/test/gmock-matchers_test.cc b/testing/gmock/test/gmock-matchers_test.cc
index 907749d..5557983 100644
--- a/testing/gmock/test/gmock-matchers_test.cc
+++ b/testing/gmock/test/gmock-matchers_test.cc
@@ -37,6 +37,7 @@
#include <string.h>
#include <functional>
+#include <iostream>
#include <list>
#include <map>
#include <set>
@@ -88,6 +89,8 @@ using testing::Matcher;
using testing::MatcherCast;
using testing::MatcherInterface;
using testing::Matches;
+using testing::ExplainMatchResult;
+using testing::MatchResultListener;
using testing::NanSensitiveDoubleEq;
using testing::NanSensitiveFloatEq;
using testing::Ne;
@@ -108,6 +111,7 @@ using testing::Truly;
using testing::TypedEq;
using testing::Value;
using testing::_;
+using testing::internal::DummyMatchResultListener;
using testing::internal::FloatingEqMatcher;
using testing::internal::FormatMatcherDescriptionSyntaxError;
using testing::internal::GetParamIndex;
@@ -115,8 +119,10 @@ using testing::internal::Interpolation;
using testing::internal::Interpolations;
using testing::internal::JoinAsTuple;
using testing::internal::SkipPrefix;
+using testing::internal::StreamMatchResultListener;
using testing::internal::String;
using testing::internal::Strings;
+using testing::internal::StringMatchResultListener;
using testing::internal::ValidateMatcherDescription;
using testing::internal::kInvalidInterpolation;
using testing::internal::kPercentInterpolation;
@@ -125,32 +131,31 @@ using testing::internal::linked_ptr;
using testing::internal::scoped_ptr;
using testing::internal::string;
-#ifdef GMOCK_HAS_REGEX
using testing::ContainsRegex;
using testing::MatchesRegex;
using testing::internal::RE;
-#endif // GMOCK_HAS_REGEX
// For testing ExplainMatchResultTo().
class GreaterThanMatcher : public MatcherInterface<int> {
public:
explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
- virtual bool Matches(int lhs) const { return lhs > rhs_; }
-
virtual void DescribeTo(::std::ostream* os) const {
*os << "is greater than " << rhs_;
}
- virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const {
+ virtual bool MatchAndExplain(int lhs,
+ MatchResultListener* listener) const {
const int diff = lhs - rhs_;
if (diff > 0) {
- *os << "is " << diff << " more than " << rhs_;
+ *listener << "which is " << diff << " more than " << rhs_;
} else if (diff == 0) {
- *os << "is the same as " << rhs_;
+ *listener << "which is the same as " << rhs_;
} else {
- *os << "is " << -diff << " less than " << rhs_;
+ *listener << "which is " << -diff << " less than " << rhs_;
}
+
+ return lhs > rhs_;
}
private:
@@ -185,11 +190,39 @@ string Explain(const MatcherType& m, const Value& x) {
return ss.str();
}
+TEST(MatchResultListenerTest, StreamingWorks) {
+ StringMatchResultListener listener;
+ listener << "hi" << 5;
+ EXPECT_EQ("hi5", listener.str());
+
+ // Streaming shouldn't crash when the underlying ostream is NULL.
+ DummyMatchResultListener dummy;
+ dummy << "hi" << 5;
+}
+
+TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
+ EXPECT_TRUE(DummyMatchResultListener().stream() == NULL);
+ EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL);
+
+ EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
+}
+
+TEST(MatchResultListenerTest, IsInterestedWorks) {
+ EXPECT_TRUE(StringMatchResultListener().IsInterested());
+ EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
+
+ EXPECT_FALSE(DummyMatchResultListener().IsInterested());
+ EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested());
+}
+
// Makes sure that the MatcherInterface<T> interface doesn't
// change.
class EvenMatcherImpl : public MatcherInterface<int> {
public:
- virtual bool Matches(int x) const { return x % 2 == 0; }
+ virtual bool MatchAndExplain(int x,
+ MatchResultListener* /* listener */) const {
+ return x % 2 == 0;
+ }
virtual void DescribeTo(::std::ostream* os) const {
*os << "is an even number";
@@ -200,10 +233,40 @@ class EvenMatcherImpl : public MatcherInterface<int> {
// two methods is optional.
};
-TEST(MatcherInterfaceTest, CanBeImplemented) {
+// Makes sure that the MatcherInterface API doesn't change.
+TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
EvenMatcherImpl m;
}
+// Tests implementing a monomorphic matcher using MatchAndExplain().
+
+class NewEvenMatcherImpl : public MatcherInterface<int> {
+ public:
+ virtual bool MatchAndExplain(int x, MatchResultListener* listener) const {
+ const bool match = x % 2 == 0;
+ // Verifies that we can stream to a listener directly.
+ *listener << "value % " << 2;
+ if (listener->stream() != NULL) {
+ // Verifies that we can stream to a listener's underlying stream
+ // too.
+ *listener->stream() << " == " << (x % 2);
+ }
+ return match;
+ }
+
+ virtual void DescribeTo(::std::ostream* os) const {
+ *os << "is an even number";
+ }
+};
+
+TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
+ Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
+ EXPECT_TRUE(m.Matches(2));
+ EXPECT_FALSE(m.Matches(3));
+ EXPECT_EQ("value % 2 == 0", Explain(m, 2));
+ EXPECT_EQ("value % 2 == 1", Explain(m, 3));
+}
+
// Tests default-constructing a matcher.
TEST(MatcherTest, CanBeDefaultConstructed) {
Matcher<double> m;
@@ -252,6 +315,18 @@ TEST(MatcherTest, CanDescribeItself) {
Describe(Matcher<int>(new EvenMatcherImpl)));
}
+// Tests Matcher<T>::MatchAndExplain().
+TEST(MatcherTest, MatchAndExplain) {
+ Matcher<int> m = GreaterThan(0);
+ StringMatchResultListener listener1;
+ EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
+ EXPECT_EQ("which is 42 more than 0", listener1.str());
+
+ StringMatchResultListener listener2;
+ EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
+ EXPECT_EQ("which is 9 less than 0", listener2.str());
+}
+
// Tests that a C-string literal can be implicitly converted to a
// Matcher<string> or Matcher<const string&>.
TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
@@ -284,13 +359,14 @@ TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
Matcher<int> m = MakeMatcher(dummy_impl);
}
-// Tests that MakePolymorphicMatcher() constructs a polymorphic
-// matcher from its implementation.
+// Tests that MakePolymorphicMatcher() can construct a polymorphic
+// matcher from its implementation using the old API.
const int bar = 1;
class ReferencesBarOrIsZeroImpl {
public:
template <typename T>
- bool Matches(const T& x) const {
+ bool MatchAndExplain(const T& x,
+ MatchResultListener* /* listener */) const {
const void* p = &x;
return p == &bar || x == 0;
}
@@ -308,7 +384,7 @@ PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
}
-TEST(MakePolymorphicMatcherTest, ConstructsMatcherFromImpl) {
+TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
// Using a polymorphic matcher to match a reference type.
Matcher<const int&> m1 = ReferencesBarOrIsZero();
EXPECT_TRUE(m1.Matches(0));
@@ -324,6 +400,57 @@ TEST(MakePolymorphicMatcherTest, ConstructsMatcherFromImpl) {
EXPECT_EQ("bar or zero", Describe(m2));
}
+// Tests implementing a polymorphic matcher using MatchAndExplain().
+
+class PolymorphicIsEvenImpl {
+ public:
+ void DescribeTo(::std::ostream* os) const { *os << "is even"; }
+
+ void DescribeNegationTo(::std::ostream* os) const {
+ *os << "is odd";
+ }
+
+ template <typename T>
+ bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
+ // Verifies that we can stream to the listener directly.
+ *listener << "% " << 2;
+ if (listener->stream() != NULL) {
+ // Verifies that we can stream to the listener's underlying stream
+ // too.
+ *listener->stream() << " == " << (x % 2);
+ }
+ return (x % 2) == 0;
+ }
+};
+
+PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
+ return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
+}
+
+TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
+ // Using PolymorphicIsEven() as a Matcher<int>.
+ const Matcher<int> m1 = PolymorphicIsEven();
+ EXPECT_TRUE(m1.Matches(42));
+ EXPECT_FALSE(m1.Matches(43));
+ EXPECT_EQ("is even", Describe(m1));
+
+ const Matcher<int> not_m1 = Not(m1);
+ EXPECT_EQ("is odd", Describe(not_m1));
+
+ EXPECT_EQ("% 2 == 0", Explain(m1, 42));
+
+ // Using PolymorphicIsEven() as a Matcher<char>.
+ const Matcher<char> m2 = PolymorphicIsEven();
+ EXPECT_TRUE(m2.Matches('\x42'));
+ EXPECT_FALSE(m2.Matches('\x43'));
+ EXPECT_EQ("is even", Describe(m2));
+
+ const Matcher<char> not_m2 = Not(m2);
+ EXPECT_EQ("is odd", Describe(not_m2));
+
+ EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
+}
+
// Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
TEST(MatcherCastTest, FromPolymorphicMatcher) {
Matcher<int> m = MatcherCast<int>(Eq(5));
@@ -1050,21 +1177,41 @@ TEST(PairTest, CanDescribeSelf) {
}
TEST(PairTest, CanExplainMatchResultTo) {
- const Matcher<std::pair<int, int> > m0 = Pair(0, 0);
- EXPECT_EQ("", Explain(m0, std::make_pair(25, 42)));
-
- const Matcher<std::pair<int, int> > m1 = Pair(GreaterThan(0), 0);
- EXPECT_EQ("the first field is 25 more than 0",
- Explain(m1, std::make_pair(25, 42)));
-
- const Matcher<std::pair<int, int> > m2 = Pair(0, GreaterThan(0));
- EXPECT_EQ("the second field is 42 more than 0",
- Explain(m2, std::make_pair(25, 42)));
-
- const Matcher<std::pair<int, int> > m3 = Pair(GreaterThan(0), GreaterThan(0));
- EXPECT_EQ("the first field is 25 more than 0"
- ", and the second field is 42 more than 0",
- Explain(m3, std::make_pair(25, 42)));
+ // If neither field matches, Pair() should explain about the first
+ // field.
+ const Matcher<std::pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
+ EXPECT_EQ("whose first field does not match, which is 1 less than 0",
+ Explain(m, std::make_pair(-1, -2)));
+
+ // If the first field matches but the second doesn't, Pair() should
+ // explain about the second field.
+ EXPECT_EQ("whose second field does not match, which is 2 less than 0",
+ Explain(m, std::make_pair(1, -2)));
+
+ // If the first field doesn't match but the second does, Pair()
+ // should explain about the first field.
+ EXPECT_EQ("whose first field does not match, which is 1 less than 0",
+ Explain(m, std::make_pair(-1, 2)));
+
+ // If both fields match, Pair() should explain about them both.
+ EXPECT_EQ("whose both fields match, where the first field is a value "
+ "which is 1 more than 0, and the second field is a value "
+ "which is 2 more than 0",
+ Explain(m, std::make_pair(1, 2)));
+
+ // If only the first match has an explanation, only this explanation should
+ // be printed.
+ const Matcher<std::pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
+ EXPECT_EQ("whose both fields match, where the first field is a value "
+ "which is 1 more than 0",
+ Explain(explain_first, std::make_pair(1, 0)));
+
+ // If only the second match has an explanation, only this explanation should
+ // be printed.
+ const Matcher<std::pair<int, int> > explain_second = Pair(0, GreaterThan(0));
+ EXPECT_EQ("whose both fields match, where the second field is a value "
+ "which is 1 more than 0",
+ Explain(explain_second, std::make_pair(0, 1)));
}
TEST(PairTest, MatchesCorrectly) {
@@ -1150,8 +1297,6 @@ TEST(EndsWithTest, CanDescribeSelf) {
EXPECT_EQ("ends with \"Hi\"", Describe(m));
}
-#ifdef GMOCK_HAS_REGEX
-
// Tests MatchesRegex().
TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
@@ -1170,8 +1315,8 @@ TEST(MatchesRegexTest, CanDescribeSelf) {
Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
- Matcher<const char*> m2 = MatchesRegex(new RE("[a-z].*"));
- EXPECT_EQ("matches regular expression \"[a-z].*\"", Describe(m2));
+ Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
+ EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
}
// Tests ContainsRegex().
@@ -1192,10 +1337,9 @@ TEST(ContainsRegexTest, CanDescribeSelf) {
Matcher<const std::string> m1 = ContainsRegex("Hi.*");
EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
- Matcher<const char*> m2 = ContainsRegex(new RE("[a-z].*"));
- EXPECT_EQ("contains regular expression \"[a-z].*\"", Describe(m2));
+ Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
+ EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
}
-#endif // GMOCK_HAS_REGEX
// Tests for wide strings.
#if GTEST_HAS_STD_WSTRING
@@ -1949,6 +2093,36 @@ TEST(ValueTest, WorksWithMonomorphicMatcher) {
EXPECT_FALSE(Value(1, ref_n));
}
+TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
+ StringMatchResultListener listener1;
+ EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
+ EXPECT_EQ("% 2 == 0", listener1.str());
+
+ StringMatchResultListener listener2;
+ EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
+ EXPECT_EQ("", listener2.str());
+}
+
+TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
+ const Matcher<int> is_even = PolymorphicIsEven();
+ StringMatchResultListener listener1;
+ EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
+ EXPECT_EQ("% 2 == 0", listener1.str());
+
+ const Matcher<const double&> is_zero = Eq(0);
+ StringMatchResultListener listener2;
+ EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
+ EXPECT_EQ("", listener2.str());
+}
+
+MATCHER_P(Really, inner_matcher, "") {
+ return ExplainMatchResult(inner_matcher, arg, result_listener);
+}
+
+TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
+ EXPECT_THAT(0, Really(Eq(0)));
+}
+
TEST(AllArgsTest, WorksForTuple) {
EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
@@ -2041,8 +2215,8 @@ TEST(MatcherAssertionTest, WorksForByRefArguments) {
// 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 Matches(T x) const {
-// return source_matcher_.Matches(static_cast<U>(x));
+// 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.
@@ -2385,7 +2559,14 @@ TEST(PointeeTest, CanExplainMatchResult) {
const Matcher<int*> m2 = Pointee(GreaterThan(1));
int n = 3;
- EXPECT_EQ("points to a value that is 2 more than 1", Explain(m2, &n));
+ EXPECT_EQ("which points to 3, which is 2 more than 1",
+ Explain(m2, &n));
+}
+
+TEST(PointeeTest, AlwaysExplainsPointee) {
+ const Matcher<int*> m = Pointee(0);
+ int n = 42;
+ EXPECT_EQ("which points to 42", Explain(m, &n));
}
// An uncopyable class.
@@ -2512,8 +2693,9 @@ TEST(FieldTest, WorksForCompatibleMatcherType) {
TEST(FieldTest, CanDescribeSelf) {
Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
- EXPECT_EQ("the given field is greater than or equal to 0", Describe(m));
- EXPECT_EQ("the given field is not greater than or equal to 0",
+ EXPECT_EQ("is an object whose given field is greater than or equal to 0",
+ Describe(m));
+ EXPECT_EQ("is an object whose given field is not greater than or equal to 0",
DescribeNegation(m));
}
@@ -2523,10 +2705,10 @@ TEST(FieldTest, CanExplainMatchResult) {
AStruct a;
a.x = 1;
- EXPECT_EQ("", Explain(m, a));
+ EXPECT_EQ("whose given field is 1", Explain(m, a));
m = Field(&AStruct::x, GreaterThan(0));
- EXPECT_EQ("the given field is 1 more than 0", Explain(m, a));
+ EXPECT_EQ("whose given field is 1, which is 1 more than 0", Explain(m, a));
}
// Tests that Field() works when the argument is a pointer to const.
@@ -2549,6 +2731,16 @@ TEST(FieldForPointerTest, WorksForPointerToNonConst) {
EXPECT_FALSE(m.Matches(&a));
}
+// Tests that Field() works when the argument is a reference to a const pointer.
+TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
+ Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
+
+ AStruct a;
+ EXPECT_TRUE(m.Matches(&a));
+ a.x = -1;
+ EXPECT_FALSE(m.Matches(&a));
+}
+
// Tests that Field() does not match the NULL pointer.
TEST(FieldForPointerTest, DoesNotMatchNull) {
Matcher<const AStruct*> m = Field(&AStruct::x, _);
@@ -2572,8 +2764,9 @@ TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
TEST(FieldForPointerTest, CanDescribeSelf) {
Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
- EXPECT_EQ("the given field is greater than or equal to 0", Describe(m));
- EXPECT_EQ("the given field is not greater than or equal to 0",
+ EXPECT_EQ("is an object whose given field is greater than or equal to 0",
+ Describe(m));
+ EXPECT_EQ("is an object whose given field is not greater than or equal to 0",
DescribeNegation(m));
}
@@ -2584,10 +2777,11 @@ TEST(FieldForPointerTest, CanExplainMatchResult) {
AStruct a;
a.x = 1;
EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
- EXPECT_EQ("", Explain(m, &a));
+ EXPECT_EQ("which points to an object whose given field is 1", Explain(m, &a));
m = Field(&AStruct::x, GreaterThan(0));
- EXPECT_EQ("the given field is 1 more than 0", Explain(m, &a));
+ EXPECT_EQ("which points to an object whose given field is 1, "
+ "which is 1 more than 0", Explain(m, &a));
}
// A user-defined class for testing Property().
@@ -2706,9 +2900,10 @@ TEST(PropertyTest, WorksForCompatibleMatcherType) {
TEST(PropertyTest, CanDescribeSelf) {
Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
- EXPECT_EQ("the given property is greater than or equal to 0", Describe(m));
- EXPECT_EQ("the given property is not greater than or equal to 0",
- DescribeNegation(m));
+ EXPECT_EQ("is an object whose given property is greater than or equal to 0",
+ Describe(m));
+ EXPECT_EQ("is an object whose given property "
+ "is not greater than or equal to 0", DescribeNegation(m));
}
// Tests that Property() can explain the match result.
@@ -2717,10 +2912,10 @@ TEST(PropertyTest, CanExplainMatchResult) {
AClass a;
a.set_n(1);
- EXPECT_EQ("", Explain(m, a));
+ EXPECT_EQ("whose given property is 1", Explain(m, a));
m = Property(&AClass::n, GreaterThan(0));
- EXPECT_EQ("the given property is 1 more than 0", Explain(m, a));
+ EXPECT_EQ("whose given property is 1, which is 1 more than 0", Explain(m, a));
}
// Tests that Property() works when the argument is a pointer to const.
@@ -2747,6 +2942,19 @@ TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
EXPECT_FALSE(m.Matches(&a));
}
+// Tests that Property() works when the argument is a reference to a
+// const pointer.
+TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
+ Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
+
+ AClass a;
+ a.set_s("hill");
+ EXPECT_TRUE(m.Matches(&a));
+
+ a.set_s("hole");
+ EXPECT_FALSE(m.Matches(&a));
+}
+
// Tests that Property() does not match the NULL pointer.
TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
Matcher<const AClass*> m = Property(&AClass::x, _);
@@ -2772,9 +2980,10 @@ TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
TEST(PropertyForPointerTest, CanDescribeSelf) {
Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
- EXPECT_EQ("the given property is greater than or equal to 0", Describe(m));
- EXPECT_EQ("the given property is not greater than or equal to 0",
- DescribeNegation(m));
+ EXPECT_EQ("is an object whose given property is greater than or equal to 0",
+ Describe(m));
+ EXPECT_EQ("is an object whose given property "
+ "is not greater than or equal to 0", DescribeNegation(m));
}
// Tests that Property() can explain the result of matching a pointer.
@@ -2784,10 +2993,12 @@ TEST(PropertyForPointerTest, CanExplainMatchResult) {
AClass a;
a.set_n(1);
EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
- EXPECT_EQ("", Explain(m, &a));
+ EXPECT_EQ("which points to an object whose given property is 1",
+ Explain(m, &a));
m = Property(&AClass::n, GreaterThan(0));
- EXPECT_EQ("the given property is 1 more than 0", Explain(m, &a));
+ EXPECT_EQ("which points to an object whose given property is 1, "
+ "which is 1 more than 0", Explain(m, &a));
}
// Tests ResultOf.
@@ -2807,10 +3018,10 @@ TEST(ResultOfTest, WorksForFunctionPointers) {
TEST(ResultOfTest, CanDescribeItself) {
Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
- EXPECT_EQ("result of the given callable is equal to \"foo\"",
- Describe(matcher));
- EXPECT_EQ("result of the given callable is not equal to \"foo\"",
- DescribeNegation(matcher));
+ EXPECT_EQ("is mapped by the given callable to a value that "
+ "is equal to \"foo\"", Describe(matcher));
+ EXPECT_EQ("is mapped by the given callable to a value that "
+ "is not equal to \"foo\"", DescribeNegation(matcher));
}
// Tests that ResultOf() can explain the match result.
@@ -2818,11 +3029,12 @@ int IntFunction(int input) { return input == 42 ? 80 : 90; }
TEST(ResultOfTest, CanExplainMatchResult) {
Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
- EXPECT_EQ("", Explain(matcher, 36));
+ EXPECT_EQ("which is mapped by the given callable to 90",
+ Explain(matcher, 36));
matcher = ResultOf(&IntFunction, GreaterThan(85));
- EXPECT_EQ("result of the given callable is 5 more than 85",
- Explain(matcher, 36));
+ EXPECT_EQ("which is mapped by the given callable to 90, "
+ "which is 5 more than 85", Explain(matcher, 36));
}
// Tests that ResultOf(f, ...) compiles and works as expected when f(x)
@@ -2958,8 +3170,11 @@ class DivisibleByImpl {
public:
explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
+ // For testing using ExplainMatchResultTo() with polymorphic matchers.
template <typename T>
- bool Matches(const T& n) const {
+ bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
+ *listener << "is " << (n % divider_) << " modulo "
+ << divider_;
return (n % divider_) == 0;
}
@@ -2978,14 +3193,6 @@ class DivisibleByImpl {
int divider_;
};
-// For testing using ExplainMatchResultTo() with polymorphic matchers.
-template <typename T>
-void ExplainMatchResultTo(const DivisibleByImpl& impl, const T& n,
- ::std::ostream* os) {
- *os << "is " << (n % impl.divider()) << " modulo "
- << impl.divider();
-}
-
PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
return MakePolymorphicMatcher(DivisibleByImpl(n));
}
@@ -3025,7 +3232,7 @@ TEST(ExplainMatchResultTest, AllOf_True_True_2) {
TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
const Matcher<int> m = GreaterThan(5);
- EXPECT_EQ("is 1 more than 5", Explain(m, 6));
+ EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
}
// The following two tests verify that values without a public copy
@@ -3335,6 +3542,16 @@ TEST(ValidateMatcherDescriptionTest,
ElementsAre());
}
+// The MATCHER*() macros trigger warning C4100 (unreferenced formal
+// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
+// the macro definition, as the warnings are generated when the macro
+// is expanded and macro expansion cannot contain #pragma. Therefore
+// we suppress them here.
+#ifdef _MSC_VER
+#pragma warning(push)
+#pragma warning(disable:4100)
+#endif
+
// We use MATCHER_P3() to define a matcher for testing
// ValidateMatcherDescription(); otherwise we'll end up with much
// plumbing code. This is not circular as
@@ -3345,6 +3562,10 @@ MATCHER_P3(EqInterpolation, start, end, index, "equals Interpolation%(*)s") {
arg.param_index == index;
}
+#ifdef _MSC_VER
+#pragma warning(pop)
+#endif
+
TEST(ValidateMatcherDescriptionTest, AcceptsPercentInterpolation) {
const char* params[] = { "foo", NULL };
const char* const desc = "one %%";
diff --git a/testing/gmock/test/gmock-nice-strict_test.cc b/testing/gmock/test/gmock-nice-strict_test.cc
index 1d36e03..f6f278e 100644
--- a/testing/gmock/test/gmock-nice-strict_test.cc
+++ b/testing/gmock/test/gmock-nice-strict_test.cc
@@ -57,6 +57,11 @@ using testing::HasSubstr;
using testing::NiceMock;
using testing::StrictMock;
+#if GTEST_HAS_STREAM_REDIRECTION_
+using testing::internal::CaptureStdout;
+using testing::internal::GetCapturedStdout;
+#endif // GTEST_HAS_STREAM_REDIRECTION_
+
// Defines some mock classes needed by the tests.
class Foo {
@@ -102,17 +107,16 @@ class MockBar {
GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar);
};
-// TODO(wan@google.com): find a way to re-enable these tests.
-#if 0
+#if GTEST_HAS_STREAM_REDIRECTION_
// Tests that a nice mock generates no warning for uninteresting calls.
TEST(NiceMockTest, NoWarningForUninterestingCall) {
NiceMock<MockFoo> nice_foo;
- CaptureTestStdout();
+ CaptureStdout();
nice_foo.DoThis();
nice_foo.DoThat(true);
- EXPECT_EQ("", GetCapturedTestStdout());
+ EXPECT_STREQ("", GetCapturedStdout().c_str());
}
// Tests that a nice mock generates no warning for uninteresting calls
@@ -123,9 +127,9 @@ TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
ON_CALL(*nice_foo, DoThis())
.WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
- CaptureTestStdout();
+ CaptureStdout();
nice_foo->DoThis();
- EXPECT_EQ("", GetCapturedTestStdout());
+ EXPECT_STREQ("", GetCapturedStdout().c_str());
}
// Tests that a nice mock generates informational logs for
@@ -134,18 +138,18 @@ TEST(NiceMockTest, InfoForUninterestingCall) {
NiceMock<MockFoo> nice_foo;
GMOCK_FLAG(verbose) = "info";
- CaptureTestStdout();
+ CaptureStdout();
nice_foo.DoThis();
- EXPECT_THAT(GetCapturedTestStdout(),
+ EXPECT_THAT(GetCapturedStdout(),
HasSubstr("Uninteresting mock function call"));
- CaptureTestStdout();
+ CaptureStdout();
nice_foo.DoThat(true);
- EXPECT_THAT(GetCapturedTestStdout(),
+ EXPECT_THAT(GetCapturedStdout(),
HasSubstr("Uninteresting mock function call"));
}
-#endif // 0
+#endif // GTEST_HAS_STREAM_REDIRECTION_
// Tests that a nice mock allows expected calls.
TEST(NiceMockTest, AllowsExpectedCall) {
diff --git a/testing/gmock/test/gmock-printers_test.cc b/testing/gmock/test/gmock-printers_test.cc
index 0553e9c..92c8413 100644
--- a/testing/gmock/test/gmock-printers_test.cc
+++ b/testing/gmock/test/gmock-printers_test.cc
@@ -153,6 +153,7 @@ using ::std::tr1::make_tuple;
using ::std::tr1::tuple;
using ::std::vector;
using ::testing::ElementsAre;
+using ::testing::PrintToString;
using ::testing::StartsWith;
using ::testing::internal::NativeArray;
using ::testing::internal::Strings;
@@ -1010,19 +1011,24 @@ TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
+ " " + Print(sizeof(p)) + "-byte object "));
}
-TEST(PrintToStringTest, WorksForNonReference) {
- EXPECT_EQ("123", UniversalPrinter<int>::PrintToString(123));
+TEST(PrintToStringTest, WorksForScalar) {
+ EXPECT_EQ("123", PrintToString(123));
}
-TEST(PrintToStringTest, WorksForReference) {
- int n = 123;
- EXPECT_EQ("@" + PrintPointer(&n) + " 123",
- UniversalPrinter<const int&>::PrintToString(n));
+TEST(PrintToStringTest, WorksForPointerToConstChar) {
+ const char* p = "hello";
+ EXPECT_EQ("\"hello\"", PrintToString(p));
+}
+
+TEST(PrintToStringTest, WorksForPointerToNonConstChar) {
+ char s[] = "hello";
+ char* p = s;
+ EXPECT_EQ("\"hello\"", PrintToString(p));
}
TEST(PrintToStringTest, WorksForArray) {
int n[3] = { 1, 2, 3 };
- EXPECT_EQ("{ 1, 2, 3 }", UniversalPrinter<int[3]>::PrintToString(n));
+ EXPECT_EQ("{ 1, 2, 3 }", PrintToString(n));
}
TEST(UniversalTersePrintTest, WorksForNonReference) {
diff --git a/testing/gmock/test/gmock-spec-builders_test.cc b/testing/gmock/test/gmock-spec-builders_test.cc
index c1e7738..e5fc2ec 100644
--- a/testing/gmock/test/gmock-spec-builders_test.cc
+++ b/testing/gmock/test/gmock-spec-builders_test.cc
@@ -68,6 +68,7 @@ using testing::AtMost;
using testing::Between;
using testing::Cardinality;
using testing::CardinalityInterface;
+using testing::ContainsRegex;
using testing::Const;
using testing::DoAll;
using testing::DoDefault;
@@ -86,13 +87,20 @@ using testing::Mock;
using testing::Ne;
using testing::Return;
using testing::Sequence;
+using testing::internal::ExpectationTester;
using testing::internal::g_gmock_mutex;
using testing::internal::kErrorVerbosity;
using testing::internal::kInfoVerbosity;
using testing::internal::kWarningVerbosity;
-using testing::internal::ExpectationTester;
+using testing::internal::String;
using testing::internal::string;
+#if GTEST_HAS_STREAM_REDIRECTION_
+using testing::HasSubstr;
+using testing::internal::CaptureStdout;
+using testing::internal::GetCapturedStdout;
+#endif // GTEST_HAS_STREAM_REDIRECTION_
+
class Result {};
class MockA {
@@ -510,13 +518,12 @@ TEST(ExpectCallSyntaxTest, DefaultCardinalityIsOnce) {
}, "to be called once");
}
-// TODO(wan@google.com): find a way to re-enable these tests.
-#if 0
+#if GTEST_HAS_STREAM_REDIRECTION_
// Tests that Google Mock doesn't print a warning when the number of
// WillOnce() is adequate.
TEST(ExpectCallSyntaxTest, DoesNotWarnOnAdequateActionCount) {
- CaptureTestStdout();
+ CaptureStdout();
{
MockB b;
@@ -546,14 +553,13 @@ TEST(ExpectCallSyntaxTest, DoesNotWarnOnAdequateActionCount) {
b.DoB(2);
b.DoB(3);
}
- const string& output = GetCapturedTestStdout();
- EXPECT_EQ("", output);
+ EXPECT_STREQ("", GetCapturedStdout().c_str());
}
// Tests that Google Mock warns on having too many actions in an
// expectation compared to its cardinality.
TEST(ExpectCallSyntaxTest, WarnsOnTooManyActions) {
- CaptureTestStdout();
+ CaptureStdout();
{
MockB b;
@@ -585,7 +591,7 @@ TEST(ExpectCallSyntaxTest, WarnsOnTooManyActions) {
b.DoB(1);
b.DoB(2);
}
- const string& output = GetCapturedTestStdout();
+ const String output = GetCapturedStdout();
EXPECT_PRED_FORMAT2(
IsSubstring,
"Too many actions specified in EXPECT_CALL(b, DoB())...\n"
@@ -625,9 +631,9 @@ TEST(ExpectCallSyntaxTest, WarnsOnTooFewActions) {
.Times(Between(2, 3))
.WillOnce(Return(1));
- CaptureTestStdout();
+ CaptureStdout();
b.DoB();
- const string& output = GetCapturedTestStdout();
+ const String output = GetCapturedStdout();
EXPECT_PRED_FORMAT2(
IsSubstring,
"Too few actions specified in EXPECT_CALL(b, DoB())...\n"
@@ -637,7 +643,7 @@ TEST(ExpectCallSyntaxTest, WarnsOnTooFewActions) {
b.DoB();
}
-#endif // 0
+#endif // GTEST_HAS_STREAM_REDIRECTION_
// Tests the semantics of ON_CALL().
@@ -791,8 +797,7 @@ TEST(ExpectCallTest, NthMatchTakesNthAction) {
EXPECT_EQ(3, b.DoB());
}
-// TODO(wan@google.com): find a way to re-enable these tests.
-#if 0
+#if GTEST_HAS_STREAM_REDIRECTION_
// Tests that the default action is taken when the WillOnce(...) list is
// exhausted and there is no WillRepeatedly().
@@ -805,29 +810,29 @@ TEST(ExpectCallTest, TakesDefaultActionWhenWillListIsExhausted) {
.WillOnce(Return(1))
.WillOnce(Return(2));
- CaptureTestStdout();
+ CaptureStdout();
EXPECT_EQ(0, b.DoB(1)); // Shouldn't generate a warning as the
// expectation has no action clause at all.
EXPECT_EQ(1, b.DoB());
EXPECT_EQ(2, b.DoB());
- const string& output1 = GetCapturedTestStdout();
- EXPECT_EQ("", output1);
+ const String output1 = GetCapturedStdout();
+ EXPECT_STREQ("", output1.c_str());
- CaptureTestStdout();
+ CaptureStdout();
EXPECT_EQ(0, b.DoB());
EXPECT_EQ(0, b.DoB());
- const string& output2 = GetCapturedTestStdout();
- EXPECT_PRED2(RE::PartialMatch, output2,
- "Actions ran out\\.\n"
- "Called 3 times, but only 2 WillOnce\\(\\)s are specified - "
- "returning default value\\.");
- EXPECT_PRED2(RE::PartialMatch, output2,
- "Actions ran out\\.\n"
- "Called 4 times, but only 2 WillOnce\\(\\)s are specified - "
- "returning default value\\.");
+ const String output2 = GetCapturedStdout();
+ EXPECT_THAT(output2.c_str(),
+ HasSubstr("Actions ran out in EXPECT_CALL(b, DoB())...\n"
+ "Called 3 times, but only 2 WillOnce()s are specified"
+ " - returning default value."));
+ EXPECT_THAT(output2.c_str(),
+ HasSubstr("Actions ran out in EXPECT_CALL(b, DoB())...\n"
+ "Called 4 times, but only 2 WillOnce()s are specified"
+ " - returning default value."));
}
-#endif // 0
+#endif // GTEST_HAS_STREAM_REDIRECTION_
// Tests that the WillRepeatedly() action is taken when the WillOnce(...)
// list is exhausted.
@@ -978,8 +983,6 @@ TEST(UnexpectedCallTest, UnmatchedArguments) {
b.DoB(1);
}
-#ifdef GMOCK_HAS_REGEX
-
// Tests that Google Mock explains that an expectation with
// unsatisfied pre-requisites doesn't match the call.
TEST(UnexpectedCallTest, UnsatisifiedPrerequisites) {
@@ -1010,31 +1013,33 @@ TEST(UnexpectedCallTest, UnsatisifiedPrerequisites) {
// Verifies that the failure message contains the two unsatisfied
// pre-requisites but not the satisfied one.
- const char* const pattern =
-#if GMOCK_USES_PCRE
+#if GTEST_USES_PCRE
+ EXPECT_THAT(r.message(), ContainsRegex(
// PCRE has trouble using (.|\n) to match any character, but
// supports the (?s) prefix for using . to match any character.
"(?s)the following immediate pre-requisites are not satisfied:\n"
".*: pre-requisite #0\n"
- ".*: pre-requisite #1";
-#else
+ ".*: pre-requisite #1"));
+#elif GTEST_USES_POSIX_RE
+ EXPECT_THAT(r.message(), ContainsRegex(
// POSIX RE doesn't understand the (?s) prefix, but has no trouble
// with (.|\n).
"the following immediate pre-requisites are not satisfied:\n"
"(.|\n)*: pre-requisite #0\n"
- "(.|\n)*: pre-requisite #1";
-#endif // GMOCK_USES_PCRE
+ "(.|\n)*: pre-requisite #1"));
+#else
+ // We can only use Google Test's own simple regex.
+ EXPECT_THAT(r.message(), ContainsRegex(
+ "the following immediate pre-requisites are not satisfied:"));
+ EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #0"));
+ EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #1"));
+#endif // GTEST_USES_PCRE
- EXPECT_TRUE(
- ::testing::internal::RE::PartialMatch(r.message(), pattern))
- << " where the message is " << r.message();
b.DoB(1);
b.DoB(3);
b.DoB(4);
}
-#endif // GMOCK_HAS_REGEX
-
TEST(UndefinedReturnValueTest, ReturnValueIsMandatory) {
MockA a;
// TODO(wan@google.com): We should really verify the output message,
@@ -1778,16 +1783,15 @@ class MockC {
GTEST_DISALLOW_COPY_AND_ASSIGN_(MockC);
};
-// TODO(wan@google.com): find a way to re-enable these tests.
-#if 0
+#if GTEST_HAS_STREAM_REDIRECTION_
// Tests that an uninteresting mock function call generates a warning
// containing the stack trace.
TEST(FunctionCallMessageTest, UninterestingCallGeneratesFyiWithStackTrace) {
MockC c;
- CaptureTestStdout();
+ CaptureStdout();
c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable());
- const string& output = GetCapturedTestStdout();
+ const String output = GetCapturedStdout();
EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", output);
EXPECT_PRED_FORMAT2(IsSubstring, "Stack trace:", output);
#ifndef NDEBUG
@@ -1796,14 +1800,14 @@ TEST(FunctionCallMessageTest, UninterestingCallGeneratesFyiWithStackTrace) {
// Verifies that a void mock function's name appears in the stack
// trace.
- EXPECT_PRED_FORMAT2(IsSubstring, "::MockC::VoidMethod(", output);
+ EXPECT_PRED_FORMAT2(IsSubstring, "VoidMethod(", output);
// Verifies that a non-void mock function's name appears in the
// stack trace.
- CaptureTestStdout();
+ CaptureStdout();
c.NonVoidMethod();
- const string& output2 = GetCapturedTestStdout();
- EXPECT_PRED_FORMAT2(IsSubstring, "::MockC::NonVoidMethod(", output2);
+ const String output2 = GetCapturedStdout();
+ EXPECT_PRED_FORMAT2(IsSubstring, "NonVoidMethod(", output2);
#endif // NDEBUG
}
@@ -1812,26 +1816,27 @@ TEST(FunctionCallMessageTest, UninterestingCallGeneratesFyiWithStackTrace) {
TEST(FunctionCallMessageTest, UninterestingCallPrintsArgumentsAndReturnValue) {
// A non-void mock function.
MockB b;
- CaptureTestStdout();
+ CaptureStdout();
b.DoB();
- const string& output1 = GetCapturedTestStdout();
+ const String output1 = GetCapturedStdout();
EXPECT_PRED_FORMAT2(
IsSubstring,
"Uninteresting mock function call - returning default value.\n"
" Function call: DoB()\n"
- " Returns: 0\n", output1);
+ " Returns: 0\n", output1.c_str());
// Makes sure the return value is printed.
// A void mock function.
MockC c;
- CaptureTestStdout();
+ CaptureStdout();
c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable());
- const string& output2 = GetCapturedTestStdout();
- EXPECT_PRED2(RE::PartialMatch, output2,
- "Uninteresting mock function call - returning directly\\.\n"
- " Function call: VoidMethod"
- "\\(false, 5, \"Hi\", NULL, @0x\\w+ "
- "Printable, 4-byte object <0000 0000>\\)");
+ const String output2 = GetCapturedStdout();
+ EXPECT_THAT(output2.c_str(),
+ ContainsRegex(
+ "Uninteresting mock function call - returning directly\\.\n"
+ " Function call: VoidMethod"
+ "\\(false, 5, \"Hi\", NULL, @.+ "
+ "Printable, 4-byte object <0000 0000>\\)"));
// A void function has no return value to print.
}
@@ -1843,18 +1848,21 @@ class GMockVerboseFlagTest : public testing::Test {
// should_print is true, the output should match the given regex and
// contain the given function name in the stack trace. When it's
// false, the output should be empty.)
- void VerifyOutput(const string& output, bool should_print,
- const string& regex,
+ void VerifyOutput(const String& output, bool should_print,
+ const string& expected_substring,
const string& function_name) {
if (should_print) {
- EXPECT_PRED2(RE::PartialMatch, output, regex);
+ EXPECT_THAT(output.c_str(), HasSubstr(expected_substring));
#ifndef NDEBUG
// We check the stack trace content in dbg-mode only, as opt-mode
// may inline the call we are interested in seeing.
- EXPECT_PRED_FORMAT2(IsSubstring, function_name, output);
+ EXPECT_THAT(output.c_str(), HasSubstr(function_name));
+#else
+ // Suppresses 'unused function parameter' warnings.
+ static_cast<void>(function_name);
#endif // NDEBUG
} else {
- EXPECT_EQ("", output);
+ EXPECT_STREQ("", output.c_str());
}
}
@@ -1866,27 +1874,27 @@ class GMockVerboseFlagTest : public testing::Test {
.WillOnce(Return(true));
// A void-returning function.
- CaptureTestStdout();
+ CaptureStdout();
a.DoA(5);
VerifyOutput(
- GetCapturedTestStdout(),
+ GetCapturedStdout(),
should_print,
- "Expected mock function call\\.\n"
- " Function call: DoA\\(5\\)\n"
- "Stack trace:",
- "MockA::DoA");
+ "Mock function call matches EXPECT_CALL(a, DoA(5))...\n"
+ " Function call: DoA(5)\n"
+ "Stack trace:\n",
+ "DoA");
// A non-void-returning function.
- CaptureTestStdout();
+ CaptureStdout();
a.Binary(2, 1);
VerifyOutput(
- GetCapturedTestStdout(),
+ GetCapturedStdout(),
should_print,
- "Expected mock function call\\.\n"
- " Function call: Binary\\(2, 1\\)\n"
+ "Mock function call matches EXPECT_CALL(a, Binary(_, 1))...\n"
+ " Function call: Binary(2, 1)\n"
" Returns: true\n"
- "Stack trace:",
- "MockA::Binary");
+ "Stack trace:\n",
+ "Binary");
}
// Tests how the flag affects uninteresting calls.
@@ -1894,31 +1902,29 @@ class GMockVerboseFlagTest : public testing::Test {
MockA a;
// A void-returning function.
- CaptureTestStdout();
+ CaptureStdout();
a.DoA(5);
VerifyOutput(
- GetCapturedTestStdout(),
+ GetCapturedStdout(),
should_print,
"\nGMOCK WARNING:\n"
- "Uninteresting mock function call - returning directly\\.\n"
- " Function call: DoA\\(5\\)\n"
- "Stack trace:\n"
- "[\\s\\S]*",
- "MockA::DoA");
+ "Uninteresting mock function call - returning directly.\n"
+ " Function call: DoA(5)\n"
+ "Stack trace:\n",
+ "DoA");
// A non-void-returning function.
- CaptureTestStdout();
+ CaptureStdout();
a.Binary(2, 1);
VerifyOutput(
- GetCapturedTestStdout(),
+ GetCapturedStdout(),
should_print,
"\nGMOCK WARNING:\n"
- "Uninteresting mock function call - returning default value\\.\n"
- " Function call: Binary\\(2, 1\\)\n"
+ "Uninteresting mock function call - returning default value.\n"
+ " Function call: Binary(2, 1)\n"
" Returns: false\n"
- "Stack trace:\n"
- "[\\s\\S]*",
- "MockA::Binary");
+ "Stack trace:\n",
+ "Binary");
}
};
@@ -1954,7 +1960,7 @@ TEST_F(GMockVerboseFlagTest, InvalidFlagIsTreatedAsWarning) {
TestUninterestingCall(true);
}
-#endif // 0
+#endif // GTEST_HAS_STREAM_REDIRECTION_
// A helper class that generates a failure when printed. We use it to
// ensure that Google Mock doesn't print a value (even to an internal
diff --git a/testing/gmock/test/gmock_all_test.cc b/testing/gmock/test/gmock_all_test.cc
new file mode 100644
index 0000000..7361259
--- /dev/null
+++ b/testing/gmock/test/gmock_all_test.cc
@@ -0,0 +1,49 @@
+// Copyright 2009, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+// Author: wan@google.com (Zhanyong Wan)
+//
+// Tests for Google C++ Mocking Framework (Google Mock)
+//
+// Sometimes it's desirable to build most of Google Mock's own tests
+// by compiling a single file. This file serves this purpose.
+#include "test/gmock-actions_test.cc"
+#include "test/gmock-cardinalities_test.cc"
+#include "test/gmock-generated-actions_test.cc"
+#include "test/gmock-generated-function-mockers_test.cc"
+#include "test/gmock-generated-internal-utils_test.cc"
+#include "test/gmock-generated-matchers_test.cc"
+#include "test/gmock-internal-utils_test.cc"
+#include "test/gmock-matchers_test.cc"
+#include "test/gmock-more-actions_test.cc"
+#include "test/gmock-nice-strict_test.cc"
+#include "test/gmock-port_test.cc"
+#include "test/gmock-printers_test.cc"
+#include "test/gmock-spec-builders_test.cc"
+#include "test/gmock_test.cc"
diff --git a/testing/gmock/test/gmock_link_test.h b/testing/gmock/test/gmock_link_test.h
index d963590..aa9aab3 100644
--- a/testing/gmock/test/gmock_link_test.h
+++ b/testing/gmock/test/gmock_link_test.h
@@ -185,10 +185,8 @@ using testing::SetErrnoAndReturn;
using testing::Throw;
#endif
-#if GMOCK_HAS_REGEX
using testing::ContainsRegex;
using testing::MatchesRegex;
-#endif
class Interface {
public:
@@ -547,7 +545,6 @@ TEST(LinkTest, TestMatchersFloatingPoint) {
.WillByDefault(Return());
}
-#if GMOCK_HAS_REGEX
// Tests the linkage of the ContainsRegex matcher.
TEST(LinkTest, TestMatcherContainsRegex) {
Mock mock;
@@ -561,7 +558,6 @@ TEST(LinkTest, TestMatcherMatchesRegex) {
ON_CALL(mock, VoidFromString(MatchesRegex(".*"))).WillByDefault(Return());
}
-#endif // GMOCK_HAS_REGEX
// Tests the linkage of the StartsWith, EndsWith, and HasSubstr matchers.
TEST(LinkTest, TestMatchersSubstrings) {