mirror of
				https://github.com/nsnail/spectre.console.git
				synced 2025-10-31 09:09:25 +08:00 
			
		
		
		
	Check for default value before writing property values
Previous version was overwriting values that might have been set via a property initializer. Closes #422
This commit is contained in:
		 Phil Scott
					Phil Scott
				
			
				
					committed by
					
						 Patrik Svensson
						Patrik Svensson
					
				
			
			
				
	
			
			
			 Patrik Svensson
						Patrik Svensson
					
				
			
						parent
						
							21f731ebd5
						
					
				
				
					commit
					ebb1076dd0
				
			| @@ -1,3 +1,4 @@ | ||||
| using System; | ||||
| using System.ComponentModel; | ||||
| using Spectre.Console.Cli; | ||||
|  | ||||
| @@ -10,6 +11,12 @@ namespace Spectre.Console.Tests.Data | ||||
|         public string Greeting { get; set; } | ||||
|     } | ||||
|  | ||||
|     public sealed class OptionalArgumentWithPropertyInitializerSettings : CommandSettings | ||||
|     { | ||||
|         [CommandArgument(0, "[NAMES]")] | ||||
|         public string[] Names { get; set; } = Array.Empty<string>(); | ||||
|     } | ||||
|  | ||||
|     public sealed class OptionalArgumentWithDefaultValueAndTypeConverterSettings : CommandSettings | ||||
|     { | ||||
|         [CommandArgument(0, "[GREETING]")] | ||||
|   | ||||
| @@ -244,6 +244,50 @@ namespace Spectre.Console.Tests.Unit.Cli | ||||
|             }); | ||||
|         } | ||||
|  | ||||
|         [Fact] | ||||
|         public void Should_Assign_Property_Initializer_To_Optional_Argument() | ||||
|         { | ||||
|             // Given | ||||
|             var app = new CommandAppTester(); | ||||
|             app.SetDefaultCommand<GenericCommand<OptionalArgumentWithPropertyInitializerSettings>>(); | ||||
|             app.Configure(config => | ||||
|             { | ||||
|                 config.PropagateExceptions(); | ||||
|             }); | ||||
|  | ||||
|             // When | ||||
|             var result = app.Run(Array.Empty<string>()); | ||||
|  | ||||
|             // Then | ||||
|             result.ExitCode.ShouldBe(0); | ||||
|             result.Settings | ||||
|                 .ShouldBeOfType<OptionalArgumentWithPropertyInitializerSettings>() | ||||
|                 .And(settings => settings.Names.ShouldNotBeNull()) | ||||
|                 .And(settings => settings.Names.ShouldBeEmpty()); | ||||
|         } | ||||
|  | ||||
|         [Fact] | ||||
|         public void Should_Overwrite_Property_Initializer_With_Argument_Value() | ||||
|         { | ||||
|             // Given | ||||
|             var app = new CommandAppTester(); | ||||
|             app.SetDefaultCommand<GenericCommand<OptionalArgumentWithPropertyInitializerSettings>>(); | ||||
|             app.Configure(config => | ||||
|             { | ||||
|                 config.PropagateExceptions(); | ||||
|             }); | ||||
|  | ||||
|             // When | ||||
|             var result = app.Run("ABBA", "Herreys"); | ||||
|  | ||||
|             // Then | ||||
|             result.ExitCode.ShouldBe(0); | ||||
|             result.Settings | ||||
|                 .ShouldBeOfType<OptionalArgumentWithPropertyInitializerSettings>() | ||||
|                 .And(settings => settings.Names.ShouldContain("ABBA")) | ||||
|                 .And(settings => settings.Names.ShouldContain("Herreys")); | ||||
|         } | ||||
|  | ||||
|         [Fact] | ||||
|         public void Should_Assign_Default_Value_To_Optional_Argument_Using_Converter_If_Necessary() | ||||
|         { | ||||
|   | ||||
| @@ -10,7 +10,10 @@ namespace Spectre.Console.Cli | ||||
|  | ||||
|             foreach (var (parameter, value) in lookup) | ||||
|             { | ||||
|                 parameter.Property.SetValue(settings, value); | ||||
|                 if (value != default) | ||||
|                 { | ||||
|                     parameter.Property.SetValue(settings, value); | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             // Validate the settings. | ||||
|   | ||||
		Reference in New Issue
	
	Block a user