-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors_test.go
137 lines (119 loc) · 3.76 KB
/
errors_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package splitter
import (
"errors"
"fmt"
"github.com/stretchr/testify/require"
"testing"
)
func TestWrappedSplittingError(t *testing.T) {
sErr := &splittingError{
errorType: Wrapped,
wrapped: errors.New("whoops"),
}
require.Error(t, sErr)
require.Equal(t, "whoops", sErr.Error())
}
func TestWrappedAsSplittingError(t *testing.T) {
sErr := asSplittingError(errors.New("whoops"), 16)
require.Error(t, sErr)
require.Equal(t, "whoops", sErr.Error())
sErr2 := asSplittingError(sErr, 0)
require.Error(t, sErr2)
require.Equal(t, sErr, sErr2)
var err error
sErr = asSplittingError(err, 0)
require.NoError(t, sErr)
require.Nil(t, sErr)
}
func TestNewSplittingError(t *testing.T) {
err := newSplittingError(Unopened, 16, '(', Parenthesis)
require.Error(t, err)
require.Equal(t, fmt.Sprintf(unopenedFmt, "(", 16), err.Error())
err = newSplittingError(Unclosed, 16, ')', Parenthesis)
require.Equal(t, fmt.Sprintf(unclosedFmt, ")", 16), err.Error())
}
func TestSplittingError_DefaultMessage(t *testing.T) {
err := &splittingError{
errorType: -1,
message: "whoops",
}
require.Error(t, err)
require.Equal(t, "whoops", err.Error())
}
func TestNewOptionFailError(t *testing.T) {
err := NewOptionFailError("whoops", 16, nil)
require.Error(t, err)
require.Equal(t, "whoops", err.Error())
sErr, ok := err.(*splittingError)
require.True(t, ok)
require.Equal(t, OptionFail, sErr.Type())
require.Equal(t, 16, sErr.Position())
subPart := &subPart{
enc: *Parenthesis,
openPos: 5,
closePos: 10,
}
err = NewOptionFailError("whoops", 0, subPart)
require.Error(t, err)
require.Equal(t, "whoops", err.Error())
sErr, ok = err.(*splittingError)
require.True(t, ok)
require.Equal(t, OptionFail, sErr.Type())
require.Equal(t, 5, sErr.Position())
}
func TestSplitAlwaysReturnsSplittingError(t *testing.T) {
s, err := NewSplitter(',', Parenthesis)
require.NoError(t, err)
s.AddDefaultOptions(NotEmptyFirstMsg("whoops at position %d"))
_, err = s.Split(`(`)
require.Error(t, err)
require.Equal(t, fmt.Sprintf(unclosedFmt, `(`, 0), err.Error())
sErr, ok := err.(SplittingError)
require.True(t, ok)
require.Equal(t, Unclosed, sErr.Type())
require.Equal(t, '(', sErr.Rune())
require.Equal(t, 0, sErr.Position())
require.Equal(t, Parenthesis, sErr.Enclosure())
require.Nil(t, sErr.Wrapped())
_, err = s.Split(`)`)
require.Error(t, err)
require.Equal(t, fmt.Sprintf(unopenedFmt, `)`, 0), err.Error())
sErr, ok = err.(SplittingError)
require.True(t, ok)
require.Equal(t, Unopened, sErr.Type())
require.Equal(t, ')', sErr.Rune())
require.Equal(t, 0, sErr.Position())
require.Equal(t, Parenthesis, sErr.Enclosure())
require.Nil(t, sErr.Wrapped())
require.NoError(t, sErr.Unwrap())
require.NoError(t, errors.Unwrap(sErr))
_, err = s.Split(`,a`)
require.Error(t, err)
require.Equal(t, `whoops at position 0`, err.Error())
sErr, ok = err.(SplittingError)
require.True(t, ok)
require.Equal(t, OptionFail, sErr.Type())
require.Equal(t, rune(0), sErr.Rune())
require.Equal(t, 0, sErr.Position())
require.Nil(t, sErr.Enclosure())
require.Nil(t, sErr.Wrapped())
require.NoError(t, sErr.Unwrap())
require.NoError(t, errors.Unwrap(sErr))
_, err = s.Split(`a`, &errorOption{})
require.Error(t, err)
require.Equal(t, "error option", err.Error())
sErr, ok = err.(SplittingError)
require.True(t, ok)
require.Equal(t, Wrapped, sErr.Type())
require.Error(t, sErr.Wrapped())
require.Error(t, sErr.Unwrap())
require.Error(t, errors.Unwrap(sErr))
require.Equal(t, rune(0), sErr.Rune())
require.Nil(t, sErr.Enclosure())
require.Equal(t, 0, sErr.Position())
}
type errorOption struct {
}
func (o *errorOption) Apply(s string, pos int, totalLen int, captured int, skipped int, isLast bool, subParts ...SubPart) (string, bool, error) {
return s, false, errors.New("error option")
}