|
1 | 1 | // Copyright (c) Nate McMaster. |
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
3 | 3 |
|
| 4 | +using System; |
| 5 | +using System.Linq; |
4 | 6 | using Xunit; |
5 | 7 |
|
6 | 8 | namespace McMaster.Extensions.CommandLineUtils.Tests |
@@ -136,5 +138,322 @@ public void UnrecognizedArgumentHandling_DefaultsToThrow() |
136 | 138 | } |
137 | 139 |
|
138 | 140 | #endregion |
| 141 | + |
| 142 | + #region Name Property Tests |
| 143 | + |
| 144 | + [Fact] |
| 145 | + public void Name_SetToNull_ClearsNames() |
| 146 | + { |
| 147 | + // This tests lines 57-59: when Name is set to null, _names becomes empty |
| 148 | + var attr = new CommandAttribute("initial-name"); |
| 149 | + Assert.Equal("initial-name", attr.Name); |
| 150 | + |
| 151 | + attr.Name = null; |
| 152 | + |
| 153 | + Assert.Null(attr.Name); |
| 154 | + Assert.Empty(attr.Names); |
| 155 | + } |
| 156 | + |
| 157 | + [Fact] |
| 158 | + public void Name_SetToValue_CreatesSingleElementArray() |
| 159 | + { |
| 160 | + var attr = new CommandAttribute(); |
| 161 | + Assert.Null(attr.Name); |
| 162 | + |
| 163 | + attr.Name = "test-name"; |
| 164 | + |
| 165 | + Assert.Equal("test-name", attr.Name); |
| 166 | + Assert.Single(attr.Names); |
| 167 | + } |
| 168 | + |
| 169 | + [Fact] |
| 170 | + public void Names_WithMultipleNames_FirstIsName() |
| 171 | + { |
| 172 | + var attr = new CommandAttribute("primary", "alias1", "alias2"); |
| 173 | + |
| 174 | + Assert.Equal("primary", attr.Name); |
| 175 | + Assert.Equal(new[] { "primary", "alias1", "alias2" }, attr.Names); |
| 176 | + } |
| 177 | + |
| 178 | + [Fact] |
| 179 | + public void Name_Getter_ReturnsNull_WhenNamesEmpty() |
| 180 | + { |
| 181 | + var attr = new CommandAttribute(); |
| 182 | + |
| 183 | + Assert.Null(attr.Name); |
| 184 | + Assert.Empty(attr.Names); |
| 185 | + } |
| 186 | + |
| 187 | + #endregion |
| 188 | + |
| 189 | + #region Configure Method Tests |
| 190 | + |
| 191 | + [Command("cmd", "alias1", "alias2")] |
| 192 | + private class CommandWithAliases |
| 193 | + { } |
| 194 | + |
| 195 | + [Fact] |
| 196 | + public void Configure_AddsAliases_FromNames() |
| 197 | + { |
| 198 | + // This tests lines 179-182: foreach loop adding aliases |
| 199 | + var app = new CommandLineApplication<CommandWithAliases>(); |
| 200 | + app.Conventions.UseCommandAttribute(); |
| 201 | + |
| 202 | + Assert.Equal("cmd", app.Name); |
| 203 | + Assert.Contains("alias1", app.Names); |
| 204 | + Assert.Contains("alias2", app.Names); |
| 205 | + } |
| 206 | + |
| 207 | + [Command( |
| 208 | + Name = "full-test", |
| 209 | + FullName = "Full Test Command", |
| 210 | + Description = "A test description", |
| 211 | + ExtendedHelpText = "Extended help here", |
| 212 | + ShowInHelpText = false, |
| 213 | + AllowArgumentSeparator = true, |
| 214 | + ResponseFileHandling = ResponseFileHandling.ParseArgsAsSpaceSeparated, |
| 215 | + OptionsComparison = StringComparison.OrdinalIgnoreCase, |
| 216 | + UsePagerForHelpText = true)] |
| 217 | + private class CommandWithAllProperties |
| 218 | + { } |
| 219 | + |
| 220 | + [Fact] |
| 221 | + public void Configure_SetsAllProperties() |
| 222 | + { |
| 223 | + // This tests lines 184-192 in Configure method |
| 224 | + var app = new CommandLineApplication<CommandWithAllProperties>(); |
| 225 | + app.Conventions.UseCommandAttribute(); |
| 226 | + |
| 227 | + Assert.Equal("full-test", app.Name); |
| 228 | + Assert.Equal("Full Test Command", app.FullName); |
| 229 | + Assert.Equal("A test description", app.Description); |
| 230 | + Assert.Equal("Extended help here", app.ExtendedHelpText); |
| 231 | + Assert.False(app.ShowInHelpText); |
| 232 | + Assert.True(app.AllowArgumentSeparator); |
| 233 | + Assert.Equal(ResponseFileHandling.ParseArgsAsSpaceSeparated, app.ResponseFileHandling); |
| 234 | + Assert.Equal(StringComparison.OrdinalIgnoreCase, app.OptionsComparison); |
| 235 | + Assert.True(app.UsePagerForHelpText); |
| 236 | + } |
| 237 | + |
| 238 | + [Command] |
| 239 | + private class CommandWithNoName |
| 240 | + { } |
| 241 | + |
| 242 | + [Fact] |
| 243 | + public void Configure_PreservesExistingName_WhenAttributeNameIsNull() |
| 244 | + { |
| 245 | + // This tests line 177: app.Name = Name ?? app.Name |
| 246 | + var app = new CommandLineApplication<CommandWithNoName>(); |
| 247 | + app.Name = "existing-name"; |
| 248 | + app.Conventions.UseCommandAttribute(); |
| 249 | + |
| 250 | + Assert.Equal("existing-name", app.Name); |
| 251 | + } |
| 252 | + |
| 253 | + [Command("override-name")] |
| 254 | + private class CommandWithName |
| 255 | + { } |
| 256 | + |
| 257 | + [Fact] |
| 258 | + public void Configure_OverridesExistingName_WhenAttributeNameIsSet() |
| 259 | + { |
| 260 | + var app = new CommandLineApplication<CommandWithName>(); |
| 261 | + app.Name = "original-name"; |
| 262 | + app.Conventions.UseCommandAttribute(); |
| 263 | + |
| 264 | + Assert.Equal("override-name", app.Name); |
| 265 | + } |
| 266 | + |
| 267 | + [Fact] |
| 268 | + public void Configure_SetsParseCulture() |
| 269 | + { |
| 270 | + // Test line 191: app.ValueParsers.ParseCulture = ParseCulture |
| 271 | + var app = new CommandLineApplication<CommandWithAllProperties>(); |
| 272 | + app.Conventions.UseCommandAttribute(); |
| 273 | + |
| 274 | + // The default ParseCulture is CurrentCulture |
| 275 | + Assert.Equal(System.Globalization.CultureInfo.CurrentCulture, app.ValueParsers.ParseCulture); |
| 276 | + } |
| 277 | + |
| 278 | + #endregion |
| 279 | + |
| 280 | + #region Constructor Tests |
| 281 | + |
| 282 | + [Fact] |
| 283 | + public void Constructor_Default_HasEmptyNames() |
| 284 | + { |
| 285 | + var attr = new CommandAttribute(); |
| 286 | + |
| 287 | + Assert.Null(attr.Name); |
| 288 | + Assert.Empty(attr.Names); |
| 289 | + } |
| 290 | + |
| 291 | + [Fact] |
| 292 | + public void Constructor_WithSingleName_SetsName() |
| 293 | + { |
| 294 | + var attr = new CommandAttribute("single"); |
| 295 | + |
| 296 | + Assert.Equal("single", attr.Name); |
| 297 | + Assert.Single(attr.Names); |
| 298 | + } |
| 299 | + |
| 300 | + [Fact] |
| 301 | + public void Constructor_WithMultipleNames_SetsAllNames() |
| 302 | + { |
| 303 | + var attr = new CommandAttribute("primary", "secondary", "tertiary"); |
| 304 | + |
| 305 | + Assert.Equal("primary", attr.Name); |
| 306 | + Assert.Equal(3, attr.Names.Count()); |
| 307 | + } |
| 308 | + |
| 309 | + #endregion |
| 310 | + |
| 311 | + #region Configure Method Direct Tests |
| 312 | + |
| 313 | + [Fact] |
| 314 | + public void Configure_Direct_SetsAllProperties() |
| 315 | + { |
| 316 | + // Directly test the internal Configure method |
| 317 | + var attr = new CommandAttribute("test-cmd") |
| 318 | + { |
| 319 | + FullName = "Test Command Full Name", |
| 320 | + Description = "Test description", |
| 321 | + ExtendedHelpText = "Extended help text", |
| 322 | + ShowInHelpText = false, |
| 323 | + AllowArgumentSeparator = true, |
| 324 | + ResponseFileHandling = ResponseFileHandling.ParseArgsAsLineSeparated, |
| 325 | + OptionsComparison = StringComparison.OrdinalIgnoreCase, |
| 326 | + UsePagerForHelpText = true |
| 327 | + }; |
| 328 | + |
| 329 | + var app = new CommandLineApplication(); |
| 330 | + attr.Configure(app); |
| 331 | + |
| 332 | + Assert.Equal("test-cmd", app.Name); |
| 333 | + Assert.Equal("Test Command Full Name", app.FullName); |
| 334 | + Assert.Equal("Test description", app.Description); |
| 335 | + Assert.Equal("Extended help text", app.ExtendedHelpText); |
| 336 | + Assert.False(app.ShowInHelpText); |
| 337 | + Assert.True(app.AllowArgumentSeparator); |
| 338 | + Assert.Equal(ResponseFileHandling.ParseArgsAsLineSeparated, app.ResponseFileHandling); |
| 339 | + Assert.Equal(StringComparison.OrdinalIgnoreCase, app.OptionsComparison); |
| 340 | + Assert.True(app.UsePagerForHelpText); |
| 341 | + } |
| 342 | + |
| 343 | + [Fact] |
| 344 | + public void Configure_Direct_WithAliases_AddsAllNames() |
| 345 | + { |
| 346 | + // Test lines 179-182: foreach loop for aliases |
| 347 | + var attr = new CommandAttribute("primary", "alias1", "alias2"); |
| 348 | + |
| 349 | + var app = new CommandLineApplication(); |
| 350 | + attr.Configure(app); |
| 351 | + |
| 352 | + Assert.Equal("primary", app.Name); |
| 353 | + Assert.Contains("alias1", app.Names); |
| 354 | + Assert.Contains("alias2", app.Names); |
| 355 | + Assert.Equal(3, app.Names.Count()); |
| 356 | + } |
| 357 | + |
| 358 | + [Fact] |
| 359 | + public void Configure_Direct_WithNullName_PreservesExistingName() |
| 360 | + { |
| 361 | + // Test line 177: app.Name = Name ?? app.Name |
| 362 | + var attr = new CommandAttribute(); |
| 363 | + Assert.Null(attr.Name); |
| 364 | + |
| 365 | + var app = new CommandLineApplication { Name = "existing" }; |
| 366 | + attr.Configure(app); |
| 367 | + |
| 368 | + Assert.Equal("existing", app.Name); |
| 369 | + } |
| 370 | + |
| 371 | + [Fact] |
| 372 | + public void Configure_Direct_WithClusterOptionsSet_SetsValue() |
| 373 | + { |
| 374 | + // Test lines 194-197 |
| 375 | + var attr = new CommandAttribute { ClusterOptions = false }; |
| 376 | + |
| 377 | + var app = new CommandLineApplication { ClusterOptions = true }; |
| 378 | + attr.Configure(app); |
| 379 | + |
| 380 | + Assert.False(app.ClusterOptions); |
| 381 | + } |
| 382 | + |
| 383 | + [Fact] |
| 384 | + public void Configure_Direct_WithClusterOptionsNotSet_PreservesDefault() |
| 385 | + { |
| 386 | + // Test lines 194-197 (branch not taken) |
| 387 | + var attr = new CommandAttribute(); |
| 388 | + Assert.False(attr.ClusterOptionsWasSet); |
| 389 | + |
| 390 | + var app = new CommandLineApplication { ClusterOptions = false }; |
| 391 | + attr.Configure(app); |
| 392 | + |
| 393 | + // Should remain false because ClusterOptions wasn't explicitly set |
| 394 | + Assert.False(app.ClusterOptions); |
| 395 | + } |
| 396 | + |
| 397 | + [Fact] |
| 398 | + public void Configure_Direct_WithUnrecognizedArgumentHandlingSet_SetsValue() |
| 399 | + { |
| 400 | + // Test lines 199-202 |
| 401 | + var attr = new CommandAttribute |
| 402 | + { |
| 403 | + UnrecognizedArgumentHandling = UnrecognizedArgumentHandling.StopParsingAndCollect |
| 404 | + }; |
| 405 | + |
| 406 | + var app = new CommandLineApplication(); |
| 407 | + attr.Configure(app); |
| 408 | + |
| 409 | + Assert.Equal(UnrecognizedArgumentHandling.StopParsingAndCollect, app.UnrecognizedArgumentHandling); |
| 410 | + } |
| 411 | + |
| 412 | + [Fact] |
| 413 | + public void Configure_Direct_WithUnrecognizedArgumentHandlingNotSet_PreservesDefault() |
| 414 | + { |
| 415 | + // Test lines 199-202 (branch not taken) |
| 416 | + var attr = new CommandAttribute(); |
| 417 | + Assert.False(attr.UnrecognizedArgumentHandlingWasSet); |
| 418 | + |
| 419 | + var app = new CommandLineApplication |
| 420 | + { |
| 421 | + UnrecognizedArgumentHandling = UnrecognizedArgumentHandling.CollectAndContinue |
| 422 | + }; |
| 423 | + attr.Configure(app); |
| 424 | + |
| 425 | + // Should remain CollectAndContinue because UnrecognizedArgumentHandling wasn't explicitly set |
| 426 | + Assert.Equal(UnrecognizedArgumentHandling.CollectAndContinue, app.UnrecognizedArgumentHandling); |
| 427 | + } |
| 428 | + |
| 429 | + [Fact] |
| 430 | + public void Configure_Direct_SetsParseCulture() |
| 431 | + { |
| 432 | + // Test line 191 |
| 433 | + var attr = new CommandAttribute |
| 434 | + { |
| 435 | + ParseCulture = System.Globalization.CultureInfo.InvariantCulture |
| 436 | + }; |
| 437 | + |
| 438 | + var app = new CommandLineApplication(); |
| 439 | + attr.Configure(app); |
| 440 | + |
| 441 | + Assert.Equal(System.Globalization.CultureInfo.InvariantCulture, app.ValueParsers.ParseCulture); |
| 442 | + } |
| 443 | + |
| 444 | + [Fact] |
| 445 | + public void Configure_Direct_NoAliases_DoesNotAddNames() |
| 446 | + { |
| 447 | + // Test lines 179-182 with empty iteration |
| 448 | + var attr = new CommandAttribute("single-name"); |
| 449 | + |
| 450 | + var app = new CommandLineApplication(); |
| 451 | + attr.Configure(app); |
| 452 | + |
| 453 | + Assert.Equal("single-name", app.Name); |
| 454 | + Assert.Single(app.Names); |
| 455 | + } |
| 456 | + |
| 457 | + #endregion |
139 | 458 | } |
140 | 459 | } |
0 commit comments