-
-
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.
🔨 GuidConverter should be consistent
GuidConverter should be consistent with other value converters
- Loading branch information
Showing
2 changed files
with
69 additions
and
3 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
66 changes: 66 additions & 0 deletions
66
tests/CsvHelper.Tests/TypeConversion/GuidConverterTests.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,66 @@ | ||
// Copyright 2009-2024 Josh Close | ||
// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. | ||
// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. | ||
// https://github.com/JoshClose/CsvHelper | ||
using System; | ||
using System.Globalization; | ||
using CsvHelper.Configuration; | ||
using CsvHelper.Tests.Mocks; | ||
using CsvHelper.TypeConversion; | ||
using Xunit; | ||
|
||
namespace CsvHelper.Tests.TypeConversion | ||
{ | ||
|
||
public class GuidConverterTests | ||
{ | ||
[Fact] | ||
public void ConvertToStringTest() | ||
{ | ||
var converter = new GuidConverter(); | ||
var propertyMapData = new MemberMapData(null) | ||
{ | ||
TypeConverter = converter, | ||
TypeConverterOptions = { CultureInfo = CultureInfo.CurrentCulture } | ||
}; | ||
|
||
var value = Guid.NewGuid(); | ||
|
||
// Valid conversions. | ||
Assert.Equal(value.ToString(), converter.ConvertToString(value, null, propertyMapData)); | ||
|
||
// Invalid conversions. | ||
Assert.Equal("1", converter.ConvertToString(1, null, propertyMapData)); | ||
Assert.Equal("", converter.ConvertToString(null, null, propertyMapData)); | ||
} | ||
|
||
[Fact] | ||
public void ConvertFromStringTest() | ||
{ | ||
var converter = new GuidConverter(); | ||
var propertyMapData = new MemberMapData(null) | ||
{ | ||
TypeConverterOptions = { CultureInfo = CultureInfo.CurrentCulture } | ||
}; | ||
|
||
var row = new CsvReader(new ParserMock()); | ||
|
||
var value = Guid.NewGuid(); | ||
|
||
// Valid conversions. | ||
Assert.Equal(value.ToString(), converter.ConvertFromString(value.ToString(), null, propertyMapData).ToString()); | ||
Assert.Equal(value.ToString(), converter.ConvertFromString(value.ToString("N"), null, propertyMapData).ToString()); | ||
Assert.Equal(value.ToString(), converter.ConvertFromString(value.ToString("D"), null, propertyMapData).ToString()); | ||
Assert.Equal(value.ToString(), converter.ConvertFromString(value.ToString("B"), null, propertyMapData).ToString()); | ||
Assert.Equal(value.ToString(), converter.ConvertFromString(value.ToString("P"), null, propertyMapData).ToString()); | ||
Assert.Equal(value.ToString(), converter.ConvertFromString(value.ToString("X"), null, propertyMapData).ToString()); | ||
|
||
// Invalid conversions. | ||
Assert.Throws<TypeConverterException>(() => converter.ConvertFromString(null, row, propertyMapData)); | ||
Assert.Throws<TypeConverterException>(() => converter.ConvertFromString("", row, propertyMapData)); | ||
Assert.Throws<TypeConverterException>(() => converter.ConvertFromString(" ", row, propertyMapData)); | ||
Assert.Throws<TypeConverterException>(() => converter.ConvertFromString("Not A Guid", row, propertyMapData)); | ||
Assert.Throws<TypeConverterException>(() => converter.ConvertFromString("GGGGAAAA-0000-0000-0000-000000000000", row, propertyMapData)); | ||
} | ||
} | ||
} |