forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptional_object_test.go
79 lines (69 loc) · 1.93 KB
/
optional_object_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
package types_test
import (
"code.cloudfoundry.org/cli/types"
"code.cloudfoundry.org/jsonry"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("optional object", func() {
It("has an unset zero value", func() {
var s types.OptionalObject
Expect(s.IsSet).To(BeFalse())
Expect(s.Value).To(SatisfyAll(BeEmpty(), BeNil()))
})
It("has a set empty value", func() {
s := types.NewOptionalObject(map[string]interface{}{})
Expect(s.IsSet).To(BeTrue())
Expect(s.Value).To(BeEmpty())
Expect(s.Value).NotTo(BeNil())
t := types.NewOptionalObject(nil)
Expect(t.IsSet).To(BeTrue())
Expect(t.Value).To(BeEmpty())
Expect(t.Value).NotTo(BeNil())
Expect(s).To(Equal(t))
})
When("marshaling", func() {
It("can marshal to an object", func() {
s := struct {
S types.OptionalObject
}{
S: types.NewOptionalObject(map[string]interface{}{"foo": "bar"}),
}
Expect(jsonry.Marshal(s)).To(MatchJSON(`{"S":{"foo":"bar"}}`))
})
It("can marshal to an empty object", func() {
s := struct {
S types.OptionalObject
}{
S: types.NewOptionalObject(map[string]interface{}{}),
}
Expect(jsonry.Marshal(s)).To(MatchJSON(`{"S":{}}`))
})
It("can be omitted during marshaling", func() {
var s struct {
S types.OptionalObject
}
Expect(jsonry.Marshal(s)).To(MatchJSON(`{}`))
})
})
When("unmarshaling", func() {
It("can be unmarshaled from an empty object", func() {
var s struct {
S types.OptionalObject
}
Expect(jsonry.Unmarshal([]byte(`{"S":{}}`), &s)).NotTo(HaveOccurred())
Expect(s.S.IsSet).To(BeTrue())
Expect(s.S.Value).To(BeEmpty())
Expect(s.S.Value).NotTo(BeNil())
})
It("can be unmarshaled from null", func() {
var s struct {
S types.OptionalObject
}
Expect(jsonry.Unmarshal([]byte(`{"S":null}`), &s)).NotTo(HaveOccurred())
Expect(s.S.IsSet).To(BeTrue())
Expect(s.S.Value).To(BeEmpty())
Expect(s.S.Value).NotTo(BeNil())
})
})
})