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 TsvStreamReader(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\t1B"); writer.Flush(); stream.Position = 0; using (var reader = new TsvStreamReader(stream, headers)) { var actual = reader.ReadEntry(); Assert.Equal("A\tB", reader.Header); Assert.Equal(1, reader.LineNumber); Assert.Equal(1, reader.EntryNumber); Assert.Equal("1A", actual["A"]); Assert.Equal("1B", actual["B"]); } } } }
public void op_ReadEntry_whenUnixLF() { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.WriteLine("A\tB\n1A\t1B"); writer.Flush(); stream.Position = 0; using (var reader = new TsvStreamReader(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\tB\tC"); writer.WriteLine("1\t2"); writer.Flush(); stream.Position = 0; using (var reader = new TsvStreamReader(stream)) { // ReSharper disable AccessToDisposedClosure Assert.Throws <FormatException>(() => reader.ReadEntry()); // ReSharper restore AccessToDisposedClosure } } } }
public void op_ReadEntry_whenTrailingNewLine() { using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.WriteLine("A\tB"); writer.WriteLine("1A\t1B"); writer.WriteLine(string.Empty); writer.Flush(); stream.Position = 0; using (var reader = new TsvStreamReader(stream)) { reader.ReadEntry(); var actual = reader.ReadEntry(); Assert.Equal(3, reader.LineNumber); Assert.Equal(1, reader.EntryNumber); Assert.Null(actual); } } } }
public void op_ReadEntry_whenHeaderCtor() { const string header = "A\tB"; using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream)) { writer.WriteLine("1A\t1B"); writer.Flush(); stream.Position = 0; using (var reader = new TsvStreamReader(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"]); } } } }