-
Notifications
You must be signed in to change notification settings - Fork 2
/
error_test.go
102 lines (92 loc) · 2.86 KB
/
error_test.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package verify_test
import (
"errors"
"fmt"
"testing"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/fluentassert/verify"
)
func TestNoError(t *testing.T) {
t.Run("Passed", func(t *testing.T) {
got := verify.NoError(nil)
assertPassed(t, got)
})
t.Run("Failed", func(t *testing.T) {
got := verify.NoError(errors.New("some error"))
assertFailed(t, got, "non-nil error:\nsome error")
})
}
func TestIsError(t *testing.T) {
t.Run("Passed", func(t *testing.T) {
got := verify.IsError(errors.New(""))
assertPassed(t, got)
})
t.Run("Failed", func(t *testing.T) {
var err error
got := verify.IsError(err)
assertFailed(t, got, "the error is <nil>")
})
}
func TestError(t *testing.T) {
err := errors.New("expected error")
t.Run("Is", func(t *testing.T) {
t.Run("Passed", func(t *testing.T) {
got := verify.Error(fmt.Errorf("wrap: %w", err)).Is(err)
assertPassed(t, got)
})
t.Run("Failed", func(t *testing.T) {
got := verify.Error(fmt.Errorf("wrap: %v", err)).Is(err)
assertFailed(t, got, "no error in err's chain matches")
})
})
t.Run("IsNot", func(t *testing.T) {
t.Run("Passed", func(t *testing.T) {
got := verify.Error(fmt.Errorf("wrap: %v", err)).IsNot(err)
assertPassed(t, got)
})
t.Run("Failed", func(t *testing.T) {
got := verify.Error(fmt.Errorf("wrap: %w", err)).IsNot(err)
assertFailed(t, got, "some error in err's chain matches")
})
})
t.Run("As", func(t *testing.T) {
t.Run("Passed", func(t *testing.T) {
var wantErr *stubError
got := verify.Error(fmt.Errorf("wrap: %w", &stubError{})).As(&wantErr)
assertPassed(t, got)
assertEqual(t, wantErr, &stubError{})
})
t.Run("Failed", func(t *testing.T) {
var wantErr *stubError
got := verify.Error(fmt.Errorf("wrap: %v", &stubError{})).As(&wantErr)
assertFailed(t, got, "no error in err's chain matches")
assertEqual(t, wantErr, nil)
})
})
t.Run("AsNot", func(t *testing.T) {
t.Run("Passed", func(t *testing.T) {
var wantErr *stubError
got := verify.Error(fmt.Errorf("wrap: %v", &stubError{})).AsNot(&wantErr)
assertPassed(t, got)
assertEqual(t, wantErr, nil)
})
t.Run("Failed", func(t *testing.T) {
var wantErr *stubError
got := verify.Error(fmt.Errorf("wrap: %w", &stubError{})).AsNot(&wantErr)
assertFailed(t, got, "some error in err's chain matches")
assertEqual(t, wantErr, &stubError{})
})
})
t.Run("has assertions from Any", func(t *testing.T) {
want := errors.New("an error")
got := verify.Error(want).FluentAny.Got // type embedding done properly
assertEqual(t, got, want, cmpopts.EquateErrors())
})
t.Run("has assertions from String, Ordered, Obj for string", func(t *testing.T) {
want := "an error"
got := verify.Error(errors.New(want)).FluentString.Got // type embedding done properly
assertEqual(t, got, want)
})
}
type stubError struct{}
func (*stubError) Error() string { return "stubError" }