// LumenWorks.Framework.Tests.Unit.IO.CSV.CsvReaderTest
// Copyright (c) 2005 S�bastien Lorion
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// A special thanks goes to "shriop" at CodeProject for providing many of the standard and Unicode parsing tests.
#region Using directives
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using NUnit.Framework;
using LumenWorks.Framework.IO.Csv;
#endregion
namespace LumenWorks.Framework.Tests.Unit.IO.Csv
{
[TestFixture()]
public class CsvReaderMalformedTest
{
[Test()]
[ExpectedException(typeof(MalformedCsvException))]
public void MissingFieldUnquotedTest1()
{
const string Data = "a,b,c,d\n1,1,1,1\n2,2\n3,3,3,3";
try
{
using (CsvReader csv = new CsvReader(new StringReader(Data), false))
{
while (csv.ReadNextRecord())
for (int i = 0; i < csv.FieldCount; i++)
{
string s = csv[i];
}
}
}
catch (MalformedCsvException ex)
{
if (ex.CurrentRecordIndex == 2 && ex.CurrentPosition == 19)
throw ex;
}
}
[Test()]
[ExpectedException(typeof(MalformedCsvException))]
public void MissingFieldUnquotedTest2()
{
const string Data = "a,b,c,d\n1,1,1,1\n2,2,\n3,3,3,3";
try
{
// With bufferSize = 10, faulty new line char is at the start of next buffer read
using (CsvReader csv = new CsvReader(new StringReader(Data), false, 10))
{
while (csv.ReadNextRecord())
for (int i = 0; i < csv.FieldCount; i++)
{
string s = csv[i];
}
}
}
catch (MalformedCsvException ex)
{
if (ex.CurrentRecordIndex == 2 && ex.CurrentPosition == 0)
throw ex;
}
}
[Test()]
[ExpectedException(typeof(MalformedCsvException))]
public void MissingFieldQuotedTest1()
{
const string Data = "a,b,c,d\n1,1,1,1\n2,\"2\"\n3,3,3,3";
try
{
using (CsvReader csv = new CsvReader(new StringReader(Data), false))
{
while (csv.ReadNextRecord())
for (int i = 0; i < csv.FieldCount; i++)
{
string s = csv[i];
}
}
}
catch (MalformedCsvException ex)
{
if (ex.CurrentRecordIndex == 2 && ex.CurrentPosition == 21)
throw ex;
}
}
[Test()]
[ExpectedException(typeof(MalformedCsvException))]
public void MissingFieldQuotedTest2()
{
const string Data = "a,b,c,d\n1,1,1,1\n2,\"2\",\n3,3,3,3";
try
{
using (CsvReader csv = new CsvReader(new StringReader(Data), false, 11))
{
while (csv.ReadNextRecord())
for (int i = 0; i < csv.FieldCount; i++)
{
string s = csv[i];
}
}
}
catch (MalformedCsvException ex)
{
if (ex.CurrentRecordIndex == 2 && ex.CurrentPosition == 0)
throw ex;
}
}
[Test()]
[ExpectedException(typeof(MalformedCsvException))]
public void MissingFieldQuotedTest3()
{
const string Data = "a,b,c,d\n1,1,1,1\n2,\"2\"\n\"3\",3,3,3";
try
{
using (CsvReader csv = new CsvReader(new StringReader(Data), false))
{
while (csv.ReadNextRecord())
for (int i = 0; i < csv.FieldCount; i++)
{
string s = csv[i];
}
}
}
catch (MalformedCsvException ex)
{
if (ex.CurrentRecordIndex == 2 && ex.CurrentPosition == 21)
throw ex;
}
}
[Test()]
[ExpectedException(typeof(MalformedCsvException))]
public void MissingFieldQuotedTest4()
{
const string Data = "a,b,c,d\n1,1,1,1\n2,\"2\",\n\"3\",3,3,3";
try
{
using (CsvReader csv = new CsvReader(new StringReader(Data), false, 11))
{
while (csv.ReadNextRecord())
for (int i = 0; i < csv.FieldCount; i++)
{
string s = csv[i];
}
}
}
catch (MalformedCsvException ex)
{
if (ex.CurrentRecordIndex == 2 && ex.CurrentPosition == 0)
throw ex;
}
}
}
}