-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder_test.go
154 lines (144 loc) · 3.99 KB
/
decoder_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
143
144
145
146
147
148
149
150
151
152
153
154
package jsonptrerror_test
import (
"bytes"
"encoding/json"
"io"
"reflect"
"sort"
"strings"
"testing"
"github.com/mohae/deepcopy"
"github.com/dolmen-go/jsonptrerror"
)
func TestDecoder(t *testing.T) {
decodeAll := func(dec interface {
Decode(interface{}) error
Token() (json.Token, error)
}, value interface{}) error {
err := dec.Decode(deepcopy.Copy(value))
if err == nil {
_, err := dec.Token()
if err != io.EOF {
t.Fatalf("decodeAll unexpected error %q", err)
}
}
return err
}
for _, test := range []struct {
in string
value interface{}
ptr interface{}
}{
{`"a"`, new(string), nil},
{`true`, new(bool), nil},
{`null`, new(interface{}), nil},
{`[]`, new([]interface{}), nil},
{`{}`, new(map[string]interface{}), nil},
{`"a"`, new(bool), ""},
{`[]`, new(map[string]interface{}), nil},
{`["a"]`, &([]string{}), nil},
{`["a"]`, &([]int{}), "/0"},
{`[1]`, &([]int{}), nil},
{`[1]`, &([]string{}), "/0"},
{`[1]`, &([]bool{}), "/0"},
{`[true]`, &([]int{}), "/0"},
{`[true]`, &([]string{}), "/0"},
{`[1,true]`, &([]int{}), "/1"},
{`["a",true]`, &([]string{}), "/1"},
{`[1,true,1]`, &([]int{}), "/1"},
{`{}`, new([]interface{}), nil},
{`{"a":1}`, new(map[string]interface{}), nil},
{`{"a":1}`, new(map[string]string), "/a"},
{`{"~":1}`, new(map[string]string), "/~0"},
{`{"/":1}`, new(map[string]string), "/~1"},
{`{"/":[1]}`, new(map[string][]string), "/~1/0"},
{` { "/" : [ 1]}`, new(map[string][]string), "/~1/0"},
{` { "\u002f" : [ 1]}`, new(map[string][]string), "/~1/0"},
{`[{},{"a":1}]`, new([]map[string]int), nil},
{`[{},{"a":1}]`, new([]map[string]bool), "/1/a"},
{`{"a":true,"b":2}`, new(map[string]bool), "/b"},
{`{"a":true,"b":2}`, &struct{ A, B bool }{}, "/b"},
// TODO structs
} {
t.Logf("%s -> %T", test.in, test.value)
errRef := decodeAll(json.NewDecoder(bytes.NewBufferString(test.in)), test.value)
checkErr := func(err error) error {
if (err == nil) != (errRef == nil) {
t.Errorf("err = %q, want: %q", err, errRef)
} else if test.ptr != nil {
if e, ok := err.(*jsonptrerror.UnmarshalTypeError); ok {
if e.Pointer.String() != test.ptr.(string) {
t.Errorf("ptr = %q, want: %q", e.Pointer, test.ptr)
}
//t.Log(e.UnmarshalTypeError.Error())
//t.Log(e.Error())
} else {
t.Errorf("err = %q, want *jsponptrerror.UnmarshalTypeError", err)
}
}
return err
}
checkErr(decodeAll(jsonptrerror.NewDecoder(bytes.NewBufferString(test.in)), test.value))
checkErr(jsonptrerror.Unmarshal([]byte(test.in), deepcopy.Copy(test.value)))
}
}
func listTypes(num int, at func(int) reflect.Type) []string {
if num == 0 {
return nil
}
res := make([]string, 0, num)
for i := 0; i < num; i++ {
res = append(res, at(i).String())
}
return res
}
func listMethods(p interface{}) []string {
t := reflect.ValueOf(p).Type()
meths := make([]string, 0, t.NumMethod())
for i := 0; i < t.NumMethod(); i++ {
m := t.Method(i)
// Skip unexported methods
if len(m.PkgPath) != 0 {
continue
}
mt := m.Type
in := listTypes(mt.NumIn()-1, func(i int) reflect.Type { return mt.In(i + 1) })
out := listTypes(mt.NumOut(), mt.Out)
sig := bytes.NewBufferString(m.Name)
sig.WriteByte('(')
if in != nil {
sig.WriteString(strings.Join(in, ", "))
}
sig.WriteByte(')')
switch len(out) {
case 0:
case 1:
sig.WriteByte(' ')
sig.WriteString(out[0])
default:
sig.WriteString(" (")
sig.WriteString(strings.Join(out, ", "))
sig.WriteByte(')')
}
meths = append(meths, sig.String())
}
sort.Strings(meths)
return meths
}
func TestInterface(t *testing.T) {
m1 := listMethods(&json.Decoder{})
m2 := listMethods(&jsonptrerror.Decoder{})
if len(m1) == 0 {
t.Fatal("bug in listMethods")
}
if len(m1) != len(m2) {
t.Fatalf("%v != %v", m1, m2)
}
for i := range m1 {
if m1[i] != m2[i] {
t.Fatalf("decoder interface doesn't match json.Decoder: %#v != %#v\n"+
"Check: grep 'pkg encoding/json, method (\\*Decoder)' $GOROOT/api/go*.txt",
m1[i], m2[i])
}
}
}