Skip to content

Commit c6303be

Browse files
hovinenbcopybara-github
authored andcommitted
Restructure eq_matcher::create_diff so that it only returns the report on the difference between the strings and not the entire match explanation.
Previously, the method `EqMatcher::explain_match` would defer entirely to `eq_matcher::create_diff`. This made `eq_matcher::create_diff` unusable for `StrMatcher`, where it is intended also to be used. This restructures the function to be reusable by focusing on just the difference between the two input strings. This also replaces the text "Debug diff" in the match explanation output with "Difference", which should be clearer. This also adds a parameter to allow the caller to specify the edit distance mode, which will be needed by `StrMatcher`. PiperOrigin-RevId: 533479136
1 parent 33c86b7 commit c6303be

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

googletest/src/matcher_support/edit_distance.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,17 @@ pub(crate) fn edit_list<T: Distance + Copy>(
105105
}
106106

107107
/// Controls how `right` should match `left`.
108-
#[allow(dead_code)]
109108
pub(crate) enum Mode {
110109
/// `right` is fully matching `left`
111110
FullMatch,
112111
/// `right` should match the beginning of `left`
112+
#[allow(unused)]
113113
StartsWith,
114114
/// `right` should match the end of `left`
115+
#[allow(unused)]
115116
EndsWith,
116117
/// `right` should be contained in `left`
118+
#[allow(unused)]
117119
Contains,
118120
}
119121

googletest/src/matchers/eq_deref_of_matcher.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::matcher::{Matcher, MatcherResult};
16-
use crate::matchers::eq_matcher::create_diff;
15+
use crate::{
16+
matcher::{Matcher, MatcherResult},
17+
matcher_support::edit_distance,
18+
matchers::eq_matcher::create_diff,
19+
};
1720
use std::{fmt::Debug, marker::PhantomData, ops::Deref};
1821

1922
/// Matches a value equal (in the sense of `==`) to the dereferenced value of
@@ -82,10 +85,14 @@ where
8285
}
8386

8487
fn explain_match(&self, actual: &ActualT) -> String {
85-
create_diff(
86-
&format!("{:#?}", self.expected.deref()),
87-
&format!("{:#?}", actual),
88+
format!(
89+
"which {}{}",
8890
&self.describe(self.matches(actual)),
91+
create_diff(
92+
&format!("{:#?}", self.expected.deref()),
93+
&format!("{:#?}", actual),
94+
edit_distance::Mode::FullMatch,
95+
)
8996
)
9097
}
9198
}
@@ -132,7 +139,7 @@ mod tests {
132139
r#"
133140
Actual: Strukt { int: 123, string: "something" },
134141
which isn't equal to Strukt { int: 321, string: "someone" }
135-
Debug diff:
142+
Difference:
136143
Strukt {
137144
+ int: 123,
138145
- int: 321,

googletest/src/matchers/eq_matcher.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use crate::matcher::{Matcher, MatcherResult};
1616
use crate::matcher_support::edit_distance;
17-
use std::{fmt::Debug, marker::PhantomData};
17+
use std::{borrow::Cow, fmt::Debug, marker::PhantomData};
1818

1919
/// Matches a value equal (in the sense of `==`) to `expected`.
2020
///
@@ -95,31 +95,35 @@ impl<A: Debug + ?Sized, T: PartialEq<A> + Debug> Matcher for EqMatcher<A, T> {
9595
}
9696

9797
fn explain_match(&self, actual: &A) -> String {
98-
create_diff(
99-
&format!("{:#?}", self.expected),
100-
&format!("{:#?}", actual),
98+
format!(
99+
"which {}{}",
101100
&self.describe(self.matches(actual)),
101+
create_diff(
102+
&format!("{:#?}", self.expected),
103+
&format!("{:#?}", actual),
104+
edit_distance::Mode::FullMatch,
105+
)
102106
)
103107
}
104108
}
105109

106-
pub(super) fn create_diff(expected_debug: &str, actual_debug: &str, description: &str) -> String {
110+
pub(super) fn create_diff(
111+
expected_debug: &str,
112+
actual_debug: &str,
113+
mode: edit_distance::Mode,
114+
) -> Cow<'static, str> {
107115
if actual_debug.lines().count() < 2 {
108116
// If the actual debug is only one line, then there is no point in doing a
109117
// line-by-line diff.
110-
return format!("which {description}",);
118+
return "".into();
111119
}
112-
let edit_list = edit_distance::edit_list(
113-
actual_debug.lines(),
114-
expected_debug.lines(),
115-
edit_distance::Mode::FullMatch,
116-
);
120+
let edit_list = edit_distance::edit_list(actual_debug.lines(), expected_debug.lines(), mode);
117121

118122
if edit_list.is_empty() {
119-
return format!("which {description}\nNo difference found between debug strings.",);
123+
return "No difference found between debug strings.".into();
120124
}
121125

122-
format!("which {description}\nDebug diff:{}", edit_list_summary(&edit_list))
126+
format!("\nDifference:{}", edit_list_summary(&edit_list)).into()
123127
}
124128

125129
fn edit_list_summary(edit_list: &[edit_distance::Edit<&str>]) -> String {
@@ -197,7 +201,7 @@ mod tests {
197201
r#"
198202
Actual: Strukt { int: 123, string: "something" },
199203
which isn't equal to Strukt { int: 321, string: "someone" }
200-
Debug diff:
204+
Difference:
201205
Strukt {
202206
+ int: 123,
203207
- int: 321,
@@ -219,7 +223,7 @@ mod tests {
219223
Expected: is equal to [1, 3, 4]
220224
Actual: [1, 2, 3],
221225
which isn't equal to [1, 3, 4]
222-
Debug diff:
226+
Difference:
223227
[
224228
1,
225229
+ 2,
@@ -241,7 +245,7 @@ mod tests {
241245
Expected: is equal to [1, 3, 5]
242246
Actual: [1, 2, 3, 4, 5],
243247
which isn't equal to [1, 3, 5]
244-
Debug diff:
248+
Difference:
245249
[
246250
1,
247251
+ 2,

0 commit comments

Comments
 (0)