Skip to content

Commit

Permalink
Merge pull request #5 from lvaleriu/master
Browse files Browse the repository at this point in the history
Pull request
  • Loading branch information
mperdeck committed Feb 13, 2014
2 parents 0b4d54a + 62acd5d commit 63a9037
Show file tree
Hide file tree
Showing 10 changed files with 553 additions and 18 deletions.
185 changes: 185 additions & 0 deletions LINQtoCSV.Tests/CsvContextReadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,191 @@ namespace LINQtoCSV.Tests
[TestClass()]
public class CsvContextReadTests : Test
{
[TestMethod()]
public void GoodFileUsingOutputFormatForParsingDatesCharUSEnglish()
{
// Arrange

CsvFileDescription fileDescription_namesUs = new CsvFileDescription
{
SeparatorChar = ';',
FirstLineHasColumnNames = false,
UseOutputFormatForParsingCsvValue = true,
EnforceCsvColumnAttribute = true, // default is false
FileCultureName = "en-US" // default is the current culture
};

string testInput =
"AAAAAAAA;052308" + Environment.NewLine +
"BBBBBBBB;051212" + Environment.NewLine +
"CCCCCCCC;122308";

var expected = new[] {
new ProductDataParsingOutputFormat() {
name = "AAAAAAAA", startDate = new DateTime(2008, 5, 23),
},
new ProductDataParsingOutputFormat {
name = "BBBBBBBB", startDate = new DateTime(2012, 5, 12),
},
new ProductDataParsingOutputFormat {
name = "CCCCCCCC", startDate = new DateTime(2008, 12, 23),
}
};

// Act and Assert

AssertRead(testInput, fileDescription_namesUs, expected);
}

[TestMethod()]
public void GoodFileNoSeparatorCharUseOutputFormatForParsingUSEnglish()
{
// Arrange

CsvFileDescription fileDescription_namesUs = new CsvFileDescription
{
NoSeparatorChar = true,
UseOutputFormatForParsingCsvValue = true,
FirstLineHasColumnNames = false,
EnforceCsvColumnAttribute = true, // default is false
FileCultureName = "en-US" // default is the current culture
};

string testInput =
@"AAAAAAAA34.18405/23/08\n
BBBBBBBB10.31105/12/12\n
CCCCCCCC12.00012/23/08";

var expected = new[] {
new ProductDataCharLength() {
name = "AAAAAAAA", weight = 34.184, startDate = new DateTime(2008, 5, 23),
},
new ProductDataCharLength {
name = "BBBBBBBB", weight = 10.311, startDate = new DateTime(2012, 5, 12),
},
new ProductDataCharLength {
name = "CCCCCCCC", weight = 12.000, startDate = new DateTime(2008, 12, 23),
}
};

// Act and Assert

AssertRead(testInput, fileDescription_namesUs, expected);
}

[TestMethod()]
public void GoodFileNoSeparatorCharUSEnglish()
{
// Arrange

CsvFileDescription fileDescription_namesUs = new CsvFileDescription
{
NoSeparatorChar = true,
UseOutputFormatForParsingCsvValue = false,
FirstLineHasColumnNames = false,
EnforceCsvColumnAttribute = true, // default is false
FileCultureName = "en-US" // default is the current culture
};

string testInput =
@"AAAAAAAA34.18405/23/08\n
BBBBBBBB10.31105/12/12\n
CCCCCCCC12.00012/23/08";

var expected = new[] {
new ProductDataCharLength() {
name = "AAAAAAAA", weight = 34.184, startDate = new DateTime(2008, 5, 23),
},
new ProductDataCharLength {
name = "BBBBBBBB", weight = 10.311, startDate = new DateTime(2012, 5, 12),
},
new ProductDataCharLength {
name = "CCCCCCCC", weight = 12.000, startDate = new DateTime(2008, 12, 23),
}
};

// Act and Assert

AssertRead(testInput, fileDescription_namesUs, expected);
}

[TestMethod()]
public void GoodFileCommaDelimitedUseFieldIndexForReadingDataCharUSEnglish()
{
// Arrange

CsvFileDescription fileDescription_namesUs = new CsvFileDescription
{
SeparatorChar = ',',
IgnoreMissingColumns = true,
UseFieldIndexForReadingData = true,
FirstLineHasColumnNames = false,
EnforceCsvColumnAttribute = true, // default is false
FileCultureName = "en-US" // default is the current culture
};

string testInput =
"AAAAAAAA,__,34.184,05/23/08" + Environment.NewLine +
"BBBBBBBB,__,10.311,05/12/12" + Environment.NewLine +
"CCCCCCCC,__,12.000,12/23/08";

var expected = new[] {
new ProductDataSpecificFieldIndex() {
name = "AAAAAAAA", weight = 34.184, startDate = new DateTime(2008, 5, 23),
},
new ProductDataSpecificFieldIndex {
name = "BBBBBBBB", weight = 10.311, startDate = new DateTime(2012, 5, 12),
},
new ProductDataSpecificFieldIndex {
name = "CCCCCCCC", weight = 12.000, startDate = new DateTime(2008, 12, 23),
}
};

// Act and Assert

AssertRead(testInput, fileDescription_namesUs, expected);
}

[TestMethod()]
public void GoodFileCommaDelimitedUseFieldIndexForReadingDataCharUseOutputFormatForParsingUSEnglish()
{
// Arrange

CsvFileDescription fileDescription_namesUs = new CsvFileDescription
{
SeparatorChar = ',',
IgnoreMissingColumns = true,
UseOutputFormatForParsingCsvValue = true,

UseFieldIndexForReadingData = true,
FirstLineHasColumnNames = false,
EnforceCsvColumnAttribute = true, // default is false
FileCultureName = "en-US" // default is the current culture
};

string testInput =
"AAAAAAAA,__,34.184,05/23/08" + Environment.NewLine +
"BBBBBBBB,__,10.311,05/12/12" + Environment.NewLine +
"CCCCCCCC,__,12.000,12/23/08";

var expected = new[] {
new ProductDataSpecificFieldIndex() {
name = "AAAAAAAA", weight = 34.184, startDate = new DateTime(2008, 5, 23),
},
new ProductDataSpecificFieldIndex {
name = "BBBBBBBB", weight = 10.311, startDate = new DateTime(2012, 5, 12),
},
new ProductDataSpecificFieldIndex {
name = "CCCCCCCC", weight = 12.000, startDate = new DateTime(2008, 12, 23),
}
};

// Act and Assert

AssertRead(testInput, fileDescription_namesUs, expected);
}


[TestMethod()]
public void GoodFileCommaDelimitedNamesInFirstLineUSEnglish()
{
Expand Down
1 change: 1 addition & 0 deletions LINQtoCSV.Tests/LINQtoCSV.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<Compile Include="CsvContextWriteTests.cs" />
<Compile Include="CsvContextReadTests.cs" />
<Compile Include="IAssertable.cs" />
<Compile Include="ProductDataSpecificFieldIndex.cs" />
<Compile Include="ProductData.cs" />
<Compile Include="ProductData_DuplicateIndices.cs" />
<Compile Include="ProductData_MissingFieldIndex.cs" />
Expand Down
84 changes: 84 additions & 0 deletions LINQtoCSV.Tests/ProductDataSpecificFieldIndex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Globalization;
using LINQtoCSV;

namespace LINQtoCSV.Tests
{
// Because the fields in this type are used only indirectly, the compiler
// will warn they are unused or unassigned. Disable those warnings.
#pragma warning disable 0169, 0414, 0649

internal class ProductDataSpecificFieldIndex : IAssertable<ProductDataSpecificFieldIndex>
{
[CsvColumn(FieldIndex = 1)]
public string name;

// OutputFormat uses the same codes as the standard ToString method (search MSDN).
[CsvColumn(FieldIndex = 4, OutputFormat = "MM/dd/yy")]
public DateTime startDate;

// Can use both fields and properties
[CsvColumn(FieldIndex = 3, CanBeNull = false)]
public double weight { get; set; }

#pragma warning restore 0169, 0414, 0649

public void AssertEqual(ProductDataSpecificFieldIndex other)
{
Assert.AreNotEqual(other, null);

Assert.AreEqual(other.name, name, "name");
Assert.AreEqual(other.startDate, startDate, "startDate");
Assert.AreEqual(other.weight, weight, "weight");
}
}

internal class ProductDataCharLength : IAssertable<ProductDataCharLength>
{
[CsvColumn(FieldIndex = 1, CharLength = 8)]
public string name;

// OutputFormat uses the same codes as the standard ToString method (search MSDN).
[CsvColumn(FieldIndex = 3, OutputFormat = "MM/dd/yy", CharLength = 8)]
public DateTime startDate;

// Can use both fields and properties
[CsvColumn(FieldIndex = 2, CanBeNull = false, CharLength = 6)]
public double weight { get; set; }

#pragma warning restore 0169, 0414, 0649

public void AssertEqual(ProductDataCharLength other)
{
Assert.AreNotEqual(other, null);

Assert.AreEqual(other.name, name, "name");
Assert.AreEqual(other.startDate, startDate, "startDate");
Assert.AreEqual(other.weight, weight, "weight");
}
}

internal class ProductDataParsingOutputFormat : IAssertable<ProductDataParsingOutputFormat>
{
[CsvColumn(FieldIndex = 1)]
public string name;

// OutputFormat uses the same codes as the standard ToString method (search MSDN).
[CsvColumn(FieldIndex = 2, OutputFormat = "MMddyy")]
public DateTime startDate;

#pragma warning restore 0169, 0414, 0649

public void AssertEqual(ProductDataParsingOutputFormat other)
{
Assert.AreNotEqual(other, null);

Assert.AreEqual(other.name, name, "name");
Assert.AreEqual(other.startDate, startDate, "startDate");
}
}
}
6 changes: 5 additions & 1 deletion LINQtoCSV/CsvColumnAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class CsvColumnAttribute : System.Attribute
public int FieldIndex { get; set; }
public NumberStyles NumberStyle { get; set; }
public string OutputFormat { get; set; }
public int CharLength { get; set; }

public CsvColumnAttribute()
{
Expand All @@ -35,13 +36,16 @@ public CsvColumnAttribute(
int fieldIndex,
bool canBeNull,
string outputFormat,
NumberStyles numberStyle)
NumberStyles numberStyle,
int charLength)
{
Name = name;
FieldIndex = fieldIndex;
CanBeNull = canBeNull;
NumberStyle = numberStyle;
OutputFormat = outputFormat;

CharLength = charLength;
}
}
}
8 changes: 7 additions & 1 deletion LINQtoCSV/CsvContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,14 @@ private IEnumerable<T> ReadData<T>(

try
{
List<int> charLengths = null;
if (!readingRawDataRows)
{
charLengths = fm.GetCharLengths();
}

bool firstRow = true;
while (cs.ReadRow(ref row))
while (cs.ReadRow(ref row, charLengths))
{
// Skip empty lines.
// Important. If there is a newline at the end of the last data line, the code
Expand Down
10 changes: 10 additions & 0 deletions LINQtoCSV/CsvFileDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class CsvFileDescription
// the tab character ('\t').
public char SeparatorChar { get; set; }

public bool NoSeparatorChar { get; set; }

// Only used when writing a file
//
// If true, all fields are quoted whatever their content.
Expand Down Expand Up @@ -85,6 +87,10 @@ public int MaximumNbrExceptions
public Encoding TextEncoding { get; set; }
public bool DetectEncodingFromByteOrderMarks { get; set; }

public bool IgnoreMissingColumns { get; set; }
public bool UseFieldIndexForReadingData { get; set; }
public bool UseOutputFormatForParsingCsvValue { get; set; }


// ---------------

Expand All @@ -97,6 +103,10 @@ public CsvFileDescription()
SeparatorChar = ',';
TextEncoding = Encoding.UTF8;
DetectEncodingFromByteOrderMarks = true;

NoSeparatorChar = false;
UseFieldIndexForReadingData = false;
UseOutputFormatForParsingCsvValue = false;
}
}
}
Loading

0 comments on commit 63a9037

Please sign in to comment.