Skip to content

Commit f4ea561

Browse files
hovinenbcopybara-github
authored andcommitted
Support up to three containers in the pointwise! matcher.
This allows the caller to specify up to three containers to supply values to a lambda expression which generates a matcher: ``` verify_that!(value, pointwise!(near, [1.0, 2.0, 3.0], [0.01, 0.0001, 0.0001]))?; verify_that!(value, pointwise!(|v, t, u| near(v, t * u), [1.0, 2.0, 3.0], [0.01, 0.001, 0.0001], [0.5, 0.5, 1.0]))?; ``` The limit of three was chosen because it's highly uncommon for a (function) matcher to have more than three parameters. PiperOrigin-RevId: 524003958
1 parent cbd6ed7 commit f4ea561

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

googletest/src/matchers/pointwise_matcher.rs

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,28 @@
3636
/// verify_that!(value, pointwise!(|v| near(v, 0.001), [1.0, 2.0, 3.0]))?;
3737
/// ```
3838
///
39+
/// One can pass up to three containers to supply arguments to the function
40+
/// creating the matcher:
41+
///
42+
/// ```
43+
/// let value = vec![1.00001, 2.000001, 3.00001];
44+
/// verify_that!(value, pointwise!(|v, t| near(v, t), [1.0, 2.0, 3.0], [0.001, 0.0001, 0.01]))?;
45+
/// verify_that!(value, pointwise!(near, [1.0, 2.0, 3.0], [0.001, 0.0001, 0.01]))?; // Same as above
46+
/// verify_that!(
47+
/// value,
48+
/// pointwise!(
49+
/// |v, t, u| near(v, t * u),
50+
/// [1.0, 2.0, 3.0],
51+
/// [0.001, 0.0001, 0.01],
52+
/// [0.5, 0.5, 1.0]
53+
/// )
54+
/// )?;
55+
/// ```
56+
///
57+
/// When using `pointwise!` with multiple containers, the caller must ensure
58+
/// that all of the containers have the same size. This matcher does not check
59+
/// whether the sizes match.
60+
///
3961
/// The actual value must be a container implementing [`IntoIterator`]. This
4062
/// includes standard containers, slices (when dereferenced) and arrays.
4163
///
@@ -63,12 +85,40 @@
6385
#[macro_export]
6486
macro_rules! pointwise {
6587
($matcher:expr, $container:expr) => {{
66-
#[cfg(google3)]
67-
use $crate::internal::PointwiseMatcher;
68-
#[cfg(not(google3))]
69-
use $crate::matchers::pointwise_matcher::internal::PointwiseMatcher;
70-
PointwiseMatcher::new($container.into_iter().map($matcher).collect())
71-
}};
88+
#[cfg(google3)]
89+
use $crate::internal::PointwiseMatcher;
90+
#[cfg(not(google3))]
91+
use $crate::matchers::pointwise_matcher::internal::PointwiseMatcher;
92+
PointwiseMatcher::new($container.into_iter().map($matcher).collect())
93+
}};
94+
95+
($matcher:expr, $left_container:expr, $right_container:expr) => {{
96+
#[cfg(google3)]
97+
use $crate::internal::PointwiseMatcher;
98+
#[cfg(not(google3))]
99+
use $crate::matchers::pointwise_matcher::internal::PointwiseMatcher;
100+
PointwiseMatcher::new(
101+
$left_container
102+
.into_iter()
103+
.zip($right_container.into_iter())
104+
.map(|(l, r)| $matcher(l, r))
105+
.collect(),
106+
)
107+
}};
108+
109+
($matcher:expr, $left_container:expr, $middle_container:expr, $right_container:expr) => {{
110+
#[cfg(google3)]
111+
use $crate::internal::PointwiseMatcher;
112+
#[cfg(not(google3))]
113+
use $crate::matchers::pointwise_matcher::internal::PointwiseMatcher;
114+
PointwiseMatcher::new(
115+
$left_container
116+
.into_iter()
117+
.zip($right_container.into_iter().zip($middle_container.into_iter()))
118+
.map(|(l, (m, r))| $matcher(l, m, r))
119+
.collect(),
120+
)
121+
}};
72122
}
73123

74124
/// Module for use only by the procedural macros in this module.

googletest/tests/pointwise_matcher_test.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,18 @@ fn pointwise_matches_single_element_with_lambda_expression_with_extra_value() ->
172172
let value = vec![1.00001f32];
173173
verify_that!(value, pointwise!(|v| near(v, 0.0001), vec![1.0]))
174174
}
175+
176+
#[google_test]
177+
fn pointwise_matches_single_element_with_two_containers() -> Result<()> {
178+
let value = vec![1.00001f32];
179+
verify_that!(value, pointwise!(near, vec![1.0], vec![0.0001]))
180+
}
181+
182+
#[google_test]
183+
fn pointwise_matches_single_element_with_three_containers() -> Result<()> {
184+
let value = vec![1.00001f32];
185+
verify_that!(
186+
value,
187+
pointwise!(|v, t, u| near(v, t * u), vec![1.0f32], vec![0.0001f32], vec![0.5f32])
188+
)
189+
}

0 commit comments

Comments
 (0)