@@ -84,6 +84,73 @@ func TestStringOrUndefined(t *testing.T) {
84
84
}
85
85
}
86
86
87
+ func TestBoolOrUndefinedUsage (t * testing.T ) {
88
+ var output bytes.Buffer
89
+
90
+ cmd := & cobra.Command {}
91
+ cmd .Flags ().Var (& BoolOrUndefined {}, "bool-flag" , "use it like this" )
92
+ cmd .SetOut (& output )
93
+ cmd .Usage ()
94
+
95
+ testutil .CheckDeepEqual (t , "Usage:\n \n Flags:\n --bool-flag use it like this\n " , output .String ())
96
+ }
97
+
98
+ func TestBoolOrUndefined_SetNil (t * testing.T ) {
99
+ var s BoolOrUndefined
100
+ s .Set ("false" )
101
+ testutil .CheckDeepEqual (t , "false" , s .String ())
102
+ s .SetNil ()
103
+ testutil .CheckDeepEqual (t , "" , s .String ())
104
+ testutil .CheckDeepEqual (t , (* bool )(nil ), s .value )
105
+ testutil .CheckDeepEqual (t , (* bool )(nil ), s .Value ())
106
+ }
107
+
108
+ func TestBoolOrUndefined (t * testing.T ) {
109
+ tests := []struct {
110
+ description string
111
+ args []string
112
+ expected * bool
113
+ }{
114
+ {
115
+ description : "undefined" ,
116
+ args : []string {},
117
+ expected : nil ,
118
+ },
119
+ {
120
+ description : "empty" ,
121
+ args : []string {"--bool-flag=" },
122
+ expected : nil ,
123
+ },
124
+ {
125
+ description : "invalid" ,
126
+ args : []string {"--bool-flag=invalid" },
127
+ expected : nil ,
128
+ },
129
+ {
130
+ description : "true" ,
131
+ args : []string {"--bool-flag=true" },
132
+ expected : util .BoolPtr (true ),
133
+ },
134
+ {
135
+ description : "false" ,
136
+ args : []string {"--bool-flag=false" },
137
+ expected : util .BoolPtr (false ),
138
+ },
139
+ }
140
+ for _ , test := range tests {
141
+ testutil .Run (t , test .description , func (t * testutil.T ) {
142
+ var flag BoolOrUndefined
143
+
144
+ cmd := & cobra.Command {}
145
+ cmd .Flags ().Var (& flag , "bool-flag" , "" )
146
+ cmd .SetArgs (test .args )
147
+ cmd .Execute ()
148
+
149
+ t .CheckDeepEqual (test .expected , flag .value )
150
+ })
151
+ }
152
+ }
153
+
87
154
func TestMuted (t * testing.T ) {
88
155
tests := []struct {
89
156
phases []string
0 commit comments