public void TestReadArrayRecord([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { var csv = string.Format("1,2,3,4,5,6,一,二{0}", eol.AsNewline()); var settings = new CsvReaderSettings() { RecordDelimiter = eol, }; using (var stringReader = new StringReader(csv)) using (var reader = new CsvReader(stringReader, settings)) { Assert.That(reader.MoveNext(), Is.True); var record = reader.Current.Convert <ArrayRecord>(); Assert.That(record, Is.Not.Null); var property = record.Property; var field = record.Field; Assert.That(property, Is.Not.Null); Assert.That(field, Is.Not.Null); Assert.That(property[0, 0], Is.EqualTo(1)); Assert.That(property[0, 1], Is.EqualTo(2)); Assert.That(property[1, 0], Is.EqualTo(3)); Assert.That(property[1, 1], Is.EqualTo(4)); Assert.That(property[2, 0], Is.EqualTo(5)); Assert.That(property[2, 1], Is.EqualTo(6)); Assert.That(field[0], Is.EqualTo(1)); Assert.That(field[1], Is.EqualTo(2)); Assert.That(reader.MoveNext(), Is.False); } }
public void TestReadOmittableRecord([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { var csv = string.Format("1,2,3{0}4,5{0}6{0}", eol.AsNewline()); var settings = new CsvReaderSettings() { RecordDelimiter = eol, }; using (var stringReader = new StringReader(csv)) using (var reader = new CsvReader(stringReader, settings)) { Assert.That(reader.MoveNext(), Is.True); var record = reader.Current.Convert <OmittableRecord>(); Assert.That(record, Is.Not.Null); Assert.That(record.Column1, Is.EqualTo(1)); Assert.That(record.Column2, Is.EqualTo(2)); Assert.That(record.Column3, Is.EqualTo(3)); Assert.That(reader.MoveNext(), Is.True); record = reader.Current.Convert <OmittableRecord>(); Assert.That(record, Is.Not.Null); Assert.That(record.Column1, Is.EqualTo(4)); Assert.That(record.Column2, Is.EqualTo(5)); Assert.That(record.Column3, Is.EqualTo(default(int))); Assert.That(reader.MoveNext(), Is.True); record = reader.Current.Convert <OmittableRecord>(); Assert.That(record, Is.Not.Null); Assert.That(record.Column1, Is.EqualTo(6)); Assert.That(record.Column2, Is.EqualTo(default(int))); Assert.That(record.Column3, Is.EqualTo(default(int))); Assert.That(reader.MoveNext(), Is.False); } }
public void TestReadRecordRaw([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { const string ascii = "ASCII"; const string nonAscii = "日本語"; const string empty = ""; var withNewline = string.Format("hello{0}world", eol.AsNewline()); var csv = string.Format("{0},{1},{2},\"{3}\",{0}{4}", ascii, nonAscii, empty, withNewline, eol.AsNewline()); var settings = new CsvReaderSettings() { RecordDelimiter = eol, }; using (var stringReader = new StringReader(csv)) using (var reader = new CsvReader(stringReader, settings)) { Assert.That(reader.MoveNext(), Is.True); var record = reader.Current; Assert.That(record, Is.Not.Null); Assert.That(record.FieldCount, Is.EqualTo(5)); Assert.That(record[0], Is.EqualTo(ascii)); Assert.That(record[1], Is.EqualTo(nonAscii)); Assert.That(record[2], Is.EqualTo(empty)); Assert.That(record[3], Is.EqualTo(withNewline)); Assert.That(record[0], Is.EqualTo(ascii)); Assert.That(reader.MoveNext(), Is.False); } }
public void TestReadArrayRecord([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { var csv = string.Format("1,2,3,4,5,6,一,二{0}", eol.AsNewline()); var settings = new CsvReaderSettings() { RecordDelimiter = eol, }; using (var stringReader = new StringReader(csv)) using (var reader = new CsvReader(stringReader, settings)) { Assert.That(reader.MoveNext(), Is.True); var record = reader.Current.Convert<ArrayRecord>(); Assert.That(record, Is.Not.Null); var property = record.Property; var field = record.Field; Assert.That(property, Is.Not.Null); Assert.That(field, Is.Not.Null); Assert.That(property[0, 0], Is.EqualTo(1)); Assert.That(property[0, 1], Is.EqualTo(2)); Assert.That(property[1, 0], Is.EqualTo(3)); Assert.That(property[1, 1], Is.EqualTo(4)); Assert.That(property[2, 0], Is.EqualTo(5)); Assert.That(property[2, 1], Is.EqualTo(6)); Assert.That(field[0], Is.EqualTo(1)); Assert.That(field[1], Is.EqualTo(2)); Assert.That(reader.MoveNext(), Is.False); } }
public void TestReadToEndWithParameteredConstructor([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { var csv = string.Format("1,2,3,4,5{0}6,7,8{0}", eol.AsNewline()); var settings = new CsvReaderSettings() { RecordDelimiter = eol, }; using (var stringReader = new StringReader(csv)) using (var reader = new CsvReader(stringReader, settings)) { var records = reader.ReadToEnd <RecordWithParameteredConstructor>().ToList(); Assert.That(records.Count, Is.EqualTo(2)); var record = records[0]; Assert.That(record, Is.Not.Null); Assert.That(record.Column1, Is.EqualTo(1)); Assert.That(record.Column2, Is.EqualTo(2)); Assert.That(record.Column3, Is.EqualTo(3)); //Assert.That(record.Column4, Is.EqualTo(4)); Assert.That(record.Column5, Is.EqualTo(5)); record = records[1]; Assert.That(record, Is.Not.Null); Assert.That(record.Column1, Is.EqualTo(6)); Assert.That(record.Column2, Is.EqualTo(7)); Assert.That(record.Column3, Is.EqualTo(8)); //Assert.That(record.Column4, Is.EqualTo(default(int))); Assert.That(record.Column5, Is.EqualTo(-1)); } }
public void TestReadToEndGeneric([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { var headerLine = "string1,int1,double1,parsed1,ignored1,string2,int2,double2,parsed2,ignored2"; var csv = string.Format( "{0}{1}" + "hello,1,-4.0,一,_,world,-1,3.0,二,_{1}" + "good,2,NA,NA,_,bye,0,NA,NA,_{1}", headerLine, eol.AsNewline() ); var settings = new CsvReaderSettings() { RecordDelimiter = eol, }; using (var stringReader = new StringReader(csv)) using (var reader = new CsvReader(stringReader, settings)) { Assert.That(reader.MoveNext(), Is.True); var header = reader.Current.ToArray(); reader.SetHeader(header); Assert.That(header, Is.Not.Null); Assert.That(header, Is.EquivalentTo(headerLine.Split(','))); var records = reader.ReadToEnd <PartialRecord>().ToList(); Assert.That(records.Count, Is.EqualTo(2)); Assert.That(reader.MoveNext(), Is.False); var record = records[0]; Assert.That(record, Is.Not.Null); Assert.That(record.StringField, Is.EqualTo("hello")); Assert.That(record.IntField, Is.EqualTo(1)); Assert.That(record.NullableDoubleField, Is.EqualTo(-4.0)); Assert.That(record.ParsedField, Is.EqualTo(1)); Assert.That(record.IgnoredField, Is.EqualTo(default(object))); Assert.That(record.StringProperty, Is.EqualTo("world")); Assert.That(record.IntProperty, Is.EqualTo(-1)); Assert.That(record.NullableDoubleProperty, Is.EqualTo(3.0)); Assert.That(record.ParsedProperty, Is.EqualTo(2)); Assert.That(record.IgnoredProperty, Is.EqualTo(default(int))); record = records[1]; Assert.That(record, Is.Not.Null); Assert.That(record.StringField, Is.EqualTo("good")); Assert.That(record.IntField, Is.EqualTo(2)); Assert.That(record.NullableDoubleField, Is.Null); Assert.That(record.ParsedField, Is.Null); Assert.That(record.IgnoredField, Is.EqualTo(default(object))); Assert.That(record.StringProperty, Is.EqualTo("bye")); Assert.That(record.IntProperty, Is.EqualTo(0)); Assert.That(record.NullableDoubleProperty, Is.Null); Assert.That(record.ParsedProperty, Is.Null); Assert.That(record.IgnoredProperty, Is.EqualTo(default(int))); } }
[ExpectedException(typeof(InvalidOperationException))] // Single() throws InvalidOperationException with multiple elements. public void TestRecordWithMultipleRecordConstructor([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { var csv = string.Format("1,2,3{0}4,5,6{0}", eol.AsNewline()); var settings = new CsvReaderSettings() { RecordDelimiter = eol, }; using (var stringReader = new StringReader(csv)) using (var reader = new CsvReader(stringReader, settings)) { Assert.That(reader.MoveNext(), Is.True); Assert.That(reader.Current.Convert <RecordWithMultipleRecordConstructor>(), Throws.Exception); } }
private CsvReader(TextReader reader, CsvReaderSettings settings, bool disposed) { if (reader == null) { throw new ArgumentNullException("reader"); } settings = settings ?? new CsvReaderSettings(); this.reader = reader is StackBufferedTextReader ? (StackBufferedTextReader)reader : new StackBufferedTextReader(reader); this.delimiter = settings.RecordDelimiter.AsNewline(); this.delimiterFirst = this.delimiter[0]; this.separator = settings.FieldDelimiter; this.separatorFirst = this.separator[0]; this.quotation = settings.QuotationCharacter; this.buffer = new char[Math.Max(this.delimiter.Length, this.separator.Length)]; this.disposed = disposed; }
public void TestMultipleColumn([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { var csv = string.Format("1,二{0}", eol.AsNewline()); var settings = new CsvReaderSettings() { RecordDelimiter = eol, }; using (var stringReader = new StringReader(csv)) using (var reader = new CsvReader(stringReader, settings)) { Assert.That(reader.MoveNext(), Is.True); var record = reader.Current.Convert<MultipleColumnRecord>(); Assert.That(record, Is.Not.Null); Assert.That(record.Value, Is.EqualTo(1).Or.EqualTo(2)); Assert.That(reader.MoveNext(), Is.False); } }
public void TestMultipleColumn([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { var csv = string.Format("1,二{0}", eol.AsNewline()); var settings = new CsvReaderSettings() { RecordDelimiter = eol, }; using (var stringReader = new StringReader(csv)) using (var reader = new CsvReader(stringReader, settings)) { Assert.That(reader.MoveNext(), Is.True); var record = reader.Current.Convert <MultipleColumnRecord>(); Assert.That(record, Is.Not.Null); Assert.That(record.Value, Is.EqualTo(1).Or.EqualTo(2)); Assert.That(reader.MoveNext(), Is.False); } }
public void TestReadIndexerRecord([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { var csv = string.Format("1,二,3{0}", eol.AsNewline()); var settings = new CsvReaderSettings() { RecordDelimiter = eol, }; using (var stringReader = new StringReader(csv)) using (var reader = new CsvReader(stringReader, settings)) { Assert.That(reader.MoveNext(), Is.True); var record = reader.Current.Convert <IndexerRecord>(); Assert.That(record, Is.Not.Null); Assert.That(record["A"], Is.EqualTo(1)); Assert.That(record["B"], Is.EqualTo(2)); Assert.That(record["C"], Is.EqualTo(3)); Assert.That(reader.MoveNext(), Is.False); } }
public void TestReadToEndGeneric([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { var headerLine = "string1,int1,double1,parsed1,ignored1,string2,int2,double2,parsed2,ignored2"; var csv = string.Format( "{0}{1}" + "hello,1,-4.0,一,_,world,-1,3.0,二,_{1}" + "good,2,NA,NA,_,bye,0,NA,NA,_{1}", headerLine, eol.AsNewline() ); var settings = new CsvReaderSettings() { RecordDelimiter = eol, }; using (var stringReader = new StringReader(csv)) using (var reader = new CsvReader(stringReader, settings)) { Assert.That(reader.MoveNext(), Is.True); var header = reader.Current.ToArray(); reader.SetHeader(header); Assert.That(header, Is.Not.Null); Assert.That(header, Is.EquivalentTo(headerLine.Split(','))); var records = reader.ReadToEnd<PartialRecord>().ToList(); Assert.That(records.Count, Is.EqualTo(2)); Assert.That(reader.MoveNext(), Is.False); var record = records[0]; Assert.That(record, Is.Not.Null); Assert.That(record.StringField, Is.EqualTo("hello")); Assert.That(record.IntField, Is.EqualTo(1)); Assert.That(record.NullableDoubleField, Is.EqualTo(-4.0)); Assert.That(record.ParsedField, Is.EqualTo(1)); Assert.That(record.IgnoredField, Is.EqualTo(default(object))); Assert.That(record.StringProperty, Is.EqualTo("world")); Assert.That(record.IntProperty, Is.EqualTo(-1)); Assert.That(record.NullableDoubleProperty, Is.EqualTo(3.0)); Assert.That(record.ParsedProperty, Is.EqualTo(2)); Assert.That(record.IgnoredProperty, Is.EqualTo(default(int))); record = records[1]; Assert.That(record, Is.Not.Null); Assert.That(record.StringField, Is.EqualTo("good")); Assert.That(record.IntField, Is.EqualTo(2)); Assert.That(record.NullableDoubleField, Is.Null); Assert.That(record.ParsedField, Is.Null); Assert.That(record.IgnoredField, Is.EqualTo(default(object))); Assert.That(record.StringProperty, Is.EqualTo("bye")); Assert.That(record.IntProperty, Is.EqualTo(0)); Assert.That(record.NullableDoubleProperty, Is.Null); Assert.That(record.ParsedProperty, Is.Null); Assert.That(record.IgnoredProperty, Is.EqualTo(default(int))); } }
public void TestReadOmittableRecord([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { var csv = string.Format("1,2,3{0}4,5{0}6{0}", eol.AsNewline()); var settings = new CsvReaderSettings() { RecordDelimiter = eol, }; using (var stringReader = new StringReader(csv)) using (var reader = new CsvReader(stringReader, settings)) { Assert.That(reader.MoveNext(), Is.True); var record = reader.Current.Convert<OmittableRecord>(); Assert.That(record, Is.Not.Null); Assert.That(record.Column1, Is.EqualTo(1)); Assert.That(record.Column2, Is.EqualTo(2)); Assert.That(record.Column3, Is.EqualTo(3)); Assert.That(reader.MoveNext(), Is.True); record = reader.Current.Convert<OmittableRecord>(); Assert.That(record, Is.Not.Null); Assert.That(record.Column1, Is.EqualTo(4)); Assert.That(record.Column2, Is.EqualTo(5)); Assert.That(record.Column3, Is.EqualTo(default(int))); Assert.That(reader.MoveNext(), Is.True); record = reader.Current.Convert<OmittableRecord>(); Assert.That(record, Is.Not.Null); Assert.That(record.Column1, Is.EqualTo(6)); Assert.That(record.Column2, Is.EqualTo(default(int))); Assert.That(record.Column3, Is.EqualTo(default(int))); Assert.That(reader.MoveNext(), Is.False); } }
public void TestRecordWithParameteredConstructor([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { var csv = string.Format("1,2,3,4,5{0}6,7,8{0}", eol.AsNewline()); var settings = new CsvReaderSettings() { RecordDelimiter = eol, }; using (var stringReader = new StringReader(csv)) using (var reader = new CsvReader(stringReader, settings)) { Assert.That(reader.MoveNext(), Is.True); var record = reader.Current.Convert<RecordWithParameteredConstructor>(); Assert.That(record, Is.Not.Null); Assert.That(record.Column1, Is.EqualTo(1)); Assert.That(record.Column2, Is.EqualTo(2)); Assert.That(record.Column3, Is.EqualTo(3)); //Assert.That(record.Column4, Is.EqualTo(4)); Assert.That(record.Column5, Is.EqualTo(5)); Assert.That(reader.MoveNext(), Is.True); record = reader.Current.Convert<RecordWithParameteredConstructor>(); Assert.That(record, Is.Not.Null); Assert.That(record.Column1, Is.EqualTo(6)); Assert.That(record.Column2, Is.EqualTo(7)); Assert.That(record.Column3, Is.EqualTo(8)); //Assert.That(record.Column4, Is.EqualTo(default(int))); Assert.That(record.Column5, Is.EqualTo(-1)); Assert.That(reader.MoveNext(), Is.False); } }
/// <summary> /// Initializes a new <see cref="CsvReader"/> with the specified path to reading file. /// </summary> /// <param name="reader">The reader.</param> /// <param name="settings">The settings.</param> public CsvReader(TextReader reader, CsvReaderSettings settings = null) : this(reader, settings, true) { }
/// <summary> /// Initializes a new <see cref="CsvReader"/> with the specified file stream. /// </summary> /// <param name="stream">The file stream.</param> /// <param name="settings">The settings.</param> public CsvReader(Stream stream, CsvReaderSettings settings = null) : this(new StreamReader(stream), settings, false) { }
/// <summary> /// Initializes a new <see cref="CsvReader"/> with the specified path to reading file. /// </summary> /// <param name="path">The path to file to read.</param> /// <param name="settings">The settings.</param> public CsvReader(string path, CsvReaderSettings settings = null) : this(new StreamReader(path), settings, false) { }
/// <summary> /// Initializes a new <see cref="CsvReader"/> with the specified file stream. /// </summary> /// <param name="stream">The file stream.</param> /// <param name="encoding">The encoding.</param> /// <param name="settings">The settings.</param> public CsvReader(Stream stream, Encoding encoding, CsvReaderSettings settings = null) : this(new StreamReader(stream, encoding), settings, false) { }
public void TestRecordWithMultipleRecordConstructor([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { var csv = string.Format("1,2,3{0}4,5,6{0}", eol.AsNewline()); var settings = new CsvReaderSettings() { RecordDelimiter = eol, }; using (var stringReader = new StringReader(csv)) using (var reader = new CsvReader(stringReader, settings)) { Assert.That(reader.MoveNext(), Is.True); Assert.That(reader.Current.Convert<RecordWithMultipleRecordConstructor>(), Throws.Exception); } }
public void TestReadIndexerRecord([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { var csv = string.Format("1,二,3{0}", eol.AsNewline()); var settings = new CsvReaderSettings() { RecordDelimiter = eol, }; using (var stringReader = new StringReader(csv)) using (var reader = new CsvReader(stringReader, settings)) { Assert.That(reader.MoveNext(), Is.True); var record = reader.Current.Convert<IndexerRecord>(); Assert.That(record, Is.Not.Null); Assert.That(record["A"], Is.EqualTo(1)); Assert.That(record["B"], Is.EqualTo(2)); Assert.That(record["C"], Is.EqualTo(3)); Assert.That(reader.MoveNext(), Is.False); } }