Codebase list golang-github-frankban-quicktest / a3064dc
Equals: show multi-line strings diff Francesco Banconi 3 years ago
4 changed file(s) with 98 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
99 {{ .EmitSynopsis }}
1010
1111 For a complete API reference, see the
12 [package documentation](https://godoc.org/github.com/frankban/quicktest).
12 [package documentation](https://pkg.go.dev/github.com/frankban/quicktest).
314314 up resources when available, and fall back to Defer otherwise.
315315
316316 For a complete API reference, see the
317 [package documentation](https://godoc.org/github.com/frankban/quicktest).
317 [package documentation](https://pkg.go.dev/github.com/frankban/quicktest).
6363 err = fmt.Errorf("%s", r)
6464 }
6565 }()
66 if want := args[0]; got != want {
67 if _, ok := got.(error); ok && want == nil {
68 return errors.New("got non-nil error")
66 want := args[0]
67 if got == want {
68 return nil
69 }
70
71 // Customize error message for non-nil errors.
72 if _, ok := got.(error); ok && want == nil {
73 return errors.New("got non-nil error")
74 }
75
76 // Show error types when comparing errors with different types.
77 if got, ok := got.(error); ok {
78 if want, ok := want.(error); ok {
79 gotType := reflect.TypeOf(got)
80 wantType := reflect.TypeOf(want)
81 if gotType != wantType {
82 note("got type", Unquoted(gotType.String()))
83 note("want type", Unquoted(wantType.String()))
84 }
6985 }
7086 return errors.New("values are not equal")
7187 }
72 return nil
88
89 // Show line diff when comparing different multi-line strings.
90 if got, ok := got.(string); ok {
91 if want, ok := want.(string); ok {
92 isMultiline := func(s []string) bool {
93 return len(s) > 2 || (len(s) == 2 && s[1] != "")
94 }
95 gotLines := strings.SplitAfter(got, "\n")
96 wantLines := strings.SplitAfter(want, "\n")
97 if isMultiline(gotLines) || isMultiline(wantLines) {
98 diff := cmp.Diff(gotLines, wantLines)
99 note("line diff (-got +want)", Unquoted(diff))
100 }
101 }
102 }
103 return errors.New("values are not equal")
73104 }
74105
75106 // CmpEquals returns a Checker checking equality of two arbitrary values
44 import (
55 "bytes"
66 "encoding/json"
7 "errors"
78 "fmt"
89 "strings"
910 "testing"
103104 ~string "bar"~
104105 `),
105106 }, {
107 about: "Equals: same multiline strings",
108 checker: qt.Equals,
109 got: "a\nmultiline\nstring",
110 args: []interface{}{"a\nmultiline\nstring"},
111 expectedNegateFailure: `
112 error:
113 unexpected success
114 got:
115 "a\nmultiline\nstring"
116 want:
117 <same as "got">
118 `,
119 }, {
120 about: "Equals: different multi-line strings",
121 checker: qt.Equals,
122 got: "a\nlong\nmultiline\nstring",
123 args: []interface{}{"just\na\nlong\nmulti-line\nstring\n"},
124 expectedCheckFailure: fmt.Sprintf(`
125 error:
126 values are not equal
127 line diff (-got +want):
128 %s
129 got:
130 "a\nlong\nmultiline\nstring"
131 want:
132 "just\na\nlong\nmulti-line\nstring\n"
133 `, diff([]string{"a\n", "long\n", "multiline\n", "string"}, []string{"just\n", "a\n", "long\n", "multi-line\n", "string\n", ""})),
134 }, {
135 about: "Equals: different single-line strings ending with newline",
136 checker: qt.Equals,
137 got: "foo\n",
138 args: []interface{}{"bar\n"},
139 expectedCheckFailure: `
140 error:
141 values are not equal
142 got:
143 "foo\n"
144 want:
145 "bar\n"
146 `,
147 }, {
106148 about: "Equals: different types",
107149 checker: qt.Equals,
108150 got: 42,
185227 want:
186228 nil
187229 `),
230 }, {
231 about: "Equals: different errors with same message",
232 checker: qt.Equals,
233 got: &errTest{
234 msg: "bad wolf",
235 },
236 args: []interface{}{errors.New("bad wolf")},
237 expectedCheckFailure: `
238 error:
239 values are not equal
240 got type:
241 *quicktest_test.errTest
242 want type:
243 *errors.errorString
244 got:
245 e"bad wolf"
246 want:
247 <same as "got">
248 `,
188249 }, {
189250 about: "Equals: nil struct",
190251 checker: qt.Equals,