Skip to content

Commit 60517b4

Browse files
ClaytonKnittelcopybara-github
authored andcommitted
Change NearMatcher to support references to the target type.
This allows for matchers like `unordered_elements_are` to support `approx_eq`. PiperOrigin-RevId: 749164688
1 parent 514ac48 commit 60517b4

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

googletest/src/matchers/near_matcher.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
matcher::{Matcher, MatcherBase, MatcherResult},
1818
};
1919
use num_traits::{Float, FloatConst};
20-
use std::fmt::Debug;
20+
use std::{borrow::Borrow, fmt::Debug};
2121

2222
/// Matches a value equal within `max_abs_error` of `expected`.
2323
///
@@ -168,13 +168,13 @@ impl<T: Debug> NearMatcher<T> {
168168
}
169169
}
170170

171-
impl<T: Debug + Float + Copy> Matcher<T> for NearMatcher<T> {
171+
impl<T: Borrow<F> + Debug + Copy, F: Debug + Float> Matcher<T> for NearMatcher<F> {
172172
fn matches(&self, actual: T) -> MatcherResult {
173-
if self.nans_are_equal && self.expected.is_nan() && actual.is_nan() {
173+
if self.nans_are_equal && self.expected.is_nan() && actual.borrow().is_nan() {
174174
return MatcherResult::Match;
175175
}
176176

177-
let delta = actual - self.expected;
177+
let delta = *actual.borrow() - self.expected;
178178
if delta >= -self.max_abs_error && delta <= self.max_abs_error {
179179
MatcherResult::Match
180180
} else {
@@ -347,4 +347,17 @@ mod tests {
347347
fn approx_eq_does_not_match_distant_number() -> Result<()> {
348348
verify_that!(0.0f64, not(approx_eq(1.0f64)))
349349
}
350+
351+
#[test]
352+
fn approx_eq_supports_ref() -> Result<()> {
353+
verify_that!(&0.0f64, approx_eq(0.0f64))
354+
}
355+
356+
#[test]
357+
fn approx_eq_supports_container_matchers() -> Result<()> {
358+
verify_that!(
359+
vec![1., 2., 3.],
360+
unordered_elements_are![approx_eq(2.), approx_eq(3.), approx_eq(1.)]
361+
)
362+
}
350363
}

0 commit comments

Comments
 (0)