Skip to content

Commit

Permalink
Adding dynamic class support and tests for FieldNotEmptyAttribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbparker committed Mar 22, 2015
1 parent cbfc700 commit 1f36aab
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 0 deletions.
1 change: 1 addition & 0 deletions FileHelpers.Tests/Data/Bad/FieldNotEmpty1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
34579,,Joe Test,1,5/1/2014
1 change: 1 addition & 0 deletions FileHelpers.Tests/Data/Bad/FieldNotEmpty2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
,Test LLC,Joe Test,1,5/1/2014
1 change: 1 addition & 0 deletions FileHelpers.Tests/Data/Bad/FieldNotEmpty3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
34579,,Joe Test,1,
60 changes: 60 additions & 0 deletions FileHelpers.Tests/Data/GenerateTestData.autogen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,30 @@ protected override string GetFullPathName()
}
}

public partial class FieldNotEmpty1 : FileTestBase
{
protected override string GetFullPathName()
{
return @"Bad\FieldNotEmpty1.txt";
}
}

public partial class FieldNotEmpty2 : FileTestBase
{
protected override string GetFullPathName()
{
return @"Bad\FieldNotEmpty2.txt";
}
}

public partial class FieldNotEmpty3 : FileTestBase
{
protected override string GetFullPathName()
{
return @"Bad\FieldNotEmpty3.txt";
}
}

public partial class InNewLine3 : FileTestBase
{
protected override string GetFullPathName()
Expand Down Expand Up @@ -900,6 +924,14 @@ protected override string GetFullPathName()
}
}

public partial class FieldNotEmpty1 : FileTestBase
{
protected override string GetFullPathName()
{
return @"Good\FieldNotEmpty1.txt";
}
}

public partial class IgnoreComments1 : FileTestBase
{
protected override string GetFullPathName()
Expand Down Expand Up @@ -1559,6 +1591,27 @@ public static ClassesFileTest.Bad.EmptyLineInTheMiddle EmptyLineInTheMiddle
get { return mEmptyLineInTheMiddle; }
}

private static ClassesFileTest.Bad.FieldNotEmpty1 mFieldNotEmpty1 = new ClassesFileTest.Bad.FieldNotEmpty1();

public static ClassesFileTest.Bad.FieldNotEmpty1 FieldNotEmpty1
{
get { return mFieldNotEmpty1; }
}

private static ClassesFileTest.Bad.FieldNotEmpty2 mFieldNotEmpty2 = new ClassesFileTest.Bad.FieldNotEmpty2();

public static ClassesFileTest.Bad.FieldNotEmpty2 FieldNotEmpty2
{
get { return mFieldNotEmpty2; }
}

private static ClassesFileTest.Bad.FieldNotEmpty3 mFieldNotEmpty3 = new ClassesFileTest.Bad.FieldNotEmpty3();

public static ClassesFileTest.Bad.FieldNotEmpty3 FieldNotEmpty3
{
get { return mFieldNotEmpty3; }
}

private static ClassesFileTest.Bad.InNewLine3 mInNewLine3 = new ClassesFileTest.Bad.InNewLine3();

public static ClassesFileTest.Bad.InNewLine3 InNewLine3
Expand Down Expand Up @@ -2187,6 +2240,13 @@ public static ClassesFileTest.Good.FieldIgnoredAdvanced FieldIgnoredAdvanced
get { return mFieldIgnoredAdvanced; }
}

private static ClassesFileTest.Good.FieldNotEmpty1 mFieldNotEmpty1 = new ClassesFileTest.Good.FieldNotEmpty1();

public static ClassesFileTest.Good.FieldNotEmpty1 FieldNotEmpty1
{
get { return mFieldNotEmpty1; }
}

private static ClassesFileTest.Good.IgnoreComments1 mIgnoreComments1 = new ClassesFileTest.Good.IgnoreComments1();

