github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/command/flag/bytes_with_unlimited_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("BytesWithUnlimited", func() {
    11  	var bytesWithUnlimited BytesWithUnlimited
    12  
    13  	Describe("UnmarshalFlag", func() {
    14  		BeforeEach(func() {
    15  			bytesWithUnlimited = BytesWithUnlimited{}
    16  		})
    17  
    18  		When("the suffix is B", func() {
    19  			It("interprets the number as bytes", func() {
    20  				err := bytesWithUnlimited.UnmarshalFlag("17B")
    21  				Expect(err).ToNot(HaveOccurred())
    22  				Expect(bytesWithUnlimited.Value).To(BeEquivalentTo(17))
    23  			})
    24  		})
    25  
    26  		When("the suffix is K", func() {
    27  			It("interprets the number as kilobytes", func() {
    28  				err := bytesWithUnlimited.UnmarshalFlag("64K")
    29  				Expect(err).ToNot(HaveOccurred())
    30  				Expect(bytesWithUnlimited.Value).To(BeEquivalentTo(64 * 1024))
    31  			})
    32  		})
    33  
    34  		When("the suffix is KB", func() {
    35  			It("interprets the number as kilobytes", func() {
    36  				err := bytesWithUnlimited.UnmarshalFlag("64KB")
    37  				Expect(err).ToNot(HaveOccurred())
    38  				Expect(bytesWithUnlimited.Value).To(BeEquivalentTo(64 * 1024))
    39  			})
    40  		})
    41  
    42  		When("the suffix is M", func() {
    43  			It("interprets the number as megabytes", func() {
    44  				err := bytesWithUnlimited.UnmarshalFlag("17M")
    45  				Expect(err).ToNot(HaveOccurred())
    46  				Expect(bytesWithUnlimited.Value).To(BeEquivalentTo(17 * 1024 * 1024))
    47  			})
    48  		})
    49  
    50  		When("the suffix is MB", func() {
    51  			It("interprets the number as megabytes", func() {
    52  				err := bytesWithUnlimited.UnmarshalFlag("19MB")
    53  				Expect(err).ToNot(HaveOccurred())
    54  				Expect(bytesWithUnlimited.Value).To(BeEquivalentTo(19 * 1024 * 1024))
    55  			})
    56  		})
    57  
    58  		When("the suffix is G", func() {
    59  			It("interprets the number as gigabytes", func() {
    60  				err := bytesWithUnlimited.UnmarshalFlag("2G")
    61  				Expect(err).ToNot(HaveOccurred())
    62  				Expect(bytesWithUnlimited.Value).To(BeEquivalentTo(2 * 1024 * 1024 * 1024))
    63  			})
    64  		})
    65  
    66  		When("the suffix is GB", func() {
    67  			It("interprets the number as gigabytes", func() {
    68  				err := bytesWithUnlimited.UnmarshalFlag("3GB")
    69  				Expect(err).ToNot(HaveOccurred())
    70  				Expect(bytesWithUnlimited.Value).To(BeEquivalentTo(3 * 1024 * 1024 * 1024))
    71  			})
    72  		})
    73  
    74  		When("the value is -1 (unlimited)", func() {
    75  			It("interprets the number as -1 (unlimited)", func() {
    76  				err := bytesWithUnlimited.UnmarshalFlag("-1")
    77  				Expect(err).ToNot(HaveOccurred())
    78  				Expect(bytesWithUnlimited.Value).To(BeEquivalentTo(-1))
    79  			})
    80  
    81  			It("accepts units", func() {
    82  				err := bytesWithUnlimited.UnmarshalFlag("-1T")
    83  				Expect(err).ToNot(HaveOccurred())
    84  				Expect(bytesWithUnlimited.Value).To(BeEquivalentTo(-1))
    85  			})
    86  		})
    87  
    88  		When("the value is 0", func() {
    89  			It("sets the value to 0", func() {
    90  				err := bytesWithUnlimited.UnmarshalFlag("0")
    91  				Expect(err).ToNot(HaveOccurred())
    92  				Expect(bytesWithUnlimited.Value).To(BeEquivalentTo(0))
    93  			})
    94  
    95  			It("accepts units", func() {
    96  				err := bytesWithUnlimited.UnmarshalFlag("0TB")
    97  				Expect(err).ToNot(HaveOccurred())
    98  				Expect(bytesWithUnlimited.Value).To(BeEquivalentTo(0))
    99  			})
   100  		})
   101  
   102  		When("the suffix is lowercase", func() {
   103  			It("is case insensitive", func() {
   104  				err := bytesWithUnlimited.UnmarshalFlag("7m")
   105  				Expect(err).ToNot(HaveOccurred())
   106  				Expect(bytesWithUnlimited.Value).To(BeEquivalentTo(7 * 1024 * 1024))
   107  			})
   108  		})
   109  
   110  		When("the value is invalid", func() {
   111  			It("returns an error", func() {
   112  				err := bytesWithUnlimited.UnmarshalFlag("invalid")
   113  				Expect(err).To(MatchError(&flags.Error{
   114  					Type:    flags.ErrRequired,
   115  					Message: `Byte quantity must be an integer with a unit of measurement like B, K, KB, M, MB, G, or GB`,
   116  				}))
   117  			})
   118  		})
   119  
   120  		When("a decimal is used", func() {
   121  			It("returns an error", func() {
   122  				err := bytesWithUnlimited.UnmarshalFlag("1.2M")
   123  				Expect(err).To(MatchError(&flags.Error{
   124  					Type:    flags.ErrRequired,
   125  					Message: `Byte quantity must be an integer with a unit of measurement like B, K, KB, M, MB, G, or GB`,
   126  				}))
   127  			})
   128  		})
   129  
   130  		When("the value is too large", func() {
   131  			It("returns an error", func() {
   132  				err := bytesWithUnlimited.UnmarshalFlag("9999999TB")
   133  				Expect(err).To(MatchError(&flags.Error{
   134  					Type:    flags.ErrRequired,
   135  					Message: `Byte quantity must be an integer with a unit of measurement like B, K, KB, M, MB, G, or GB`,
   136  				}))
   137  			})
   138  		})
   139  
   140  		When("the units are missing", func() {
   141  			It("returns an error", func() {
   142  				err := bytesWithUnlimited.UnmarshalFlag("37")
   143  				Expect(err).To(MatchError(&flags.Error{
   144  					Type:    flags.ErrRequired,
   145  					Message: `Byte quantity must be an integer with a unit of measurement like B, K, KB, M, MB, G, or GB`,
   146  				}))
   147  			})
   148  		})
   149  
   150  		When("value is empty", func() {
   151  			It("sets IsSet to false", func() {
   152  				err := bytesWithUnlimited.UnmarshalFlag("")
   153  				Expect(err).NotTo(HaveOccurred())
   154  				Expect(bytesWithUnlimited.IsSet).To(BeFalse())
   155  			})
   156  		})
   157  	})
   158  })