protected async Task RunTestAsync(Func <CsvAsyncInputBase> sutCreator, string filePath, bool ignoreUTF8ByteOrderMark) { (byte[] fileData, int originalLength) = GetExpectedCsvData(filePath, ignoreUTF8ByteOrderMark); var expected = TokenizeCsvFileUsingCursively(fileData, fileData.Length, (byte)','); // run without progress { var sut = sutCreator(); var inputVisitor = new StringBufferingVisitor(); await sut.ProcessAsync(inputVisitor).ConfigureAwait(false); Assert.Equal(expected, inputVisitor.Records); await Assert.ThrowsAsync <InvalidOperationException>(() => sut.ProcessAsync(null).AsTask()); } // run with progress { var sut = sutCreator(); var inputVisitor = new StringBufferingVisitor(); var readSoFar = new List <int>(); var progress = new ImmediateProgress <int>(readSoFar.Add); await sut.ProcessAsync(inputVisitor, progress).ConfigureAwait(false); Assert.Equal(expected, inputVisitor.Records); Assert.Equal(originalLength, readSoFar.Sum()); Assert.Equal(0, readSoFar.Last()); await Assert.ThrowsAsync <InvalidOperationException>(() => sut.ProcessAsync(null).AsTask()); } }
public static List <string[]> TokenizeCsvFileUsingCursively(ReadOnlySpan <byte> fileData, int chunkLength, byte delimiter) { var tokenizer = new CsvTokenizer(delimiter); var visitor = new StringBufferingVisitor(); while (fileData.Length > chunkLength) { tokenizer.ProcessNextChunk(fileData.Slice(0, chunkLength), visitor); fileData = fileData.Slice(chunkLength); } tokenizer.ProcessNextChunk(fileData, visitor); tokenizer.ProcessEndOfStream(visitor); return(visitor.Records); }
public void MemoryMappedCsvShouldMatchCsvHelper(string filePath) { // arrange filePath = Path.Combine(TestCsvFilesFolderPath, filePath); var visitor = new StringBufferingVisitor(); // act CsvSyncInput.ForMemoryMappedFile(filePath).WithIgnoreUTF8ByteOrderMark(false).Process(visitor); var actual = visitor.Records; // assert var expected = TokenizeCsvFileUsingCsvHelper(File.ReadAllBytes(filePath), ","); Assert.Equal(expected, actual); }
protected void RunTest(CsvSyncInputBase sut, string filePath, bool ignoreUTF8ByteOrderMark) { // arrange (byte[] fileData, _) = GetExpectedCsvData(filePath, ignoreUTF8ByteOrderMark); var expected = TokenizeCsvFileUsingCursively(fileData, fileData.Length, (byte)','); var inputVisitor = new StringBufferingVisitor(); // act sut.Process(inputVisitor); // assert Assert.Equal(expected, inputVisitor.Records); Assert.Throws <InvalidOperationException>(() => sut.Process(null)); }