From 2dff646ffc3955888ba00a56990429cd896f41c1 Mon Sep 17 00:00:00 2001 From: Giacomo Stelluti Scala Date: Tue, 17 Mar 2020 23:01:52 +0100 Subject: [PATCH] Fix/issue #579 (#580) * Added tests to verify issue #579 * Unparsing TimeSpan without quotes --- src/CommandLine/UnParserExtensions.cs | 2 +- .../Unit/Core/InstanceBuilderTests.cs | 16 ++++++++++++++++ .../Unit/UnParserExtensionsTests.cs | 13 ++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/CommandLine/UnParserExtensions.cs b/src/CommandLine/UnParserExtensions.cs index e06ebf56..63921e22 100644 --- a/src/CommandLine/UnParserExtensions.cs +++ b/src/CommandLine/UnParserExtensions.cs @@ -204,7 +204,7 @@ private static string FormatValue(Specification spec, object value) private static object FormatWithQuotesIfString(object value) { - if (value is DateTime || value is TimeSpan || value is DateTimeOffset) return $"\"{value}\""; + if (value is DateTime || value is DateTimeOffset) return $"\"{value}\""; Func doubQt = v => v.Contains("\"") ? v.Replace("\"", "\\\"") : v; diff --git a/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs b/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs index 8dd7371c..3ef33261 100644 --- a/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs +++ b/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs @@ -1086,6 +1086,22 @@ public void Parse_TimeSpan() expectedResult.Should().BeEquivalentTo(((Parsed)result).Value); } + #region Issue 579 + [Fact] + public void Should_not_parse_quoted_TimeSpan() + { + // Exercize system + var result = InvokeBuild(new[] { "--duration=\"00:42:00\"" }); + + var outcome = result as NotParsed; + + // Verify outcome + outcome.Should().NotBeNull(); + outcome.Errors.Should().NotBeNullOrEmpty() + .And.HaveCount(1) + .And.OnlyContain(e => e.GetType().Equals(typeof(BadFormatConversionError))); + } + #endregion [Fact] public void OptionClass_IsImmutable_HasNoCtor() diff --git a/tests/CommandLine.Tests/Unit/UnParserExtensionsTests.cs b/tests/CommandLine.Tests/Unit/UnParserExtensionsTests.cs index 2c95e893..1c295642 100644 --- a/tests/CommandLine.Tests/Unit/UnParserExtensionsTests.cs +++ b/tests/CommandLine.Tests/Unit/UnParserExtensionsTests.cs @@ -106,6 +106,17 @@ public static void UnParsing_instance_with_dash_in_value_and_dashdash_disabled_r .Should().BeEquivalentTo("-something with dash"); } + #region Issue 579 + [Fact] + public static void UnParsing_instance_with_TimeSpan_returns_the_value_unquoted_in_command_line() + { + var options = new Options_With_TimeSpan { Duration = TimeSpan.FromMinutes(1) }; + new Parser() + .FormatCommandLine(options) + .Should().Be("--duration 00:01:00"); + } + #endregion + #region PR 550 [Fact] @@ -175,7 +186,7 @@ public static void UnParsing_instance_with_timespan() var options = new Options_TimeSpan { Start = ts }; var result = new Parser() .FormatCommandLine(options) - .Should().BeEquivalentTo("--start \"01:02:03\""); + .Should().BeEquivalentTo("--start 01:02:03"); //changed for issue 579 } [Theory]