-
Notifications
You must be signed in to change notification settings - Fork 3
/
e2e_test.go
122 lines (115 loc) · 2.83 KB
/
e2e_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
package jsonry_test
import (
"code.cloudfoundry.org/jsonry"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("end to end", func() {
It("marshals and unmarshals symmetrically", func() {
type space struct {
Name string `jsonry:"name,omitempty"`
GUID string `jsonry:"guid"`
}
type s struct {
Num float32 `json:"number"`
Spaces []space `jsonry:"relationships.space.data"`
Orgs []string `jsonry:"relationships.orgs.data.guids"`
OtherSpace []space `jsonry:"other_space[].data"`
Type string `jsonry:"authentication.type"`
Username string `jsonry:"authentication.credentials.username"`
Password string `jsonry:"authentication.credentials.password"`
Labels map[string]nullString `jsonry:"metadata.labels"`
Annotations map[string]space `jsonry:"metadata.annotations"`
}
o := s{
Num: 12,
Spaces: []space{{GUID: "foo"}, {Name: "Bar", GUID: "bar"}},
Orgs: []string{"baz", "quz"},
OtherSpace: []space{{GUID: "alpha"}, {Name: "Beta", GUID: "beta"}},
Type: "basic",
Username: "fake-user",
Password: "fake secret",
Labels: map[string]nullString{"first": {value: "one"}, "second": {null: true}},
Annotations: map[string]space{"alpha": {GUID: "foo"}, "beta": {Name: "Bar", GUID: "bar"}},
}
r, err := jsonry.Marshal(o)
Expect(err).NotTo(HaveOccurred())
Expect(r).To(MatchJSON(`
{
"number": 12,
"relationships": {
"orgs": {
"data": {
"guids": ["baz","quz"]
}
},
"space": {
"data": [
{"guid": "foo"},
{"guid": "bar","name": "Bar"}
]
}
},
"authentication": {
"type": "basic",
"credentials": {
"username": "fake-user",
"password": "fake secret"
}
},
"metadata": {
"annotations": {
"alpha": {
"guid": "foo"
},
"beta": {
"guid": "bar",
"name": "Bar"
}
},
"labels": {
"first": "one",
"second": null
}
},
"other_space": [
{
"data": {"guid": "alpha"}
},
{
"data": {"guid": "beta","name": "Beta"}
}
]
}`))
var t s
err = jsonry.Unmarshal(r, &t)
Expect(err).NotTo(HaveOccurred())
Expect(t).To(Equal(o))
})
It("produces error messages that point to the cause", func() {
type a struct{ A string }
var s struct {
S map[string][]a
}
json := `
{
"S": {
"foo": [
{"A": "one"},
{"A": "two"},
{"A": 12}
]
}
}`
err := jsonry.Unmarshal([]byte(json), &s)
Expect(err).To(
MatchError(`cannot unmarshal "12" type "number" into field "A" (type "string") path S["foo"][2].A`),
func() string {
if err == nil {
return "did not error"
}
return err.Error()
},
)
})
})