github.com/arunkumar7540/cli@v6.45.0+incompatible/types/null_int_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("NullInt", func() { 11 var nullInt NullInt 12 13 BeforeEach(func() { 14 nullInt = NullInt{} 15 }) 16 17 Describe("IsValidValue", func() { 18 var ( 19 input string 20 executeErr error 21 ) 22 23 JustBeforeEach(func() { 24 executeErr = nullInt.IsValidValue(input) 25 }) 26 27 When("the value is a positive integer", func() { 28 BeforeEach(func() { 29 input = "1" 30 }) 31 32 It("does not error", func() { 33 Expect(executeErr).ToNot(HaveOccurred()) 34 }) 35 }) 36 37 When("the value is a negative integer", func() { 38 BeforeEach(func() { 39 input = "-21" 40 }) 41 42 It("does not error", func() { 43 Expect(executeErr).ToNot(HaveOccurred()) 44 }) 45 }) 46 47 When("the value is a non integer", func() { 48 BeforeEach(func() { 49 input = "not-a-integer" 50 }) 51 52 It("returns an error", func() { 53 Expect(executeErr).To(HaveOccurred()) 54 }) 55 }) 56 }) 57 58 Describe("ParseIntValue", func() { 59 When("nil is provided", func() { 60 It("sets IsSet to false", func() { 61 nullInt.ParseIntValue(nil) 62 Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false})) 63 }) 64 }) 65 66 When("non-nil pointer is provided", func() { 67 It("sets IsSet to true and Value to provided value", func() { 68 n := 5 69 nullInt.ParseIntValue(&n) 70 Expect(nullInt).To(Equal(NullInt{Value: 5, IsSet: true})) 71 }) 72 }) 73 }) 74 75 Describe("ParseStringValue", func() { 76 When("the empty string is provided", func() { 77 It("sets IsSet to false", func() { 78 err := nullInt.ParseStringValue("") 79 Expect(err).ToNot(HaveOccurred()) 80 Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false})) 81 }) 82 }) 83 84 When("an invalid integer is provided", func() { 85 It("returns an error", func() { 86 err := nullInt.ParseStringValue("abcdef") 87 Expect(err).To(HaveOccurred()) 88 Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false})) 89 }) 90 }) 91 92 When("a valid integer is provided", func() { 93 It("stores the integer and sets IsSet to true", func() { 94 err := nullInt.ParseStringValue("0") 95 Expect(err).ToNot(HaveOccurred()) 96 Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: true})) 97 }) 98 }) 99 }) 100 101 Describe("UnmarshalJSON", func() { 102 When("integer value is provided", func() { 103 It("parses JSON number correctly", func() { 104 err := nullInt.UnmarshalJSON([]byte("42")) 105 Expect(err).ToNot(HaveOccurred()) 106 Expect(nullInt).To(Equal(NullInt{Value: 42, IsSet: true})) 107 }) 108 }) 109 110 When("empty json is provided", func() { 111 It("returns an unset NullInt", func() { 112 err := nullInt.UnmarshalJSON([]byte(`""`)) 113 Expect(err).ToNot(HaveOccurred()) 114 Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false})) 115 }) 116 }) 117 }) 118 119 DescribeTable("MarshalJSON", 120 func(nullInt NullInt, expectedBytes []byte) { 121 bytes, err := nullInt.MarshalJSON() 122 Expect(err).ToNot(HaveOccurred()) 123 Expect(bytes).To(Equal(expectedBytes)) 124 }, 125 Entry("negative number", NullInt{IsSet: true, Value: -1}, []byte("-1")), 126 Entry("positive number", NullInt{IsSet: true, Value: 1}, []byte("1")), 127 Entry("0", NullInt{IsSet: true, Value: 0}, []byte("0")), 128 Entry("no value", NullInt{IsSet: false}, []byte("null")), 129 ) 130 })