github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+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("ParseIntValue", func() { 18 Context("when nil is provided", func() { 19 It("sets IsSet to false", func() { 20 nullInt.ParseIntValue(nil) 21 Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false})) 22 }) 23 }) 24 25 Context("when non-nil pointer is provided", func() { 26 It("sets IsSet to true and Value to provided value", func() { 27 n := 5 28 nullInt.ParseIntValue(&n) 29 Expect(nullInt).To(Equal(NullInt{Value: 5, IsSet: true})) 30 }) 31 }) 32 }) 33 34 Describe("ParseStringValue", func() { 35 Context("when the empty string is provided", func() { 36 It("sets IsSet to false", func() { 37 err := nullInt.ParseStringValue("") 38 Expect(err).ToNot(HaveOccurred()) 39 Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false})) 40 }) 41 }) 42 43 Context("when an invalid integer is provided", func() { 44 It("returns an error", func() { 45 err := nullInt.ParseStringValue("abcdef") 46 Expect(err).To(HaveOccurred()) 47 Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false})) 48 }) 49 }) 50 51 Context("when a valid integer is provided", func() { 52 It("stores the integer and sets IsSet to true", func() { 53 err := nullInt.ParseStringValue("0") 54 Expect(err).ToNot(HaveOccurred()) 55 Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: true})) 56 }) 57 }) 58 }) 59 60 Describe("UnmarshalJSON", func() { 61 Context("when integer value is provided", func() { 62 It("parses JSON number correctly", func() { 63 err := nullInt.UnmarshalJSON([]byte("42")) 64 Expect(err).ToNot(HaveOccurred()) 65 Expect(nullInt).To(Equal(NullInt{Value: 42, IsSet: true})) 66 }) 67 }) 68 69 Context("when empty json is provided", func() { 70 It("returns an unset NullInt", func() { 71 err := nullInt.UnmarshalJSON([]byte(`""`)) 72 Expect(err).ToNot(HaveOccurred()) 73 Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false})) 74 }) 75 }) 76 }) 77 78 DescribeTable("MarshalJSON", 79 func(nullInt NullInt, expectedBytes []byte) { 80 bytes, err := nullInt.MarshalJSON() 81 Expect(err).ToNot(HaveOccurred()) 82 Expect(bytes).To(Equal(expectedBytes)) 83 }, 84 Entry("negative number", NullInt{IsSet: true, Value: -1}, []byte("-1")), 85 Entry("positive number", NullInt{IsSet: true, Value: 1}, []byte("1")), 86 Entry("0", NullInt{IsSet: true, Value: 0}, []byte("0")), 87 Entry("no value", NullInt{IsSet: false}, []byte("null")), 88 ) 89 })