public void DefaultCommentPrefixIsPound() { StringReader documentReader = new StringReader(""); CsvReader reader = new CsvReader(documentReader); Assert.AreEqual('#', reader.CommentPrefix); }
public void DefaultFieldDelimiterIsComma() { StringReader document = new StringReader(""); CsvReader reader = new CsvReader(document); Assert.AreEqual(',', reader.FieldDelimiter); }
public void ReadRecords(string document, char fieldDelimiter, char commentPrefix, object[] expectedRecords) { StringReader documentReader = new StringReader(document); CsvReader reader = new CsvReader(documentReader); reader.FieldDelimiter = fieldDelimiter; Assert.AreEqual(fieldDelimiter, reader.FieldDelimiter); reader.CommentPrefix = commentPrefix; Assert.AreEqual(commentPrefix, reader.CommentPrefix); List<string[]> actualRecords = new List<string[]>(); string[] record; while ((record = reader.ReadRecord()) != null) actualRecords.Add(record); Assert.Over.Pairs(expectedRecords, actualRecords, (expectedRecord, actualRecord) => Assert.AreEqual((string[])expectedRecord, actualRecord)); }
/// <inheritdoc /> protected override IEnumerable<IDataItem> GetItemsImpl(ICollection<DataBinding> bindings, bool includeDynamicItems) { if (!isDynamic || includeDynamicItems) { using (var reader = new CsvReader(documentReaderProvider())) { reader.FieldDelimiter = fieldDelimiter; reader.CommentPrefix = commentPrefix; string[] header = null; Dictionary<string, int> metadataColumns = null; if (hasHeader) { header = reader.ReadRecord(); if (header == null) yield break; metadataColumns = new Dictionary<string, int>(); for (int i = 0; i < header.Length; i++) { string columnName = header[i]; if (columnName.StartsWith("[") && columnName.EndsWith("]")) metadataColumns[columnName.Substring(1, columnName.Length - 2)] = i; } } for (; ; ) { string[] record = reader.ReadRecord(); if (record == null) yield break; CodeLocation dataLocation = dataLocationName != null ? new CodeLocation(dataLocationName, reader.PreviousRecordLineNumber, 0) : CodeLocation.Unknown; yield return new Item(record, header, GetMetadata(dataLocation, record, metadataColumns), isDynamic); } } } }
public void UpdatesTheLineNumber() { StringReader documentReader = new StringReader("abc\n#123\ndef"); CsvReader reader = new CsvReader(documentReader); Assert.AreEqual(0, reader.PreviousRecordLineNumber); Assert.AreEqual(new string[] { "abc" }, reader.ReadRecord()); Assert.AreEqual(1, reader.PreviousRecordLineNumber); Assert.AreEqual(new string[] { "def" }, reader.ReadRecord()); Assert.AreEqual(3, reader.PreviousRecordLineNumber); Assert.IsNull(reader.ReadRecord()); Assert.AreEqual(3, reader.PreviousRecordLineNumber); }
public void DisposeClosesTheTextReader() { TextReader mockTextReader = Mocks.StrictMock<TextReader>(); mockTextReader.Close(); Mocks.ReplayAll(); IDisposable reader = new CsvReader(mockTextReader); reader.Dispose(); }