Skip to content

Commit 294678d

Browse files
hovinenbcopybara-github
authored andcommitted
Ensure that the diff output of starts_with contains the last line of the actual value corresponding to the expected value when they are not equal.
Previously, the displayed diff from `starts_with` was suboptimal when the last line of the prefix differs from its corresponding line in the actual value (which occurs often): Actual: ``` First line Second line Third line ``` Prefix: ``` First line Second ``` Diff: ``` First line -Second <---- remaining lines omitted ----> ``` The line ``` +Second line ``` is missing from the diff. This adds a hack which manually introduces the missing line to the diff in the above case. Doing this manually should not result in any incorrect output -- at worst, it could result in an additional line of output which was not strictly necessary. PiperOrigin-RevId: 540592869
1 parent d9717c4 commit 294678d

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

googletest/src/matcher_support/edit_distance.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,20 @@ pub(crate) fn edit_list<T: PartialEq + Copy>(
180180
.filter(|p| p.right_endpoint == right.len())
181181
.max_by(|p1, p2| p1.edits.len().cmp(&p2.edits.len()))
182182
{
183+
// We've reached the end of the right side but there could still be a
184+
// corresponding line on the left which we haven't picked up into the edit list.
185+
// We'll just add it manually to the edit list. There's no real harm doing so --
186+
// worst case is that there's an additional line when there didn't have to be.
187+
if let Some(Edit::ExtraRight(_)) = path.edits.last() {
188+
if path.left_endpoint < left.len() {
189+
// The edits from the left should come before the corresponding one from the
190+
// right, so we insert rather than push.
191+
path.edits.insert(
192+
path.edits.len() - 1,
193+
Edit::ExtraLeft(left[path.left_endpoint]),
194+
);
195+
}
196+
}
183197
path.edits.push(Edit::AdditionalLeft);
184198
return if path.edits.iter().any(|v| !matches!(v, Edit::Both(_))) {
185199
Difference::Editable(std::mem::take(&mut path.edits))
@@ -447,6 +461,20 @@ mod tests {
447461
)
448462
}
449463

464+
#[test]
465+
fn does_not_skip_corresponding_line_on_left_when_left_and_right_differ_in_prefix_mode()
466+
-> Result<()> {
467+
let result = edit_list(["Left only"], ["Right only"], Mode::Prefix);
468+
verify_that!(
469+
result,
470+
matches_pattern!(Difference::Editable(elements_are![
471+
matches_pattern!(Edit::ExtraLeft(eq("Left only"))),
472+
matches_pattern!(Edit::ExtraRight(eq("Right only"))),
473+
matches_pattern!(Edit::AdditionalLeft),
474+
]))
475+
)
476+
}
477+
450478
#[test]
451479
fn returns_unrelated_when_maximum_distance_exceeded() -> Result<()> {
452480
let result = edit_list(0..=20, 20..40, Mode::Exact);

googletest/src/matchers/str_matcher.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,38 @@ mod tests {
10101010
)
10111011
}
10121012

1013+
#[test]
1014+
fn match_explanation_for_starts_with_includes_both_versions_of_differing_last_line()
1015+
-> Result<()> {
1016+
let result = verify_that!(
1017+
indoc!(
1018+
"
1019+
First line
1020+
Second line
1021+
Third line
1022+
"
1023+
),
1024+
starts_with(indoc!(
1025+
"
1026+
First line
1027+
Second lines
1028+
"
1029+
))
1030+
);
1031+
1032+
verify_that!(
1033+
result,
1034+
err(displays_as(contains_substring(indoc!(
1035+
"
1036+
First line
1037+
+Second line
1038+
-Second lines
1039+
<---- remaining lines omitted ---->
1040+
"
1041+
))))
1042+
)
1043+
}
1044+
10131045
#[test]
10141046
fn match_explanation_for_eq_does_not_ignore_trailing_lines_in_actual_string() -> Result<()> {
10151047
let result = verify_that!(

0 commit comments

Comments
 (0)