-
Notifications
You must be signed in to change notification settings - Fork 44
/
maybeerror_test.go
142 lines (125 loc) · 2.88 KB
/
maybeerror_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
138
139
140
141
142
package cmds
import (
"encoding/json"
"io"
"reflect"
"strings"
"testing"
)
func errcmp(t *testing.T, exp, got error, msg string) {
if exp == nil && got == nil {
return
}
if exp != nil && got != nil {
if exp.Error() == got.Error() {
return
}
t.Errorf("expected %s to be %q but got %q", msg, exp, got)
return
}
if exp == nil {
t.Errorf("expected %s to be nil but got %q", msg, got)
}
if got == nil {
t.Errorf("expected %s to be %q but got nil", msg, exp)
}
}
type Foo struct {
Bar int
}
type Bar struct {
Foo string
}
type ValueError struct {
DecodeError error
Error *Error
Value interface{}
}
type anyTestCase struct {
Name string
Value interface{}
JSON string
Decoded []ValueError
}
func TestMaybeError(t *testing.T) {
testcases := []anyTestCase{
{
Name: "typed-pointer",
Value: &Foo{},
JSON: `{"Bar":23}{"Bar":42}{"Message":"some error", "Type": "error"}`,
Decoded: []ValueError{
{Value: &Foo{23}},
{Value: &Foo{42}},
{Error: &Error{Message: "some error", Code: 0}},
},
},
{
Name: "typed-value",
Value: Foo{},
JSON: `{"Bar":23}{"Bar":42}{"Message":"some error", "Type": "error"}`,
Decoded: []ValueError{
{Value: &Foo{23}},
{Value: &Foo{42}},
{Error: &Error{Message: "some error", Code: 0}},
},
},
{
Name: "typed2-pointer",
Value: &Bar{},
JSON: `{"Foo":""}{"Foo":"Qmabc"}{"Message":"some error", "Type": "error"}`,
Decoded: []ValueError{
{Value: &Bar{""}},
{Value: &Bar{"Qmabc"}},
{Error: &Error{Message: "some error", Code: 0}},
},
},
{
Name: "typed2-value",
Value: Bar{},
JSON: `{"Foo":""}{"Foo":"Qmabc"}{"Message":"some error", "Type": "error"}`,
Decoded: []ValueError{
{Value: &Bar{""}},
{Value: &Bar{"Qmabc"}},
{Error: &Error{Message: "some error", Code: 0}},
},
},
{
Name: "untyped",
JSON: `{"Foo":"bar", "i": 4}"some string"5{"Message":"some error", "Type": "error"}`,
Decoded: []ValueError{
{Value: map[string]interface{}{"Foo": "bar", "i": 4.0}},
{Value: "some string"},
{Value: 5.0},
{Error: &Error{Message: "some error", Code: 0}},
},
},
}
for _, tc := range testcases {
t.Run(tc.Name, func(t *testing.T) {
r := strings.NewReader(tc.JSON)
d := json.NewDecoder(r)
var err error
for _, dec := range tc.Decoded {
m := &MaybeError{Value: tc.Value}
err = d.Decode(m)
errcmp(t, dec.DecodeError, err, "decode error")
val, err := m.Get()
if dec.Value != nil {
ex := dec.Value
if !reflect.DeepEqual(ex, val) {
t.Errorf("value is %#v(%T), expected %#v(%T)", val, val, ex, ex)
}
} else {
errcmp(t, dec.Error, err, "response error")
}
}
m := &MaybeError{Value: tc.Value}
err = d.Decode(m)
val, e := m.Get()
if err != io.EOF {
t.Log("superflouus data:", val, e)
errcmp(t, io.EOF, err, "final decode error")
}
})
}
}