github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+incompatible/command/flag/port_test.go (about)

     1  package flag_test
     2  
     3  import (
     4  	. "code.cloudfoundry.org/cli/command/flag"
     5  	"code.cloudfoundry.org/cli/types"
     6  	flags "github.com/jessevdk/go-flags"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  )
    10  
    11  var _ = Describe("Port", func() {
    12  	var port Port
    13  	BeforeEach(func() {
    14  		port = Port{}
    15  	})
    16  
    17  	Describe("UnmarshalFlag", func() {
    18  		When("the empty string is provided", func() {
    19  			It("sets IsSet to false", func() {
    20  				err := port.UnmarshalFlag("")
    21  				Expect(err).ToNot(HaveOccurred())
    22  				Expect(port).To(Equal(Port{NullInt: types.NullInt{Value: 0, IsSet: false}}))
    23  			})
    24  		})
    25  
    26  		When("an invalid integer is provided", func() {
    27  			It("returns an error", func() {
    28  				err := port.UnmarshalFlag("abcdef")
    29  				Expect(err).To(MatchError(&flags.Error{
    30  					Type:    flags.ErrRequired,
    31  					Message: "invalid argument for flag '--port' (expected int > 0)",
    32  				}))
    33  				Expect(port).To(Equal(Port{NullInt: types.NullInt{Value: 0, IsSet: false}}))
    34  			})
    35  		})
    36  
    37  		When("a negative integer is provided", func() {
    38  			It("returns an error", func() {
    39  				err := port.UnmarshalFlag("-10")
    40  				Expect(err).To(MatchError(&flags.Error{
    41  					Type:    flags.ErrRequired,
    42  					Message: "invalid argument for flag '--port' (expected int > 0)",
    43  				}))
    44  				Expect(port).To(Equal(Port{NullInt: types.NullInt{Value: -10, IsSet: true}}))
    45  			})
    46  		})
    47  
    48  		When("a valid integer is provided", func() {
    49  			It("stores the integer and sets IsSet to true", func() {
    50  				err := port.UnmarshalFlag("0")
    51  				Expect(err).ToNot(HaveOccurred())
    52  				Expect(port).To(Equal(Port{NullInt: types.NullInt{Value: 0, IsSet: true}}))
    53  			})
    54  		})
    55  	})
    56  })