|
1 | 1 | package pkg |
2 | 2 |
|
3 | 3 | import ( |
| 4 | +"errors" |
4 | 5 | "reflect" |
5 | 6 | "testing" |
6 | 7 | ) |
@@ -140,3 +141,106 @@ func TestUniqueStringAppend(t *testing.T) { |
140 | 141 | }) |
141 | 142 | } |
142 | 143 | } |
| 144 | + |
| 145 | +func TestBoolFlag_String(t *testing.T) { |
| 146 | +tests := []struct { |
| 147 | +name string |
| 148 | +set bool |
| 149 | +value bool |
| 150 | +expected string |
| 151 | +}{ |
| 152 | +{"Unset flag", false, false, "not set"}, |
| 153 | +{"Set flag to true", true, true, "true"}, |
| 154 | +{"Set flag to false", true, false, "false"}, |
| 155 | +} |
| 156 | + |
| 157 | +for _, tt := range tests { |
| 158 | +t.Run(tt.name, func(t *testing.T) { |
| 159 | +b := &BoolFlag{ |
| 160 | +set: tt.set, |
| 161 | +value: tt.value, |
| 162 | +} |
| 163 | +if got := b.String(); got != tt.expected { |
| 164 | +t.Errorf("BoolFlag.String() = %v, want %v", got, tt.expected) |
| 165 | +} |
| 166 | +}) |
| 167 | +} |
| 168 | +} |
| 169 | + |
| 170 | +func TestBoolFlag_Set(t *testing.T) { |
| 171 | +tests := []struct { |
| 172 | +name string |
| 173 | +input string |
| 174 | +expectedSet bool |
| 175 | +expectedValue bool |
| 176 | +expectedErr error |
| 177 | +}{ |
| 178 | +{"Set true", "true", true, true, nil}, |
| 179 | +{"Set false", "false", true, false, nil}, |
| 180 | +{"Set invalid", "invalid", false, false, errors.New("invalid boolean value")}, |
| 181 | +} |
| 182 | + |
| 183 | +for _, tt := range tests { |
| 184 | +t.Run(tt.name, func(t *testing.T) { |
| 185 | +b := &BoolFlag{} |
| 186 | +err := b.Set(tt.input) |
| 187 | +if (err != nil) != (tt.expectedErr != nil) { |
| 188 | +t.Errorf("BoolFlag.Set() error = %v, expectedErr %v", err, tt.expectedErr) |
| 189 | +return |
| 190 | +} |
| 191 | +if err != nil && err.Error() != tt.expectedErr.Error() { |
| 192 | +t.Errorf("BoolFlag.Set() error = %v, expectedErr %v", err, tt.expectedErr) |
| 193 | +} |
| 194 | +if b.set != tt.expectedSet { |
| 195 | +t.Errorf("BoolFlag.set = %v, expected %v", b.set, tt.expectedSet) |
| 196 | +} |
| 197 | +if b.value != tt.expectedValue { |
| 198 | +t.Errorf("BoolFlag.value = %v, expected %v", b.value, tt.expectedValue) |
| 199 | +} |
| 200 | +}) |
| 201 | +} |
| 202 | +} |
| 203 | + |
| 204 | +func TestBoolFlag_IsSet(t *testing.T) { |
| 205 | +tests := []struct { |
| 206 | +name string |
| 207 | +set bool |
| 208 | +expected bool |
| 209 | +}{ |
| 210 | +{"Flag is not set", false, false}, |
| 211 | +{"Flag is set", true, true}, |
| 212 | +} |
| 213 | + |
| 214 | +for _, tt := range tests { |
| 215 | +t.Run(tt.name, func(t *testing.T) { |
| 216 | +b := &BoolFlag{ |
| 217 | +set: tt.set, |
| 218 | +} |
| 219 | +if got := b.IsSet(); got != tt.expected { |
| 220 | +t.Errorf("BoolFlag.IsSet() = %v, want %v", got, tt.expected) |
| 221 | +} |
| 222 | +}) |
| 223 | +} |
| 224 | +} |
| 225 | + |
| 226 | +func TestBoolFlag_GetValue(t *testing.T) { |
| 227 | +tests := []struct { |
| 228 | +name string |
| 229 | +value bool |
| 230 | +expected bool |
| 231 | +}{ |
| 232 | +{"Flag value is false", false, false}, |
| 233 | +{"Flag value is true", true, true}, |
| 234 | +} |
| 235 | + |
| 236 | +for _, tt := range tests { |
| 237 | +t.Run(tt.name, func(t *testing.T) { |
| 238 | +b := &BoolFlag{ |
| 239 | +value: tt.value, |
| 240 | +} |
| 241 | +if got := b.Value(); got != tt.expected { |
| 242 | +t.Errorf("BoolFlag.GetValue() = %v, want %v", got, tt.expected) |
| 243 | +} |
| 244 | +}) |
| 245 | +} |
| 246 | +} |
0 commit comments