github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/types/null_int_test.go (about)

     1  package types_test
     2  
     3  import (
     4  	. "code.cloudfoundry.org/cli/types"
     5  	"github.com/jessevdk/go-flags"
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/ginkgo/extensions/table"
     8  	. "github.com/onsi/gomega"
     9  )
    10  
    11  var _ = Describe("NullInt", func() {
    12  	var nullInt NullInt
    13  
    14  	BeforeEach(func() {
    15  		nullInt = NullInt{
    16  			IsSet: true,
    17  			Value: 0xBAD,
    18  		}
    19  	})
    20  
    21  	Describe("IsValidValue", func() {
    22  		var (
    23  			input      string
    24  			executeErr error
    25  		)
    26  
    27  		JustBeforeEach(func() {
    28  			executeErr = nullInt.IsValidValue(input)
    29  		})
    30  
    31  		When("the value is a positive integer", func() {
    32  			BeforeEach(func() {
    33  				input = "1"
    34  			})
    35  
    36  			It("does not error", func() {
    37  				Expect(executeErr).ToNot(HaveOccurred())
    38  			})
    39  		})
    40  
    41  		When("the value is a negative integer", func() {
    42  			BeforeEach(func() {
    43  				input = "-21"
    44  			})
    45  
    46  			It("does not error", func() {
    47  				Expect(executeErr).ToNot(HaveOccurred())
    48  			})
    49  		})
    50  
    51  		When("the value is a non integer", func() {
    52  			BeforeEach(func() {
    53  				input = "not-a-integer"
    54  			})
    55  
    56  			It("returns an error", func() {
    57  				Expect(executeErr).To(MatchError("invalid integer value `not-a-integer`"))
    58  			})
    59  		})
    60  	})
    61  
    62  	Describe("ParseIntValue", func() {
    63  		When("nil is provided", func() {
    64  			It("sets IsSet to false", func() {
    65  				nullInt.ParseIntValue(nil)
    66  				Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false}))
    67  			})
    68  		})
    69  
    70  		When("non-nil pointer is provided", func() {
    71  			It("sets IsSet to true and Value to provided value", func() {
    72  				n := 5
    73  				nullInt.ParseIntValue(&n)
    74  				Expect(nullInt).To(Equal(NullInt{Value: 5, IsSet: true}))
    75  			})
    76  		})
    77  	})
    78  
    79  	Describe("ParseStringValue", func() {
    80  		When("the empty string is provided", func() {
    81  			It("sets IsSet to false", func() {
    82  				err := nullInt.ParseStringValue("")
    83  				Expect(err).ToNot(HaveOccurred())
    84  				Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false}))
    85  			})
    86  		})
    87  
    88  		When("an invalid integer is provided", func() {
    89  			It("returns an error", func() {
    90  				err := nullInt.ParseStringValue("abcdef")
    91  				Expect(err).To(MatchError(&flags.Error{
    92  					Type:    flags.ErrMarshal,
    93  					Message: "invalid integer value `abcdef`",
    94  				}))
    95  				Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false}))
    96  			})
    97  		})
    98  
    99  		When("a valid integer is provided", func() {
   100  			It("stores the integer and sets IsSet to true", func() {
   101  				err := nullInt.ParseStringValue("0")
   102  				Expect(err).ToNot(HaveOccurred())
   103  				Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: true}))
   104  			})
   105  		})
   106  	})
   107  
   108  	Describe("UnmarshalJSON", func() {
   109  		When("integer value is provided", func() {
   110  			It("parses JSON number correctly", func() {
   111  				err := nullInt.UnmarshalJSON([]byte("42"))
   112  				Expect(err).ToNot(HaveOccurred())
   113  				Expect(nullInt).To(Equal(NullInt{Value: 42, IsSet: true}))
   114  			})
   115  		})
   116  
   117  		When("a null value is provided", func() {
   118  			It("returns an unset NullInt", func() {
   119  				err := nullInt.UnmarshalJSON([]byte("null"))
   120  				Expect(err).ToNot(HaveOccurred())
   121  				Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false}))
   122  			})
   123  		})
   124  
   125  		When("an empty string is provided", func() {
   126  			It("returns an unset NullInt", func() {
   127  				err := nullInt.UnmarshalJSON([]byte(""))
   128  				Expect(err).ToNot(HaveOccurred())
   129  				Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false}))
   130  			})
   131  		})
   132  	})
   133  
   134  	DescribeTable("MarshalJSON",
   135  		func(nullInt NullInt, expectedBytes []byte) {
   136  			bytes, err := nullInt.MarshalJSON()
   137  			Expect(err).ToNot(HaveOccurred())
   138  			Expect(bytes).To(Equal(expectedBytes))
   139  		},
   140  		Entry("negative number", NullInt{IsSet: true, Value: -1}, []byte("-1")),
   141  		Entry("positive number", NullInt{IsSet: true, Value: 1}, []byte("1")),
   142  		Entry("0", NullInt{IsSet: true, Value: 0}, []byte("0")),
   143  		Entry("no value", NullInt{IsSet: false}, []byte("null")),
   144  	)
   145  })