public void MalformedTest1() { using (var csv = new DelimitedRecordReader(new StringReader("11,12,13\n21,\"\"22\",23\n31,32,33"))) { csv.ParseErrorAction = ParseErrorAction.SkipToNextLine; csv.DynamicColumnCount = true; Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.AreEqual("11", csv[0]); Assert.AreEqual("12", csv[1]); Assert.AreEqual("13", csv[2]); csv.DynamicColumnCount = false; Assert.AreEqual(ReadResult.ParseError, csv.Read()); Assert.AreEqual("21", csv[0]); Assert.AreEqual(null, csv[1]); Assert.AreEqual(null, csv[2]); Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.AreEqual("31", csv[0]); Assert.AreEqual("32", csv[1]); Assert.AreEqual("33", csv[2]); Assert.AreEqual(ReadResult.EndOfFile, csv.Read()); } }
public void SkipEmptyLinesTest1() { using (var csv = new DelimitedRecordReader(new StringReader("00\n\n10"))) { csv.SkipEmptyLines = false; csv.MissingFieldAction = MissingRecordFieldAction.ReturnEmptyValue; Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.AreEqual(1, csv.Columns.Count); Assert.AreEqual("00", csv[0]); // if dynamic column count stays active, there will be 0 column for the empty line csv.DynamicColumnCount = false; Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.AreEqual(1, csv.Columns.Count); Assert.AreEqual(string.Empty, csv[0]); Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.AreEqual(1, csv.Columns.Count); Assert.AreEqual("10", csv[0]); Assert.AreEqual(ReadResult.EndOfFile, csv.Read()); } }
public void MoveToTest_SeekableStream(bool withCaching) { using (var ms = new MemoryStream()) { var writer = new StreamWriter(ms, Encoding.Unicode); writer.Write(DelimitedRecordReaderTestData.SampleData1); writer.Flush(); ms.Position = 0; using (var csv = new DelimitedRecordReader(new StreamReader(ms, Encoding.Unicode))) { if (withCaching) { csv.StartCachingRecordPositions(); } while (csv.Read() == ReadResult.Success) { } for (int i = DelimitedRecordReaderTestData.SampleData1RecordCount - 1; i >= 0; i--) { Assert.AreEqual(ReadResult.Success, csv.MoveTo(i)); DelimitedRecordReaderTestData.CheckSampleData1(csv, false, i); } } } }
public static void ReadAll(DelimitedRecordReaderBenchmarkArguments args) { using (var reader = new DelimitedRecordReader(new StreamReader(args.Path, args.Encoding, true, args.BufferSize), args.BufferSize)) { reader.AdvancedEscapingEnabled = args.AdvancedEscapingEnabled; reader.DoubleQuoteEscapingEnabled = args.DoubleQuoteEscapingEnabled; reader.SkipEmptyLines = args.SkipEmptyLines; reader.TrimWhiteSpaces = args.TrimWhiteSpaces; if (args.FieldIndex > -1) { reader.DynamicColumnCount = false; for (int i = 0; i < args.FieldIndex + 1; i++) reader.Columns.Add(new DelimitedRecordColumn(reader.GetDefaultColumnName(i))); } string s; while (reader.Read() != ReadResult.EndOfFile) { for (int i = 0; i < reader.Columns.Count - 1; i++) s = reader[i]; } } }
public static void CheckSampleData1(DelimitedRecordReader csv, bool hasHeaders, bool readToEnd) { if (!readToEnd) { CheckSampleData1(csv, hasHeaders, csv.CurrentRecordIndex); } else { long recordIndex = 0; while (csv.Read() == ReadResult.Success) { CheckSampleData1(csv, hasHeaders, recordIndex); recordIndex++; } if (hasHeaders) { Assert.AreEqual(SampleData1RecordCount - 1, csv.CurrentRecordIndex); } else { Assert.AreEqual(SampleData1RecordCount, csv.CurrentRecordIndex); } } }
public static void ReadAll(DelimitedRecordReaderBenchmarkArguments args) { using (var reader = new DelimitedRecordReader(new StreamReader(args.Path, args.Encoding, true, args.BufferSize), args.BufferSize)) { reader.AdvancedEscapingEnabled = args.AdvancedEscapingEnabled; reader.DoubleQuoteEscapingEnabled = args.DoubleQuoteEscapingEnabled; reader.SkipEmptyLines = args.SkipEmptyLines; reader.TrimWhiteSpaces = args.TrimWhiteSpaces; if (args.FieldIndex > -1) { reader.DynamicColumnCount = false; for (int i = 0; i < args.FieldIndex + 1; i++) { reader.Columns.Add(new DelimitedRecordColumn(reader.GetDefaultColumnName(i))); } } string s; while (reader.Read() != ReadResult.EndOfFile) { for (int i = 0; i < reader.Columns.Count - 1; i++) { s = reader[i]; } } } }
public void ArgumentTestCopyCurrentRecordTo() { using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws <ArgumentNullException>(() => csv.CopyCurrentRecordTo(null)); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws <ArgumentOutOfRangeException>(() => csv.CopyCurrentRecordTo(new string[1], -1)); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws <ArgumentOutOfRangeException>(() => csv.CopyCurrentRecordTo(new string[1], 1)); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws <ArgumentOutOfRangeException>(() => csv.CopyCurrentRecordTo(new string[DelimitedRecordReaderTestData.SampleData1RecordCount - 1], 0)); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws <ArgumentOutOfRangeException>(() => csv.CopyCurrentRecordTo(new string[DelimitedRecordReaderTestData.SampleData1RecordCount], 1)); } }
public void MissingDelimiterAfterQuotedFieldTest2() { const string Data = "\"111\",\"222\",\"333\"\n\"111\",\"222\"\"333\""; using (var csv = new DelimitedRecordReader(new StringReader(Data))) { csv.DynamicColumnCount = false; for (int i = 0; i < 3; i++) { csv.Columns.Add(new DelimitedRecordColumn(csv.GetDefaultColumnName(i))); } csv.DoubleQuoteEscapingEnabled = false; var ex = Assert.Throws <MalformedRecordException>( () => { while (csv.Read() == ReadResult.Success) { } }); Assert.AreEqual(1, ex.CurrentRecordIndex); Assert.AreEqual(1, ex.CurrentColumnIndex); } }
public void SkipEmptyLinesTest2() { using (var csv = new DelimitedRecordReader(new StringReader("00\n\n10"))) { csv.SkipEmptyLines = true; Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.AreEqual(1, csv.Columns.Count); Assert.AreEqual("00", csv[0]); Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.AreEqual(1, csv.Columns.Count); Assert.AreEqual("10", csv[0]); Assert.AreEqual(ReadResult.EndOfFile, csv.Read()); } }
public void MissingFieldUnquotedTest4() { using (var csv = new DelimitedRecordReader(new StringReader("a,b\n "))) { csv.SkipEmptyLines = true; csv.MissingFieldAction = MissingRecordFieldAction.ReturnNullValue; csv.DynamicColumnCount = true; Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.AreEqual(2, csv.Columns.Count); Assert.AreEqual("a", csv[0]); Assert.AreEqual("b", csv[1]); csv.DynamicColumnCount = false; Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.AreEqual(string.Empty, csv[0]); Assert.AreEqual(null, csv[1]); } }
public void AvancedParsingTest_SpecificBufferSize([Random(10, 1024, 1000)] int bufferSize) { const string Data = @"\u0031\u0032\u033\u34\x0035\x0036\x037\x38\u0039,\d00049\d0050\d051\d52\o000065\o00066\o0067\o070\o71 ""\n\t\\\r\f\v\a\b\e"""; string[] values = { "123456789", "123456789", "\n\t\\\r\f\v\a\b\u001b" }; using (DelimitedRecordReader reader = new DelimitedRecordReader(new StringReader(Data), bufferSize)) { reader.AdvancedEscapingEnabled = true; Assert.AreEqual(ReadResult.Success, reader.Read()); Assert.AreEqual(values[0], reader[0]); Assert.AreEqual(values[1], reader[1]); Assert.AreEqual(ReadResult.Success, reader.Read()); Assert.AreEqual(values[2], reader[0]); Assert.AreEqual(ReadResult.EndOfFile, reader.Read()); } }
public void ReadColumnHeadersTest_NotCalledWithoutDynamicColumns() { using (var csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { csv.DynamicColumnCount = false; while (csv.Read() == ReadResult.Success) { Assert.AreEqual(0, csv.Columns.Count); } } }
public void SkipEmptyLinesTest3() { using (var csv = new DelimitedRecordReader(new StringReader("00\n\n10"))) { csv.SkipEmptyLines = false; csv.MissingFieldAction = MissingRecordFieldAction.ReturnEmptyValue; Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.AreEqual(1, csv.Columns.Count); Assert.AreEqual("00", csv[0]); Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.AreEqual(0, csv.Columns.Count); Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.AreEqual(1, csv.Columns.Count); Assert.AreEqual("10", csv[0]); Assert.AreEqual(ReadResult.EndOfFile, csv.Read()); } }
public void AvancedParsingTest_SpecificBufferSize([Random(10, 1024, 1000)] int bufferSize) { const string Data = @"\u0031\u0032\u033\u34\x0035\x0036\x037\x38\u0039,\d00049\d0050\d051\d52\o000065\o00066\o0067\o070\o71 ""\n\t\\\r\f\v\a\b\e""" ; string[] values = { "123456789", "123456789", "\n\t\\\r\f\v\a\b\u001b" }; using (DelimitedRecordReader reader = new DelimitedRecordReader(new StringReader(Data), bufferSize)) { reader.AdvancedEscapingEnabled = true; Assert.AreEqual(ReadResult.Success, reader.Read()); Assert.AreEqual(values[0], reader[0]); Assert.AreEqual(values[1], reader[1]); Assert.AreEqual(ReadResult.Success, reader.Read()); Assert.AreEqual(values[2], reader[0]); Assert.AreEqual(ReadResult.EndOfFile, reader.Read()); } }
public void MoreFieldsTest(bool dynamicColumnCount) { const string Data = "ORIGIN,DESTINATION\nPHL,FLL,kjhkj kjhkjh,eg,fhgf\nNYC,LAX"; using (var csv = new DelimitedRecordReader(new StringReader(Data))) { csv.DynamicColumnCount = dynamicColumnCount; Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders()); Assert.AreEqual(2, csv.Columns.Count); Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.AreEqual("PHL", csv[0]); Assert.AreEqual("FLL", csv[1]); Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.AreEqual("NYC", csv[0]); Assert.AreEqual("LAX", csv[1]); Assert.AreEqual(ReadResult.EndOfFile, csv.Read()); } }
public void UnicodeParsingTest1() { // control characters and comma are skipped char[] raw = new char[65536 - 13]; for (int i = 0; i < raw.Length; i++) { raw[i] = (char)(i + 14); } raw[44 - 14] = ' '; // skip comma string data = new string(raw); using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(data))) { Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.AreEqual(data, csv[0]); Assert.AreEqual(ReadResult.EndOfFile, csv.Read()); } }
public void CopyCurrentRecordToTest([Range(0, 5)] int startIndex) { using (var csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { var record = new string[DelimitedRecordReaderTestData.SampleData1ColumnCount + startIndex]; while (csv.Read() == ReadResult.Success) { csv.CopyCurrentRecordTo(record, startIndex); DelimitedRecordReaderTestData.CheckSampleData1(record, false, csv.CurrentRecordIndex, startIndex); } } }
public void UnicodeParsingTest3() { const string test = "München"; byte[] buffer; using (MemoryStream stream = new MemoryStream()) { using (TextWriter writer = new StreamWriter(stream, Encoding.Unicode)) { writer.Write(test); } buffer = stream.ToArray(); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StreamReader(new MemoryStream(buffer), Encoding.Unicode, false))) { Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.AreEqual(test, csv[0]); Assert.AreEqual(ReadResult.EndOfFile, csv.Read()); } }
public static void CheckSampleData1(DelimitedRecordReader csv, bool hasHeaders, bool readToEnd) { if (!readToEnd) CheckSampleData1(csv, hasHeaders, csv.CurrentRecordIndex); else { long recordIndex = 0; while (csv.Read() == ReadResult.Success) { CheckSampleData1(csv, hasHeaders, recordIndex); recordIndex++; } if (hasHeaders) Assert.AreEqual(SampleData1RecordCount - 1, csv.CurrentRecordIndex); else Assert.AreEqual(SampleData1RecordCount, csv.CurrentRecordIndex); } }
public void ColumnsTest_SpecificValueConverterIsUsed() { using (var csv = new DelimitedRecordReader(new StringReader("a,b\n1.0,2.0"))) { Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders()); Assert.AreEqual(ReadResult.Success, csv.Read()); CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); csv.Columns["a"].ValueConverter = new StringValueConverter { IntegerNumberStyles = NumberStyles.Integer }; csv.Columns["b"].ValueConverter = new StringValueConverter { IntegerNumberStyles = NumberStyles.Float }; Assert.Throws <FormatException>(() => csv.GetValue("a", 0)); Assert.AreEqual(2.0f, csv.GetValue("b", 0.0f)); } }
public void MissingFieldQuotedTest4() { const string Data = "a,b,c,d\n1,1,1,1\n2,\"2\",\n\"3\",3,3,3"; using (var csv = new DelimitedRecordReader(new StringReader(Data), 11)) { csv.DynamicColumnCount = false; for (int i = 0; i < 4; i++) { csv.Columns.Add(new DelimitedRecordColumn(csv.GetDefaultColumnName(i))); } var ex = Assert.Throws <MissingRecordFieldException>( () => { while (csv.Read() == ReadResult.Success) { } }); Assert.AreEqual(2, ex.CurrentRecordIndex); Assert.AreEqual(3, ex.CurrentColumnIndex); } }
public void ArgumentTestIndexer() { using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.Throws <ArgumentOutOfRangeException>(() => { string s = csv[-1]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.Throws <ArgumentOutOfRangeException>(() => { string s = csv[DelimitedRecordReaderTestData.SampleData1RecordCount]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws <ArgumentOutOfRangeException>(() => { string s = csv[-1]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws <ArgumentOutOfRangeException>(() => { string s = csv[DelimitedRecordReaderTestData.SampleData1RecordCount]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.Throws <InvalidOperationException>(() => { string s = csv["asdf"]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.Throws <InvalidOperationException>(() => { string s = csv[DelimitedRecordReaderTestData.SampleData1Header0]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders()); Assert.Throws <ArgumentNullException>(() => { string s = csv[null]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders()); Assert.Throws <ArgumentNullException>(() => { string s = csv[string.Empty]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders()); Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws <ArgumentNullException>(() => { string s = csv[null]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders()); Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws <ArgumentNullException>(() => { string s = csv[string.Empty]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders()); Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws <ArgumentException>(() => { string s = csv["asdf"]; }); } }
public void ArgumentTestIndexer() { using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.Throws<ArgumentOutOfRangeException>(() => { string s = csv[-1]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.Throws<ArgumentOutOfRangeException>(() => { string s = csv[DelimitedRecordReaderTestData.SampleData1RecordCount]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws<ArgumentOutOfRangeException>(() => { string s = csv[-1]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws<ArgumentOutOfRangeException>(() => { string s = csv[DelimitedRecordReaderTestData.SampleData1RecordCount]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.Throws<InvalidOperationException>(() => { string s = csv["asdf"]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.Throws<InvalidOperationException>(() => { string s = csv[DelimitedRecordReaderTestData.SampleData1Header0]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders()); Assert.Throws<ArgumentNullException>(() => { string s = csv[null]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders()); Assert.Throws<ArgumentNullException>(() => { string s = csv[string.Empty]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders()); Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws<ArgumentNullException>(() => { string s = csv[null]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders()); Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws<ArgumentNullException>(() => { string s = csv[string.Empty]; }); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders()); Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws<ArgumentException>(() => { string s = csv["asdf"]; }); } }
public void ParsingTest(string data, int? bufferSize, ReaderOptions? options, char? delimiter, char? quote, char? comment, string expectedColumnHeaders, IEnumerable<string> expectedRecords) { using (var reader = new DelimitedRecordReader(new StringReader(data), bufferSize.HasValue ? bufferSize.Value : DelimitedRecordReader.DefaultBufferSize)) { if (options != null) { reader.AdvancedEscapingEnabled = (options & ReaderOptions.AdvancedEscaping) != 0; reader.DoubleQuoteEscapingEnabled = (options & ReaderOptions.NoDoubleQuoteEscaping) == 0; reader.SkipEmptyLines = (options & ReaderOptions.NoSkipEmptyLines) == 0; reader.TrimWhiteSpaces = (options & ReaderOptions.NoTrim) == 0; } if (comment != null) reader.CommentCharacter = comment.Value; if (delimiter != null) reader.DelimiterCharacter = delimiter.Value; if (quote != null) reader.QuoteCharacter = quote.Value; string[] headers = null; if (!string.IsNullOrEmpty(expectedColumnHeaders)) { reader.DynamicColumnCount = false; Assert.AreEqual(ReadResult.Success, reader.ReadColumnHeaders()); headers = expectedColumnHeaders.Split('|'); Assert.AreEqual(headers.Length, reader.Columns.Count); for (int i = 0; i < headers.Length; i++) Assert.AreEqual(headers[i], reader.Columns[i].Name); } foreach (var record in expectedRecords) { Assert.AreEqual(ReadResult.Success, reader.Read()); string[] values = record.Split('|'); if (headers != null) Assert.AreEqual(headers.Length, values.Length); Assert.AreEqual(values.Length, reader.Columns.Count); for (int columnIndex = 0; columnIndex < values.Length; columnIndex++) Assert.AreEqual(values[columnIndex], reader[columnIndex]); if (headers != null) { for (int columnIndex = 0; columnIndex < values.Length; columnIndex++) Assert.AreEqual(values[columnIndex], reader[headers[columnIndex]]); } } Assert.AreEqual(ReadResult.EndOfFile, reader.Read()); } }
public void ParsingTest(string data, int?bufferSize, ReaderOptions?options, char?delimiter, char?quote, char?comment, string expectedColumnHeaders, IEnumerable <string> expectedRecords) { using (var reader = new DelimitedRecordReader(new StringReader(data), bufferSize.HasValue ? bufferSize.Value : DelimitedRecordReader.DefaultBufferSize)) { if (options != null) { reader.AdvancedEscapingEnabled = (options & ReaderOptions.AdvancedEscaping) != 0; reader.DoubleQuoteEscapingEnabled = (options & ReaderOptions.NoDoubleQuoteEscaping) == 0; reader.SkipEmptyLines = (options & ReaderOptions.NoSkipEmptyLines) == 0; reader.TrimWhiteSpaces = (options & ReaderOptions.NoTrim) == 0; } if (comment != null) { reader.CommentCharacter = comment.Value; } if (delimiter != null) { reader.DelimiterCharacter = delimiter.Value; } if (quote != null) { reader.QuoteCharacter = quote.Value; } string[] headers = null; if (!string.IsNullOrEmpty(expectedColumnHeaders)) { reader.DynamicColumnCount = false; Assert.AreEqual(ReadResult.Success, reader.ReadColumnHeaders()); headers = expectedColumnHeaders.Split('|'); Assert.AreEqual(headers.Length, reader.Columns.Count); for (int i = 0; i < headers.Length; i++) { Assert.AreEqual(headers[i], reader.Columns[i].Name); } } foreach (var record in expectedRecords) { Assert.AreEqual(ReadResult.Success, reader.Read()); string[] values = record.Split('|'); if (headers != null) { Assert.AreEqual(headers.Length, values.Length); } Assert.AreEqual(values.Length, reader.Columns.Count); for (int columnIndex = 0; columnIndex < values.Length; columnIndex++) { Assert.AreEqual(values[columnIndex], reader[columnIndex]); } if (headers != null) { for (int columnIndex = 0; columnIndex < values.Length; columnIndex++) { Assert.AreEqual(values[columnIndex], reader[headers[columnIndex]]); } } } Assert.AreEqual(ReadResult.EndOfFile, reader.Read()); } }
private void CheckMissingFieldUnquoted(long recordCount, int columnCount, long badRecordIndex, int badColumnIndex, int bufferSize, MissingRecordFieldAction action) { // construct the csv data with template "00,01,02\n10,11,12\n...." and calculate expected error position long capacity = recordCount * (columnCount * 2 + columnCount - 1) + recordCount; Assert.IsTrue(capacity <= int.MaxValue); var sb = new StringBuilder((int)capacity); for (long i = 0; i < recordCount; i++) { int readColumnCount; if (i == badRecordIndex) { readColumnCount = badColumnIndex; } else { readColumnCount = columnCount; } for (int j = 0; j < readColumnCount; j++) { sb.Append(i); sb.Append(j); sb.Append(DelimitedRecordReader.DefaultDelimiterCharacter); } sb.Length--; sb.Append('\n'); } // test csv const string AssertMessage = "RecordIndex={0}; ColumnIndex={1}; Position={2}; Action={3}"; using (var csv = new DelimitedRecordReader(new StringReader(sb.ToString()), bufferSize)) { csv.DynamicColumnCount = false; for (int i = 0; i < columnCount; i++) { csv.Columns.Add(new DelimitedRecordColumn(csv.GetDefaultColumnName(i))); } csv.MissingFieldAction = action; Assert.AreEqual(columnCount, csv.Columns.Count); try { while (csv.Read() == ReadResult.Success) { Assert.AreEqual(columnCount, csv.Columns.Count); for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) { string s = csv[columnIndex]; if (csv.CurrentRecordIndex != badRecordIndex || columnIndex < badColumnIndex) { Assert.AreEqual(csv.CurrentRecordIndex.ToString() + columnIndex.ToString(), s); //, AssertMessage, csv.CurrentRecordIndex, columnIndex, -1, action); } else { switch (action) { case MissingRecordFieldAction.ReturnEmptyValue: Assert.AreEqual(string.Empty, s); //, AssertMessage, csv.CurrentRecordIndex, columnIndex, -1, action); break; case MissingRecordFieldAction.ReturnNullValue: Assert.IsNull(s); //, AssertMessage, csv.CurrentRecordIndex, columnIndex, -1, action); break; case MissingRecordFieldAction.HandleAsParseError: throw new AssertionException(string.Format("Failed to throw HandleAsParseError. - " + AssertMessage, csv.CurrentRecordIndex, columnIndex, -1, action)); default: throw new AssertionException(string.Format("'{0}' is not handled by this test.", action)); } } } } } catch (MissingRecordFieldException ex) { Assert.AreEqual(badRecordIndex, ex.CurrentRecordIndex, AssertMessage, ex.CurrentRecordIndex, ex.CurrentColumnIndex, ex.BufferPosition, action); Assert.IsTrue(ex.CurrentColumnIndex >= badColumnIndex, AssertMessage, ex.CurrentRecordIndex, ex.CurrentColumnIndex, ex.BufferPosition, action); } } }
public void ArgumentTestCopyCurrentRecordTo() { using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws<ArgumentNullException>(() => csv.CopyCurrentRecordTo(null)); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws<ArgumentOutOfRangeException>(() => csv.CopyCurrentRecordTo(new string[1], -1)); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws<ArgumentOutOfRangeException>(() => csv.CopyCurrentRecordTo(new string[1], 1)); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws<ArgumentOutOfRangeException>(() => csv.CopyCurrentRecordTo(new string[DelimitedRecordReaderTestData.SampleData1RecordCount - 1], 0)); } using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1))) { Assert.AreEqual(ReadResult.Success, csv.Read()); Assert.Throws<ArgumentOutOfRangeException>(() => csv.CopyCurrentRecordTo(new string[DelimitedRecordReaderTestData.SampleData1RecordCount], 1)); } }
public void ReadColumnHeadersTest_TypedColumns() { Triple <string, Type, object>[] supported = new Triple <string, Type, object>[] { new Triple <string, Type, object>("string", typeof(string), default(string)), new Triple <string, Type, object>("char", typeof(char), default(char)), new Triple <string, Type, object>("sbyte", typeof(sbyte), default(sbyte)), new Triple <string, Type, object>("byte", typeof(byte), default(byte)), new Triple <string, Type, object>("short", typeof(short), default(short)), new Triple <string, Type, object>("ushort", typeof(ushort), default(ushort)), new Triple <string, Type, object>("int", typeof(int), default(int)), new Triple <string, Type, object>("uint", typeof(uint), default(uint)), new Triple <string, Type, object>("long", typeof(long), default(long)), new Triple <string, Type, object>("ulong", typeof(ulong), default(ulong)), new Triple <string, Type, object>("float", typeof(float), default(float)), new Triple <string, Type, object>("double", typeof(double), default(double)), new Triple <string, Type, object>("decimal", typeof(decimal), default(decimal)), new Triple <string, Type, object>("DateTime", typeof(DateTime), default(DateTime)), new Triple <string, Type, object>("TimeSpan", typeof(TimeSpan), default(TimeSpan)), new Triple <string, Type, object>("String", typeof(String), default(String)), new Triple <string, Type, object>("Char", typeof(Char), default(Char)), new Triple <string, Type, object>("SByte", typeof(SByte), default(SByte)), new Triple <string, Type, object>("Byte", typeof(Byte), default(Byte)), new Triple <string, Type, object>("Int16", typeof(Int16), default(Int16)), new Triple <string, Type, object>("UInt16", typeof(UInt16), default(UInt16)), new Triple <string, Type, object>("Int32", typeof(Int32), default(Int32)), new Triple <string, Type, object>("UInt32", typeof(UInt32), default(UInt32)), new Triple <string, Type, object>("Int64", typeof(Int64), default(Int64)), new Triple <string, Type, object>("UInt64", typeof(UInt64), default(UInt64)), new Triple <string, Type, object>("Single", typeof(Single), default(Single)), new Triple <string, Type, object>("Double", typeof(Double), default(Double)), new Triple <string, Type, object>("Decimal", typeof(Decimal), default(Decimal)) }; StringBuilder sb = new StringBuilder(); for (int i = 0; i < supported.Length; i++) { Triple <string, Type, object> entry = supported[i]; sb.Append(entry.First); sb.Append(i); sb.Append(':'); sb.Append(entry.First); if (i < supported.Length - 1) { sb.Append(','); } } using (var csv = new DelimitedRecordReader(new StringReader(sb.ToString()))) { Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders()); Assert.AreEqual(supported.Length, csv.Columns.Count); for (int i = 0; i < supported.Length; i++) { Assert.AreEqual(supported[i].Second, csv.Columns[i].DataType); Assert.AreEqual(supported[i].Third, csv.Columns[i].DefaultValue); } Assert.AreEqual(ReadResult.EndOfFile, csv.Read()); } }