Skip to content

Commit e516f1c

Browse files
Googlercopybara-github
authored andcommitted
Check for newline terminators in summarize_diff when line diff sees no difference.
The line differ wasn't sensitive to when actual and expected string were different only based on the final line terminator, so we add some extra logic to detect this condition and report accordingly. PiperOrigin-RevId: 698448355
1 parent 5e28dbe commit e516f1c

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

googletest/src/matcher_support/summarize_diff.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,27 @@ pub(crate) fn create_diff(
3838
// line-by-line diff.
3939
return "".into();
4040
}
41+
4142
match edit_distance::edit_list(actual_debug.lines(), expected_debug.lines(), diff_mode) {
42-
edit_distance::Difference::Equal => "No difference found between debug strings.".into(),
43+
edit_distance::Difference::Equal => {
44+
// str.lines() is oblivious to the last newline in a
45+
// string, so we need to check this to make sure we don't spuriously
46+
// claim that 'hello' and 'hello\n' are identical debug strings.
47+
//
48+
// Although we would have liked to resolve by replacing
49+
// str::lines() with str::split('\n'), the potentially
50+
// empty last element interferes with good diff output for
51+
// "contains" checks.
52+
let actual_newline_terminated = actual_debug.ends_with('\n');
53+
let expected_newline_terminated = expected_debug.ends_with('\n');
54+
if actual_newline_terminated && !expected_newline_terminated {
55+
"Actual includes a terminating newline that is absent from expected.".into()
56+
} else if !actual_newline_terminated && expected_newline_terminated {
57+
"Actual omits a terminating newline that is present in expected.".into()
58+
} else {
59+
"No difference found between debug strings.".into()
60+
}
61+
}
4362
edit_distance::Difference::Editable(edit_list) => {
4463
format!("{}{}", summary_header(), edit_list.into_iter().collect::<BufferedSummary>(),)
4564
.into()
@@ -553,4 +572,20 @@ mod tests {
553572
})
554573
)
555574
}
575+
576+
#[test]
577+
fn create_diff_line_termination_diff() -> Result<()> {
578+
verify_that!(
579+
create_diff("1\n2\n3", "1\n2\n3\n", Mode::Exact),
580+
eq("Actual omits a terminating newline that is present in expected.")
581+
)?;
582+
verify_that!(
583+
create_diff("1\n2\n3\n", "1\n2\n3", Mode::Exact),
584+
eq("Actual includes a terminating newline that is absent from expected.")
585+
)?;
586+
verify_that!(
587+
create_diff("1\n2\n3\n", "1\n2\n3\n", Mode::Exact),
588+
eq("No difference found between debug strings.")
589+
)
590+
}
556591
}

integration_tests/src/integration_tests.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,21 @@ mod tests {
895895
verify_eq!(vec![1, 2], {2, 1,},)
896896
}
897897

898+
#[gtest]
899+
fn verify_eq_reports_diff_in_trailing_newline() -> Result<()> {
900+
let result = verify_eq!("hello\nworld\n", "hello\nworld");
901+
verify_that!(
902+
result,
903+
err(displays_as(contains_substring(indoc! {r#"
904+
Expected: is equal to "hello\nworld"
905+
Actual: "hello\nworld\n",
906+
which isn't equal to "hello\nworld"
907+
908+
Actual includes a terminating newline that is absent from expected.
909+
"#})))
910+
)
911+
}
912+
898913
#[gtest]
899914
fn verify_eq_when_not_equal_returns_error() -> Result<()> {
900915
let output =

0 commit comments

Comments
 (0)