Skip to content

Commit 59c5082

Browse files
Merge pull request #259 from google:rename-matcher-result-into-bool
PiperOrigin-RevId: 548142983
2 parents 354ebf3 + e58547a commit 59c5082

File tree

6 files changed

+21
-23
lines changed

6 files changed

+21
-23
lines changed

googletest/src/matcher.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -247,22 +247,20 @@ impl From<bool> for MatcherResult {
247247

248248
impl From<MatcherResult> for bool {
249249
fn from(matcher_result: MatcherResult) -> Self {
250-
match matcher_result {
251-
MatcherResult::Match => true,
252-
MatcherResult::NoMatch => false,
253-
}
250+
matcher_result.is_match()
254251
}
255252
}
256253

257254
impl MatcherResult {
258255
/// Returns `true` if `self` is [`MatcherResult::Match`], otherwise
259256
/// `false`.
260-
///
261-
/// This delegates to `Into<bool>` but coerce the return type to `bool`
262-
/// instead only calling `into()`. This is useful in `if
263-
/// !matcher_result.into()`, which requires a constraint on the result
264-
/// type of `into()` to compile.
265-
pub fn into_bool(self) -> bool {
266-
self.into()
257+
pub fn is_match(self) -> bool {
258+
matches!(self, MatcherResult::Match)
259+
}
260+
261+
/// Returns `true` if `self` is [`MatcherResult::NoMatch`], otherwise
262+
/// `false`.
263+
pub fn is_no_match(self) -> bool {
264+
matches!(self, MatcherResult::NoMatch)
267265
}
268266
}

googletest/src/matchers/all_matcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub mod internal {
109109
let failures = self
110110
.components
111111
.iter()
112-
.filter(|component| !component.matches(actual).into_bool())
112+
.filter(|component| !component.matches(actual).is_match())
113113
.map(|component| component.explain_match(actual))
114114
.collect::<Description>();
115115
if failures.len() == 1 {

googletest/src/matchers/each_matcher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ where
8080

8181
fn matches(&self, actual: &ActualT) -> MatcherResult {
8282
for element in actual {
83-
if !self.inner.matches(element).into_bool() {
83+
if self.inner.matches(element).is_no_match() {
8484
return MatcherResult::NoMatch;
8585
}
8686
}
@@ -90,7 +90,7 @@ where
9090
fn explain_match(&self, actual: &ActualT) -> String {
9191
let mut non_matching_elements = Vec::new();
9292
for (index, element) in actual.into_iter().enumerate() {
93-
if !self.inner.matches(element).into_bool() {
93+
if self.inner.matches(element).is_no_match() {
9494
non_matching_elements.push((index, element, self.inner.explain_match(element)));
9595
}
9696
}

googletest/src/matchers/elements_are_matcher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub mod internal {
122122
fn matches(&self, actual: &ContainerT) -> MatcherResult {
123123
let mut zipped_iterator = zip(actual.into_iter(), self.elements.iter());
124124
for (a, e) in zipped_iterator.by_ref() {
125-
if !e.matches(a).into_bool() {
125+
if e.matches(a).is_no_match() {
126126
return MatcherResult::NoMatch;
127127
}
128128
}
@@ -138,7 +138,7 @@ pub mod internal {
138138
let mut zipped_iterator = zip(actual_iterator, self.elements.iter());
139139
let mut mismatches = Vec::new();
140140
for (idx, (a, e)) in zipped_iterator.by_ref().enumerate() {
141-
if !e.matches(a).into_bool() {
141+
if e.matches(a).is_no_match() {
142142
mismatches.push(format!("element #{idx} is {a:?}, {}", e.explain_match(a)));
143143
}
144144
}

googletest/src/matchers/pointwise_matcher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ pub mod internal {
179179
fn matches(&self, actual: &ContainerT) -> MatcherResult {
180180
let mut zipped_iterator = zip(actual.into_iter(), self.matchers.iter());
181181
for (element, matcher) in zipped_iterator.by_ref() {
182-
if !matcher.matches(element).into_bool() {
182+
if matcher.matches(element).is_no_match() {
183183
return MatcherResult::NoMatch;
184184
}
185185
}
@@ -198,7 +198,7 @@ pub mod internal {
198198
let mut zipped_iterator = zip(actual_iterator, self.matchers.iter());
199199
let mut mismatches = Vec::new();
200200
for (idx, (a, e)) in zipped_iterator.by_ref().enumerate() {
201-
if !e.matches(a).into_bool() {
201+
if e.matches(a).is_no_match() {
202202
mismatches.push(format!("element #{idx} is {a:?}, {}", e.explain_match(a)));
203203
}
204204
}

googletest/src/matchers/unordered_elements_are_matcher.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -655,25 +655,25 @@ pub mod internal {
655655
// than `find_best_match()`.
656656
fn find_unmatchable_elements(&self) -> UnmatchableElements<N> {
657657
let unmatchable_actual =
658-
self.0.iter().map(|row| row.iter().all(|&e| !e.into_bool())).collect();
658+
self.0.iter().map(|row| row.iter().all(|&e| e.is_no_match())).collect();
659659
let mut unmatchable_expected = [false; N];
660660
for (col_idx, expected) in unmatchable_expected.iter_mut().enumerate() {
661-
*expected = self.0.iter().map(|row| row[col_idx]).all(|e| !e.into_bool());
661+
*expected = self.0.iter().map(|row| row[col_idx]).all(|e| e.is_no_match());
662662
}
663663
UnmatchableElements { unmatchable_actual, unmatchable_expected }
664664
}
665665

666666
fn find_unmatched_expected(&self) -> UnmatchableElements<N> {
667667
let mut unmatchable_expected = [false; N];
668668
for (col_idx, expected) in unmatchable_expected.iter_mut().enumerate() {
669-
*expected = self.0.iter().map(|row| row[col_idx]).all(|e| !e.into_bool());
669+
*expected = self.0.iter().map(|row| row[col_idx]).all(|e| e.is_no_match());
670670
}
671671
UnmatchableElements { unmatchable_actual: vec![false; N], unmatchable_expected }
672672
}
673673

674674
fn find_unmatched_actual(&self) -> UnmatchableElements<N> {
675675
let unmatchable_actual =
676-
self.0.iter().map(|row| row.iter().all(|e| !e.into_bool())).collect();
676+
self.0.iter().map(|row| row.iter().all(|e| e.is_no_match())).collect();
677677
UnmatchableElements { unmatchable_actual, unmatchable_expected: [false; N] }
678678
}
679679

@@ -794,7 +794,7 @@ pub mod internal {
794794
if seen[expected_idx] {
795795
continue;
796796
}
797-
if !self.0[actual_idx][expected_idx].into_bool() {
797+
if self.0[actual_idx][expected_idx].is_no_match() {
798798
continue;
799799
}
800800
// There is an edge between `actual_idx` and `expected_idx`.

0 commit comments

Comments
 (0)