Skip to content

Commit

Permalink
support diff display for string variables
Browse files Browse the repository at this point in the history
  • Loading branch information
timfeirg authored and ernesto-jimenez committed Jun 9, 2018
1 parent ef2d015 commit 2a15e20
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
12 changes: 9 additions & 3 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1338,12 +1338,18 @@ func diff(expected interface{}, actual interface{}) string {
return ""
}

if ek != reflect.Struct && ek != reflect.Map && ek != reflect.Slice && ek != reflect.Array {
if ek != reflect.Struct && ek != reflect.Map && ek != reflect.Slice && ek != reflect.Array && ek != reflect.String {
return ""
}

e := spewConfig.Sdump(expected)
a := spewConfig.Sdump(actual)
var e, a string
if ek != reflect.String {
e = spewConfig.Sdump(expected)
a = spewConfig.Sdump(actual)
} else {
e = expected.(string)
a = actual.(string)
}

diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
A: difflib.SplitLines(e),
Expand Down
19 changes: 17 additions & 2 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,30 @@ func (t *bufferT) Errorf(format string, args ...interface{}) {
t.buf.WriteString(decorate(fmt.Sprintf(format, args...)))
}

func TestStringEqual(t *testing.T) {
for i, currCase := range []struct {
equalWant string
equalGot string
msgAndArgs []interface{}
want string
}{
{equalWant: "hi, \nmy name is", equalGot: "what,\nmy name is", want: "\tassertions.go:\\d+: \n\t+Error Trace:\t\n\t+Error:\\s+Not equal:\\s+\n\\s+expected: \"hi, \\\\nmy name is\"\n\\s+actual\\s+: \"what,\\\\nmy name is\"\n\\s+Diff:\n\\s+-+ Expected\n\\s+\\++ Actual\n\\s+@@ -1,2 \\+1,2 @@\n\\s+-hi, \n\\s+\\+what,\n\\s+my name is"},
} {
mockT := &bufferT{}
Equal(mockT, currCase.equalWant, currCase.equalGot, currCase.msgAndArgs...)
Regexp(t, regexp.MustCompile(currCase.want), mockT.buf.String(), "Case %d", i)
}
}

func TestEqualFormatting(t *testing.T) {
for i, currCase := range []struct {
equalWant string
equalGot string
msgAndArgs []interface{}
want string
}{
{equalWant: "want", equalGot: "got", want: "\tassertions.go:[0-9]+: \n\t\t\tError Trace:\t\n\t\t\tError: \tNot equal: \n\t\t\t \texpected: \"want\"\n\t\t\t \tactual : \"got\"\n"},
{equalWant: "want", equalGot: "got", msgAndArgs: []interface{}{"hello, %v!", "world"}, want: "\tassertions.go:[0-9]+: \n\t\t\tError Trace:\t\n\t\t\tError: \tNot equal: \n\t\t\t \texpected: \"want\"\n\t\t\t \tactual : \"got\"\n\t\t\tMessages: \thello, world!\n"},
{equalWant: "want", equalGot: "got", want: "\tassertions.go:\\d+: \n\t+Error Trace:\t\n\t+Error:\\s+Not equal:\\s+\n\\s+expected: \"want\"\n\\s+actual\\s+: \"got\"\n\\s+Diff:\n\\s+-+ Expected\n\\s+\\++ Actual\n\\s+@@ -1 \\+1 @@\n\\s+-want\n\\s+\\+got\n"},
{equalWant: "want", equalGot: "got", msgAndArgs: []interface{}{"hello, %v!", "world"}, want: "\tassertions.go:[0-9]+: \n\t+Error Trace:\t\n\t+Error:\\s+Not equal:\\s+\n\\s+expected: \"want\"\n\\s+actual\\s+: \"got\"\n\\s+Diff:\n\\s+-+ Expected\n\\s+\\++ Actual\n\\s+@@ -1 \\+1 @@\n\\s+-want\n\\s+\\+got\n\\s+Messages:\\s+hello, world!\n"},
} {
mockT := &bufferT{}
Equal(mockT, currCase.equalWant, currCase.equalGot, currCase.msgAndArgs...)
Expand Down

0 comments on commit 2a15e20

Please sign in to comment.