Skip to content

Commit 84907e4

Browse files
Merge pull request #206 from google:fix-all-matcher-references
PiperOrigin-RevId: 532057117
2 parents 36a4b9a + 93c7823 commit 84907e4

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
lines changed

googletest/src/matchers/all_matcher.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
macro_rules! all {
5555
($($matcher:expr),* $(,)?) => {{
5656
use $crate::matchers::all_matcher::internal::AllMatcher;
57-
AllMatcher::new([$(&$matcher),*])
57+
AllMatcher::new([$(Box::new($matcher)),*])
5858
}}
5959
}
6060

@@ -73,24 +73,24 @@ pub mod internal {
7373
///
7474
/// For internal use only. API stablility is not guaranteed!
7575
#[doc(hidden)]
76-
pub struct AllMatcher<'a, T: Debug + ?Sized, const N: usize> {
77-
components: [&'a dyn Matcher<ActualT = T>; N],
76+
pub struct AllMatcher<T: Debug + ?Sized, const N: usize> {
77+
components: [Box<dyn Matcher<ActualT = T>>; N],
7878
}
7979

80-
impl<'a, T: Debug + ?Sized, const N: usize> AllMatcher<'a, T, N> {
80+
impl<T: Debug + ?Sized, const N: usize> AllMatcher<T, N> {
8181
/// Constructs an [`AllMatcher`] with the given component matchers.
8282
///
8383
/// Intended for use only by the [`all`] macro.
84-
pub fn new(components: [&'a dyn Matcher<ActualT = T>; N]) -> Self {
84+
pub fn new(components: [Box<dyn Matcher<ActualT = T>>; N]) -> Self {
8585
Self { components }
8686
}
8787
}
8888

89-
impl<'a, T: Debug + ?Sized, const N: usize> Matcher for AllMatcher<'a, T, N> {
89+
impl<T: Debug + ?Sized, const N: usize> Matcher for AllMatcher<T, N> {
9090
type ActualT = T;
9191

9292
fn matches(&self, actual: &Self::ActualT) -> MatcherResult {
93-
for component in self.components {
93+
for component in &self.components {
9494
match component.matches(actual) {
9595
MatcherResult::DoesNotMatch => {
9696
return MatcherResult::DoesNotMatch;

googletest/tests/composition_test.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use googletest::prelude::*;
16+
17+
#[test]
18+
fn all_matcher_works_as_inner_matcher() -> Result<()> {
19+
let value = vec![1];
20+
verify_that!(value, contains_each![all!(gt(0), lt(2))])
21+
}
22+
23+
#[test]
24+
fn matches_pattern_works_as_inner_matcher() -> Result<()> {
25+
#[derive(Debug)]
26+
struct AStruct(i32);
27+
verify_that!(vec![AStruct(123)], contains_each![matches_pattern!(AStruct(eq(123)))])
28+
}
29+
30+
#[test]
31+
fn matches_pattern_works_with_property_as_inner_matcher() -> Result<()> {
32+
#[derive(Debug)]
33+
struct AStruct(i32);
34+
impl AStruct {
35+
fn get_value(&self) -> i32 {
36+
self.0
37+
}
38+
}
39+
verify_that!(
40+
vec![AStruct(123)],
41+
contains_each![matches_pattern!(AStruct {
42+
get_value(): eq(123)
43+
})]
44+
)
45+
}
46+
47+
#[test]
48+
fn contains_each_works_as_inner_matcher() -> Result<()> {
49+
#[derive(Debug)]
50+
struct AStruct(Vec<i32>);
51+
verify_that!(AStruct(vec![123]), matches_pattern!(AStruct(contains_each![eq(123)])))
52+
}
53+
54+
#[test]
55+
fn pointwise_works_as_inner_matcher() -> Result<()> {
56+
#[derive(Debug)]
57+
struct AStruct(Vec<i32>);
58+
verify_that!(AStruct(vec![123]), matches_pattern!(AStruct(pointwise!(eq, [123]))))
59+
}
60+
61+
#[test]
62+
fn elements_are_works_as_inner_matcher() -> Result<()> {
63+
#[derive(Debug)]
64+
struct AStruct(Vec<i32>);
65+
verify_that!(AStruct(vec![123]), matches_pattern!(AStruct(elements_are![eq(123)])))
66+
}
67+
68+
#[test]
69+
fn tuple_works_as_inner_matcher() -> Result<()> {
70+
verify_that!(vec![(123,)], elements_are![tuple!(eq(123))])
71+
}

googletest/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
mod all_matcher_test;
16+
mod composition_test;
1617
mod elements_are_matcher_test;
1718
mod field_matcher_test;
1819
mod matches_pattern_test;

0 commit comments

Comments
 (0)