github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/types/optional_string_slice_test.go (about) 1 package types_test 2 3 import ( 4 "code.cloudfoundry.org/cli/types" 5 "code.cloudfoundry.org/jsonry" 6 . "github.com/onsi/ginkgo" 7 . "github.com/onsi/gomega" 8 ) 9 10 var _ = Describe("optional string slice", func() { 11 It("has an unset zero value", func() { 12 var s types.OptionalStringSlice 13 Expect(s.IsSet).To(BeFalse()) 14 Expect(s.Value).To(SatisfyAll(BeEmpty(), BeNil())) 15 }) 16 17 It("can be converted to a string", func() { 18 Expect(types.OptionalStringSlice{}.String()).To(Equal("")) 19 Expect(types.NewOptionalStringSlice().String()).To(Equal("")) 20 Expect(types.NewOptionalStringSlice("foo", "bar", "baz").String()).To(Equal("foo, bar, baz")) 21 }) 22 23 When("marshaling", func() { 24 It("can marshal to a slice", func() { 25 s := struct { 26 S types.OptionalStringSlice 27 }{ 28 S: types.NewOptionalStringSlice("foo", "bar"), 29 } 30 Expect(jsonry.Marshal(s)).To(MatchJSON(`{"S":["foo","bar"]}`)) 31 }) 32 33 It("can marshal to an empty slice", func() { 34 s := struct { 35 S types.OptionalStringSlice 36 }{ 37 S: types.NewOptionalStringSlice(), 38 } 39 Expect(jsonry.Marshal(s)).To(MatchJSON(`{"S":[]}`)) 40 }) 41 42 It("can be omitted during marshaling", func() { 43 var s struct { 44 S types.OptionalStringSlice 45 } 46 Expect(jsonry.Marshal(s)).To(MatchJSON(`{}`)) 47 }) 48 }) 49 50 When("unmarshaling", func() { 51 It("can be unmarshaled from an empty slice", func() { 52 var s struct { 53 S types.OptionalStringSlice 54 } 55 Expect(jsonry.Unmarshal([]byte(`{"S":[]}`), &s)).NotTo(HaveOccurred()) 56 Expect(s.S.IsSet).To(BeTrue()) 57 Expect(s.S.Value).To(SatisfyAll(BeEmpty(), BeNil())) 58 }) 59 60 It("can be unmarshaled from null", func() { 61 var s struct { 62 S types.OptionalStringSlice 63 } 64 Expect(jsonry.Unmarshal([]byte(`{"S":null}`), &s)).NotTo(HaveOccurred()) 65 Expect(s.S.IsSet).To(BeTrue()) 66 Expect(s.S.Value).To(SatisfyAll(BeEmpty(), BeNil())) 67 }) 68 }) 69 })