-
Notifications
You must be signed in to change notification settings - Fork 348
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
Added DelimitedFieldPaddingAttribute #373
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using NUnit.Framework; | ||
|
||
namespace FileHelpers.Tests.CommonTests | ||
{ | ||
[TestFixture] | ||
public class DelimitedFieldPadding | ||
{ | ||
|
||
[DelimitedRecord(",")] | ||
private class AlignCenterSample | ||
{ | ||
public AlignCenterSample() | ||
{ | ||
|
||
} | ||
|
||
public AlignCenterSample(string name, int amount, DateTime date ) | ||
{ | ||
this.Name = name; | ||
this.Amount = amount; | ||
this.Date = date; | ||
} | ||
|
||
|
||
[DelimitedFieldPadding(6, AlignMode.Center, '+')] | ||
public string Name { get; set; } | ||
|
||
[DelimitedFieldPadding(5, AlignMode.Center, '+')] | ||
[FieldConverter(ConverterKind.Int32)] | ||
public int Amount { get; set; } | ||
|
||
[DelimitedFieldPadding(12, AlignMode.Center, '+')] | ||
[FieldConverter(ConverterKind.Date, "yyyy-MM-dd")] | ||
public DateTime Date { get; set; } | ||
} | ||
|
||
|
||
[Test] | ||
public void TestPaddingCenterAlign() | ||
{ | ||
|
||
List<AlignCenterSample> records = new List<AlignCenterSample>(); | ||
records.Add(new AlignCenterSample("AA", 100, new DateTime(2020, 1, 1))); | ||
|
||
|
||
var engine = new FileHelperEngine<AlignCenterSample>(); | ||
string output = engine.WriteString(records).TrimEnd(); | ||
|
||
string expected = "++AA++,+100+,+2020-01-01+"; | ||
|
||
Assert.AreEqual(expected, output); | ||
} | ||
|
||
|
||
[DelimitedRecord(",")] | ||
private class AlignLeftSample | ||
{ | ||
public AlignLeftSample() | ||
{ | ||
|
||
} | ||
|
||
public AlignLeftSample(string name, int amount, DateTime date) | ||
{ | ||
this.Name = name; | ||
this.Amount = amount; | ||
this.Date = date; | ||
} | ||
|
||
|
||
[DelimitedFieldPadding(6, AlignMode.Left, ' ')] | ||
public string Name { get; set; } | ||
|
||
[DelimitedFieldPadding(5, AlignMode.Left, ' ')] | ||
[FieldConverter(ConverterKind.Int32)] | ||
public int Amount { get; set; } | ||
|
||
[DelimitedFieldPadding(12, AlignMode.Left, '*')] | ||
[FieldConverter(ConverterKind.Date, "yyyy-MM-dd")] | ||
public DateTime Date { get; set; } | ||
} | ||
|
||
|
||
[Test] | ||
public void TestPaddingLeftAlign() | ||
{ | ||
|
||
var records = new List<AlignLeftSample>(); | ||
records.Add(new AlignLeftSample("AA", 100, new DateTime(2020, 1, 1))); | ||
|
||
|
||
var engine = new FileHelperEngine<AlignLeftSample>(); | ||
string output = engine.WriteString(records).TrimEnd(); | ||
|
||
string expected = "AA ,100 ,2020-01-01**"; | ||
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. I think the expectation should be the other way round. The .Net string behaves as follows |
||
|
||
Assert.AreEqual(expected, output); | ||
} | ||
|
||
|
||
[DelimitedRecord(",")] | ||
private class AlignRightSample | ||
{ | ||
public AlignRightSample() | ||
{ | ||
|
||
} | ||
|
||
public AlignRightSample(string name, int amount, DateTime date) | ||
{ | ||
this.Name = name; | ||
this.Amount = amount; | ||
this.Date = date; | ||
} | ||
|
||
|
||
[DelimitedFieldPadding(6, AlignMode.Right, ' ')] | ||
public string Name { get; set; } | ||
|
||
[DelimitedFieldPadding(5, AlignMode.Right, '*')] | ||
[FieldConverter(ConverterKind.Int32)] | ||
public int Amount { get; set; } | ||
|
||
[DelimitedFieldPadding(12, AlignMode.Right, '+')] | ||
[FieldConverter(ConverterKind.Date, "yyyy-MM-dd")] | ||
public DateTime Date { get; set; } | ||
} | ||
|
||
|
||
|
||
|
||
[Test] | ||
public void TestPaddingRightAlign() | ||
{ | ||
|
||
var records = new List<AlignRightSample>(); | ||
records.Add(new AlignRightSample("AA", 100, new DateTime(2020, 1, 1))); | ||
|
||
|
||
var engine = new FileHelperEngine<AlignRightSample>(); | ||
string output = engine.WriteString(records).TrimEnd(); | ||
|
||
string expected = " AA,**100,++2020-01-01"; | ||
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. Same as left above: I think it should be the other way round. |
||
|
||
Assert.AreEqual(expected, output); | ||
} | ||
|
||
|
||
[FixedLengthRecord] | ||
private class FixedWithWithDelimitedFiledPaddingSample | ||
{ | ||
public FixedWithWithDelimitedFiledPaddingSample() | ||
{ | ||
|
||
} | ||
|
||
public FixedWithWithDelimitedFiledPaddingSample(string name, string description) | ||
{ | ||
this.Name = name; | ||
this.Description = description; | ||
} | ||
|
||
[DelimitedFieldPadding(10, AlignMode.Left, ' ')] | ||
[FieldFixedLength(10)] | ||
public string Name { get; set; } | ||
|
||
[DelimitedFieldPadding(50, AlignMode.Left, ' ')] | ||
[FieldFixedLength(50)] | ||
public string Description { get; set; } | ||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
[Test] | ||
public void DelimitedFieldPaddingInvalidOnFixedWidthFile() | ||
{ | ||
|
||
Assert.Throws<BadUsageException>(() => | ||
{ | ||
var engine = new FileHelperEngine<FixedWithWithDelimitedFiledPaddingSample>(); | ||
}); | ||
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
|
||
|
||
namespace FileHelpers | ||
{ | ||
/// <summary> | ||
/// Supports padding when applied to a Delimited record field or property | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] | ||
public sealed class DelimitedFieldPaddingAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Total length of the fields in the output file. | ||
/// </summary> | ||
public int TotalLength { get; private set; } | ||
|
||
/// <summary> | ||
/// The position of the alignment. | ||
/// </summary> | ||
public AlignMode AlignMode { get; private set; } | ||
|
||
/// <summary> | ||
/// The character for padding. | ||
/// </summary> | ||
public char PaddingChar { get; private set; } | ||
|
||
|
||
/// <summary> | ||
/// Adds padding for a delimited file record field | ||
/// </summary> | ||
/// <param name="totalLength">Total length</param> | ||
/// <param name="alignMode">Alignment position</param> | ||
/// <param name="paddingChar">Character used for padding</param> | ||
public DelimitedFieldPaddingAttribute(int totalLength, AlignMode alignMode, char paddingChar) | ||
{ | ||
|
||
this.PaddingChar = paddingChar; | ||
|
||
this.TotalLength = totalLength; | ||
|
||
this.AlignMode = alignMode; | ||
|
||
} | ||
} | ||
} |
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.
Given the value would be "AAA" and the field length would be 4:
" AAA" or "AAA " ?