-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed issue with
TypeConverter<T>
where T
is Nullable
did not w…
…ork.
- Loading branch information
Showing
2 changed files
with
46 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
tests/CsvHelper.Tests/TypeConversion/TypeConverter`1Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using CsvHelper.Configuration; | ||
using CsvHelper.TypeConversion; | ||
using Xunit; | ||
|
||
namespace CsvHelper.Tests.TypeConversion | ||
{ | ||
public class TypeConverter1Tests | ||
{ | ||
[Fact] | ||
public void ConvertToString_NullableBoolean_Converts() | ||
{ | ||
ITypeConverter converter = new GenericBoolConverter(); | ||
var result = converter.ConvertToString(null, null, null); | ||
|
||
Assert.Equal(string.Empty, result); | ||
} | ||
|
||
[Fact] | ||
public void ConvertFromString_NullableBoolean_Converts() | ||
{ | ||
ITypeConverter converter = new GenericBoolConverter(); | ||
var result = (bool?)converter.ConvertFromString("true", null, null); | ||
|
||
Assert.True(result); | ||
} | ||
|
||
private class GenericBoolConverter : TypeConverter<bool?> | ||
{ | ||
public override bool? ConvertFromString(string text, IReaderRow row, MemberMapData memberMapData) | ||
{ | ||
if (bool.TryParse(text, out var result)) | ||
{ | ||
return result; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public override string ConvertToString(bool? value, IWriterRow row, MemberMapData memberMapData) | ||
{ | ||
return value?.ToString() ?? ""; | ||
} | ||
} | ||
} | ||
} |