github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/flags/flag_constructor_test.go (about)

     1  package flags_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/flags"
     5  	. "github.com/onsi/ginkgo"
     6  	. "github.com/onsi/gomega"
     7  )
     8  
     9  var _ = Describe("Flag Constructors", func() {
    10  
    11  	var (
    12  		fc flags.FlagContext
    13  	)
    14  
    15  	BeforeEach(func() {
    16  		fc = flags.New()
    17  	})
    18  
    19  	Describe("NewStringFlag()", func() {
    20  		It("init the flag context with a new string flagset", func() {
    21  			fc.Parse("-s", "test")
    22  			Expect(fc.IsSet("s")).To(BeFalse())
    23  			Expect(fc.String("s")).To(Equal(""))
    24  
    25  			fc.NewStringFlag("s", "s2", "setting new string flag")
    26  			fc.Parse("-s", "test2")
    27  			Expect(fc.IsSet("s")).To(BeTrue())
    28  			Expect(fc.IsSet("s2")).To(BeTrue())
    29  			Expect(fc.String("s")).To(Equal("test2"))
    30  			Expect(fc.String("s2")).To(Equal("test2"))
    31  		})
    32  	})
    33  
    34  	Describe("NewStringFlagWithDefault()", func() {
    35  		It("init the flag context with a new string flagset with default value", func() {
    36  			fc.Parse("-s", "test")
    37  			Expect(fc.IsSet("s")).To(BeFalse())
    38  			Expect(fc.String("s")).To(Equal(""))
    39  
    40  			fc.NewStringFlagWithDefault("s", "s2", "setting new string flag", "barz")
    41  			fc.Parse()
    42  			Expect(fc.IsSet("s")).To(BeTrue())
    43  			Expect(fc.IsSet("s2")).To(BeTrue())
    44  			Expect(fc.String("s")).To(Equal("barz"))
    45  			Expect(fc.String("s2")).To(Equal("barz"))
    46  		})
    47  	})
    48  
    49  	Describe("NewBoolFlag()", func() {
    50  		It("init the flag context with a new bool flagset", func() {
    51  			fc.Parse("--force")
    52  			Expect(fc.IsSet("force")).To(BeFalse())
    53  
    54  			fc.NewBoolFlag("force", "f", "force process")
    55  			fc.Parse("--force")
    56  			Expect(fc.IsSet("force")).To(BeTrue())
    57  			Expect(fc.IsSet("f")).To(BeTrue())
    58  			Expect(fc.Bool("force")).To(BeTrue())
    59  			Expect(fc.Bool("f")).To(BeTrue())
    60  		})
    61  	})
    62  
    63  	Describe("NewIntFlag()", func() {
    64  		It("init the flag context with a new int flagset", func() {
    65  			fc.Parse("-i", "5")
    66  			Expect(fc.IsSet("i")).To(BeFalse())
    67  			Expect(fc.Int("i")).To(Equal(0))
    68  
    69  			fc.NewIntFlag("i", "i2", "setting new int flag")
    70  			fc.Parse("-i", "5")
    71  			Expect(fc.IsSet("i")).To(BeTrue())
    72  			Expect(fc.IsSet("i2")).To(BeTrue())
    73  			Expect(fc.Int("i")).To(Equal(5))
    74  			Expect(fc.Int("i2")).To(Equal(5))
    75  		})
    76  	})
    77  
    78  	Describe("NewIntFlagWithDefault()", func() {
    79  		It("init the flag context with a new int flagset with default value", func() {
    80  			fc.Parse("-i", "5")
    81  			Expect(fc.IsSet("i")).To(BeFalse())
    82  			Expect(fc.Int("i")).To(Equal(0))
    83  
    84  			fc.NewIntFlagWithDefault("i", "i2", "setting new int flag", 10)
    85  			fc.Parse()
    86  			Expect(fc.IsSet("i")).To(BeTrue())
    87  			Expect(fc.IsSet("i2")).To(BeTrue())
    88  			Expect(fc.Int("i")).To(Equal(10))
    89  			Expect(fc.Int("i2")).To(Equal(10))
    90  		})
    91  	})
    92  
    93  	Describe("NewFloat64Flag()", func() {
    94  		It("init the flag context with a new float64 flagset", func() {
    95  			fc.Parse("-f", "5.5")
    96  			Expect(fc.IsSet("f")).To(BeFalse())
    97  			Expect(fc.Float64("f")).To(Equal(float64(0)))
    98  
    99  			fc.NewFloat64Flag("f", "f2", "setting new flag")
   100  			fc.Parse("-f", "5.5")
   101  			Expect(fc.IsSet("f")).To(BeTrue())
   102  			Expect(fc.IsSet("f2")).To(BeTrue())
   103  			Expect(fc.Float64("f")).To(Equal(5.5))
   104  			Expect(fc.Float64("f2")).To(Equal(5.5))
   105  		})
   106  	})
   107  
   108  	Describe("NewFloat64FlagWithDefault()", func() {
   109  		It("init the flag context with a new Float64 flagset with default value", func() {
   110  			fc.Parse()
   111  			Expect(fc.IsSet("i")).To(BeFalse())
   112  			Expect(fc.Float64("i")).To(Equal(float64(0)))
   113  
   114  			fc.NewFloat64FlagWithDefault("i", "i2", "setting new flag", 5.5)
   115  			fc.Parse()
   116  			Expect(fc.IsSet("i")).To(BeTrue())
   117  			Expect(fc.IsSet("i2")).To(BeTrue())
   118  			Expect(fc.Float64("i")).To(Equal(5.5))
   119  			Expect(fc.Float64("i2")).To(Equal(5.5))
   120  		})
   121  	})
   122  
   123  	Describe("NewStringSliceFlag()", func() {
   124  		It("init the flag context with a new StringSlice flagset", func() {
   125  			fc.Parse("-s", "5", "-s", "6")
   126  			Expect(fc.IsSet("s")).To(BeFalse())
   127  			Expect(fc.StringSlice("s")).To(Equal([]string{}))
   128  
   129  			fc.NewStringSliceFlag("s", "s2", "setting new StringSlice flag")
   130  			fc.Parse("-s", "5", "-s", "6")
   131  			Expect(fc.IsSet("s")).To(BeTrue())
   132  			Expect(fc.IsSet("s2")).To(BeTrue())
   133  			Expect(fc.StringSlice("s")).To(Equal([]string{"5", "6"}))
   134  			Expect(fc.StringSlice("s2")).To(Equal([]string{"5", "6"}))
   135  		})
   136  	})
   137  
   138  	Describe("NewStringSliceFlagWithDefault()", func() {
   139  		It("init the flag context with a new StringSlice flagset with default value", func() {
   140  			fc.Parse()
   141  			Expect(fc.IsSet("s")).To(BeFalse())
   142  			Expect(fc.StringSlice("s")).To(Equal([]string{}))
   143  
   144  			fc.NewStringSliceFlagWithDefault("s", "s2", "setting new StringSlice flag", []string{"5", "6", "7"})
   145  			fc.Parse()
   146  			Expect(fc.IsSet("s")).To(BeTrue())
   147  			Expect(fc.IsSet("s2")).To(BeTrue())
   148  			Expect(fc.StringSlice("s")).To(Equal([]string{"5", "6", "7"}))
   149  			Expect(fc.StringSlice("s2")).To(Equal([]string{"5", "6", "7"}))
   150  		})
   151  	})
   152  
   153  })