Skip to content

Commit 3284d74

Browse files
bjacotgcopybara-github
authored andcommitted
Automatic code cleanup.
PiperOrigin-RevId: 517917647
1 parent 1c8b92d commit 3284d74

22 files changed

+70
-88
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ struct MyEqMatcher<T> {
140140

141141
impl<T: PartialEq + Debug> Matcher<T> for MyEqMatcher<T> {
142142
fn matches(&self, actual: &A) -> MatcherResult {
143-
if self.expected == *actual { MatcherResult::Matches } else { MatcherResult::DoesNotMatch }
143+
(self.expected == *actual).into()
144144
}
145145

146146
fn describe(&self, matcher_result: MatcherResult) -> String {

googletest/src/matcher.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,33 @@ pub enum MatcherResult {
162162
DoesNotMatch,
163163
}
164164

165-
impl MatcherResult {
166-
/// Returns `matches_value` if the `self` is `Matches`, otherwise
167-
/// `does_not_match_value`.
168-
///
169-
/// This is a helper method to simplify `Describe` implementation.
170-
pub fn pick<T>(&self, matches_value: T, does_not_match_value: T) -> T {
171-
match self {
172-
MatcherResult::Matches => matches_value,
173-
MatcherResult::DoesNotMatch => does_not_match_value,
165+
impl From<bool> for MatcherResult {
166+
fn from(b: bool) -> Self {
167+
if b { MatcherResult::Matches } else { MatcherResult::DoesNotMatch }
168+
}
169+
}
170+
171+
impl From<MatcherResult> for bool {
172+
fn from(matcher_result: MatcherResult) -> Self {
173+
match matcher_result {
174+
MatcherResult::Matches => true,
175+
MatcherResult::DoesNotMatch => false,
174176
}
175177
}
176178
}
177179

180+
impl MatcherResult {
181+
/// Returns `true` if `self` is [`MatcherResult::Matches`], otherwise
182+
/// `false`.
183+
///
184+
/// This delegates to `Into<bool>` but coerce the return type to `bool`
185+
/// instead only calling `into()`. This is useful in `if
186+
/// !matcher_result.into()`, which requires a constraint on the result
187+
/// type of `into()` to compile.
188+
pub fn into_bool(self) -> bool {
189+
self.into()
190+
}
191+
}
178192
/// Human-readable explanation of why a value was matched or not matched by a
179193
/// matcher.
180194
///

googletest/src/matchers/all_matcher.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ pub mod internal {
105105
let failures = self
106106
.components
107107
.iter()
108-
.filter(|component| {
109-
matches!(component.matches(actual), MatcherResult::DoesNotMatch)
110-
})
108+
.filter(|component| !component.matches(actual).into_bool())
111109
.map(|component| format!("{}", component.explain_match(actual)))
112110
.collect::<Description>();
113111
if failures.len() == 1 {
@@ -133,10 +131,11 @@ pub mod internal {
133131
.indent();
134132
format!(
135133
"{}:\n{properties}",
136-
matcher_result.pick(
137-
"has all the following properties",
134+
if matcher_result.into() {
135+
"has all the following properties"
136+
} else {
138137
"has at least one of the following properties"
139-
)
138+
}
140139
)
141140
}
142141
}

googletest/src/matchers/container_eq_matcher.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ where
8686
for<'a> &'a ContainerT: IntoIterator<Item = &'a T>,
8787
{
8888
fn matches(&self, actual: &ContainerT) -> MatcherResult {
89-
if *actual == self.expected { MatcherResult::Matches } else { MatcherResult::DoesNotMatch }
89+
(*actual == self.expected).into()
9090
}
9191

9292
fn explain_match(&self, actual: &ContainerT) -> MatchExplanation {
@@ -100,11 +100,7 @@ where
100100

101101
impl<T: PartialEq + Debug, const N: usize> Matcher<Vec<T>> for ContainerEqMatcher<[T; N]> {
102102
fn matches(&self, actual: &Vec<T>) -> MatcherResult {
103-
if actual.as_slice() == self.expected {
104-
MatcherResult::Matches
105-
} else {
106-
MatcherResult::DoesNotMatch
107-
}
103+
(actual.as_slice() == self.expected).into()
108104
}
109105

110106
fn explain_match(&self, actual: &Vec<T>) -> MatchExplanation {
@@ -118,7 +114,7 @@ impl<T: PartialEq + Debug, const N: usize> Matcher<Vec<T>> for ContainerEqMatche
118114

119115
impl<T: PartialEq + Debug, const N: usize> Matcher<[T]> for ContainerEqMatcher<[T; N]> {
120116
fn matches(&self, actual: &[T]) -> MatcherResult {
121-
if actual == self.expected { MatcherResult::Matches } else { MatcherResult::DoesNotMatch }
117+
(actual == self.expected).into()
122118
}
123119

124120
fn explain_match(&self, actual: &[T]) -> MatchExplanation {

googletest/src/matchers/contains_matcher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ where
7777
count.matches(&self.count_matches(actual))
7878
} else {
7979
for v in actual.into_iter() {
80-
if matches!(self.inner.matches(&v), MatcherResult::Matches) {
80+
if self.inner.matches(v).into() {
8181
return MatcherResult::Matches;
8282
}
8383
}
@@ -129,7 +129,7 @@ impl<InnerMatcherT> ContainsMatcher<InnerMatcherT> {
129129
{
130130
let mut count = 0;
131131
for v in actual.into_iter() {
132-
if matches!(self.inner.matches(&v), MatcherResult::Matches) {
132+
if self.inner.matches(v).into() {
133133
count += 1;
134134
}
135135
}

googletest/src/matchers/contains_regex_matcher.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@ pub struct ContainsRegexMatcher {
5454

5555
impl<ActualT: AsRef<str> + Debug + ?Sized> Matcher<ActualT> for ContainsRegexMatcher {
5656
fn matches(&self, actual: &ActualT) -> MatcherResult {
57-
if self.regex.is_match(actual.as_ref()) {
58-
MatcherResult::Matches
59-
} else {
60-
MatcherResult::DoesNotMatch
61-
}
57+
self.regex.is_match(actual.as_ref()).into()
6258
}
6359

6460
fn describe(&self, matcher_result: MatcherResult) -> String {

googletest/src/matchers/each_matcher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ where
6262
{
6363
fn matches(&self, actual: &ActualT) -> MatcherResult {
6464
for element in actual {
65-
if matches!(self.inner.matches(element), MatcherResult::DoesNotMatch) {
65+
if !self.inner.matches(element).into_bool() {
6666
return MatcherResult::DoesNotMatch;
6767
}
6868
}
@@ -72,7 +72,7 @@ where
7272
fn explain_match(&self, actual: &ActualT) -> MatchExplanation {
7373
let mut non_matching_elements = Vec::new();
7474
for (index, element) in actual.into_iter().enumerate() {
75-
if matches!(self.inner.matches(element), MatcherResult::DoesNotMatch) {
75+
if !self.inner.matches(element).into_bool() {
7676
non_matching_elements.push((index, element, self.inner.explain_match(element)));
7777
}
7878
}

googletest/src/matchers/elements_are_matcher.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub mod internal {
100100
fn matches(&self, actual: &ContainerT) -> MatcherResult {
101101
let mut zipped_iterator = zip(actual.into_iter(), self.elements.iter());
102102
for (a, e) in zipped_iterator.by_ref() {
103-
if matches!(e.matches(a), MatcherResult::DoesNotMatch) {
103+
if !e.matches(a).into_bool() {
104104
return MatcherResult::DoesNotMatch;
105105
}
106106
}
@@ -116,7 +116,7 @@ pub mod internal {
116116
let mut zipped_iterator = zip(actual_iterator, self.elements.iter());
117117
let mut mismatches = Vec::new();
118118
for (idx, (a, e)) in zipped_iterator.by_ref().enumerate() {
119-
if matches!(e.matches(a), MatcherResult::DoesNotMatch) {
119+
if !e.matches(a).into_bool() {
120120
mismatches.push(format!("element #{idx} is {a:?}, {}", e.explain_match(a)));
121121
}
122122
}
@@ -141,7 +141,7 @@ pub mod internal {
141141
fn describe(&self, matcher_result: MatcherResult) -> String {
142142
format!(
143143
"{} elements:\n{}",
144-
matcher_result.pick("has", "doesn't have"),
144+
if matcher_result.into() { "has" } else { "doesn't have" },
145145
&self
146146
.elements
147147
.iter()

googletest/src/matchers/empty_matcher.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,11 @@ where
4949
for<'a> &'a T: IntoIterator,
5050
{
5151
fn matches(&self, actual: &T) -> MatcherResult {
52-
if actual.into_iter().next().is_none() {
53-
MatcherResult::Matches
54-
} else {
55-
MatcherResult::DoesNotMatch
56-
}
52+
actual.into_iter().next().is_none().into()
5753
}
5854

5955
fn describe(&self, matcher_result: MatcherResult) -> String {
60-
matcher_result.pick("is empty", "isn't empty").to_string()
56+
if matcher_result.into() { "is empty" } else { "isn't empty" }.to_string()
6157
}
6258
}
6359

googletest/src/matchers/eq_matcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub struct EqMatcher<T> {
6666

6767
impl<A: Debug, T: PartialEq<A> + Debug> Matcher<A> for EqMatcher<T> {
6868
fn matches(&self, actual: &A) -> MatcherResult {
69-
if self.expected == *actual { MatcherResult::Matches } else { MatcherResult::DoesNotMatch }
69+
(self.expected == *actual).into()
7070
}
7171

7272
fn describe(&self, matcher_result: MatcherResult) -> String {

0 commit comments

Comments
 (0)