-
Notifications
You must be signed in to change notification settings - Fork 14
/
unmarshal_test.go
98 lines (93 loc) · 3.15 KB
/
unmarshal_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
package refmt
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/polydawn/refmt/json"
"github.com/polydawn/refmt/obj/atlas"
)
func TestUnmarshal(t *testing.T) {
Convey("json", t, func() {
Convey("string", func() {
var slot string
bs := []byte(`"str"`)
err := Unmarshal(json.DecodeOptions{}, bs, &slot)
So(err, ShouldBeNil)
So(slot, ShouldEqual, "str")
})
Convey("map", func() {
var slot map[string]string
bs := []byte(`{"x":"1"}`)
err := Unmarshal(json.DecodeOptions{}, bs, &slot)
So(err, ShouldBeNil)
So(slot, ShouldResemble, map[string]string{"x": "1"})
})
Convey("map comma handling", func() {
var slot map[string]string
bs := []byte(`{"x":"1","y":"2"}`)
err := Unmarshal(json.DecodeOptions{}, bs, &slot)
So(err, ShouldBeNil)
So(slot, ShouldResemble, map[string]string{"x": "1", "y": "2"})
})
Convey("map comma handling errors", func() {
for _, tc := range []struct {
name string
j string
err string
}{
{"trailing commas", `{"x":"1","y":"2",,,}`, "invalid char while expecting start of key: comma"},
{"just commas", `{,,,}`, "invalid char while expecting start of key: comma"},
{"leading commas", `{,,,"x":"1","y":"2",,,}`, "invalid char while expecting start of key: comma"},
{"no commas", `{"x":"1""y":"2"}`, "expected comma or map close after map value; got quote"},
{"no commas, just spaces", `{ "x":"1" "y":"2" }`, "expected comma or map close after map value; got quote"},
{"no commas, just tabs", `{ "x":"1" "y":"2" }`, "expected comma or map close after map value; got quote"},
} {
Convey(tc.name, func() {
var slot map[string]string
err := Unmarshal(json.DecodeOptions{}, []byte(tc.j), &slot)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, tc.err)
})
}
})
Convey("array comma handling errors", func() {
for _, tc := range []struct {
name string
j string
err string
}{
{"trailing commas", `["1","2",,,]`, "invalid char while expecting start of value: comma"},
{"just commas", `[,,,]`, "invalid char while expecting start of value: comma"},
{"leading commas", `[,,,"1","2",,,]`, "invalid char while expecting start of value: comma"},
{"no commas", `["1""2"]`, "expected comma or array close after array value; got quote"},
{"no commas, just spaces", `[ "1" "2" ]`, "expected comma or array close after array value; got quote"},
{"no commas, just tabs", `[ "1" "2" ]`, "expected comma or array close after array value; got quote"},
} {
Convey(tc.name, func() {
var slot []string
err := Unmarshal(json.DecodeOptions{}, []byte(tc.j), &slot)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, tc.err)
})
}
})
})
}
func TestUnmarshalAtlased(t *testing.T) {
Convey("json", t, func() {
Convey("obj", func() {
type testObj struct {
X string
Y string
}
var slot testObj
atl := atlas.MustBuild(
atlas.BuildEntry(testObj{}).
StructMap().Autogenerate().
Complete(),
)
bs := []byte(`{"x":"1","y":"2"}`)
err := UnmarshalAtlased(json.DecodeOptions{}, bs, &slot, atl)
So(err, ShouldBeNil)
})
})
}