From 2752693510d78a1318c32ed7ce100af2ac28feea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Kr=C3=B6ner?= <36126706+Crown0815@users.noreply.github.com> Date: Wed, 3 Jan 2024 21:31:22 +0100 Subject: [PATCH 1/2] Fix typo in README.md I could not find anything about a MaxLangeAttribute so I assume the MaxLengthAttribute was meant here. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 269a3a8..0d52a7d 100644 --- a/README.md +++ b/README.md @@ -646,7 +646,7 @@ Cocona can use attributes to validate options and arguments. It is similar to AS .NET BCL (`System.ComponentModel.DataAnnotations`) has some pre-defined attributes: - `RangeAttribute` -- `MaxLangeAttribute` +- `MaxLengthAttribute` - `MinLengthAttribute` - ... From 2f73dd8d2a8545a40bb102edb16e50ee3cfaead5 Mon Sep 17 00:00:00 2001 From: Gleb Date: Wed, 3 Jan 2024 23:34:12 +0000 Subject: [PATCH 2/2] CoconaValueConverter to support FileInfo and DirectoryInfo --- .../Command/Binder/CoconaValueConverter.cs | 10 +++- .../ParameterBinder/ValueConverterTest.cs | 54 ++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/Cocona.Core/Command/Binder/CoconaValueConverter.cs b/src/Cocona.Core/Command/Binder/CoconaValueConverter.cs index d908aeb..a5143bc 100644 --- a/src/Cocona.Core/Command/Binder/CoconaValueConverter.cs +++ b/src/Cocona.Core/Command/Binder/CoconaValueConverter.cs @@ -22,7 +22,15 @@ public class CoconaValueConverter : ICoconaValueConverter { return value; } + else if (t == typeof(FileInfo)) + { + return value is null ? null : new FileInfo(value); + } + else if (t == typeof(DirectoryInfo)) + { + return value is null ? null : new DirectoryInfo(value); + } return TypeDescriptor.GetConverter(t).ConvertFrom(value); } -} \ No newline at end of file +} diff --git a/test/Cocona.Test/Command/ParameterBinder/ValueConverterTest.cs b/test/Cocona.Test/Command/ParameterBinder/ValueConverterTest.cs index 8412e92..0cf4518 100644 --- a/test/Cocona.Test/Command/ParameterBinder/ValueConverterTest.cs +++ b/test/Cocona.Test/Command/ParameterBinder/ValueConverterTest.cs @@ -4,6 +4,32 @@ namespace Cocona.Test.Command.ParameterBinder; public class ValueConverterTest { + internal class FileInfoComparer : IEqualityComparer + { + public bool Equals(FileInfo? x, FileInfo? y) + { + return x?.Name == y?.Name; + } + + public int GetHashCode(FileInfo obj) + { + return obj.GetHashCode(); + } + } + + internal class DirectoryInfoComparer : IEqualityComparer + { + public bool Equals(DirectoryInfo? x, DirectoryInfo? y) + { + return x?.Name == y?.Name; + } + + public int GetHashCode(DirectoryInfo obj) + { + return obj.GetHashCode(); + } + } + [Fact] public void CanNotConvertType_Int32() { @@ -39,4 +65,30 @@ public void Boolean_Int32() { new CoconaValueConverter().ConvertTo(typeof(int), "12345").Should().Be(12345); } -} \ No newline at end of file + + [Fact] + public void DirectoryInfo_Null() + { + new CoconaValueConverter().ConvertTo(typeof(FileInfo), null).Should().Be(null); + } + + [Fact] + public void DirectoryInfo_String() + { + new CoconaValueConverter().ConvertTo(typeof(DirectoryInfo), "somedirname").Should() + .Be(new DirectoryInfo("somedirname"), new DirectoryInfoComparer()); + } + + [Fact] + public void FileInfo_Null() + { + new CoconaValueConverter().ConvertTo(typeof(FileInfo), null).Should().Be(null); + } + + [Fact] + public void FileInfo_String() + { + new CoconaValueConverter().ConvertTo(typeof(FileInfo), "somefilename").Should() + .Be(new FileInfo("somefilename"), new FileInfoComparer()); + } +}