-
Notifications
You must be signed in to change notification settings - Fork 3
/
ex_test.go
59 lines (53 loc) · 1.11 KB
/
ex_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
package checkjson
import (
"fmt"
"testing"
)
func TestEx(t *testing.T) {
fmt.Println("===================== TestEx ...")
data := `
{
"elem1":"a simple element",
"elem2": {
"subelem":"something more complex",
"notes" :"take a look at this",
"strange": {
"irr":3,
"strange_ex":"extraneous_but_ignore"
}
},
"elem4":"extraneous"
}`
type strange struct {
Irrelevant int
}
type sub struct {
Subelem string `json:"subelem,omitempty"`
Another string `json:"another"`
Strange strange `checkjson:"norecurse"`
}
type elem struct {
Elem1 string `json:"elem1"`
Elem2 sub `json:"elem2"`
Elem3 bool `json:"elem3"`
}
e := new(elem)
result, err := MissingJSONKeys([]byte(data), e)
if err != nil {
t.Fatal("MissingJSONKeys:", err)
}
want := `[elem2.another elem3]`
s := fmt.Sprint(result)
if s != want {
t.Fatal("MissingJSONKeys", s, "!=", want)
}
result, err = UnknownJSONKeys([]byte(data), e)
if err != nil {
t.Fatal("UnknownJSONKeys:", err)
}
want = `[elem2.notes elem4]`
s = fmt.Sprint(result)
if s != want {
t.Fatal("UnknownJSONKeys", s, "!=", want)
}
}