-
Notifications
You must be signed in to change notification settings - Fork 349
Customizable CultureInfo at field and record level. #306
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2638efa
Specific culture info name can now be specified as the first argument…
75008e8
Default culture can now be set in the record attribute, and customize…
2deb9e3
Merge branch 'CustomizableCultureInfo' : added culture customization …
8bebd41
Missing comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,3 @@ | ||
| 10248|32,38|32,38|32,38 | ||
| 10249|1011,61|1011,61|1011,61 | ||
| 10250|1 165,83|1 165,83|1 165,83 |
This file contains hidden or 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
This file contains hidden or 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
158 changes: 158 additions & 0 deletions
158
FileHelpers.Tests/Tests/Converters/DefaultCultureInfo.cs
This file contains hidden or 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,158 @@ | ||
| using NUnit.Framework; | ||
|
|
||
| namespace FileHelpers.Tests.Converters | ||
| { | ||
| [TestFixture] | ||
| public class DefaultCultureInfo | ||
| { | ||
| [DelimitedRecord("|")] | ||
| public class RecordWithoutSpecifiedCulture | ||
| { | ||
| public decimal DecimalField; | ||
| } | ||
|
|
||
| [DelimitedRecord("|", "fr-FR")] | ||
| public class RecordWithDefaultCulture | ||
| { | ||
| public decimal DecimalFieldWithoutCulture; | ||
|
|
||
|
|
||
| [FieldConverter(ConverterKind.Decimal, "en-US")] | ||
| public decimal DecimalFieldWithEnglishCulture; | ||
| } | ||
|
|
||
| [DelimitedRecord("|")] | ||
| public class RecordWithFieldCulture | ||
| { | ||
| [FieldConverter(ConverterKind.Decimal, "fr-FR")] | ||
| public decimal DecimalFieldWithFrenchCulture; | ||
|
|
||
| public decimal DecimalFieldWithoutCulture; | ||
| } | ||
|
|
||
|
|
||
| [Test] | ||
| public void RecordWithoutCultureHasDefaultSeparator() | ||
| { | ||
| var engine = new FileHelperEngine<RecordWithoutSpecifiedCulture>(); | ||
| Assert.AreEqual(1, engine.Options.Fields.Count); | ||
| var decimalConverter = engine.Options.Fields[0].Converter; | ||
| AssertCanConvertEnglishNumbers(decimalConverter); | ||
| } | ||
|
|
||
| [Test] | ||
| public void RecordWithSpecifiedCultureInRecordAttributeUsesThatCultureByDefault() | ||
| { | ||
| var engine = new FileHelperEngine<RecordWithDefaultCulture>(); | ||
| Assert.AreEqual(2, engine.Options.Fields.Count); | ||
| var decimalConverterWithoutCulture = engine.Options.Fields[0].Converter; | ||
| AssertCanConvertFrenchNumbers(decimalConverterWithoutCulture); | ||
|
|
||
| var decimalConverterWithEnglishCulture = engine.Options.Fields[1].Converter; | ||
| AssertCanConvertEnglishNumbers(decimalConverterWithEnglishCulture); | ||
| } | ||
|
|
||
| [Test] | ||
| public void FieldWithSpecifiedCultureInFieldConverterAttributeUsesThatCulture() | ||
| { | ||
| var engine = new FileHelperEngine<RecordWithFieldCulture>(); | ||
| Assert.AreEqual(2, engine.Options.Fields.Count); | ||
| var decimalConverterWithFrenchCulture = engine.Options.Fields[0].Converter; | ||
| AssertCanConvertFrenchNumbers(decimalConverterWithFrenchCulture); | ||
|
|
||
| var decimalConverterWithoutCulture = engine.Options.Fields[1].Converter; | ||
| AssertCanConvertEnglishNumbers(decimalConverterWithoutCulture); | ||
| } | ||
|
|
||
| #region Helpers | ||
| private static void AssertCanConvertEnglishNumbers(ConverterBase decimalConverter) | ||
| { | ||
| Assert.AreEqual(123.12, | ||
| decimalConverter.StringToField("123.12"), | ||
| "If no culture is specified, the decimal separator should be a dot"); | ||
| Assert.AreEqual(1234.12, | ||
| decimalConverter.StringToField("1,234.12"), | ||
| "If no culture is specified, the group separator should be a comma"); | ||
| } | ||
|
|
||
| private static void AssertCanConvertFrenchNumbers(ConverterBase decimalConverterWithoutCulture) | ||
| { | ||
| Assert.AreEqual(1.23, decimalConverterWithoutCulture.StringToField("1,23"), "If a culture is specified, the decimal separator should be the specified culture decimal separator"); | ||
| Assert.AreEqual(1234.12, decimalConverterWithoutCulture.StringToField("1 234,12"), "If a culture is specified, the group separator should be the specified culture group separator"); | ||
| Assert.Catch(() => { decimalConverterWithoutCulture.StringToField("1.23"); }, "The dot is not a valid french separator"); | ||
| } | ||
| #endregion | ||
|
|
||
| [DelimitedRecord("|", defaultCultureName: "fr-FR")] | ||
| public class DecimalTypeWithFrenchConversionAsAWhole | ||
| { | ||
| public int IntField; | ||
| public float FloatField; | ||
| public double DoubleField; | ||
| public decimal DecimalField; | ||
| } | ||
|
|
||
| [DelimitedRecord("|")] | ||
| public class DecimalTypeWithFrenchConversion | ||
| { | ||
| [FieldConverter(ConverterKind.Int32, "fr-FR")] | ||
| public int IntField; | ||
| [FieldConverter(ConverterKind.Single, "fr-FR")] | ||
| public float FloatField; | ||
| [FieldConverter(ConverterKind.Double, "fr-FR")] | ||
| public double DoubleField; | ||
| [FieldConverter(ConverterKind.Decimal, "fr-FR")] | ||
| public decimal DecimalField; | ||
| } | ||
|
|
||
| [Test] | ||
| public void DecimalsWithFrenchCulture() | ||
| { | ||
| var engine = new FileHelperEngine<DecimalTypeWithFrenchConversion>(); | ||
| var res = TestCommon.ReadTest<DecimalTypeWithFrenchConversion>(engine, "Good", "NumberFormatFrench.txt"); | ||
|
|
||
| Assert.AreEqual(3, res.Length); | ||
|
|
||
| Assert.AreEqual(10248, res[0].IntField); | ||
| CheckDecimal((decimal)32.38, res[0]); | ||
|
|
||
| Assert.AreEqual(10249, res[1].IntField); | ||
| CheckDecimal((decimal)1011.61, res[1]); | ||
|
|
||
| Assert.AreEqual(10250, res[2].IntField); | ||
| CheckDecimal((decimal)1165.83, res[2]); | ||
| } | ||
|
|
||
| [Test] | ||
| public void DecimalsWithFrenchCulture2() | ||
| { | ||
| var engine = new FileHelperEngine<DecimalTypeWithFrenchConversionAsAWhole>(); | ||
| var res = TestCommon.ReadTest<DecimalTypeWithFrenchConversionAsAWhole>(engine, "Good", "NumberFormatFrench.txt"); | ||
|
|
||
| Assert.AreEqual(3, res.Length); | ||
|
|
||
| Assert.AreEqual(10248, res[0].IntField); | ||
| CheckDecimal((decimal)32.38, res[0]); | ||
|
|
||
| Assert.AreEqual(10249, res[1].IntField); | ||
| CheckDecimal((decimal)1011.61, res[1]); | ||
|
|
||
| Assert.AreEqual(10250, res[2].IntField); | ||
| CheckDecimal((decimal)1165.83, res[2]); | ||
| } | ||
|
|
||
| private static void CheckDecimal(decimal dec, DecimalTypeWithFrenchConversion res) | ||
| { | ||
| Assert.AreEqual((decimal)dec, res.DecimalField); | ||
| Assert.AreEqual((double)dec, res.DoubleField); | ||
| Assert.AreEqual((float)dec, res.FloatField); | ||
| } | ||
|
|
||
| private static void CheckDecimal(decimal dec, DecimalTypeWithFrenchConversionAsAWhole res) | ||
| { | ||
| Assert.AreEqual((decimal)dec, res.DecimalField); | ||
| Assert.AreEqual((double)dec, res.DoubleField); | ||
| Assert.AreEqual((float)dec, res.FloatField); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -10,10 +10,19 @@ namespace FileHelpers | |
| public abstract class TypedRecordAttribute | ||
| : Attribute | ||
| { | ||
| /// <summary> | ||
| /// Default culture name used for each properties if no converter is specified otherwise. If null, the default decimal separator (".") will be used. | ||
| /// </summary> | ||
| public string DefaultCultureName { get; private set; } | ||
|
|
||
|
|
||
| #region " Constructors " | ||
|
|
||
| /// <summary>Abstract class, see inheritors.</summary> | ||
| protected TypedRecordAttribute() {} | ||
| protected TypedRecordAttribute(string defaultCultureName) | ||
| { | ||
| this.DefaultCultureName = defaultCultureName; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "this" qualifier is not necessary |
||
| } | ||
|
|
||
| #endregion | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Auto propety can be get only.