Equals: show multi-line strings diff
Francesco Banconi
3 years ago
9 | 9 | {{ .EmitSynopsis }} |
10 | 10 | |
11 | 11 | 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). |
314 | 314 | up resources when available, and fall back to Defer otherwise. |
315 | 315 | |
316 | 316 | 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). |
63 | 63 | err = fmt.Errorf("%s", r) |
64 | 64 | } |
65 | 65 | }() |
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 | } | |
69 | 85 | } |
70 | 86 | return errors.New("values are not equal") |
71 | 87 | } |
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") | |
73 | 104 | } |
74 | 105 | |
75 | 106 | // CmpEquals returns a Checker checking equality of two arbitrary values |
4 | 4 | import ( |
5 | 5 | "bytes" |
6 | 6 | "encoding/json" |
7 | "errors" | |
7 | 8 | "fmt" |
8 | 9 | "strings" |
9 | 10 | "testing" |
103 | 104 | ~string "bar"~ |
104 | 105 | `), |
105 | 106 | }, { |
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 | }, { | |
106 | 148 | about: "Equals: different types", |
107 | 149 | checker: qt.Equals, |
108 | 150 | got: 42, |
185 | 227 | want: |
186 | 228 | nil |
187 | 229 | `), |
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 | `, | |
188 | 249 | }, { |
189 | 250 | about: "Equals: nil struct", |
190 | 251 | checker: qt.Equals, |