forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnull_string_test.go
63 lines (56 loc) · 1.81 KB
/
null_string_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
package types_test
import (
"encoding/json"
. "code.cloudfoundry.org/cli/types"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("NullString", func() {
type SamplePayload struct {
OptionalField NullString
}
Context("JSON marshaling", func() {
When("the NullString has a value", func() {
It("marshals to its value", func() {
toMarshal := SamplePayload{
OptionalField: NullString{Value: "some-string", IsSet: true},
}
bytes, err := json.Marshal(toMarshal)
Expect(err).ToNot(HaveOccurred())
Expect(string(bytes)).To(ContainSubstring(`"some-string"`))
})
})
When("the NullString has no value", func() {
It("marshals to a JSON null", func() {
toMarshal := SamplePayload{
OptionalField: NullString{IsSet: false},
}
bytes, err := json.Marshal(toMarshal)
Expect(err).ToNot(HaveOccurred())
Expect(string(bytes)).To(ContainSubstring(`null`))
})
})
})
Context("JSON unmarshaling", func() {
When("the JSON has a string value", func() {
It("unmarshals to a NullString with the correct value", func() {
toUnmarshal := []byte(`{"optionalField":"our-value"}`)
var samplePayload SamplePayload
err := json.Unmarshal(toUnmarshal, &samplePayload)
Expect(err).ToNot(HaveOccurred())
Expect(samplePayload.OptionalField.IsSet).To(BeTrue())
Expect(samplePayload.OptionalField.Value).To(Equal("our-value"))
})
})
When("the JSON has a null value", func() {
It("unmarshals to a NullString with no value", func() {
toUnmarshal := []byte(`{"optionalField":null}`)
var samplePayload SamplePayload
err := json.Unmarshal(toUnmarshal, &samplePayload)
Expect(err).ToNot(HaveOccurred())
Expect(samplePayload.OptionalField.IsSet).To(BeFalse())
Expect(samplePayload.OptionalField.Value).To(Equal(""))
})
})
})
})