simplifies TrulyMatcher and adds a test for it
This commit is contained in:
parent
8d7c5ad6d3
commit
8d3dc0cdd8
@ -1392,15 +1392,15 @@ class TrulyMatcher {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
bool MatchAndExplain(T& x, // NOLINT
|
bool MatchAndExplain(T& x, // NOLINT
|
||||||
MatchResultListener* /* listener */) const {
|
MatchResultListener* /* listener */) const {
|
||||||
#ifdef _MSC_VER
|
// Without the if-statement, MSVC sometimes warns about converting
|
||||||
// MSVC warns about converting a value into bool (warning 4800).
|
// a value to bool (warning 4800).
|
||||||
# pragma warning(push) // Saves the current warning state.
|
//
|
||||||
# pragma warning(disable:4800) // Temporarily disables warning 4800.
|
// We cannot write 'return !!predicate_(x);' as that doesn't work
|
||||||
#endif
|
// when predicate_(x) returns a class convertible to bool but
|
||||||
return predicate_(x);
|
// having no operator!().
|
||||||
#ifdef _MSC_VER
|
if (predicate_(x))
|
||||||
# pragma warning(pop) // Restores the warning state.
|
return true;
|
||||||
#endif
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DescribeTo(::std::ostream* os) const {
|
void DescribeTo(::std::ostream* os) const {
|
||||||
|
@ -2256,6 +2256,29 @@ TEST(TrulyTest, CanBeUsedWithFunctor) {
|
|||||||
EXPECT_FALSE(m.Matches(4));
|
EXPECT_FALSE(m.Matches(4));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A class that can be implicitly converted to bool.
|
||||||
|
class ConvertibleToBool {
|
||||||
|
public:
|
||||||
|
explicit ConvertibleToBool(int number) : number_(number) {}
|
||||||
|
operator bool() const { return number_ != 0; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
int number_;
|
||||||
|
};
|
||||||
|
|
||||||
|
ConvertibleToBool IsNotZero(int number) {
|
||||||
|
return ConvertibleToBool(number);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that the predicate used in Truly() may return a class that's
|
||||||
|
// implicitly convertible to bool, even when the class has no
|
||||||
|
// operator!().
|
||||||
|
TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
|
||||||
|
Matcher<int> m = Truly(IsNotZero);
|
||||||
|
EXPECT_TRUE(m.Matches(1));
|
||||||
|
EXPECT_FALSE(m.Matches(0));
|
||||||
|
}
|
||||||
|
|
||||||
// Tests that Truly(predicate) can describe itself properly.
|
// Tests that Truly(predicate) can describe itself properly.
|
||||||
TEST(TrulyTest, CanDescribeSelf) {
|
TEST(TrulyTest, CanDescribeSelf) {
|
||||||
Matcher<double> m = Truly(IsPositive);
|
Matcher<double> m = Truly(IsPositive);
|
||||||
|
Loading…
Reference in New Issue
Block a user