Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CoconaValueConverter to support FileInfo and DirectoryInfo #115

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
Loading