code.cloudfoundry.org/cli@v7.1.0+incompatible/cf/formatters/bytes_test.go (about)

     1  package formatters_test
     2  
     3  import (
     4  	. "code.cloudfoundry.org/cli/cf/formatters"
     5  	. "github.com/onsi/ginkgo"
     6  	. "github.com/onsi/gomega"
     7  )
     8  
     9  var _ = Describe("formatting bytes to / from strings", func() {
    10  	Describe("ByteSize()", func() {
    11  		It("converts bytes to a human readable description", func() {
    12  			Expect(ByteSize(100 * MEGABYTE)).To(Equal("100M"))
    13  			Expect(ByteSize(100 * GIGABYTE)).To(Equal("100G"))
    14  			Expect(ByteSize(int64(100.5 * MEGABYTE))).To(Equal("100.5M"))
    15  			Expect(ByteSize(int64(50))).To(Equal("50B"))
    16  		})
    17  
    18  		It("returns 0 byte as '0' without any unit", func() {
    19  			Expect(ByteSize(int64(0))).To(Equal("0"))
    20  		})
    21  	})
    22  
    23  	It("parses byte amounts with short units (e.g. M, G)", func() {
    24  		var (
    25  			megabytes int64
    26  			err       error
    27  		)
    28  
    29  		megabytes, err = ToMegabytes("5M")
    30  		Expect(megabytes).To(Equal(int64(5)))
    31  		Expect(err).NotTo(HaveOccurred())
    32  
    33  		megabytes, err = ToMegabytes("5m")
    34  		Expect(megabytes).To(Equal(int64(5)))
    35  		Expect(err).NotTo(HaveOccurred())
    36  
    37  		megabytes, err = ToMegabytes("2G")
    38  		Expect(megabytes).To(Equal(int64(2 * 1024)))
    39  		Expect(err).NotTo(HaveOccurred())
    40  
    41  		megabytes, err = ToMegabytes("3T")
    42  		Expect(megabytes).To(Equal(int64(3 * 1024 * 1024)))
    43  		Expect(err).NotTo(HaveOccurred())
    44  	})
    45  
    46  	It("parses byte amounts with long units (e.g MB, GB)", func() {
    47  		var (
    48  			megabytes int64
    49  			err       error
    50  		)
    51  
    52  		megabytes, err = ToMegabytes("5MB")
    53  		Expect(megabytes).To(Equal(int64(5)))
    54  		Expect(err).NotTo(HaveOccurred())
    55  
    56  		megabytes, err = ToMegabytes("5mb")
    57  		Expect(megabytes).To(Equal(int64(5)))
    58  		Expect(err).NotTo(HaveOccurred())
    59  
    60  		megabytes, err = ToMegabytes("2GB")
    61  		Expect(megabytes).To(Equal(int64(2 * 1024)))
    62  		Expect(err).NotTo(HaveOccurred())
    63  
    64  		megabytes, err = ToMegabytes("3TB")
    65  		Expect(megabytes).To(Equal(int64(3 * 1024 * 1024)))
    66  		Expect(err).NotTo(HaveOccurred())
    67  	})
    68  
    69  	It("returns an error when the unit is missing", func() {
    70  		_, err := ToMegabytes("5")
    71  		Expect(err).To(HaveOccurred())
    72  		Expect(err.Error()).To(ContainSubstring("unit of measurement"))
    73  	})
    74  
    75  	It("returns an error when the unit is unrecognized", func() {
    76  		_, err := ToMegabytes("5MBB")
    77  		Expect(err).To(HaveOccurred())
    78  		Expect(err.Error()).To(ContainSubstring("unit of measurement"))
    79  	})
    80  
    81  	It("allows whitespace before and after the value", func() {
    82  		megabytes, err := ToMegabytes("\t\n\r 5MB ")
    83  		Expect(megabytes).To(Equal(int64(5)))
    84  		Expect(err).NotTo(HaveOccurred())
    85  	})
    86  })