Skip to content

Commit

Permalink
feat: expose Diff() function
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed May 27, 2024
1 parent fe12b41 commit e7b6c90
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ linters:
- structcheck
- deadcode
- nosnakecase
- mnd
- perfsprint

linters-settings:
govet:
Expand Down
7 changes: 4 additions & 3 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func Equal[T any](t testing.TB, expected, actual T, msgArgsAndCompareOptions ...
}
t.Helper()
msg := formatMsgAndArgs("Expected values to be equal:", msgArgsAndCompareOptions...)
t.Fatalf("%s\n%s", msg, diff(expected, actual, compareOptions...))
t.Fatalf("%s\n%s", msg, Diff(expected, actual, compareOptions...))
}

// NotEqual asserts that "expected" is not equal to "actual".
Expand Down Expand Up @@ -165,7 +165,7 @@ func EqualError(t testing.TB, err error, errString string, msgAndArgs ...any) {
}
if err.Error() != errString {
msg := formatMsgAndArgs("Error message not as expected:", msgAndArgs...)
t.Fatalf("%s\n%s", msg, diff(errString, err.Error()))
t.Fatalf("%s\n%s", msg, Diff(errString, err.Error()))
}
}

Expand Down Expand Up @@ -248,7 +248,8 @@ func NotPanics(t testing.TB, fn func(), msgAndArgs ...any) {
fn()
}

func diff[T any](before, after T, compareOptions ...CompareOption) string {
// Diff returns a unified diff of the string representation of two values.
func Diff[T any](before, after T, compareOptions ...CompareOption) string {
var lhss, rhss string
// Special case strings so we get nice diffs.
if l, ok := any(before).(string); ok {
Expand Down
2 changes: 1 addition & 1 deletion assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func TestNotIsError(t *testing.T) {
}

func TestDiff(t *testing.T) {
Equal(t, "-before\n+after\n", diff("before", "after"))
Equal(t, "-before\n+after\n", Diff("before", "after"))
}

func TestHasSuffix(t *testing.T) {
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion bin/go
2 changes: 1 addition & 1 deletion bin/gofmt
2 changes: 1 addition & 1 deletion bin/golangci-lint
18 changes: 18 additions & 0 deletions renovate.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
$schema: "https://docs.renovatebot.com/renovate-schema.json",
extends: [
"config:recommended",
":semanticCommits",
":semanticCommitTypeAll(chore)",
":semanticCommitScope(deps)",
"group:allNonMajor",
"schedule:earlyMondays", // Run once a week.
],
packageRules: [
{
matchPackageNames: ["golangci-lint"],
matchManagers: ["hermit"],
enabled: false,
},
],
}

0 comments on commit e7b6c90

Please sign in to comment.