Skip to content

Commit

Permalink
Add support for DateOnly and TimeOnly on .Net 6 (#1732). (#1734)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobThree authored May 10, 2022
1 parent 1ac7dfd commit 4dcee4d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 21 deletions.
76 changes: 59 additions & 17 deletions src/System.CommandLine.Tests/Binding/TypeConversionTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using FluentAssertions;
using System.Collections;
using System.Collections.Generic;
using System.CommandLine.Utility;
using System.IO;
using FluentAssertions;
using System.Linq;
using Xunit;
using System.Net;
using Xunit;

namespace System.CommandLine.Tests.Binding
{
Expand Down Expand Up @@ -230,7 +230,7 @@ public void Nullable_bool_parses_as_false_when_the_option_has_been_applied(strin
public void Nullable_bool_parses_as_null_when_the_option_has_not_been_applied()
{
var option = new Option<bool?>("-x");

option
.Parse("")
.GetValueForOption(option)
Expand All @@ -252,7 +252,7 @@ public void Generic_option_bool_parses_when_passed_to_non_generic_GetValueForOpt

parseResult.GetValueForOption((Option)option).Should().Be(true);
}

[Fact]
public void When_exactly_one_argument_is_expected_and_none_are_provided_then_getting_value_throws()
{
Expand All @@ -274,7 +274,7 @@ public void When_exactly_one_argument_is_expected_and_none_are_provided_then_get
.Should()
.Be("Required argument missing for option: '-x'.");
}

[Theory]
[InlineData("c -a o c c")]
[InlineData("c c -a o c")]
Expand Down Expand Up @@ -496,7 +496,7 @@ public void Values_can_be_correctly_converted_to_decimal_without_the_parser_spec

value.Should().Be(123.456m);
}

[Fact]
public void Values_can_be_correctly_converted_to_nullable_decimal_without_the_parser_specifying_a_custom_converter()
{
Expand All @@ -506,7 +506,7 @@ public void Values_can_be_correctly_converted_to_nullable_decimal_without_the_pa

value.Should().Be(123.456m);
}

[Fact]
public void Values_can_be_correctly_converted_to_double_without_the_parser_specifying_a_custom_converter()
{
Expand All @@ -516,7 +516,7 @@ public void Values_can_be_correctly_converted_to_double_without_the_parser_speci

value.Should().Be(123.456d);
}

[Fact]
public void Values_can_be_correctly_converted_to_nullable_double_without_the_parser_specifying_a_custom_converter()
{
Expand All @@ -536,7 +536,7 @@ public void Values_can_be_correctly_converted_to_float_without_the_parser_specif

value.Should().Be(123.456f);
}

[Fact]
public void Values_can_be_correctly_converted_to_nullable_float_without_the_parser_specifying_a_custom_converter()
{
Expand All @@ -557,7 +557,7 @@ public void Values_can_be_correctly_converted_to_Guid_without_the_parser_specify

value.Should().Be(Guid.Parse(guidString));
}

[Fact]
public void Values_can_be_correctly_converted_to_nullable_Guid_without_the_parser_specifying_a_custom_converter()
{
Expand Down Expand Up @@ -689,7 +689,7 @@ public void Values_can_be_correctly_converted_to_nullable_ushort_without_the_par

value.Should().Be(1234);
}

[Fact]
public void Values_can_be_correctly_converted_to_sbyte_without_the_parser_specifying_a_custom_converter()
{
Expand All @@ -699,7 +699,7 @@ public void Values_can_be_correctly_converted_to_sbyte_without_the_parser_specif

value.Should().Be(123);
}

[Fact]
public void Values_can_be_correctly_converted_to_nullable_sbyte_without_the_parser_specifying_a_custom_converter()
{
Expand Down Expand Up @@ -732,6 +732,48 @@ public void Values_can_be_correctly_converted_to_ipendpoint_without_the_parser_s
}
#endif

#if NET6_0_OR_GREATER
[Fact]
public void Values_can_be_correctly_converted_to_dateonly_without_the_parser_specifying_a_custom_converter()
{
var option = new Option<DateOnly>("-us");

var value = option.Parse("-us 2022-03-02").GetValueForOption(option);

value.Should().Be(DateOnly.Parse("2022-03-02"));
}

[Fact]
public void Values_can_be_correctly_converted_to_nullable_dateonly_without_the_parser_specifying_a_custom_converter()
{
var option = new Option<DateOnly?>("-x");

var value = option.Parse("-x 2022-03-02").GetValueForOption(option);

value.Should().Be(DateOnly.Parse("2022-03-02"));
}

[Fact]
public void Values_can_be_correctly_converted_to_timeonly_without_the_parser_specifying_a_custom_converter()
{
var option = new Option<TimeOnly>("-us");

var value = option.Parse("-us 12:34:56").GetValueForOption(option);

value.Should().Be(TimeOnly.Parse("12:34:56"));
}

[Fact]
public void Values_can_be_correctly_converted_to_nullable_timeonly_without_the_parser_specifying_a_custom_converter()
{
var option = new Option<TimeOnly?>("-x");

var value = option.Parse("-x 12:34:56").GetValueForOption(option);

value.Should().Be(TimeOnly.Parse("12:34:56"));
}
#endif

[Fact]
public void Values_can_be_correctly_converted_to_byte_without_the_parser_specifying_a_custom_converter()
{
Expand All @@ -741,7 +783,7 @@ public void Values_can_be_correctly_converted_to_byte_without_the_parser_specify

value.Should().Be(123);
}

[Fact]
public void Values_can_be_correctly_converted_to_nullable_byte_without_the_parser_specifying_a_custom_converter()
{
Expand All @@ -751,7 +793,7 @@ public void Values_can_be_correctly_converted_to_nullable_byte_without_the_parse

value.Should().Be(123);
}

[Fact]
public void Values_can_be_correctly_converted_to_uint_without_the_parser_specifying_a_custom_converter()
{
Expand All @@ -761,7 +803,7 @@ public void Values_can_be_correctly_converted_to_uint_without_the_parser_specify

value.Should().Be(1234);
}

[Fact]
public void Values_can_be_correctly_converted_to_nullable_uint_without_the_parser_specifying_a_custom_converter()
{
Expand All @@ -781,7 +823,7 @@ public void Values_can_be_correctly_converted_to_array_of_int_without_the_parser

value.Should().BeEquivalentTo(1, 2, 3);
}

[Theory]
[InlineData(0, 100_000, typeof(string[]))]
[InlineData(0, 3, typeof(string[]))]
Expand All @@ -793,7 +835,7 @@ public void Values_can_be_correctly_converted_to_array_of_int_without_the_parser
[InlineData(0, 3, typeof(IList<string>))]
[InlineData(0, 100_000, typeof(ICollection<string>))]
[InlineData(0, 3, typeof(ICollection<string>))]

[InlineData(1, 100_000, typeof(string[]))]
[InlineData(1, 3, typeof(string[]))]
[InlineData(1, 100_000, typeof(IEnumerable<string>))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ internal static partial class ArgumentConverter
return false;
},

#if NET6_0_OR_GREATER
[typeof(DateOnly)] = (string input, out object? value) =>
{
if (DateOnly.TryParse(input, out var parsed))
{
value = parsed;
return true;
}

value = default;
return false;
},
#endif

[typeof(DateTime)] = (string input, out object? value) =>
{
if (DateTime.TryParse(input, out var parsed))
Expand Down Expand Up @@ -63,7 +77,7 @@ internal static partial class ArgumentConverter

[typeof(DirectoryInfo)] = (string path, out object? value) =>
{
if (String.IsNullOrEmpty(path))
if (string.IsNullOrEmpty(path))
{
value = default;
return false;
Expand All @@ -86,7 +100,7 @@ internal static partial class ArgumentConverter

[typeof(FileInfo)] = (string path, out object? value) =>
{
if (String.IsNullOrEmpty(path))
if (string.IsNullOrEmpty(path))
{
value = default;
return false;
Expand All @@ -97,7 +111,7 @@ internal static partial class ArgumentConverter

[typeof(FileSystemInfo)] = (string path, out object? value) =>
{
if (String.IsNullOrEmpty(path))
if (string.IsNullOrEmpty(path))
{
value = default;
return false;
Expand Down Expand Up @@ -130,7 +144,7 @@ internal static partial class ArgumentConverter
value = default;
return false;
},

[typeof(Guid)] = (string input, out object? value) =>
{
if (Guid.TryParse(input, out var parsed))
Expand Down Expand Up @@ -205,6 +219,20 @@ internal static partial class ArgumentConverter
return false;
},

#if NET6_0_OR_GREATER
[typeof(TimeOnly)] = (string input, out object? value) =>
{
if (TimeOnly.TryParse(input, out var parsed))
{
value = parsed;
return true;
}

value = default;
return false;
},
#endif

[typeof(uint)] = (string token, out object? value) =>
{
if (uint.TryParse(token, out var uintValue))
Expand Down

0 comments on commit 4dcee4d

Please sign in to comment.