@@ -63,12 +63,24 @@ func TestBoolFlagValueFromContext(t *testing.T) {
6363func TestBoolFlagApply_SetsCount (t * testing.T ) {
6464 v := false
6565 count := 0
66- fl := BoolFlag {Name : "wat" , Aliases : []string {"W" , "huh" }, Destination : & v , Count : & count }
67- set := flag .NewFlagSet ("test" , 0 )
68- err := fl .Apply (set )
69- expect (t , err , nil )
66+ app := & App {
67+ Name : "foo" ,
68+ Flags : []Flag {
69+ & BoolFlag {Name : "wat" , Aliases : []string {"W" , "huh" }, Destination : & v , Count : & count },
70+ },
71+ }
7072
71- err = set .Parse ([]string {"--wat" , "-W" , "--huh" })
73+ err := app .Run ([]string {"foo" , "--wat" , "-W" , "--huh" })
74+ if err == nil {
75+ t .Error ("Expected error" )
76+ } else if es := err .Error (); ! strings .Contains (es , "Cannot use two forms of the same flag" ) {
77+ t .Errorf ("Unexpected error %s" , es )
78+ }
79+
80+ v = false
81+ count = 0
82+
83+ err = app .Run ([]string {"foo" , "--wat" , "--wat" , "--wat" })
7284 expect (t , err , nil )
7385 expect (t , v , true )
7486 expect (t , count , 3 )
@@ -82,7 +94,12 @@ func TestBoolFlagCountFromContext(t *testing.T) {
8294 expectedCount int
8395 }{
8496 {
85- input : []string {"-tf" , "-w" , "-huh" },
97+ input : []string {"foo" , "-tf" },
98+ expectedVal : true ,
99+ expectedCount : 1 ,
100+ },
101+ {
102+ input : []string {"foo" , "-tf" , "-tf" , "-tf" },
86103 expectedVal : true ,
87104 expectedCount : 3 ,
88105 },
@@ -94,16 +111,21 @@ func TestBoolFlagCountFromContext(t *testing.T) {
94111 }
95112
96113 for _ , bct := range boolCountTests {
97- set := flag .NewFlagSet ("test" , 0 )
98- ctx := NewContext (nil , set , nil )
99114 tf := & BoolFlag {Name : "tf" , Aliases : []string {"w" , "huh" }}
100- err := tf .Apply (set )
101- expect (t , err , nil )
115+ app := & App {
116+ Name : "foo" ,
117+ Flags : []Flag {tf },
118+ Action : func (ctx * Context ) error {
119+ expect (t , tf .Get (ctx ), bct .expectedVal )
120+ expect (t , ctx .Count ("tf" ), bct .expectedCount )
121+ expect (t , ctx .Count ("w" ), bct .expectedCount )
122+ expect (t , ctx .Count ("huh" ), bct .expectedCount )
123+ return nil
124+ },
125+ }
126+
127+ app .Run (bct .input )
102128
103- err = set .Parse (bct .input )
104- expect (t , err , nil )
105- expect (t , tf .Get (ctx ), bct .expectedVal )
106- expect (t , ctx .Count ("tf" ), bct .expectedCount )
107129 }
108130}
109131
0 commit comments