github.com/sleungcy/cli@v7.1.0+incompatible/types/null_bool_test.go (about)

     1  package types_test
     2  
     3  import (
     4  	. "code.cloudfoundry.org/cli/types"
     5  	. "github.com/onsi/ginkgo"
     6  	. "github.com/onsi/ginkgo/extensions/table"
     7  	. "github.com/onsi/gomega"
     8  )
     9  
    10  var _ = Describe("NullBool", func() {
    11  	var nullBool NullBool
    12  
    13  	BeforeEach(func() {
    14  		nullBool = NullBool{}
    15  	})
    16  
    17  	Describe("ParseBoolValue", func() {
    18  		When("nil is provided", func() {
    19  			It("sets IsSet to false", func() {
    20  				nullBool.ParseBoolValue(nil)
    21  				Expect(nullBool).To(Equal(NullBool{Value: false, IsSet: false}))
    22  			})
    23  		})
    24  
    25  		When("non-nil pointer is provided", func() {
    26  			It("sets IsSet to true and Value to provided value", func() {
    27  				n := true
    28  				nullBool.ParseBoolValue(&n)
    29  				Expect(nullBool).To(Equal(NullBool{Value: true, IsSet: true}))
    30  			})
    31  		})
    32  	})
    33  
    34  	Describe("ParseStringValue", func() {
    35  		When("the empty string is provided", func() {
    36  			It("sets IsSet to false", func() {
    37  				err := nullBool.ParseStringValue("")
    38  				Expect(err).ToNot(HaveOccurred())
    39  				Expect(nullBool).To(Equal(NullBool{Value: false, IsSet: false}))
    40  			})
    41  		})
    42  
    43  		When("an invalid integer is provided", func() {
    44  			It("returns an error", func() {
    45  				err := nullBool.ParseStringValue("abcdef")
    46  				Expect(err).To(HaveOccurred())
    47  				Expect(nullBool).To(Equal(NullBool{Value: false, IsSet: false}))
    48  			})
    49  		})
    50  
    51  		When("a valid integer is provided", func() {
    52  			It("stores the bool and sets IsSet to true", func() {
    53  				err := nullBool.ParseStringValue("true")
    54  				Expect(err).ToNot(HaveOccurred())
    55  				Expect(nullBool).To(Equal(NullBool{Value: true, IsSet: true}))
    56  			})
    57  		})
    58  	})
    59  
    60  	Describe("UnmarshalJSON", func() {
    61  		When("integer value is provided", func() {
    62  			It("parses JSON number correctly", func() {
    63  				err := nullBool.UnmarshalJSON([]byte("true"))
    64  				Expect(err).ToNot(HaveOccurred())
    65  				Expect(nullBool).To(Equal(NullBool{Value: true, IsSet: true}))
    66  			})
    67  		})
    68  
    69  		When("empty json is provided", func() {
    70  			It("returns an unset NullBool", func() {
    71  				err := nullBool.UnmarshalJSON([]byte("null"))
    72  				Expect(err).ToNot(HaveOccurred())
    73  				Expect(nullBool).To(Equal(NullBool{Value: false, IsSet: false}))
    74  			})
    75  		})
    76  	})
    77  
    78  	DescribeTable("MarshalJSON",
    79  		func(nullBool NullBool, expectedBytes []byte) {
    80  			bytes, err := nullBool.MarshalJSON()
    81  			Expect(err).ToNot(HaveOccurred())
    82  			Expect(bytes).To(Equal(expectedBytes))
    83  		},
    84  		Entry("true when set", NullBool{IsSet: true, Value: true}, []byte("true")),
    85  		Entry("false when set", NullBool{IsSet: true, Value: false}, []byte("false")),
    86  		Entry("no value", NullBool{IsSet: false}, []byte("null")),
    87  	)
    88  })