-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode_bind_fast_more_test.go
More file actions
121 lines (106 loc) · 3.4 KB
/
Copy pathdecode_bind_fast_more_test.go
File metadata and controls
121 lines (106 loc) · 3.4 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
package jsonkit
import (
"fmt"
"reflect"
"testing"
)
func TestByteBinderStringAndNumberScanBranches(t *testing.T) {
b := &byteBinder{data: []byte(`"ok"`)}
s, end, err := b.parseJSONString()
if err != nil || s != "ok" || end != len(`"ok"`) {
t.Fatalf("parseJSONString success mismatch: s=%q end=%d err=%v", s, end, err)
}
for _, raw := range [][]byte{
[]byte(`"bad\x"`),
[]byte("\"\x01\""),
[]byte(`"\uD800"`),
[]byte(`"\u12"`),
} {
b = &byteBinder{data: raw}
if _, _, err := b.parseJSONString(); err == nil {
t.Fatalf("expected parseJSONString error for %q", string(raw))
}
}
cases := []struct {
in string
want bool
}{
{"0", true},
{"-0.2", true},
{"1e+2", true},
{"01", false},
{"1.", false},
{"1e", false},
}
for _, tc := range cases {
end, ok := scanStrictJSONNumber([]byte(tc.in), 0)
if ok != tc.want {
t.Fatalf("scanStrictJSONNumber(%q) ok=%v want=%v", tc.in, ok, tc.want)
}
if ok && end != len(tc.in) {
t.Fatalf("scanStrictJSONNumber(%q) end=%d want=%d", tc.in, end, len(tc.in))
}
}
}
func TestByteBinderBindIntoAndContainerBranches(t *testing.T) {
b := &byteBinder{data: []byte(`123`)}
var withMethods fmt.Stringer
if err := b.bindInto(reflect.ValueOf(&withMethods).Elem()); err == nil {
t.Fatalf("expected interface-with-methods bind error")
}
b = &byteBinder{data: []byte(`[1,2,3]`)}
var arr [1]int
if err := b.bindArray(reflect.ValueOf(&arr).Elem(), 0); err != nil {
t.Fatalf("array bind should ignore overflow elements: %v", err)
}
if arr[0] != 1 {
t.Fatalf("expected first array element to decode")
}
b = &byteBinder{data: []byte(`[1]`)}
var notArray bool
if err := b.bindArray(reflect.ValueOf(¬Array).Elem(), 0); err == nil {
t.Fatalf("expected bindArray type error for bool destination")
}
b = &byteBinder{data: []byte(`{"a":"x"}`)}
var badMap map[int]int
if err := b.bindObjectToMap(reflect.ValueOf(&badMap).Elem()); err == nil {
t.Fatalf("expected bindObjectToMap key-type error")
}
b = &byteBinder{data: []byte(`12`)}
var p *int
if err := b.bindInto(reflect.ValueOf(&p).Elem()); err != nil {
t.Fatalf("bindInto pointer allocation path failed: %v", err)
}
if p == nil || *p != 12 {
t.Fatalf("unexpected pointer decode result: %+v", p)
}
}
func TestByteBinderParseAnyAndObjectKeyBranches(t *testing.T) {
b := &byteBinder{data: []byte(`1e+`)}
if _, err := b.parseAny(); err == nil {
t.Fatalf("expected parseAny invalid-number error")
}
b = &byteBinder{data: []byte(`{"a"1}`)}
if _, err := b.parseAny(); err == nil {
t.Fatalf("expected parseAny object syntax error")
}
b = &byteBinder{data: []byte(`123`)}
var n int
if err := b.bindIntoStringTagged(reflect.ValueOf(&n).Elem()); err == nil {
t.Fatalf("expected bindIntoStringTagged unquoted error")
}
b = &byteBinder{data: []byte(`"ab"`)}
raw, key, escaped, err := b.parseObjectKey()
if err != nil || escaped || string(raw) != "ab" || key != "" {
t.Fatalf("parseObjectKey raw path mismatch: raw=%q key=%q escaped=%v err=%v", string(raw), key, escaped, err)
}
b = &byteBinder{data: []byte(`"a\u0062"`)}
raw, key, escaped, err = b.parseObjectKey()
if err != nil || !escaped || raw != nil || key != "ab" {
t.Fatalf("parseObjectKey escaped path mismatch: raw=%v key=%q escaped=%v err=%v", raw, key, escaped, err)
}
b = &byteBinder{data: []byte("\"\x01\"")}
if _, _, _, err := b.parseObjectKey(); err == nil {
t.Fatalf("expected parseObjectKey control-char error")
}
}