-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjsontype_test.go
64 lines (58 loc) · 1.25 KB
/
jsontype_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
package main
import (
"encoding/json"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestDetectTypeOfItem(t *testing.T) {
cases := []struct {
json string
typ *JSONType
}{
{
json: `[
{"prop1": true}
]`,
typ: &JSONType{Object: map[string]*JSONType{"prop1": &JSONType{IsBoolean: true}}},
},
{
json: `[
{"prop1": 1}
]`,
typ: &JSONType{Object: map[string]*JSONType{"prop1": &JSONType{IsInteger: true}}},
},
{
json: `[
{"prop1": 3.14}
]`,
typ: &JSONType{Object: map[string]*JSONType{"prop1": &JSONType{IsNumber: true}}},
},
{
json: `[
{"prop1": "foo"}
]`,
typ: &JSONType{Object: map[string]*JSONType{"prop1": &JSONType{IsString: true}}},
},
{
json: `[
{"prop1": ["foo", "bar", "baz"]}
]`,
typ: &JSONType{Object: map[string]*JSONType{"prop1": &JSONType{Array: &JSONType{IsString: true}}}},
},
}
for _, c := range cases {
var v interface{}
err := json.NewDecoder(strings.NewReader(c.json)).Decode(&v)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
got := detectTypeInStructure(v, []string{"slice"})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if diff := cmp.Diff(c.typ, got); diff != "" {
t.Fatalf("diff: %s", diff)
}
}
}