diff --git a/.gitignore b/.gitignore index b9d6bd9..4ead766 100644 --- a/.gitignore +++ b/.gitignore @@ -130,7 +130,7 @@ publish/ # NuGet Packages Directory ## TODO: If you have NuGet Package Restore enabled, uncomment the next line -#packages/ +packages/ # Windows Azure Build Output csx diff --git a/LINQtoCSV.Tests/CsvContextReadTests.cs b/LINQtoCSV.Tests/CsvContextReadTests.cs index b8f57d8..e386f45 100644 --- a/LINQtoCSV.Tests/CsvContextReadTests.cs +++ b/LINQtoCSV.Tests/CsvContextReadTests.cs @@ -1,213 +1,213 @@ using LINQtoCSV; -using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.IO; using System.Collections.Generic; +using NUnit.Framework; 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 = ',', - IgnoreUnknownColumns = 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 = ',', - IgnoreUnknownColumns = 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() - { - // Arrange - - CsvFileDescription fileDescription_namesUs = new CsvFileDescription - { - SeparatorChar = ',', // default is ',' - FirstLineHasColumnNames = true, - EnforceCsvColumnAttribute = false, // default is false - FileCultureName = "en-US" // default is the current culture - }; - - string testInput = + [TestFixture] + public class CsvContextReadTests : Test + { + [Test] + 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); + } + + [Test] + 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" + Environment.NewLine + + "BBBBBBBB10.31105/12/12" + Environment.NewLine + + "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); + } + + [Test] + 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" + Environment.NewLine + + "BBBBBBBB10.31105/12/12" + Environment.NewLine + + "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); + } + + [Test] + public void GoodFileCommaDelimitedUseFieldIndexForReadingDataCharUSEnglish() + { + // Arrange + + CsvFileDescription fileDescription_namesUs = new CsvFileDescription + { + SeparatorChar = ',', + IgnoreUnknownColumns = 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); + } + + [Test] + public void GoodFileCommaDelimitedUseFieldIndexForReadingDataCharUseOutputFormatForParsingUSEnglish() + { + // Arrange + + CsvFileDescription fileDescription_namesUs = new CsvFileDescription + { + SeparatorChar = ',', + IgnoreUnknownColumns = 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); + } + + + [Test] + public void GoodFileCommaDelimitedNamesInFirstLineUSEnglish() + { + // Arrange + + CsvFileDescription fileDescription_namesUs = new CsvFileDescription + { + SeparatorChar = ',', // default is ',' + FirstLineHasColumnNames = true, + EnforceCsvColumnAttribute = false, // default is false + FileCultureName = "en-US" // default is the current culture + }; + + string testInput = @"name, weight, startDate, launchTime, nbrAvailable,onsale,shopsAvailable, code, price, description moonbuggy, 34.184, 5/23/08, 5-May-2009 4:11 pm, 1205, true, ""Paris, New York"", 1F, $540.12, newly launched product ""mouse trap"",45E-5, 1/2/1985, ""7 August 1988, 0:00 am"", ""4,030"", FALSE, ""This field has @@ -216,88 +216,86 @@ two newlines and a quoted """"string"""""" dog house, ""45,230,990"",29 Feb 2004, , -56, True,"""", FF10, ""12,008"""; - var expected = new [] { - new ProductData { - name = "moonbuggy", weight = 34.184, startDate = new DateTime(2008, 5, 23), launchTime = new DateTime(2009, 5, 5, 16, 11, 0), - nbrAvailable = 1205, onsale = true, shopsAvailable = "Paris, New York", hexProductCode = 31, retailPrice = 540.12M, - description = "newly launched product" - }, - new ProductData { - name = "mouse trap", weight = 45E-5, startDate = new DateTime(1985, 1, 2), launchTime = new DateTime(1988, 8, 7, 0, 0, 0), - nbrAvailable = 4030, onsale = false, shopsAvailable = @"This field has + var expected = new [] { + new ProductData { + name = "moonbuggy", weight = 34.184, startDate = new DateTime(2008, 5, 23), launchTime = new DateTime(2009, 5, 5, 16, 11, 0), + nbrAvailable = 1205, onsale = true, shopsAvailable = "Paris, New York", hexProductCode = 31, retailPrice = 540.12M, + description = "newly launched product" + }, + new ProductData { + name = "mouse trap", weight = 45E-5, startDate = new DateTime(1985, 1, 2), launchTime = new DateTime(1988, 8, 7, 0, 0, 0), + nbrAvailable = 4030, onsale = false, shopsAvailable = @"This field has a newline", hexProductCode = 256, retailPrice = 78300M, - description = @"This field has quotes(""), and + description = @"This field has quotes(""), and two newlines and a quoted ""string""" - }, - new ProductData { - name = "dog house", weight = 45230990, startDate = new DateTime(2004, 2, 29), launchTime = default(DateTime), - nbrAvailable = -56, onsale = true, shopsAvailable = "", hexProductCode = 65296, retailPrice = 12008M, - description = null - } - }; - - // Act and Assert - - AssertRead(testInput, fileDescription_namesUs, expected); - } - - [TestMethod()] - public void GoodFileTabDelimitedNoNamesInFirstLineNLnl() - { - // Arrange - - CsvFileDescription fileDescription_nonamesNl = new CsvFileDescription - { - SeparatorChar = '\t', // tab character - FirstLineHasColumnNames = false, - EnforceCsvColumnAttribute = true, - FileCultureName = "nl-NL" // default is the current culture - }; - - string testInput = -"moonbuggy\t 23/5/08\t 5-Mei-2009 16:11 pm\t 34.184\t \"Paris, New York\"\t 1F\t €540,12\t true\t newly launched product\r\n\"mouse trap\"\t 2/1/1985\t \"7 Augustus 1988\t 0:00\"\t45E-5\t \"This field has\r\na newline\"\t 100\t \"€78.300\"\t FALSE\t \"This field has quotes(\"\"), and\r\ntwo newlines\r\nand a quoted \"\"string\"\"\"\r\ndog house\t29 Feb 2004\t \t \"45.230.990\"\t\"\"\t FF10\t \"12.008\"\t True"; - var expected = new[] { - new ProductData { - name = "moonbuggy", weight = 34184, startDate = new DateTime(2008, 5, 23), launchTime = new DateTime(2009, 5, 5, 16, 11, 0), - nbrAvailable = 0, onsale = true, shopsAvailable = "Paris, New York", hexProductCode = 31, retailPrice = 540.12M, - description = "newly launched product" - }, - new ProductData { - name = "mouse trap", weight = 45E-5, startDate = new DateTime(1985, 1, 2), launchTime = new DateTime(1988, 8, 7, 0, 0, 0), - nbrAvailable = 0, onsale = false, shopsAvailable = @"This field has -a newline", hexProductCode = 256, retailPrice = 78300M, - description = @"This field has quotes(""), and -two newlines -and a quoted ""string""" - }, - new ProductData { - name = "dog house", weight = 45230990, startDate = new DateTime(2004, 2, 29), launchTime = default(DateTime), - nbrAvailable = 0, onsale = true, shopsAvailable = "", hexProductCode = 65296, retailPrice = 12008M, - description = null - } - }; - - // Act and Assert - - AssertRead(testInput, fileDescription_nonamesNl, expected); - } - - [TestMethod()] - public void GoodFileCommaDelimitedWithTrailingSeparatorChars() - { - // Arrange - - CsvFileDescription fileDescription_namesUs = new CsvFileDescription - { - SeparatorChar = ',', // default is ',' - FirstLineHasColumnNames = true, - EnforceCsvColumnAttribute = false, // default is false - FileCultureName = "en-US", // default is the current culture - IgnoreTrailingSeparatorChar = true - }; - - string testInput = + }, + new ProductData { + name = "dog house", weight = 45230990, startDate = new DateTime(2004, 2, 29), launchTime = default(DateTime), + nbrAvailable = -56, onsale = true, shopsAvailable = "", hexProductCode = 65296, retailPrice = 12008M, + description = null + } + }; + + // Act and Assert + + AssertRead(testInput, fileDescription_namesUs, expected); + } + + [Test] + public void GoodFileTabDelimitedNoNamesInFirstLineNLnl() + { + // Arrange + + CsvFileDescription fileDescription_nonamesNl = new CsvFileDescription + { + SeparatorChar = '\t', // tab character + FirstLineHasColumnNames = false, + EnforceCsvColumnAttribute = true, + FileCultureName = "nl-NL" // default is the current culture + }; + + string testInput = "moonbuggy\t 23/5/08\t 5-Mei-2009 16:11 pm\t 34.184\t \"Paris, New York\"\t 1F\t €540,12\t true\t newly launched product\r\n\"mouse trap\"\t 2/1/1985\t \"7 Augustus 1988\t 0:00\"\t45E-5\t \"This field has\r\na newline\"\t 100\t \"€78.300\"\t FALSE\t \"This field has quotes(\"\"), and\r\ntwo newlines\r\nand a quoted \"\"string\"\"\"\r\ndog house\t29 Feb 2004\t \t \"45.230.990\"\t\"\"\t FF10\t \"12.008\"\t True"; + var expected = new[] { + new ProductData { + name = "moonbuggy", weight = 34184, startDate = new DateTime(2008, 5, 23), launchTime = new DateTime(2009, 5, 5, 16, 11, 0), + nbrAvailable = 0, onsale = true, shopsAvailable = "Paris, New York", hexProductCode = 31, retailPrice = 540.12M, + description = "newly launched product" + }, + new ProductData { + name = "mouse trap", weight = 45E-5, startDate = new DateTime(1985, 1, 2), launchTime = new DateTime(1988, 8, 7, 0, 0, 0), + nbrAvailable = 0, onsale = false, shopsAvailable = @"This field has" + Environment.NewLine + "a newline", hexProductCode = 256, retailPrice = 78300M, + description = @"This field has quotes(""), and" + Environment.NewLine + + "two newlines" + Environment.NewLine + + "and a quoted \"string\"" + }, + new ProductData { + name = "dog house", weight = 45230990, startDate = new DateTime(2004, 2, 29), launchTime = default(DateTime), + nbrAvailable = 0, onsale = true, shopsAvailable = "", hexProductCode = 65296, retailPrice = 12008M, + description = null + } + }; + + // Act and Assert + + AssertRead(testInput, fileDescription_nonamesNl, expected); + } + + [Test] + public void GoodFileCommaDelimitedWithTrailingSeparatorChars() + { + // Arrange + + CsvFileDescription fileDescription_namesUs = new CsvFileDescription + { + SeparatorChar = ',', // default is ',' + FirstLineHasColumnNames = true, + EnforceCsvColumnAttribute = false, // default is false + FileCultureName = "en-US", // default is the current culture + IgnoreTrailingSeparatorChar = true + }; + + string testInput = @"name, weight, startDate, launchTime, nbrAvailable,onsale,shopsAvailable, code, price, description, moonbuggy, 34.184, 5/23/08, 5-May-2009 4:11 pm, 1205, true, ""Paris, New York"", 1F, $540.12, newly launched product, ""mouse trap"",45E-5, 1/2/1985, ""7 August 1988, 0:00 am"", ""4,030"", FALSE, ""This field has @@ -306,66 +304,103 @@ two newlines and a quoted """"string"""""" dog house, ""45,230,990"",29 Feb 2004, , -56, True,"""", FF10, ""12,008"","; - var expected = new[] { - new ProductData { - name = "moonbuggy", weight = 34.184, startDate = new DateTime(2008, 5, 23), launchTime = new DateTime(2009, 5, 5, 16, 11, 0), - nbrAvailable = 1205, onsale = true, shopsAvailable = "Paris, New York", hexProductCode = 31, retailPrice = 540.12M, - description = "newly launched product" - }, - new ProductData { - name = "mouse trap", weight = 45E-5, startDate = new DateTime(1985, 1, 2), launchTime = new DateTime(1988, 8, 7, 0, 0, 0), - nbrAvailable = 4030, onsale = false, shopsAvailable = @"This field has + var expected = new[] { + new ProductData { + name = "moonbuggy", weight = 34.184, startDate = new DateTime(2008, 5, 23), launchTime = new DateTime(2009, 5, 5, 16, 11, 0), + nbrAvailable = 1205, onsale = true, shopsAvailable = "Paris, New York", hexProductCode = 31, retailPrice = 540.12M, + description = "newly launched product" + }, + new ProductData { + name = "mouse trap", weight = 45E-5, startDate = new DateTime(1985, 1, 2), launchTime = new DateTime(1988, 8, 7, 0, 0, 0), + nbrAvailable = 4030, onsale = false, shopsAvailable = @"This field has a newline", hexProductCode = 256, retailPrice = 78300M, - description = @"This field has quotes(""), and + description = @"This field has quotes(""), and two newlines and a quoted ""string""" - }, - new ProductData { - name = "dog house", weight = 45230990, startDate = new DateTime(2004, 2, 29), launchTime = default(DateTime), - nbrAvailable = -56, onsale = true, shopsAvailable = "", hexProductCode = 65296, retailPrice = 12008M, - description = null - } - }; - - // Act and Assert - - AssertRead(testInput, fileDescription_namesUs, expected); - } - - [TestMethod()] - public void FileWithUnknownColumns_ShouldDiscardColumns() { - var description = new CsvFileDescription - { - SeparatorChar = ',', - FirstLineHasColumnNames = true, - IgnoreUnknownColumns = true, - }; - - //The following input has 5 columns: Id | Name | Last Name | Age | City. Only the Name, Last Name and Age will be read. - - string input = + }, + new ProductData { + name = "dog house", weight = 45230990, startDate = new DateTime(2004, 2, 29), launchTime = default(DateTime), + nbrAvailable = -56, onsale = true, shopsAvailable = "", hexProductCode = 65296, retailPrice = 12008M, + description = null + } + }; + + // Act and Assert + + AssertRead(testInput, fileDescription_namesUs, expected); + } + + [Test] + public void FileWithUnknownColumns_ShouldDiscardColumns() { + var description = new CsvFileDescription + { + SeparatorChar = ',', + FirstLineHasColumnNames = true, + IgnoreUnknownColumns = true, + }; + + //The following input has 5 columns: Id | Name | Last Name | Age | City. Only the Name, Last Name and Age will be read. + + string input = @"Id,Name,Last Name,Age,City 1,John,Doe,15,Washington 2,Jane,Doe,20,New York "; - var expected = new[] - { - new Person - { - Name = "John", - LastName = "Doe", - Age = 15 - }, - new Person - { - Name = "Jane", - LastName = "Doe", - Age = 20 - }, - }; - - AssertRead(input, description, expected); - - } - } + var expected = new[] + { + new Person + { + Name = "John", + LastName = "Doe", + Age = 15 + }, + new Person + { + Name = "Jane", + LastName = "Doe", + Age = 20 + }, + }; + + AssertRead(input, description, expected); + + } + + [Test] + public void FileWithTrailingEmptyColumns_ShouldDiscardColumns() + { + var description = new CsvFileDescription { + SeparatorChar = ',', + FirstLineHasColumnNames = true, + IgnoreUnknownColumns = true, + IgnoreTrailingEmptyColumns = true + }; + + //The following input has 5 columns: Id | Name | Last Name | Age | City. Only the Name, Last Name and Age will be read. + + string input = +@"Id,Name,Last Name,Age,City, , , ,,,, +1,John,Doe,15,Washington, , , , , +2,Jane,Doe,20,New York,, , , +"; + var expected = new[] + { + new Person + { + Name = "John", + LastName = "Doe", + Age = 15 + }, + new Person + { + Name = "Jane", + LastName = "Doe", + Age = 20 + }, + }; + + AssertRead(input, description, expected); + + } + } } diff --git a/LINQtoCSV.Tests/CsvContextWriteTests.cs b/LINQtoCSV.Tests/CsvContextWriteTests.cs index aebc0a1..1f66c37 100644 --- a/LINQtoCSV.Tests/CsvContextWriteTests.cs +++ b/LINQtoCSV.Tests/CsvContextWriteTests.cs @@ -1,45 +1,43 @@ -using LINQtoCSV; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; -using System.IO; +using System; using System.Collections.Generic; using System.Text; +using NUnit.Framework; namespace LINQtoCSV.Tests { - [TestClass()] - public class CsvContextWriteTests : Test - { - [TestMethod()] - public void GoodFileCommaDelimitedNamesInFirstLineNLnl() - { - // Arrange + [TestFixture] + public class CsvContextWriteTests : Test + { + [Test] + public void GoodFileCommaDelimitedNamesInFirstLineNLnl() + { + // Arrange - List dataRows_Test = new List(); - dataRows_Test.Add(new ProductData { retailPrice = 4.59M, name = "Wooden toy", startDate = new DateTime(2008, 2, 1), nbrAvailable = 67 }); - dataRows_Test.Add(new ProductData { onsale = true, weight = 4.03, shopsAvailable = "Ashfield", description = "" }); - dataRows_Test.Add(new ProductData { name = "Metal box", launchTime = new DateTime(2009, 11, 5, 4, 50, 0), description = "Great\nproduct" }); + List dataRows_Test = new List(); + dataRows_Test.Add(new ProductData { id = Guid.NewGuid(), retailPrice = 4.59M, name = "Wooden toy", startDate = new DateTime(2008, 2, 1), nbrAvailable = 67 }); + dataRows_Test.Add(new ProductData { id = Guid.NewGuid(), onsale = true, weight = 4.03, shopsAvailable = "Ashfield", description = "" }); + dataRows_Test.Add(new ProductData { id = Guid.NewGuid(), name = "Metal box", launchTime = new DateTime(2009, 11, 5, 4, 50, 0), description = "Great\nproduct" }); - CsvFileDescription fileDescription_namesNl2 = new CsvFileDescription - { - SeparatorChar = ',', - FirstLineHasColumnNames = true, - EnforceCsvColumnAttribute = false, - TextEncoding = Encoding.Unicode, - FileCultureName = "nl-Nl" // default is the current culture - }; + CsvFileDescription fileDescription_namesNl2 = new CsvFileDescription + { + SeparatorChar = ',', + FirstLineHasColumnNames = true, + EnforceCsvColumnAttribute = false, + TextEncoding = Encoding.Unicode, + FileCultureName = "nl-Nl" // default is the current culture + }; - string expected = -@"name,startDate,launchTime,weight,shopsAvailable,code,price,onsale,description,nbrAvailable,unusedField -Wooden toy,1-2-2008,01 jan 00:00:00,""000,000"",,0,""€ 4,59"",False,,67, -,1-1-0001,01 jan 00:00:00,""004,030"",Ashfield,0,""€ 0,00"",True,"""",0, + string expected = +@"name,startDate,launchTime,weight,shopsAvailable,code,price,onsale,description,id,nbrAvailable,unusedField +Wooden toy,1-2-2008,01 jan 00:00:00,""000,000"",,0,""€ 4,59"",False,," + dataRows_Test[0].id + @",67, +,1-1-0001,01 jan 00:00:00,""004,030"",Ashfield,0,""€ 0,00"",True,""""," + dataRows_Test[1].id + @",0, Metal box,1-1-0001,05 nov 04:50:00,""000,000"",,0,""€ 0,00"",False,""Great -product"",0, +product""," + dataRows_Test[2].id + @",0, "; - // Act and Assert + // Act and Assert - AssertWrite(dataRows_Test, fileDescription_namesNl2, expected); - } - } -} + AssertWrite(dataRows_Test, fileDescription_namesNl2, expected); + } + } +} \ No newline at end of file diff --git a/LINQtoCSV.Tests/LINQtoCSV.Tests.csproj b/LINQtoCSV.Tests/LINQtoCSV.Tests.csproj index 38d8f6b..1dd2654 100644 --- a/LINQtoCSV.Tests/LINQtoCSV.Tests.csproj +++ b/LINQtoCSV.Tests/LINQtoCSV.Tests.csproj @@ -33,7 +33,9 @@ 4 - + + ..\packages\NUnit.2.6.4\lib\nunit.framework.dll + 3.5 @@ -68,6 +70,9 @@ LINQtoCSV + + +