Skip to content

Commit

Permalink
Fix/issue #579 (#580)
Browse files Browse the repository at this point in the history
* Added tests to verify issue #579

* Unparsing TimeSpan without quotes
  • Loading branch information
gsscoder authored Mar 17, 2020
1 parent 182e72f commit 2dff646
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/CommandLine/UnParserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> doubQt = v
=> v.Contains("\"") ? v.Replace("\"", "\\\"") : v;

Expand Down
16 changes: 16 additions & 0 deletions tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,22 @@ public void Parse_TimeSpan()
expectedResult.Should().BeEquivalentTo(((Parsed<Options_With_TimeSpan>)result).Value);
}

#region Issue 579
[Fact]
public void Should_not_parse_quoted_TimeSpan()
{
// Exercize system
var result = InvokeBuild<Options_With_TimeSpan>(new[] { "--duration=\"00:42:00\"" });

var outcome = result as NotParsed<Options_With_TimeSpan>;

// 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()
Expand Down
13 changes: 12 additions & 1 deletion tests/CommandLine.Tests/Unit/UnParserExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 2dff646

Please sign in to comment.