Skip to content
Open
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
28 changes: 24 additions & 4 deletions FileHelpers.Tests/Tests/Common/OverflowModeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ namespace FileHelpers.Tests.CommonTests
public class OverflowModeTests
{
[FixedLengthRecord]
public class DiscardCustomer
public class DiscardEndCustomer
{
[FieldFixedLength(10, OverflowMode = OverflowMode.DiscardEnd)]
public string mCustomerID;
}

[FixedLengthRecord]
public class DiscardStartCustomer
{
[FieldFixedLength(10, OverflowMode = OverflowMode.DiscardStart)]
public string mCustomerID;
}

[FixedLengthRecord]
public class ErrorCustomer
{
Expand All @@ -26,10 +33,23 @@ public class ErrorCustomer
[TestCase("0123456789A", "0123456789")]
[TestCase("0123456789", "0123456789")]
[TestCase("012345678", "012345678 ")]
public void Discard(string originalValue, string expectedValue)
public void DiscardEnd(string originalValue, string expectedValue)
{
var engine = new FixedFileEngine<DiscardEndCustomer>();
var customer = new DiscardEndCustomer {mCustomerID = originalValue};
var res = engine.WriteString(new[] {customer});

Check.That(res).IsEqualTo($"{expectedValue}{Environment.NewLine}");
}

[TestCase("0123456789ABC", "3456789ABC")]
[TestCase("0123456789A", "123456789A")]
[TestCase("0123456789", "0123456789")]
[TestCase("012345678", "012345678 ")]
public void DiscardStart(string originalValue, string expectedValue)
{
var engine = new FixedFileEngine<DiscardCustomer>();
var customer = new DiscardCustomer {mCustomerID = originalValue};
var engine = new FixedFileEngine<DiscardStartCustomer>();
var customer = new DiscardStartCustomer {mCustomerID = originalValue};
var res = engine.WriteString(new[] {customer});

Check.That(res).IsEqualTo($"{expectedValue}{Environment.NewLine}");
Expand Down
4 changes: 3 additions & 1 deletion FileHelpers/Fields/FixedLengthField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ private string GetActualValueBasedOnFieldConfiguration(string field)
throw new ConvertException(field,
FieldType,
$"Field value is too large for the field length ({FieldLength}) and field OverflowMode is set to {OverflowMode}.");
case OverflowMode.DiscardStart:
return field.Substring(field.Length - FieldLength);
case OverflowMode.DiscardEnd:
default:
return field.Substring(0, FieldLength);
Expand Down Expand Up @@ -186,4 +188,4 @@ protected override FieldBase CreateClone()

#endregion
}
}
}
4 changes: 4 additions & 0 deletions FileHelpers/Fields/OverflowMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public enum OverflowMode
/// </summary>
DiscardEnd,
/// <summary>
/// Discard overflowing characters at the start
/// </summary>
DiscardStart,
/// <summary>
/// Throw an exception
/// </summary>
Error
Expand Down