Implements .With() as a synonym of .WithArguments(); implements AllArgs(m) as a synonym of m; relies on gtest-port to #include tuple; fixes a compatibility with Symbian.

This commit is contained in:
zhanyong.wan 2009-06-09 06:09:53 +00:00
parent 2661c6821a
commit bf55085d45
11 changed files with 176 additions and 101 deletions

View File

@ -2354,6 +2354,16 @@ inline bool Value(const T& value, M matcher) {
return testing::Matches(matcher)(value);
}
// AllArgs(m) is a synonym of m. This is useful in
//
// EXPECT_CALL(foo, Bar(_, _)).With(AllArgs(Eq()));
//
// which is easier to read than
//
// EXPECT_CALL(foo, Bar(_, _)).With(Eq());
template <typename InnerMatcher>
inline InnerMatcher AllArgs(const InnerMatcher& matcher) { return matcher; }
// These macros allow using matchers to check values in Google Test
// tests. ASSERT_THAT(value, matcher) and EXPECT_THAT(value, matcher)
// succeed iff the value matches the matcher. If the assertion fails,

View File

@ -37,16 +37,16 @@
// a mock method. The syntax is:
//
// ON_CALL(mock_object, Method(argument-matchers))
// .WithArguments(multi-argument-matcher)
// .With(multi-argument-matcher)
// .WillByDefault(action);
//
// where the .WithArguments() clause is optional.
// where the .With() clause is optional.
//
// A user can use the EXPECT_CALL() macro to specify an expectation on
// a mock method. The syntax is:
//
// EXPECT_CALL(mock_object, Method(argument-matchers))
// .WithArguments(multi-argument-matchers)
// .With(multi-argument-matchers)
// .Times(cardinality)
// .InSequence(sequences)
// .WillOnce(action)
@ -144,31 +144,39 @@ class DefaultActionSpec {
// bug in Symbian's C++ compiler (cannot decide between two
// overloaded constructors of Matcher<const ArgumentTuple&>).
extra_matcher_(A<const ArgumentTuple&>()),
last_clause_(NONE) {
last_clause_(kNone) {
}
// Where in the source file was the default action spec defined?
const char* file() const { return file_; }
int line() const { return line_; }
// Implements the .WithArguments() clause.
DefaultActionSpec& WithArguments(const Matcher<const ArgumentTuple&>& m) {
// Implements the .With() clause.
DefaultActionSpec& With(const Matcher<const ArgumentTuple&>& m) {
// Makes sure this is called at most once.
ExpectSpecProperty(last_clause_ < WITH_ARGUMENTS,
".WithArguments() cannot appear "
ExpectSpecProperty(last_clause_ < kWith,
".With() cannot appear "
"more than once in an ON_CALL().");
last_clause_ = WITH_ARGUMENTS;
last_clause_ = kWith;
extra_matcher_ = m;
return *this;
}
// Implements the .WithArguments() clause as a synonym of .With()
// for backward compatibility. WithArguments() is deprecated and
// new code should always use With(), as .With(Args<1, 2>(m)) is
// clearer than .WithArguments(Args<1, 2>(m)).
DefaultActionSpec& WithArguments(const Matcher<const ArgumentTuple&>& m) {
return With(m);
}
// Implements the .WillByDefault() clause.
DefaultActionSpec& WillByDefault(const Action<F>& action) {
ExpectSpecProperty(last_clause_ < WILL_BY_DEFAULT,
ExpectSpecProperty(last_clause_ < kWillByDefault,
".WillByDefault() must appear "
"exactly once in an ON_CALL().");
last_clause_ = WILL_BY_DEFAULT;
last_clause_ = kWillByDefault;
ExpectSpecProperty(!action.IsDoDefault(),
"DoDefault() cannot be used in ON_CALL().");
@ -183,7 +191,7 @@ class DefaultActionSpec {
// Returns the action specified by the user.
const Action<F>& GetAction() const {
AssertSpecProperty(last_clause_ == WILL_BY_DEFAULT,
AssertSpecProperty(last_clause_ == kWillByDefault,
".WillByDefault() must appear exactly "
"once in an ON_CALL().");
return action_;
@ -193,9 +201,9 @@ class DefaultActionSpec {
enum Clause {
// Do not change the order of the enum members! The run-time
// syntax checking relies on it.
NONE,
WITH_ARGUMENTS,
WILL_BY_DEFAULT,
kNone,
kWith,
kWillByDefault,
};
// Asserts that the ON_CALL() statement has a certain property.
@ -211,7 +219,7 @@ class DefaultActionSpec {
// The information in statement
//
// ON_CALL(mock_object, Method(matchers))
// .WithArguments(multi-argument-matcher)
// .With(multi-argument-matcher)
// .WillByDefault(action);
//
// is recorded in the data members like this:
@ -228,7 +236,7 @@ class DefaultActionSpec {
Action<F> action_;
// The last clause in the ON_CALL() statement as seen so far.
// Initially NONE and changes as the statement is parsed.
// Initially kNone and changes as the statement is parsed.
Clause last_clause_;
}; // class DefaultActionSpec
@ -437,13 +445,13 @@ class ExpectationBase {
enum Clause {
// Don't change the order of the enum members!
NONE,
WITH_ARGUMENTS,
TIMES,
IN_SEQUENCE,
WILL_ONCE,
WILL_REPEATEDLY,
RETIRES_ON_SATURATION,
kNone,
kWith,
kTimes,
kInSequence,
kWillOnce,
kWillRepeatedly,
kRetiresOnSaturation,
};
// Asserts that the EXPECT_CALL() statement has the given property.
@ -581,7 +589,7 @@ class Expectation : public ExpectationBase {
repeated_action_specified_(false),
repeated_action_(DoDefault()),
retires_on_saturation_(false),
last_clause_(NONE),
last_clause_(kNone),
action_count_checked_(false) {}
virtual ~Expectation() {
@ -590,36 +598,42 @@ class Expectation : public ExpectationBase {
CheckActionCountIfNotDone();
}
// Implements the .WithArguments() clause.
Expectation& WithArguments(const Matcher<const ArgumentTuple&>& m) {
if (last_clause_ == WITH_ARGUMENTS) {
// Implements the .With() clause.
Expectation& With(const Matcher<const ArgumentTuple&>& m) {
if (last_clause_ == kWith) {
ExpectSpecProperty(false,
".WithArguments() cannot appear "
".With() cannot appear "
"more than once in an EXPECT_CALL().");
} else {
ExpectSpecProperty(last_clause_ < WITH_ARGUMENTS,
".WithArguments() must be the first "
ExpectSpecProperty(last_clause_ < kWith,
".With() must be the first "
"clause in an EXPECT_CALL().");
}
last_clause_ = WITH_ARGUMENTS;
last_clause_ = kWith;
extra_matcher_ = m;
return *this;
}
// Implements the .WithArguments() clause as a synonym of .With().
// This is deprecated and new code should always use With().
Expectation& WithArguments(const Matcher<const ArgumentTuple&>& m) {
return With(m);
}
// Implements the .Times() clause.
Expectation& Times(const Cardinality& cardinality) {
if (last_clause_ ==TIMES) {
if (last_clause_ ==kTimes) {
ExpectSpecProperty(false,
".Times() cannot appear "
"more than once in an EXPECT_CALL().");
} else {
ExpectSpecProperty(last_clause_ < TIMES,
ExpectSpecProperty(last_clause_ < kTimes,
".Times() cannot appear after "
".InSequence(), .WillOnce(), .WillRepeatedly(), "
"or .RetiresOnSaturation().");
}
last_clause_ = TIMES;
last_clause_ = kTimes;
ExpectationBase::SpecifyCardinality(cardinality);
return *this;
@ -632,11 +646,11 @@ class Expectation : public ExpectationBase {
// Implements the .InSequence() clause.
Expectation& InSequence(const Sequence& s) {
ExpectSpecProperty(last_clause_ <= IN_SEQUENCE,
ExpectSpecProperty(last_clause_ <= kInSequence,
".InSequence() cannot appear after .WillOnce(),"
" .WillRepeatedly(), or "
".RetiresOnSaturation().");
last_clause_ = IN_SEQUENCE;
last_clause_ = kInSequence;
s.AddExpectation(owner_->GetLinkedExpectationBase(this));
return *this;
@ -660,10 +674,10 @@ class Expectation : public ExpectationBase {
// Implements the .WillOnce() clause.
Expectation& WillOnce(const Action<F>& action) {
ExpectSpecProperty(last_clause_ <= WILL_ONCE,
ExpectSpecProperty(last_clause_ <= kWillOnce,
".WillOnce() cannot appear after "
".WillRepeatedly() or .RetiresOnSaturation().");
last_clause_ = WILL_ONCE;
last_clause_ = kWillOnce;
actions_.push_back(action);
if (!cardinality_specified()) {
@ -674,16 +688,16 @@ class Expectation : public ExpectationBase {
// Implements the .WillRepeatedly() clause.
Expectation& WillRepeatedly(const Action<F>& action) {
if (last_clause_ == WILL_REPEATEDLY) {
if (last_clause_ == kWillRepeatedly) {
ExpectSpecProperty(false,
".WillRepeatedly() cannot appear "
"more than once in an EXPECT_CALL().");
} else {
ExpectSpecProperty(last_clause_ < WILL_REPEATEDLY,
ExpectSpecProperty(last_clause_ < kWillRepeatedly,
".WillRepeatedly() cannot appear "
"after .RetiresOnSaturation().");
}
last_clause_ = WILL_REPEATEDLY;
last_clause_ = kWillRepeatedly;
repeated_action_specified_ = true;
repeated_action_ = action;
@ -699,10 +713,10 @@ class Expectation : public ExpectationBase {
// Implements the .RetiresOnSaturation() clause.
Expectation& RetiresOnSaturation() {
ExpectSpecProperty(last_clause_ < RETIRES_ON_SATURATION,
ExpectSpecProperty(last_clause_ < kRetiresOnSaturation,
".RetiresOnSaturation() cannot appear "
"more than once.");
last_clause_ = RETIRES_ON_SATURATION;
last_clause_ = kRetiresOnSaturation;
retires_on_saturation_ = true;
// Now that no more action clauses can be specified, we check
@ -717,7 +731,7 @@ class Expectation : public ExpectationBase {
return matchers_;
}
// Returns the matcher specified by the .WithArguments() clause.
// Returns the matcher specified by the .With() clause.
const Matcher<const ArgumentTuple&>& extra_matcher() const {
return extra_matcher_;
}

View File

@ -39,14 +39,14 @@
// This file implements the following syntax:
//
// ON_CALL(mock_object.Method(...))
// .WithArguments(...) ?
// .With(...) ?
// .WillByDefault(...);
//
// where WithArguments() is optional and WillByDefault() must appear
// exactly once.
// where With() is optional and WillByDefault() must appear exactly
// once.
//
// EXPECT_CALL(mock_object.Method(...))
// .WithArguments(...) ?
// .With(...) ?
// .Times(...) ?
// .InSequence(...) *
// .WillOnce(...) *

View File

@ -47,18 +47,8 @@
// To avoid conditional compilation everywhere, we make it
// gmock-port.h's responsibility to #include the header implementing
// tr1/tuple.
#if defined(__GNUC__) && GTEST_GCC_VER_ >= 40000
// GTEST_GCC_VER_ is defined in gtest-port.h and 40000 corresponds to
// version 4.0.0.
// GCC 4.0+ implements tr1/tuple in the <tr1/tuple> header. This does
// not conform to the TR1 spec, which requires the header to be <tuple>.
#include <tr1/tuple>
#else
// If the compiler is not GCC 4.0+, we assume the user is using a
// spec-conforming TR1 implementation.
#include <tuple>
#endif // __GNUC__
// tr1/tuple. gmock-port.h does this via gtest-port.h, which is
// guaranteed to pull in the tuple header.
#if GTEST_OS_LINUX

View File

@ -131,16 +131,19 @@ void Log(LogSeverity severity, const string& message,
// Ensures that logs from different threads don't interleave.
MutexLock l(&g_log_mutex);
using ::std::cout;
// "using ::std::cout;" doesn't work with Symbian's STLport, where cout is a
// macro.
if (severity == WARNING) {
// Prints a GMOCK WARNING marker to make the warnings easily searchable.
cout << "\nGMOCK WARNING:";
std::cout << "\nGMOCK WARNING:";
}
// Pre-pends a new-line to message if it doesn't start with one.
if (message.empty() || message[0] != '\n') {
cout << "\n";
std::cout << "\n";
}
cout << message;
std::cout << message;
if (stack_frames_to_skip >= 0) {
#ifdef NDEBUG
// In opt mode, we have to be conservative and skip no stack frame.
@ -153,13 +156,13 @@ void Log(LogSeverity severity, const string& message,
// Appends a new-line to message if it doesn't end with one.
if (!message.empty() && *message.rbegin() != '\n') {
cout << "\n";
std::cout << "\n";
}
cout << "Stack trace:\n"
std::cout << "Stack trace:\n"
<< ::testing::internal::GetCurrentOsStackTraceExceptTop(
::testing::UnitTest::GetInstance(), actual_to_skip);
}
cout << ::std::flush;
std::cout << ::std::flush;
}
} // namespace internal

View File

@ -457,7 +457,7 @@ TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
NativeArrayPassedAsPointerAndSize helper;
EXPECT_CALL(helper, Helper(_, _))
.WithArguments(ElementsAre(0, 1));
.With(ElementsAre(0, 1));
helper.Helper(array, 2);
}

View File

@ -62,6 +62,7 @@ namespace gmock_matchers_test {
using std::stringstream;
using std::tr1::make_tuple;
using testing::A;
using testing::AllArgs;
using testing::AllOf;
using testing::An;
using testing::AnyOf;
@ -1689,6 +1690,35 @@ TEST(ValueTest, WorksWithMonomorphicMatcher) {
EXPECT_FALSE(Value(1, ref_n));
}
TEST(AllArgsTest, WorksForTuple) {
EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
}
TEST(AllArgsTest, WorksForNonTuple) {
EXPECT_THAT(42, AllArgs(Gt(0)));
EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
}
class AllArgsHelper {
public:
MOCK_METHOD2(Helper, int(char x, int y));
};
TEST(AllArgsTest, WorksInWithClause) {
AllArgsHelper helper;
ON_CALL(helper, Helper(_, _))
.With(AllArgs(Lt()))
.WillByDefault(Return(1));
EXPECT_CALL(helper, Helper(_, _));
EXPECT_CALL(helper, Helper(_, _))
.With(AllArgs(Gt()))
.WillOnce(Return(2));
EXPECT_EQ(1, helper.Helper('\1', 2));
EXPECT_EQ(2, helper.Helper('a', 1));
}
// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
// matches the matcher.
TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {

View File

@ -72,6 +72,7 @@ using testing::Const;
using testing::DoAll;
using testing::DoDefault;
using testing::GMOCK_FLAG(verbose);
using testing::Gt;
using testing::InSequence;
using testing::Invoke;
using testing::InvokeWithoutArgs;
@ -96,6 +97,7 @@ class MockA {
MOCK_METHOD1(DoA, void(int n)); // NOLINT
MOCK_METHOD1(ReturnResult, Result(int n)); // NOLINT
MOCK_METHOD2(Binary, bool(int x, int y)); // NOLINT
MOCK_METHOD2(ReturnInt, int(int x, int y)); // NOLINT
};
class MockB {
@ -171,25 +173,40 @@ TEST(OnCallSyntaxTest, EvaluatesSecondArgumentOnce) {
// Tests that the syntax of ON_CALL() is enforced at run time.
TEST(OnCallSyntaxTest, WithArgumentsIsOptional) {
TEST(OnCallSyntaxTest, WithIsOptional) {
MockA a;
ON_CALL(a, DoA(5))
.WillByDefault(Return());
ON_CALL(a, DoA(_))
.WithArguments(_)
.With(_)
.WillByDefault(Return());
}
TEST(OnCallSyntaxTest, WithArgumentsCanAppearAtMostOnce) {
TEST(OnCallSyntaxTest, WithCanAppearAtMostOnce) {
MockA a;
EXPECT_NONFATAL_FAILURE({ // NOLINT
ON_CALL(a, ReturnResult(_))
.WithArguments(_)
.WithArguments(_)
.With(_)
.With(_)
.WillByDefault(Return(Result()));
}, ".WithArguments() cannot appear more than once in an ON_CALL()");
}, ".With() cannot appear more than once in an ON_CALL()");
}
TEST(OnCallSyntaxTest, WithArgumentsIsSynonymOfWith) {
MockA a;
ON_CALL(a, ReturnInt(_, _))
.WithArguments(Lt())
.WillByDefault(Return(1));
ON_CALL(a, ReturnInt(_, _))
.WithArguments(Gt())
.WillByDefault(Return(2));
EXPECT_CALL(a, ReturnInt(_, _))
.Times(AnyNumber());
EXPECT_EQ(1, a.ReturnInt(1, 2));
EXPECT_EQ(2, a.ReturnInt(2, 1));
}
#if GTEST_HAS_DEATH_TEST
@ -237,51 +254,61 @@ TEST(ExpectCallSyntaxTest, EvaluatesSecondArgumentOnce) {
// Tests that the syntax of EXPECT_CALL() is enforced at run time.
TEST(ExpectCallSyntaxTest, WithArgumentsIsOptional) {
TEST(ExpectCallSyntaxTest, WithIsOptional) {
MockA a;
EXPECT_CALL(a, DoA(5))
.Times(0);
EXPECT_CALL(a, DoA(6))
.WithArguments(_)
.With(_)
.Times(0);
}
TEST(ExpectCallSyntaxTest, WithArgumentsCanAppearAtMostOnce) {
TEST(ExpectCallSyntaxTest, WithCanAppearAtMostOnce) {
MockA a;
EXPECT_NONFATAL_FAILURE({ // NOLINT
EXPECT_CALL(a, DoA(6))
.WithArguments(_)
.WithArguments(_);
}, ".WithArguments() cannot appear more than once in "
"an EXPECT_CALL()");
.With(_)
.With(_);
}, ".With() cannot appear more than once in an EXPECT_CALL()");
a.DoA(6);
}
TEST(ExpectCallSyntaxTest, WithArgumentsMustBeFirstClause) {
TEST(ExpectCallSyntaxTest, WithMustBeFirstClause) {
MockA a;
EXPECT_NONFATAL_FAILURE({ // NOLINT
EXPECT_CALL(a, DoA(1))
.Times(1)
.WithArguments(_);
}, ".WithArguments() must be the first clause in an "
"EXPECT_CALL()");
.With(_);
}, ".With() must be the first clause in an EXPECT_CALL()");
a.DoA(1);
EXPECT_NONFATAL_FAILURE({ // NOLINT
EXPECT_CALL(a, DoA(2))
.WillOnce(Return())
.WithArguments(_);
}, ".WithArguments() must be the first clause in an "
"EXPECT_CALL()");
.With(_);
}, ".With() must be the first clause in an EXPECT_CALL()");
a.DoA(2);
}
TEST(ExpectCallSyntaxTest, WithArgumentsIsSynonymOfWith) {
MockA a;
EXPECT_CALL(a, ReturnInt(_, _))
.WithArguments(Lt())
.WillOnce(Return(1));
EXPECT_CALL(a, ReturnInt(_, _))
.WithArguments(Gt())
.WillOnce(Return(2));
EXPECT_EQ(1, a.ReturnInt(1, 2));
EXPECT_EQ(2, a.ReturnInt(2, 1));
}
TEST(ExpectCallSyntaxTest, TimesCanBeInferred) {
MockA a;

View File

@ -120,6 +120,7 @@
#include <errno.h>
#endif
#include <gmock/internal/gmock-port.h>
#include <gtest/gtest.h>
#include <iostream>
#include <vector>

View File

@ -177,19 +177,19 @@ TEST_F(GMockOutputTest, MismatchArguments) {
foo_.Bar(s, 0, 0);
}
TEST_F(GMockOutputTest, MismatchWithArguments) {
TEST_F(GMockOutputTest, MismatchWith) {
EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
.WithArguments(Ge());
.With(Ge());
foo_.Bar2(2, 3); // Mismatch WithArguments()
foo_.Bar2(2, 3); // Mismatch With()
foo_.Bar2(2, 1);
}
TEST_F(GMockOutputTest, MismatchArgumentsAndWithArguments) {
TEST_F(GMockOutputTest, MismatchArgumentsAndWith) {
EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
.WithArguments(Ge());
.With(Ge());
foo_.Bar2(1, 3); // Mismatch arguments and mismatch WithArguments()
foo_.Bar2(1, 3); // Mismatch arguments and mismatch With()
foo_.Bar2(2, 1);
}

View File

@ -174,7 +174,7 @@ FILE:#:
Expected: to be called once
Actual: never called - unsatisfied and active
[ FAILED ] GMockOutputTest.MismatchArguments
[ RUN ] GMockOutputTest.MismatchWithArguments
[ RUN ] GMockOutputTest.MismatchWith
unknown file: Failure
Unexpected mock function call - returning default value.
@ -187,8 +187,8 @@ FILE:#:
Actual: don't match
Expected: to be called once
Actual: never called - unsatisfied and active
[ FAILED ] GMockOutputTest.MismatchWithArguments
[ RUN ] GMockOutputTest.MismatchArgumentsAndWithArguments
[ FAILED ] GMockOutputTest.MismatchWith
[ RUN ] GMockOutputTest.MismatchArgumentsAndWith
unknown file: Failure
Unexpected mock function call - returning default value.
@ -203,7 +203,7 @@ FILE:#:
Actual: don't match
Expected: to be called once
Actual: never called - unsatisfied and active
[ FAILED ] GMockOutputTest.MismatchArgumentsAndWithArguments
[ FAILED ] GMockOutputTest.MismatchArgumentsAndWith
[ RUN ] GMockOutputTest.UnexpectedCallWithDefaultAction
unknown file: Failure
@ -290,8 +290,8 @@ Stack trace:
[ FAILED ] GMockOutputTest.UnsatisfiedPrerequisites
[ FAILED ] GMockOutputTest.UnsatisfiedExpectation
[ FAILED ] GMockOutputTest.MismatchArguments
[ FAILED ] GMockOutputTest.MismatchWithArguments
[ FAILED ] GMockOutputTest.MismatchArgumentsAndWithArguments
[ FAILED ] GMockOutputTest.MismatchWith
[ FAILED ] GMockOutputTest.MismatchArgumentsAndWith
[ FAILED ] GMockOutputTest.UnexpectedCallWithDefaultAction
[ FAILED ] GMockOutputTest.ExcessiveCallWithDefaultAction