-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchers_fail_test.go
56 lines (53 loc) · 1.49 KB
/
matchers_fail_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
package justest_test
import (
"fmt"
"regexp"
"testing"
. "github.com/arikkfir/justest"
)
func TestFail(t *testing.T) {
t.Parallel()
type testCase struct {
actuals []any
verifier TestOutcomeVerifier
}
testCases := map[string]testCase{
"Fails if no actuals": {
actuals: []any{},
verifier: FailureVerifier(`No error occurred`),
},
"Fails if last actual is nil": {
actuals: []any{1, 2, nil},
verifier: FailureVerifier(`No error occurred`),
},
"Succeeds if last actual is an error": {
actuals: []any{1, fmt.Errorf("expected error")},
verifier: SuccessVerifier(),
},
"Fails if last actual is non-nil and not an error": {
actuals: []any{1, 2, 3},
verifier: FailureVerifier(`No error occurred`),
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
mt := NewMockT(t)
defer mt.Verify(tc.verifier)
With(mt).VerifyThat(tc.actuals...).Will(Fail()).Now()
})
}
t.Run("Succeeds if error matches one of the patterns", func(t *testing.T) {
t.Parallel()
mt := NewMockT(t)
defer mt.Verify(SuccessVerifier())
With(mt).VerifyThat(fmt.Errorf("expected error")).Will(Fail(`^expected error$`)).Now()
})
t.Run("Fails if error matches none of the patterns", func(t *testing.T) {
t.Parallel()
mt := NewMockT(t)
defer mt.Verify(FailureVerifier(`.*` + regexp.QuoteMeta(`[^abc$ ^def$ ^ghi$]`) + `\n.*expected error`))
With(mt).VerifyThat(fmt.Errorf("expected error")).Will(Fail(`^abc$`, `^def$`, `^ghi$`)).Now()
})
}