···367367368368// Diff fails if the two strings got and want are not equal and provides a rich
369369// unified diff of the two for easy comparison.
370370+//
371371+// If either got or want do not end in a newline, one is added to avoid a
372372+// "No newline at end of file" warning in the diff which is visually distracting.
370373func Diff(tb testing.TB, got, want string) {
371374 tb.Helper()
372372-373373- // TODO(@FollowTheProcess): If either got or want don't end in a newline, add one
374374- if diff := diff.Diff("want", []byte(want), "got", []byte(got)); diff != nil {
375375- tb.Fatalf("\nDiff\n----\n%s\n", prettyDiff(string(diff)))
376376- }
375375+ DiffBytes(tb, []byte(got), []byte(want))
377376}
378377379378// DiffBytes fails if the two []byte got and want are not equal and provides a rich
380379// unified diff of the two for easy comparison.
380380+//
381381+// If either got or want do not end in a newline, one is added to avoid a
382382+// "No newline at end of file" warning in the diff which is visually distracting.
381383func DiffBytes(tb testing.TB, got, want []byte) {
382384 tb.Helper()
385385+386386+ got = fixNL(got)
387387+ want = fixNL(want)
383388384389 if diff := diff.Diff("want", want, "got", got); diff != nil {
385390 tb.Fatalf("\nDiff\n----\n%s\n", prettyDiff(string(diff)))
···507512 }
508513509514 return strings.Join(lines, "\n")
515515+}
516516+517517+// If data is empty or ends in \n, fixNL returns data.
518518+// Otherwise fixNL returns a new slice consisting of data with a final \n added.
519519+func fixNL(data []byte) []byte {
520520+ if len(data) == 0 || data[len(data)-1] == '\n' {
521521+ return data
522522+ }
523523+ d := make([]byte, len(data)+1)
524524+ copy(d, data)
525525+ d[len(data)] = '\n'
526526+527527+ return d
510528}
+19
test_test.go
···405405 wantFail: false,
406406 },
407407 {
408408+ name: "Diff/pass no trailing newline",
409409+ fn: func(tb testing.TB) {
410410+ got := "Some\nstuff here in this file\nlines as well wow\nsome more stuff"
411411+ want := "Some\nstuff here in this file\nlines as well wow\nsome more stuff"
412412+413413+ test.Diff(tb, got, want)
414414+ },
415415+ wantFail: false,
416416+ },
417417+ {
408418 name: "Diff/fail",
409419 fn: func(tb testing.TB) {
410420 got := "Some\nstuff here in this file\nlines as well wow\nsome more stuff\n"
411421 want := "Some\ndifferent stuff here in this file\nthis line is different\nsome more stuff\n"
422422+ test.Diff(tb, got, want)
423423+ },
424424+ wantFail: true,
425425+ },
426426+ {
427427+ name: "Diff/fail no trailing newline",
428428+ fn: func(tb testing.TB) {
429429+ got := "Some\nstuff here in this file\nlines as well wow\nsome more stuff"
430430+ want := "Some\ndifferent stuff here in this file\nthis line is different\nsome more stuff"
412431 test.Diff(tb, got, want)
413432 },
414433 wantFail: true,
···11+22+Diff
33+----
44+diff want got
55+--- want
66++++ got
77+@@ -1,4 +1,4 @@
88+ Some
99+- different stuff here in this file
1010+- this line is different
1111++ stuff here in this file
1212++ lines as well wow
1313+ some more stuff
1414+