Documentation ¶
Overview ¶
Package errdiff makes it easy to compare Error by code, substring or exact match in tests.
Similar in intended usage to messagediff.Diff and pretty.Compare, particularly in table-driven tests.
Example usage:
testCases := []struct { ... wantSubstring string }{ // Success {...}, // Failures {..., wantSubstring: "failed"}, {..., wantSubstring: "too many users"}, } for _, c := range testCases { got, err := fn(...) if diff := errdiff.Substring(err, c.wantSubstring); diff != "" { t.Errorf("fn() %v", diff) continue } ... }
The generic function Check may be used in place of Code or Substring or when comparing against another error or for simple existance of an error:
testCases := []struct { ... err interface{} }{ // Success {...}, // Failures {..., err: io.EOF}, // An explicit error {..., err: "my expected error string"}, // contains text {..., err: true}, // expect an error, don't care what } for _, c := range testCases { got, err := fn(...) if diff := errdiff.Check(err, c.err); diff != "" { t.Errorf("fn() %v", diff) continue } ...
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Check ¶
Check returns a message describing the difference between the error err and want. If want is a codes.Code, this function is the same as Code. If want is a string, this function is the same as Substring. If want is an error, this is essentially the same as ExactTextCompare(got, w.Error()). If want is a bool, err is simply tested for existance (want of true means an error is wanted).
Types ¶
This section is empty.