-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors_test.go
More file actions
139 lines (122 loc) · 3.92 KB
/
errors_test.go
File metadata and controls
139 lines (122 loc) · 3.92 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package valval
import (
"errors"
"reflect"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestErrors(t *testing.T) {
Convey("Errors", t, func() {
e1 := errors.New("error1")
e2 := errors.New("error2")
ve1 := valueError([]error{
e1, e2,
})
Convey("Value", func() {
ed1 := Errors(&ve1)
So(len(ed1), ShouldEqual, 2)
So(ed1[0].Path, ShouldBeEmpty)
So(ed1[1].Path, ShouldBeEmpty)
So(ed1[0].Error, ShouldEqual, e1)
So(ed1[1].Error, ShouldEqual, e2)
})
oe1 := objectError([]*objectErrorField{
{Name: "f1", Err: e1},
{Name: "f2", Err: e2},
})
oeNexted := objectError([]*objectErrorField{
{Name: "f1", Err: e1},
{Name: "nested", Err: &oe1},
})
Convey("Object", func() {
ed1 := Errors(&oe1)
So(len(ed1), ShouldEqual, 2)
So(ed1[0].Path, ShouldEqual, "f1")
So(ed1[1].Path, ShouldEqual, "f2")
So(ed1[0].Error, ShouldEqual, e1)
So(ed1[1].Error, ShouldEqual, e2)
ed2 := ErrorsBase(&oe1, "base")
So(len(ed2), ShouldEqual, 2)
So(ed2[0].Path, ShouldEqual, "base.f1")
So(ed2[1].Path, ShouldEqual, "base.f2")
So(ed2[0].Error, ShouldEqual, e1)
So(ed2[1].Error, ShouldEqual, e2)
edNested := ErrorsBase(&oeNexted, "base")
So(len(edNested), ShouldEqual, 3)
So(edNested[0].Path, ShouldEqual, "base.f1")
So(edNested[1].Path, ShouldEqual, "base.nested.f1")
So(edNested[2].Path, ShouldEqual, "base.nested.f2")
So(edNested[0].Error, ShouldEqual, e1)
So(edNested[1].Error, ShouldEqual, e1)
So(edNested[2].Error, ShouldEqual, e2)
})
se1 := sliceError([]*sliceErrorElem{
{Index: 0, Err: e1},
{Index: 1, Err: e2},
})
Convey("Slice", func() {
ed1 := ErrorsBase(&se1, "")
So(len(ed1), ShouldEqual, 2)
So(ed1[0].Path, ShouldEqual, "[0]")
So(ed1[1].Path, ShouldEqual, "[1]")
So(ed1[0].Error, ShouldEqual, e1)
So(ed1[1].Error, ShouldEqual, e2)
ed2 := ErrorsBase(&se1, "base")
So(len(ed2), ShouldEqual, 2)
So(ed2[0].Path, ShouldEqual, "base[0]")
So(ed2[1].Path, ShouldEqual, "base[1]")
So(ed2[0].Error, ShouldEqual, e1)
So(ed2[1].Error, ShouldEqual, e2)
})
Convey("Mixed", func() {
eMixedInnerInner := objectError([]*objectErrorField{
{Name: "es3", Err: e1},
})
se := sliceError([]*sliceErrorElem{
{Index: 2, Err: &eMixedInnerInner},
})
eMixedInner := objectError([]*objectErrorField{
{Name: "es1", Err: e1},
{Name: "es2", Err: &ve1},
{Name: "nested2", Err: &se1},
{Name: "nested3", Err: &se},
})
eMixed := objectError([]*objectErrorField{
{Name: "nested", Err: &eMixedInner},
})
e := ErrorsBase(&eMixed, "hoge.fuga")
So(len(e), ShouldEqual, 6)
So(e[0].Path, ShouldEqual, "hoge.fuga.nested.es1")
So(e[1].Path, ShouldEqual, "hoge.fuga.nested.es2")
So(e[2].Path, ShouldEqual, "hoge.fuga.nested.es2")
So(e[3].Path, ShouldEqual, "hoge.fuga.nested.nested2[0]")
So(e[4].Path, ShouldEqual, "hoge.fuga.nested.nested2[1]")
So(e[5].Path, ShouldEqual, "hoge.fuga.nested.nested3[2].es3")
})
Convey("JSONPath", func() {
eMixedInnerInner := objectError([]*objectErrorField{
{Name: "es3", Err: e1},
})
se := sliceError([]*sliceErrorElem{
{Index: 2, Err: &eMixedInnerInner},
})
eMixedInner := objectError([]*objectErrorField{
{Name: "es1", Err: e1, Tag: reflect.StructTag(`json:"Es1"`)},
{Name: "es2", Err: &ve1},
{Name: "nested2", Err: &se1},
{Name: "nested3", Err: &se, Tag: reflect.StructTag(`json:"Nested3"`)},
})
eMixed := objectError([]*objectErrorField{
{Name: "nested", Err: &eMixedInner},
})
e := JSONErrorsBase(&eMixed, "hoge.fuga")
So(len(e), ShouldEqual, 6)
So(e[0].Path, ShouldEqual, "hoge.fuga.nested.Es1")
So(e[1].Path, ShouldEqual, "hoge.fuga.nested.es2")
So(e[2].Path, ShouldEqual, "hoge.fuga.nested.es2")
So(e[3].Path, ShouldEqual, "hoge.fuga.nested.nested2[0]")
So(e[4].Path, ShouldEqual, "hoge.fuga.nested.nested2[1]")
So(e[5].Path, ShouldEqual, "hoge.fuga.nested.Nested3[2].es3")
})
})
}