github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/cf/flags/flags_usage_test.go (about)

     1  package flags_test
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/LukasHeimann/cloudfoundrycli/v8/cf/flags"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  )
    10  
    11  var _ = Describe("ShowUsage", func() {
    12  	var fc flags.FlagContext
    13  
    14  	Context("when given a flag with a longname", func() {
    15  		BeforeEach(func() {
    16  			fc = flags.New()
    17  			fc.NewIntFlag("flag-a", "", "")
    18  		})
    19  
    20  		It("prints the longname with two hyphens", func() {
    21  			outputs := fc.ShowUsage(0)
    22  			Expect(outputs).To(ContainSubstring("--flag-a"))
    23  		})
    24  	})
    25  
    26  	Context("when given a flag with a longname and usage", func() {
    27  		BeforeEach(func() {
    28  			fc = flags.New()
    29  			fc.NewIntFlag("flag-a", "", "Usage for flag-a")
    30  		})
    31  
    32  		It("prints the longname with two hyphens followed by the usage", func() {
    33  			outputs := fc.ShowUsage(0)
    34  			Expect(outputs).To(ContainSubstring("--flag-a      Usage for flag-a"))
    35  		})
    36  	})
    37  
    38  	Context("when given a flag with a longname and a shortname and usage", func() {
    39  		BeforeEach(func() {
    40  			fc = flags.New()
    41  			fc.NewIntFlag("flag-a", "a", "Usage for flag-a")
    42  		})
    43  
    44  		It("prints the longname with two hyphens followed by the shortname followed by the usage", func() {
    45  			outputs := fc.ShowUsage(0)
    46  			Expect(outputs).To(ContainSubstring("--flag-a, -a      Usage for flag-a"))
    47  		})
    48  	})
    49  
    50  	Context("when given a flag with a longname and a shortname", func() {
    51  		BeforeEach(func() {
    52  			fc = flags.New()
    53  			fc.NewIntFlag("flag-a", "a", "")
    54  		})
    55  
    56  		It("prints the longname with two hyphens followed by the shortname with one hyphen", func() {
    57  			outputs := fc.ShowUsage(0)
    58  			Expect(outputs).To(ContainSubstring("--flag-a, -a"))
    59  		})
    60  	})
    61  
    62  	Context("when given a flag with a shortname", func() {
    63  		BeforeEach(func() {
    64  			fc = flags.New()
    65  			fc.NewIntFlag("", "a", "")
    66  		})
    67  
    68  		It("prints the shortname with one hyphen", func() {
    69  			outputs := fc.ShowUsage(0)
    70  			Expect(outputs).To(ContainSubstring("-a"))
    71  		})
    72  	})
    73  
    74  	Context("when given a flag with a shortname and usage", func() {
    75  		BeforeEach(func() {
    76  			fc = flags.New()
    77  			fc.NewIntFlag("", "a", "Usage for a")
    78  		})
    79  
    80  		It("prints the shortname with one hyphen followed by the usage", func() {
    81  			outputs := fc.ShowUsage(0)
    82  			Expect(outputs).To(MatchRegexp("^-a      Usage for a"))
    83  		})
    84  	})
    85  
    86  	Context("when showing usage for multiple flags", func() {
    87  		BeforeEach(func() {
    88  			fc = flags.New()
    89  			fc.NewIntFlag("flag-a", "a", "Usage for flag-a")
    90  			fc.NewStringFlag("flag-b", "", "")
    91  			fc.NewBoolFlag("flag-c", "c", "Usage for flag-c")
    92  		})
    93  
    94  		It("prints each flag on its own line", func() {
    95  			outputs := fc.ShowUsage(0)
    96  			Expect(outputs).To(ContainSubstring("--flag-a, -a      Usage for flag-a\n"))
    97  			Expect(outputs).To(ContainSubstring("--flag-b\n"))
    98  			Expect(outputs).To(ContainSubstring("--flag-c, -c      Usage for flag-c"))
    99  		})
   100  	})
   101  
   102  	Context("when given a non-zero integer for padding", func() {
   103  		BeforeEach(func() {
   104  			fc = flags.New()
   105  			fc.NewIntFlag("flag-a", "", "")
   106  		})
   107  
   108  		It("prefixes the flag name with the number of spaces requested", func() {
   109  			outputs := fc.ShowUsage(5)
   110  			Expect(outputs).To(ContainSubstring("     --flag-a"))
   111  		})
   112  	})
   113  
   114  	Context("when showing usage for multiple flags", func() {
   115  		BeforeEach(func() {
   116  			fc = flags.New()
   117  			fc.NewIntFlag("foo-a", "a", "Usage for foo-a")
   118  			fc.NewStringFlag("someflag-b", "", "Usage for someflag-b")
   119  			fc.NewBoolFlag("foo-c", "c", "Usage for foo-c")
   120  			fc.NewBoolFlag("", "d", "Usage for d")
   121  		})
   122  
   123  		It("aligns the text by padding string with spaces", func() {
   124  			outputs := fc.ShowUsage(0)
   125  			Expect(outputs).To(ContainSubstring("-d                Usage for d"))
   126  			Expect(outputs).To(ContainSubstring("--foo-a, -a       Usage for foo-a"))
   127  			Expect(outputs).To(ContainSubstring("--foo-c, -c       Usage for foo-c"))
   128  			Expect(outputs).To(ContainSubstring("--someflag-b      Usage for someflag-b"))
   129  		})
   130  
   131  		It("prints the flags in order", func() {
   132  			for i := 0; i < 10; i++ {
   133  				outputs := fc.ShowUsage(0)
   134  
   135  				outputLines := strings.Split(outputs, "\n")
   136  
   137  				Expect(outputLines).To(Equal([]string{
   138  					"-d                Usage for d",
   139  					"--foo-a, -a       Usage for foo-a",
   140  					"--foo-c, -c       Usage for foo-c",
   141  					"--someflag-b      Usage for someflag-b",
   142  				}))
   143  			}
   144  		})
   145  
   146  		Context("hidden flag", func() {
   147  			BeforeEach(func() {
   148  				fs := make(map[string]flags.FlagSet)
   149  				fs["hostname"] = &flags.StringFlag{Name: "hostname", ShortName: "n", Usage: "Hostname used to identify the HTTP route", Hidden: true}
   150  				fs["path"] = &flags.StringFlag{Name: "path", Usage: "Path used to identify the HTTP route"}
   151  				fc = flags.NewFlagContext(fs)
   152  			})
   153  
   154  			It("prints the flags in order", func() {
   155  				output := fc.ShowUsage(0)
   156  
   157  				outputLines := strings.Split(output, "\n")
   158  
   159  				Expect(outputLines).To(Equal([]string{
   160  					"--path      Path used to identify the HTTP route",
   161  				}))
   162  			})
   163  		})
   164  	})
   165  })