-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_or_test.go
94 lines (76 loc) · 2.59 KB
/
error_or_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
package ctrl
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestErrorOr(t *testing.T) {
t.Run("ErrorOr", func(t *testing.T) {
// should return nil when condition is true
err := ErrorOr(true)
require.NoError(t, err)
// should return error when condition is false
err = ErrorOr(false)
require.Error(t, err)
assert.Equal(t, "assertion failed", err.Error())
})
t.Run("ErrorOrf", func(t *testing.T) {
// should return nil when condition is true
err := ErrorOrf(true, "custom message")
require.NoError(t, err)
// should return formatted error when condition is false
msg := "test message"
err = ErrorOrf(false, msg)
require.Error(t, err)
assert.Equal(t, "assertion failed: "+msg, err.Error())
// should support formatting
err = ErrorOrf(false, "value is %d", 42)
require.Error(t, err)
assert.Equal(t, "assertion failed: value is 42", err.Error())
})
t.Run("ErrorOrFunc", func(t *testing.T) {
// should return nil when function returns true
err := ErrorOrFunc(func() bool { return true })
require.NoError(t, err)
// should return error when function returns false
err = ErrorOrFunc(func() bool { return false })
require.Error(t, err)
assert.Equal(t, "assertion failed", err.Error())
// should evaluate function
counter := 0
_ = ErrorOrFunc(func() bool {
counter++
return true
})
assert.Equal(t, 1, counter)
})
t.Run("ErrorOrFuncf", func(t *testing.T) {
// should return nil when function returns true
err := ErrorOrFuncf(func() bool { return true }, "custom message")
require.NoError(t, err)
// should return formatted error when function returns false
msg := "custom func message"
err = ErrorOrFuncf(func() bool { return false }, msg)
require.Error(t, err)
assert.Equal(t, "assertion failed: "+msg, err.Error())
// should support formatting
err = ErrorOrFuncf(func() bool { return false }, "value is %d", 42)
require.Error(t, err)
assert.Equal(t, "assertion failed: value is 42", err.Error())
})
t.Run("WithCustomError", func(t *testing.T) {
customErr := errors.New("custom error")
// should return custom error when condition is false
err := ErrorOrWithErr(false, customErr)
assert.Equal(t, customErr, err)
// should return nil when condition is true
err = ErrorOrWithErr(true, customErr)
require.NoError(t, err)
// should work with function variant
err = ErrorOrFuncWithErr(func() bool { return false }, customErr)
assert.Equal(t, customErr, err)
err = ErrorOrFuncWithErr(func() bool { return true }, customErr)
require.NoError(t, err)
})
}