protected IEnumerator <T> GetEnumerator <T>() where T : KeyStringDictionary, new() { Info.Refresh(); if (!Info.Exists) { yield break; } using (var stream = Info.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (var reader = new CsvStreamReader(stream)) { while (!reader.EndOfStream) { var entry = reader.ReadEntry <T>(); if (null == entry) { break; } yield return((T)entry); } } } }
public void op_ReadEntry_whenColumnsCtor() { var headers = new[] { "A", "B" }; using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.WriteLine("1A,1B"); writer.Flush(); stream.Position = 0; using (var reader = new CsvStreamReader(stream, headers)) { var actual = reader.ReadEntry(); Assert.Equal("A,B", reader.Header); Assert.Equal(1, reader.LineNumber); Assert.Equal(1, reader.EntryNumber); Assert.Equal("1A", actual["A"]); Assert.Equal("1B", actual["B"]); } } } }
private static IEnumerable <string> ParseHeader(string header) { if (null == header) { throw new ArgumentNullException("header"); } if (0 == header.Length) { throw new ArgumentOutOfRangeException("header"); } using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.WriteLine(header); writer.Flush(); stream.Position = 0; using (var reader = new CsvStreamReader(stream)) { reader.ReadEntry(); return(reader.Columns); } } } }
public void op_ReadEntry_whenUnixLF() { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.WriteLine("A,B\n1A,1B"); writer.Flush(); stream.Position = 0; using (var reader = new CsvStreamReader(stream)) { var actual = reader.ReadEntry(); Assert.Equal(2, reader.LineNumber); Assert.Equal(1, reader.EntryNumber); Assert.Equal("1A", actual["A"]); Assert.Equal("1B", actual["B"]); } } } }
public void op_ReadEntry_whenMissingColumnItem() { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.WriteLine("A,B,C"); writer.WriteLine("1,2"); writer.Flush(); stream.Position = 0; using (var reader = new CsvStreamReader(stream)) { // ReSharper disable AccessToDisposedClosure Assert.Throws <FormatException>(() => reader.ReadEntry()); // ReSharper restore AccessToDisposedClosure } } } }
public void op_ReadEntry_whenEmbeddedLeadingAndTrailingSpaces() { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.WriteLine(" A , B "); writer.WriteLine("\" 1A \", 1B "); writer.Flush(); stream.Position = 0; using (var reader = new CsvStreamReader(stream)) { var actual = reader.ReadEntry(); Assert.Equal(2, reader.LineNumber); Assert.Equal(1, reader.EntryNumber); Assert.Equal(" 1A ", actual["A"]); Assert.Equal("1B", actual["B"]); } } } }
public void op_ReadEntry_whenTrailingNewLine() { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.WriteLine("A,B"); writer.WriteLine("1A,1B"); writer.WriteLine(string.Empty); writer.Flush(); stream.Position = 0; using (var reader = new CsvStreamReader(stream)) { reader.ReadEntry(); var actual = reader.ReadEntry(); Assert.Equal(3, reader.LineNumber); Assert.Equal(1, reader.EntryNumber); Assert.Null(actual); } } } }
public void op_ReadEntry_whenEmbeddedQuotation() { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.WriteLine("A,B,C"); writer.WriteLine("\"\"\"1A\",\"1\"\"B\",\"1C\"\"\""); writer.Flush(); stream.Position = 0; using (var reader = new CsvStreamReader(stream)) { var actual = reader.ReadEntry(); Assert.Equal(2, reader.LineNumber); Assert.Equal(1, reader.EntryNumber); Assert.Equal("\"1A", actual["A"]); Assert.Equal("1\"B", actual["B"]); Assert.Equal("1C\"", actual["C"]); } } } }
public void op_ReadEntry_whenLineBreak() { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.WriteLine("A,B,C"); writer.WriteLine("1A,\"Line1"); writer.WriteLine("Line2\nLine3\",1C"); writer.Flush(); stream.Position = 0; using (var reader = new CsvStreamReader(stream)) { var actual = reader.ReadEntry(); Assert.Equal(4, reader.LineNumber); Assert.Equal(1, reader.EntryNumber); Assert.Equal("1A", actual["A"]); Assert.Equal("Line1" + Environment.NewLine + "Line2" + Environment.NewLine + "Line3", actual["B"]); Assert.Equal("1C", actual["C"]); } } } }
public void op_ReadEntry_whenHeaderCtor() { const string header = "A,B"; using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.WriteLine("1A,1B"); writer.Flush(); stream.Position = 0; using (var reader = new CsvStreamReader(stream, header)) { Assert.Equal(header, reader.Header); var actual = reader.ReadEntry(); Assert.Equal(1, reader.LineNumber); Assert.Equal(1, reader.EntryNumber); Assert.Equal("1A", actual["A"]); Assert.Equal("1B", actual["B"]); } } } }