-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstrictjson_test.go
152 lines (131 loc) · 3.23 KB
/
strictjson_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
package strictjson
import (
"encoding/json"
"strings"
"testing"
)
const input1 = `{"a": 5, "b": "six", "c": [1,2,3]}`
const input2 = `{"a": 5, "B": "six", "c": [1,2,3], "d": 0.5}`
const input3 = `{"a": 5, "b": "six", "c": [1,2,3], "d": 0.5, "e": "foo"}`
const invalid = `:5, "b": 10}`
func TestCheckInvalidJSON(t *testing.T) {
type Foo struct {
A int `json:"a"`
B string `json:"b"`
C []int `json:"c"`
D float64 `json:"d"`
}
var f Foo
if err := Check([]byte(invalid), &f); err == nil {
t.Error("expected error")
}
}
func TestCheckExtraKey(t *testing.T) {
type Foo struct {
A int `json:"a"`
B string `json:"b"`
C []int `json:"c"`
D float64 `json:"d"`
}
var f Foo
if err := Check([]byte(input3), &f); err != nil {
t.Error(err)
}
}
func TestCheckMissingKey(t *testing.T) {
type Foo struct {
A int `json:"a"`
B string `json:"b"`
C []int `json:"c"`
D float64 `json:"d"`
}
var f Foo
if err := Check([]byte(input1), f); err == nil {
t.Errorf("expected error")
} else if !strings.Contains(err.Error(), "[d]") {
t.Errorf("bad error: %s", err)
}
if err := Check([]byte(input2), f); err == nil {
t.Errorf("expected error")
} else if !strings.Contains(err.Error(), "[b]") {
t.Errorf("bad error: %s", err)
}
// Repeat with pointer to f
if err := Check([]byte(input1), &f); err == nil {
t.Errorf("expected error")
} else if !strings.Contains(err.Error(), "[d]") {
t.Errorf("bad error: %s", err)
}
if err := Check([]byte(input2), &f); err == nil {
t.Errorf("expected error")
} else if !strings.Contains(err.Error(), "[b]") {
t.Errorf("bad error: %s", err)
}
}
func TestCheckOmitEmpty(t *testing.T) {
type Foo struct {
A int `json:"a"`
B string `json:"b"`
C []int `json:"c"`
D float64 `json:"d,omitempty"`
}
var f Foo
if err := Check([]byte(input1), &f); err != nil {
t.Error(err)
}
if err := Check([]byte(input2), &f); err == nil {
t.Error("expected error")
} else if !strings.Contains(err.Error(), "[b]") {
t.Errorf("bad error: %s", err)
}
if err := Check([]byte(input3), &f); err != nil {
t.Error(err)
}
}
func TestCheckInvalidTarget(t *testing.T) {
if err := Check(nil, 1); err == nil {
t.Error("expected error")
} else if !strings.Contains(err.Error(), "non-struct") {
t.Errorf("bad error: %s", err)
}
m := make(map[string]interface{})
if err := Check(nil, &m); err == nil {
t.Error("expected error")
} else if !strings.Contains(err.Error(), "non-struct") {
t.Errorf("bad error: %s", err)
}
}
func TestOmitField(t *testing.T) {
var f Foo
if err := Check([]byte(`{"b": "foobar"}`), &f, "a"); err != nil {
t.Error(err)
}
}
type Foo struct {
A int `json:"a"`
B string `json:"b"`
}
func (f *Foo) UnmarshalJSON(b []byte) error {
type __ Foo
var g __
if err := Check(b, f); err != nil {
return err
}
if err := json.Unmarshal(b, &g); err != nil {
return err
}
*f = Foo(g)
return nil
}
func TestUnmarshalJSON(t *testing.T) {
var f Foo
if err := json.Unmarshal([]byte(`{"a": 10, "b": "foobar"}`), &f); err != nil {
t.Error(err)
}
if want := (Foo{10, "foobar"}); f != want {
t.Errorf("bad unmarshal: want %+v, got %+v", want, f)
}
if err := json.Unmarshal([]byte(`{"b": "foobar"}`), &f); err == nil {
t.Error("expected error")
}
}