Skip to content

Commit 2dff646

Browse files
authored
Fix/issue #579 (#580)
* Added tests to verify issue #579 * Unparsing TimeSpan without quotes
1 parent 182e72f commit 2dff646

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

Diff for: src/CommandLine/UnParserExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private static string FormatValue(Specification spec, object value)
204204

205205
private static object FormatWithQuotesIfString(object value)
206206
{
207-
if (value is DateTime || value is TimeSpan || value is DateTimeOffset) return $"\"{value}\"";
207+
if (value is DateTime || value is DateTimeOffset) return $"\"{value}\"";
208208
Func<string, string> doubQt = v
209209
=> v.Contains("\"") ? v.Replace("\"", "\\\"") : v;
210210

Diff for: tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs

+16
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,22 @@ public void Parse_TimeSpan()
10861086
expectedResult.Should().BeEquivalentTo(((Parsed<Options_With_TimeSpan>)result).Value);
10871087
}
10881088

1089+
#region Issue 579
1090+
[Fact]
1091+
public void Should_not_parse_quoted_TimeSpan()
1092+
{
1093+
// Exercize system
1094+
var result = InvokeBuild<Options_With_TimeSpan>(new[] { "--duration=\"00:42:00\"" });
1095+
1096+
var outcome = result as NotParsed<Options_With_TimeSpan>;
1097+
1098+
// Verify outcome
1099+
outcome.Should().NotBeNull();
1100+
outcome.Errors.Should().NotBeNullOrEmpty()
1101+
.And.HaveCount(1)
1102+
.And.OnlyContain(e => e.GetType().Equals(typeof(BadFormatConversionError)));
1103+
}
1104+
#endregion
10891105

10901106
[Fact]
10911107
public void OptionClass_IsImmutable_HasNoCtor()

Diff for: tests/CommandLine.Tests/Unit/UnParserExtensionsTests.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ public static void UnParsing_instance_with_dash_in_value_and_dashdash_disabled_r
106106
.Should().BeEquivalentTo("-something with dash");
107107
}
108108

109+
#region Issue 579
110+
[Fact]
111+
public static void UnParsing_instance_with_TimeSpan_returns_the_value_unquoted_in_command_line()
112+
{
113+
var options = new Options_With_TimeSpan { Duration = TimeSpan.FromMinutes(1) };
114+
new Parser()
115+
.FormatCommandLine(options)
116+
.Should().Be("--duration 00:01:00");
117+
}
118+
#endregion
119+
109120
#region PR 550
110121

111122
[Fact]
@@ -175,7 +186,7 @@ public static void UnParsing_instance_with_timespan()
175186
var options = new Options_TimeSpan { Start = ts };
176187
var result = new Parser()
177188
.FormatCommandLine(options)
178-
.Should().BeEquivalentTo("--start \"01:02:03\"");
189+
.Should().BeEquivalentTo("--start 01:02:03"); //changed for issue 579
179190
}
180191

181192
[Theory]

0 commit comments

Comments
 (0)