Skip to content

Commit

Permalink
CoconaValueConverter to support FileInfo and DirectoryInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
gleblebedev committed Jan 3, 2024
1 parent a6c52f6 commit 2f73dd8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/Cocona.Core/Command/Binder/CoconaValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
54 changes: 53 additions & 1 deletion test/Cocona.Test/Command/ParameterBinder/ValueConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@ namespace Cocona.Test.Command.ParameterBinder;

public class ValueConverterTest
{
internal class FileInfoComparer : IEqualityComparer<FileInfo>
{
public bool Equals(FileInfo? x, FileInfo? y)
{
return x?.Name == y?.Name;
}

public int GetHashCode(FileInfo obj)
{
return obj.GetHashCode();
}
}

internal class DirectoryInfoComparer : IEqualityComparer<DirectoryInfo>
{
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()
{
Expand Down Expand Up @@ -39,4 +65,30 @@ public void Boolean_Int32()
{
new CoconaValueConverter().ConvertTo(typeof(int), "12345").Should().Be(12345);
}
}

[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());
}
}

0 comments on commit 2f73dd8

Please sign in to comment.