github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/command/flag/megabytes_test.go (about)

     1  package flag_test
     2  
     3  import (
     4  	. "code.cloudfoundry.org/cli/command/flag"
     5  	flags "github.com/jessevdk/go-flags"
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  )
     9  
    10  var _ = Describe("Megabytes", func() {
    11  	var megabytes Megabytes
    12  
    13  	Describe("UnmarshalFlag", func() {
    14  		BeforeEach(func() {
    15  			megabytes = Megabytes{}
    16  		})
    17  
    18  		Context("when the suffix is M", func() {
    19  			It("interprets the number as megabytes", func() {
    20  				err := megabytes.UnmarshalFlag("17M")
    21  				Expect(err).ToNot(HaveOccurred())
    22  				Expect(megabytes.Size).To(BeEquivalentTo(17))
    23  			})
    24  		})
    25  
    26  		Context("when the suffix is MB", func() {
    27  			It("interprets the number as megabytes", func() {
    28  				err := megabytes.UnmarshalFlag("19MB")
    29  				Expect(err).ToNot(HaveOccurred())
    30  				Expect(megabytes.Size).To(BeEquivalentTo(19))
    31  			})
    32  		})
    33  
    34  		Context("when the suffix is G", func() {
    35  			It("interprets the number as gigabytes", func() {
    36  				err := megabytes.UnmarshalFlag("2G")
    37  				Expect(err).ToNot(HaveOccurred())
    38  				Expect(megabytes.Size).To(BeEquivalentTo(2048))
    39  			})
    40  		})
    41  
    42  		Context("when the suffix is GB", func() {
    43  			It("interprets the number as gigabytes", func() {
    44  				err := megabytes.UnmarshalFlag("3GB")
    45  				Expect(err).ToNot(HaveOccurred())
    46  				Expect(megabytes.Size).To(BeEquivalentTo(3072))
    47  			})
    48  		})
    49  
    50  		Context("when the suffix is lowercase", func() {
    51  			It("is case insensitive", func() {
    52  				err := megabytes.UnmarshalFlag("7m")
    53  				Expect(err).ToNot(HaveOccurred())
    54  				Expect(megabytes.Size).To(BeEquivalentTo(7))
    55  			})
    56  		})
    57  
    58  		Context("when the megabytes are invalid", func() {
    59  			It("returns an error", func() {
    60  				err := megabytes.UnmarshalFlag("invalid")
    61  				Expect(err).To(MatchError(&flags.Error{
    62  					Type:    flags.ErrRequired,
    63  					Message: `Byte quantity must be an integer with a unit of measurement like M, MB, G, or GB`,
    64  				}))
    65  			})
    66  		})
    67  
    68  		Context("when a decimal is used", func() {
    69  			It("returns an error", func() {
    70  				err := megabytes.UnmarshalFlag("1.2M")
    71  				Expect(err).To(MatchError(&flags.Error{
    72  					Type:    flags.ErrRequired,
    73  					Message: `Byte quantity must be an integer with a unit of measurement like M, MB, G, or GB`,
    74  				}))
    75  			})
    76  		})
    77  
    78  		Context("when the units are missing", func() {
    79  			It("returns an error", func() {
    80  				err := megabytes.UnmarshalFlag("37")
    81  				Expect(err).To(MatchError(&flags.Error{
    82  					Type:    flags.ErrRequired,
    83  					Message: `Byte quantity must be an integer with a unit of measurement like M, MB, G, or GB`,
    84  				}))
    85  			})
    86  		})
    87  
    88  		Context("when the suffix is B", func() {
    89  			It("returns an error", func() {
    90  				err := megabytes.UnmarshalFlag("10B")
    91  				Expect(err).To(MatchError(&flags.Error{
    92  					Type:    flags.ErrRequired,
    93  					Message: `Byte quantity must be an integer with a unit of measurement like M, MB, G, or GB`,
    94  				}))
    95  			})
    96  		})
    97  		// DescribeTable("downcases and sets type",
    98  		// 	func(settingType string, expectedType string) {
    99  		// 		err := healthCheck.UnmarshalFlag(settingType)
   100  		// 		Expect(err).ToNot(HaveOccurred())
   101  		// 		Expect(healthCheck.Type).To(Equal(expectedType))
   102  		// 	},
   103  		// 	Entry("sets 'port' when passed 'port'", "port", "port"),
   104  		// 	Entry("sets 'port' when passed 'pOrt'", "pOrt", "port"),
   105  		// 	Entry("sets 'process' when passed 'none'", "none", "none"),
   106  		// 	Entry("sets 'process' when passed 'process'", "process", "process"),
   107  		// 	Entry("sets 'http' when passed 'http'", "http", "http"),
   108  		// )
   109  
   110  		// Context("when passed anything else", func() {
   111  		// 	It("returns an error", func() {
   112  		// 		err := healthCheck.UnmarshalFlag("banana")
   113  		// 		Expect(err).To(MatchError(&flags.Error{
   114  		// 			Type:    flags.ErrRequired,
   115  		// 			Message: `HEALTH_CHECK_TYPE must be "port", "process", or "http"`,
   116  		// 		}))
   117  		// 		Expect(healthCheck.Type).To(BeEmpty())
   118  		// 	})
   119  		// })
   120  	})
   121  })