FloatingEqMatcher supports conversion to const float types

This commit is contained in:
Nicholas Brekhus 2024-02-08 12:23:48 -08:00
parent b75ecf1bed
commit 8ffc42998b
2 changed files with 14 additions and 3 deletions

View File

@ -1761,9 +1761,15 @@ class FloatingEqMatcher {
const FloatType max_abs_error_;
};
// The following 3 type conversion operators allow FloatEq(expected) and
// NanSensitiveFloatEq(expected) to be used as a Matcher<float>, a
// Matcher<const float&>, or a Matcher<float&>, but nothing else.
// The following 4 type conversion operators allow FloatEq(expected) and
// NanSensitiveFloatEq(expected) to be used as a Matcher<const float>,
// a Matcher<float>, a Matcher<const float&>, or a Matcher<float&>,
// but nothing else.
operator Matcher<const FloatType>() const {
return MakeMatcher(
new Impl<const FloatType>(expected_, nan_eq_nan_, max_abs_error_));
}
operator Matcher<FloatType>() const {
return MakeMatcher(
new Impl<FloatType>(expected_, nan_eq_nan_, max_abs_error_));

View File

@ -696,6 +696,11 @@ TEST(OptionalTest, DescribesSelf) {
EXPECT_EQ("value is equal to 1", Describe(m));
}
TEST(OptionalTest, WorksWithConstDouble) {
const SampleOptional<const double> opt(3.14159);
EXPECT_THAT(opt, Optional(DoubleEq(3.14159)));
}
TEST(OptionalTest, ExplainsSelf) {
const Matcher<SampleOptional<int>> m = Optional(Eq(1));
EXPECT_EQ("whose value 1 matches", Explain(m, SampleOptional<int>(1)));