-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidationerror_test.go
More file actions
119 lines (98 loc) · 2.31 KB
/
validationerror_test.go
File metadata and controls
119 lines (98 loc) · 2.31 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package turtleware_test
import (
"github.com/kernle32dll/turtleware"
"github.com/stretchr/testify/suite"
"errors"
"testing"
)
type ValidationWrapperErrorSuite struct {
suite.Suite
}
func TestValidationWrapperErrorSuite(t *testing.T) {
suite.Run(t, &ValidationWrapperErrorSuite{})
}
func (s *ValidationWrapperErrorSuite) Test_Error() {
s.Run("Filled", func() {
// given
err := &turtleware.ValidationWrapperError{
Errors: []error{
errors.New("validation 1 error"),
errors.New("validation 2 error"),
},
}
// when
res := err.Error()
// then
s.Equal("validation 1 error, validation 2 error", res)
})
s.Run("Empty", func() {
// given
err := &turtleware.ValidationWrapperError{
Errors: nil,
}
// when
res := err.Error()
// then
s.Equal("", res)
})
}
func (s *ValidationWrapperErrorSuite) Test_Is() {
// given
chainedErr1 := errors.New("validation 1 error")
chainedErr2 := errors.New("validation 2 error")
err := &turtleware.ValidationWrapperError{
Errors: []error{
chainedErr1, chainedErr2,
},
}
// when
isChainedErr1 := errors.Is(err, chainedErr1)
isChainedErr2 := errors.Is(err, chainedErr2)
// then
s.True(isChainedErr1)
s.True(isChainedErr2)
}
type wrapped struct {
msg string
err error
}
func (e wrapped) Error() string { return e.msg }
func (e wrapped) Unwrap() error { return e.err }
func (s *ValidationWrapperErrorSuite) Test_As() {
s.Run("Error_Itself", func() {
// given
chainedErr1 := errors.New("validation 1 error")
chainedErr2 := errors.New("validation 2 error")
err := &turtleware.ValidationWrapperError{
Errors: []error{
chainedErr1, chainedErr2,
},
}
// when
asValidationWrapperError := errors.As(err, &turtleware.ValidationWrapperError{})
// then
s.True(asValidationWrapperError)
})
s.Run("Wrapped", func() {
// given
chainedErr1 := errors.New("validation 1 error")
wrappedErr := wrapped{err: chainedErr1}
err := &turtleware.ValidationWrapperError{
Errors: []error{
wrappedErr,
},
}
// when
asValidationWrapperError := errors.As(err, &wrapped{})
// then
s.True(asValidationWrapperError)
})
s.Run("Unmatched", func() {
// given
err := &turtleware.ValidationWrapperError{}
// when
asValidationWrapperError := errors.As(err, &wrapped{})
// then
s.False(asValidationWrapperError)
})
}