Adds mutable_impl() and impl() to PolymorphicMatcher (by Zhanyong Wan); Enables gMock to compile with VC 7.1 (by Vlad Losev).

This commit is contained in:
zhanyong.wan 2009-08-31 23:51:23 +00:00
parent 0ea67f88ae
commit 2b43a9ecd1
5 changed files with 65 additions and 50 deletions

View File

@ -236,6 +236,14 @@ class PolymorphicMatcher {
public:
explicit PolymorphicMatcher(const Impl& impl) : impl_(impl) {}
// Returns a mutable reference to the underlying matcher
// implementation object.
Impl& mutable_impl() { return impl_; }
// Returns an immutable reference to the underlying matcher
// implementation object.
const Impl& impl() const { return impl_; }
template <typename T>
operator Matcher<T>() const {
return Matcher<T>(new MonomorphicImpl<T>(impl_));
@ -273,11 +281,12 @@ class PolymorphicMatcher {
// doesn't need to customize it.
ExplainMatchResultTo(impl_, x, os);
}
private:
const Impl impl_;
};
const Impl impl_;
Impl impl_;
};
// Creates a matcher from its implementation. This is easier to use

View File

@ -75,33 +75,11 @@
namespace testing {
namespace internal {
// For Windows, check the compiler version. At least VS 2005 SP1 is
// For MS Visual C++, check the compiler version. At least VS 2003 is
// required to compile Google Mock.
#if GTEST_OS_WINDOWS
#if _MSC_VER < 1400
#error "At least Visual Studio 2005 SP1 is required to compile Google Mock."
#elif _MSC_VER == 1400
// Unfortunately there is no unique _MSC_VER number for SP1. So for VS 2005
// we have to check if it has SP1 by checking whether a bug fixed in SP1
// is present. The bug in question is
// http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=101702
// where the compiler incorrectly reports sizeof(poiter to an array).
class TestForSP1 {
private: // GCC complains if x_ is used by sizeof before defining it.
static char x_[100];
// VS 2005 RTM incorrectly reports sizeof(&x) as 100, and that value
// is used to trigger 'invalid negative array size' error. If you
// see this error, upgrade to VS 2005 SP1 since Google Mock will not
// compile in VS 2005 RTM.
static char Google_Mock_requires_Visual_Studio_2005_SP1_or_later_to_compile_[
sizeof(&x_) != 100 ? 1 : -1];
};
#endif // _MSC_VER
#endif // GTEST_OS_WINDOWS
#if defined(_MSC_VER) && _MSC_VER < 1310
#error "At least Visual C++ 2003 (7.1) is required to compile Google Mock."
#endif
// Use implicit_cast as a safe version of static_cast or const_cast
// for upcasting in the type hierarchy (i.e. casting a pointer to Foo

View File

@ -55,10 +55,12 @@ namespace {
using ::std::ostream;
#ifdef _WIN32_WCE
#ifdef _WIN32_WCE // Windows CE does not define _snprintf_s.
#define snprintf _snprintf
#elif GTEST_OS_WINDOWS
#elif _MSC_VER >= 1400 // VC 8.0 and later deprecate snprintf and _snprintf.
#define snprintf _snprintf_s
#elif _MSC_VER
#define snprintf _snprintf
#endif
// Prints a segment of bytes in the given object.

View File

@ -1784,17 +1784,23 @@ TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
// which cannot reference auto variables.
static int n;
n = 5;
EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Gt(10)) << "This should fail.",
// VC++ prior to version 8.0 SP1 has a bug where it will not see any
// functions declared in the namespace scope from within nested classes.
// EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
// namespace-level functions invoked inside them need to be explicitly
// resolved.
EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
"Value of: n\n"
"Expected: is greater than 10\n"
" Actual: 5\n"
"This should fail.");
" Actual: 5");
n = 0;
EXPECT_NONFATAL_FAILURE(EXPECT_THAT(n, AllOf(Le(7), Ge(5))),
"Value of: n\n"
"Expected: (is less than or equal to 7) and "
"(is greater than or equal to 5)\n"
" Actual: 0");
EXPECT_NONFATAL_FAILURE(
EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
"Value of: n\n"
"Expected: (is less than or equal to 7) and "
"(is greater than or equal to 5)\n"
" Actual: 0");
}
// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
@ -1805,11 +1811,11 @@ TEST(MatcherAssertionTest, WorksForByRefArguments) {
static int n;
n = 0;
EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))),
EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
"Value of: n\n"
"Expected: does not reference the variable @");
// Tests the "Actual" part.
EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))),
EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
"Actual: 0 (is located @");
}
@ -2745,7 +2751,6 @@ TEST(ResultOfTest, WorksForReferencingCallables) {
EXPECT_FALSE(matcher3.Matches(n2));
}
class DivisibleByImpl {
public:
explicit DivisibleByImpl(int divider) : divider_(divider) {}
@ -2763,9 +2768,11 @@ class DivisibleByImpl {
*os << "is not divisible by " << divider_;
}
void set_divider(int divider) { divider_ = divider; }
int divider() const { return divider_; }
private:
const int divider_;
int divider_;
};
// For testing using ExplainMatchResultTo() with polymorphic matchers.
@ -2859,6 +2866,7 @@ TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
EXPECT_TRUE(m.Matches(n2));
}
#if GTEST_HAS_TYPED_TEST
// Tests ContainerEq with different container types, and
// different element types.
@ -2927,6 +2935,7 @@ TYPED_TEST(ContainerEqTest, DuplicateDifference) {
// But in any case there should be no explanation.
EXPECT_EQ("", Explain(m, test_set));
}
#endif // GTEST_HAS_TYPED_TEST
// Tests that mutliple missing values are reported.
// Using just vector here, so order is predicatble.
@ -3345,5 +3354,22 @@ TEST(FormatMatcherDescriptionTest,
Strings(params, params + 1)));
}
// Tests PolymorphicMatcher::mutable_impl().
TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
DivisibleByImpl& impl = m.mutable_impl();
EXPECT_EQ(42, impl.divider());
impl.set_divider(0);
EXPECT_EQ(0, m.mutable_impl().divider());
}
// Tests PolymorphicMatcher::impl().
TEST(PolymorphicMatcherTest, CanAccessImpl) {
const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
const DivisibleByImpl& impl = m.impl();
EXPECT_EQ(42, impl.divider());
}
} // namespace gmock_matchers_test
} // namespace testing