public static ClassesFileTest.Good.IgnoreComments1 IgnoreComments1
Expand Down
2 changes: 2 additions & 0 deletions FileHelpers.Tests/Data/Good/FieldNotEmpty1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
34568,ABC123 Company,,,04032015
34579,Test LLC,Joe Test,1,01052014
6 changes: 6 additions & 0 deletions FileHelpers.Tests/FileHelpers.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
<Compile Include="Tests\Common\ChineseSupport.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Tests\Common\FieldNotEmpty.cs" />
<Compile Include="Tests\Dynamic\ClassBuilderTests.cs" />
<Compile Include="Tests\Common\ConditionalRecords.cs">
<SubType>Code</SubType>
Expand Down Expand Up @@ -321,6 +322,7 @@
<Compile Include="Tests\Errors\BadOptional.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Tests\Errors\BadNotEmpty.cs" />
<Compile Include="Tests\Errors\BadQuoted.cs">
<SubType>Code</SubType>
</Compile>
Expand Down Expand Up @@ -376,6 +378,9 @@
<Compile Include="Types\SampleTypes.cs">
<SubType>Code</SubType>
</Compile>
<Content Include="Data\Bad\FieldNotEmpty1.txt" />
<Content Include="Data\Bad\FieldNotEmpty2.txt" />
<Content Include="Data\Bad\FieldNotEmpty3.txt" />
<Content Include="Data\Excel\ExcelWithNoEmptyRows.xlsx" />
<Content Include="Data\Excel\ExcelWithOneEmptyRows.xlsx" />
<Content Include="Data\Excel\ExcelWithTwoEmptyRows.xlsx" />
Expand Down Expand Up @@ -482,6 +487,7 @@
<Content Include="Data\Good\ConditionalRecords4.txt" />
<Content Include="Data\Good\ConverterDecimals1.txt" />
<Content Include="Data\Good\ConverterDecimals2.txt" />
<Content Include="Data\Good\FieldNotEmpty1.txt" />
<Content Include="Data\Good\InNewLineAfterOptional1.txt" />
<Content Include="Data\Good\OrdersSmallVerticalBar.txt" />
<Content Include="Data\Good\CustomersFixedWithout1AndHalfFields.txt" />
Expand Down
34 changes: 34 additions & 0 deletions FileHelpers.Tests/Tests/Common/FieldNotEmpty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;

namespace FileHelpers.Tests.CommonTests
{
[TestFixture]
public class FieldNotEmpty
{
[Test]
public void FieldNotEmpty1()
{
Assert.DoesNotThrow(() => FileTest.Good.FieldNotEmpty1.ReadWithEngine<NotEmptyType>());
}
}

[DelimitedRecord(",")]
public class NotEmptyType {
[FieldNotEmpty()]
public int CustomerID;

[FieldNotEmpty()]
public string CompanyName;

public string ContactName;

[FieldNullValue(true)]
public bool IsActive;

[FieldNotEmpty()]
public DateTime? CreatedDate;
}
}
24 changes: 24 additions & 0 deletions FileHelpers.Tests/Tests/Errors/BadNotEmpty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections;
using System.Collections.Generic;
using FileHelpers.Tests.CommonTests;
using NUnit.Framework;

namespace FileHelpers.Tests.Errors
{
[TestFixture]
public class BadNotEmpty
{
[Test]
public void FieldNotEmpty1()
{
Assert.Throws<ConvertException>(() => FileTest.Bad.FieldNotEmpty1.ReadWithEngine<NotEmptyType>());
}

[Test]
public void FieldNotEmpty2()
{
Assert.Throws<ConvertException>(() => FileTest.Bad.FieldNotEmpty2.ReadWithEngine<NotEmptyType>());
}
}
}
11 changes: 11 additions & 0 deletions FileHelpers/Dynamic/FieldBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ public object FieldNullValue
set { mFieldNullValue = value; }
}

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private bool mFieldNotEmpty = false;

/// <summary>Indicates that the field cannot be empty.</summary>
public bool FieldNotEmpty
{
get { return mFieldNotEmpty; }
set { mFieldNotEmpty = value; }
}

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly ConverterBuilder mConverter = new ConverterBuilder();
Expand Down Expand Up @@ -285,6 +294,8 @@ private void AddAttributesInternal(AttributesBuilder attbs, NetLanguage lang)
if (mFieldInNewLine)
attbs.AddAttribute("FieldInNewLine()");

if (mFieldNotEmpty)
attbs.AddAttribute("FieldNotEmpty()");

if (mFieldNullValue != null) {
if (mFieldNullValue is string)
Expand Down

0 comments on commit 1f36aab

Please sign in to comment.