|
|
|
@ -411,9 +411,27 @@ class IntValue { |
|
|
|
|
int value_; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// For testing casting matchers between compatible types. This is similar to
|
|
|
|
|
// IntValue, but takes a non-const reference to the value, showing MatcherCast
|
|
|
|
|
// works with such types (and doesn't, for example, use a const ref internally).
|
|
|
|
|
class MutableIntView { |
|
|
|
|
public: |
|
|
|
|
// An int& can be statically (although not implicitly) cast to a
|
|
|
|
|
// MutableIntView.
|
|
|
|
|
explicit MutableIntView(int& a_value) : value_(a_value) {} |
|
|
|
|
|
|
|
|
|
int& value() const { return value_; } |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
int& value_; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// For testing casting matchers between compatible types.
|
|
|
|
|
bool IsPositiveIntValue(const IntValue& foo) { return foo.value() > 0; } |
|
|
|
|
|
|
|
|
|
// For testing casting matchers between compatible types.
|
|
|
|
|
bool IsPositiveMutableIntView(MutableIntView foo) { return foo.value() > 0; } |
|
|
|
|
|
|
|
|
|
// Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
|
|
|
|
|
// can be statically converted to U.
|
|
|
|
|
TEST(MatcherCastTest, FromCompatibleType) { |
|
|
|
@ -429,14 +447,34 @@ TEST(MatcherCastTest, FromCompatibleType) { |
|
|
|
|
// predicate.
|
|
|
|
|
EXPECT_TRUE(m4.Matches(1)); |
|
|
|
|
EXPECT_FALSE(m4.Matches(0)); |
|
|
|
|
|
|
|
|
|
Matcher<MutableIntView> m5 = Truly(IsPositiveMutableIntView); |
|
|
|
|
Matcher<int> m6 = MatcherCast<int>(m5); |
|
|
|
|
// In the following, the arguments 1 and 0 are statically converted to
|
|
|
|
|
// MutableIntView objects, and then tested by the IsPositiveMutableIntView()
|
|
|
|
|
// predicate.
|
|
|
|
|
EXPECT_TRUE(m6.Matches(1)); |
|
|
|
|
EXPECT_FALSE(m6.Matches(0)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
|
|
|
|
|
TEST(MatcherCastTest, FromConstReferenceToNonReference) { |
|
|
|
|
Matcher<const int&> m1 = Eq(0); |
|
|
|
|
int n = 0; |
|
|
|
|
Matcher<const int&> m1 = Ref(n); |
|
|
|
|
Matcher<int> m2 = MatcherCast<int>(m1); |
|
|
|
|
EXPECT_TRUE(m2.Matches(0)); |
|
|
|
|
EXPECT_FALSE(m2.Matches(1)); |
|
|
|
|
int n1 = 0; |
|
|
|
|
EXPECT_TRUE(m2.Matches(n)); |
|
|
|
|
EXPECT_FALSE(m2.Matches(n1)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Tests that MatcherCast<T&>(m) works when m is a Matcher<const T&>.
|
|
|
|
|
TEST(MatcherCastTest, FromConstReferenceToReference) { |
|
|
|
|
int n = 0; |
|
|
|
|
Matcher<const int&> m1 = Ref(n); |
|
|
|
|
Matcher<int&> m2 = MatcherCast<int&>(m1); |
|
|
|
|
int n1 = 0; |
|
|
|
|
EXPECT_TRUE(m2.Matches(n)); |
|
|
|
|
EXPECT_FALSE(m2.Matches(n1)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
|
|
|
|
@ -445,6 +483,12 @@ TEST(MatcherCastTest, FromReferenceToNonReference) { |
|
|
|
|
Matcher<int> m2 = MatcherCast<int>(m1); |
|
|
|
|
EXPECT_TRUE(m2.Matches(0)); |
|
|
|
|
EXPECT_FALSE(m2.Matches(1)); |
|
|
|
|
|
|
|
|
|
// Of course, reference identity isn't preserved since a copy is required.
|
|
|
|
|
int n = 0; |
|
|
|
|
Matcher<int&> m3 = Ref(n); |
|
|
|
|
Matcher<int> m4 = MatcherCast<int>(m3); |
|
|
|
|
EXPECT_FALSE(m4.Matches(n)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
|
|
|
|
@ -649,6 +693,16 @@ TEST(SafeMatcherCastTest, FromBaseClass) { |
|
|
|
|
EXPECT_FALSE(m4.Matches(d2)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<const T&>.
|
|
|
|
|
TEST(SafeMatcherCastTest, FromConstReferenceToNonReference) { |
|
|
|
|
int n = 0; |
|
|
|
|
Matcher<const int&> m1 = Ref(n); |
|
|
|
|
Matcher<int> m2 = SafeMatcherCast<int>(m1); |
|
|
|
|
int n1 = 0; |
|
|
|
|
EXPECT_TRUE(m2.Matches(n)); |
|
|
|
|
EXPECT_FALSE(m2.Matches(n1)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
|
|
|
|
|
TEST(SafeMatcherCastTest, FromConstReferenceToReference) { |
|
|
|
|
int n = 0; |
|
|
|
|