View File

@ -1432,7 +1432,7 @@ TEST(AfterTest, SucceedsWhenTotalOrderIsSatisfied) {
#if GTEST_HAS_DEATH_TEST
// Calls must be in strict order when specified so.
TEST(AfterTest, CallsMustBeInStrictOrderWhenSpecifiedSo) {
TEST(AfterDeathTest, CallsMustBeInStrictOrderWhenSpecifiedSo) {
MockA a;
MockB b;
Expectation e1 = EXPECT_CALL(a, DoA(1));
@ -1454,17 +1454,17 @@ TEST(AfterTest, CallsMustBeInStrictOrderWhenSpecifiedSo) {
// gtest and gmock print messages to stdout, which isn't captured by
// death tests. Therefore we have to match with an empty regular
// expression in all the EXPECT_DEATH()s.
EXPECT_DEATH(a.ReturnResult(2), "");
EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(2), "");
b.DoB();
EXPECT_DEATH(a.ReturnResult(2), "");
EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(2), "");
b.DoB();
a.ReturnResult(2);
}
// Calls must satisfy the partial order when specified so.
TEST(AfterTest, CallsMustSatisfyPartialOrderWhenSpecifiedSo) {
TEST(AfterDeathTest, CallsMustSatisfyPartialOrderWhenSpecifiedSo) {
MockA a;
Expectation e = EXPECT_CALL(a, DoA(1));
const ExpectationSet es = EXPECT_CALL(a, DoA(2));
@ -1472,17 +1472,17 @@ TEST(AfterTest, CallsMustSatisfyPartialOrderWhenSpecifiedSo) {
.After(e, es)
.WillOnce(Return(Result()));
EXPECT_DEATH(a.ReturnResult(3), "");
EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(3), "");
a.DoA(2);
EXPECT_DEATH(a.ReturnResult(3), "");
EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(3), "");
a.DoA(1);
a.ReturnResult(3);
}
// .After() can be combined with .InSequence().
TEST(AfterTest, CanBeUsedWithInSequence) {
TEST(AfterDeathTest, CanBeUsedWithInSequence) {
MockA a;
Sequence s;
Expectation e = EXPECT_CALL(a, DoA(1));
@ -1492,7 +1492,7 @@ TEST(AfterTest, CanBeUsedWithInSequence) {
.WillOnce(Return(Result()));
a.DoA(1);
EXPECT_DEATH(a.ReturnResult(3), "");
EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(3), "");
a.DoA(2);
a.ReturnResult(3);
@ -1551,7 +1551,7 @@ TEST(AfterTest, AcceptsDuplicatedInput) {
.WillOnce(Return(Result()));
a.DoA(1);
EXPECT_DEATH(a.ReturnResult(3), "");
EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(3), "");
a.DoA(2);
a.ReturnResult(3);