From 2f73dd8d2a8545a40bb102edb16e50ee3cfaead5 Mon Sep 17 00:00:00 2001 From: Gleb Date: Wed, 3 Jan 2024 23:34:12 +0000 Subject: [PATCH] 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()); + } +}