-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
match.go
63 lines (57 loc) · 1.51 KB
/
match.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package testutil
import (
"fmt"
"io"
"testing"
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
"github.com/olekukonko/tablewriter"
)
func Match(t *testing.T, out io.Writer, want, got error) {
t.Helper()
if got == nil && want == nil {
return
}
if got == nil || want == nil {
t.Fatalf("Wanted *errors.Error, got nil")
}
if err := matchType(want, got); err != nil {
t.Fatal(err)
}
if err := matchValues(want, got); err != nil {
printDiff(out, want.(*errors.Error), got.(*errors.Error))
t.Fatal(err)
}
}
func matchType(err1, err2 error) error {
_, ok := err1.(*errors.Error)
if !ok {
return fmt.Errorf("unexpected error type: Wanted error type: *errors.Error; Got error type: %T", err1)
}
_, ok = err2.(*errors.Error)
if !ok {
return fmt.Errorf("unexpected error type: Wanted error type: *errors.Error; Got error type: %T", err2)
}
return nil
}
func matchValues(err1, err2 error) error {
e1 := err1.(*errors.Error)
e2 := err2.(*errors.Error)
if e1.Op != e2.Op || e1.Kind != e2.Kind || e1.Error() != e2.Error() {
return fmt.Errorf("expected a different error, please check the above table")
}
return nil
}
func printDiff(out io.Writer, want, got *errors.Error) {
data := [][]string{
{"Op", string(want.Op), string(got.Op)},
{"Kind", want.Kind.String(), got.Kind.String()},
{"Error", want.Error(), got.Error()},
}
table := tablewriter.NewWriter(out)
table.SetHeader([]string{"", "Want", "Got"})
table.SetRowLine(true)
for _, v := range data {
table.Append(v)
}
table.Render()
}