public void ImprovedTextReaderPositionStoreTestFromMiddle() { using (var impStream = new ImprovedStream(new SourceAccess(UnitTestInitializeCsv.GetTestPath("txTranscripts.txt"), true))) { using (var test = new ImprovedTextReader(impStream, 65001, 1)) { test.ToBeginning(); Assert.AreEqual(2, test.LineNumber); Assert.AreEqual( @"GCS_004805_Osipova 023c25d3-3420-449c-a75b-0d74d29ddc38 Completed 04/02/2008 00:00:00 04/02/2008 00:00:00 04/02/2008 00:00:00", test.ReadLine()); var lastLine1 = string.Empty; string lastLine2; for (var i = 0; i < 5; i++) { lastLine1 = test.ReadLine(); } var store = new ImprovedTextReaderPositionStore(test); var readLine = false; while (!store.AllRead()) { lastLine2 = test.ReadLine(); // since there are buffers its we will not end up with the excact same line, but we need // to at least have read the line. if (lastLine2 == lastLine1) { readLine = true; } } Assert.IsTrue(readLine); } } }
public void ImprovedTextReaderTestGz() { using (var impStream = new ImprovedStream(new SourceAccess(UnitTestInitializeCsv.GetTestPath("BasicCsV.txt.gz")))) { using (var test = new ImprovedTextReader(impStream, 12000)) { test.ToBeginning(); Assert.AreEqual(1, test.LineNumber); Assert.AreEqual("ID,LangCodeID,ExamDate,Score,Proficiency,IsNativeLang", test.ReadLine()); Assert.AreEqual("1,German,20/01/2010,276,0.94,Y", test.ReadLine()); } } }
public void GuessHasHeader() { using (var stream = new ImprovedStream(new SourceAccess(UnitTestInitializeCsv.GetTestPath("BasicCSV.txt")))) using (var reader = new ImprovedTextReader(stream)) { reader.ToBeginning(); Assert.IsTrue(reader.GuessHasHeader("#", ",", UnitTestInitializeCsv.Token).Item1); } using (var stream = new ImprovedStream(new SourceAccess(UnitTestInitializeCsv.GetTestPath("HandlingDuplicateColumnNames.txt")))) using (var reader = new ImprovedTextReader(stream)) { reader.ToBeginning(); Assert.IsFalse(reader.GuessHasHeader("#", ",", UnitTestInitializeCsv.Token).Item1); } }
public void ImprovedTextReaderPositionStoreTest() { using (var impStream = new ImprovedStream(new SourceAccess(UnitTestInitializeCsv.GetTestPath("txTranscripts.txt"), true))) { using (var test = new ImprovedTextReader(impStream)) { test.ToBeginning(); var store = new ImprovedTextReaderPositionStore(test); Assert.AreEqual(1, test.LineNumber); Assert.AreEqual( "#UserID CurriculumID TranscriptStatus RequestDateTime RegistrationDateTime CompletionDateTime", test.ReadLine()); var lastLine = string.Empty; while (!store.AllRead()) { lastLine = test.ReadLine(); } Assert.AreEqual( @"GCS_002846_Benavides A23c25d3-3420-449c-a75b-0d74d29ddc38 Completed 13/03/2008 00:00:00 13/03/2008 00:00:00 13/03/2008 00:00:00", lastLine); } } }
public void ToBeginningTest() { // use a file with a BOM using (var impStream = new ImprovedStream(new SourceAccess(UnitTestInitializeCsv.GetTestPath("txTranscripts.txt")))) { using (var test = new ImprovedTextReader(impStream)) { test.ToBeginning(); Assert.AreEqual(1, test.LineNumber); Assert.AreEqual( "#UserID CurriculumID TranscriptStatus RequestDateTime RegistrationDateTime CompletionDateTime", test.ReadLine()); for (var i = 0; i < 200; i++) { _ = test.ReadLine(); } test.ToBeginning(); Assert.AreEqual(1, test.LineNumber); Assert.AreEqual( "#UserID CurriculumID TranscriptStatus RequestDateTime RegistrationDateTime CompletionDateTime", test.ReadLine()); for (var i = 0; i < 300; i++) { test.ReadLine(); } test.ToBeginning(); Assert.AreEqual(1, test.LineNumber); Assert.AreEqual( "#UserID CurriculumID TranscriptStatus RequestDateTime RegistrationDateTime CompletionDateTime", test.ReadLine()); } } }
public async Task BOMTest() { // create files var fn = new[] { new Tuple <string, int, byte[]>("GB18030", 54936, new[] { (byte)0x84, (byte)0x31, (byte)0x95, (byte)0x33 }), new Tuple <string, int, byte[]>("UTF-7_2", 65000, new[] { (byte)0x2B, (byte)0x2F, (byte)0x76, (byte)0x39 }), new Tuple <string, int, byte[]>("UTF-7_3", 65000, new[] { (byte)0x2B, (byte)0x2F, (byte)0x76, (byte)0x2B }), new Tuple <string, int, byte[]>("UTF-7_4", 65000, new[] { (byte)0x2B, (byte)0x2F, (byte)0x76, (byte)0x2F }), new Tuple <string, int, byte[]>("UTF-16 (BE)", 1201, new[] { (byte)0xFE, (byte)0xFF }), new Tuple <string, int, byte[]>("UTF8", 65001, new[] { (byte)0xEF, (byte)0xBB, (byte)0xBF }), new Tuple <string, int, byte[]>("UTF-16 (LE)", 1200, new[] { (byte)0xFF, (byte)0xFE }), new Tuple <string, int, byte[]>("UTF-32 (BE)", 12001, new[] { (byte)0, (byte)0, (byte)0xFE, (byte)255 }), new Tuple <string, int, byte[]>("UTF-32 (LE)", 12000, new[] { (byte)0xFF, (byte)0xFE, (byte)0, (byte)0 }), new Tuple <string, int, byte[]>("UTF-7_1", 65000, new[] { (byte)0x2B, (byte)0x2F, (byte)0x76, (byte)0x38 }) }; //var Text = Encoding.ASCII.GetBytes("This is a test\\r\nLine2"); var line1 = "This is a test - First Line!"; var line2 = "Another line..."; foreach (var type in fn) { var fileName = UnitTestInitializeCsv.GetTestPath("Test_" + type.Item1 + ".txt"); using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { // write the BOM fs.Write(type.Item3, 0, type.Item3.Length); using (var fs2 = new StreamWriter(fs, Encoding.GetEncoding(type.Item2))) { await fs2.WriteLineAsync(line1); await fs2.WriteAsync(line2); } } using (var impStream = new ImprovedStream(new SourceAccess(fileName))) { using (var test = new ImprovedTextReader(impStream, type.Item2)) { test.ToBeginning(); Assert.AreEqual(1, test.LineNumber); Assert.AreEqual(line1, test.ReadLine(), $"Issue reading Line1 {type.Item1}"); Assert.AreEqual(2, test.LineNumber); Assert.AreEqual(line2, test.ReadLine(), $"Issue reading Line2 {type.Item1}"); test.ToBeginning(); Assert.AreEqual(1, test.LineNumber); Assert.AreEqual(line1, test.ReadLine(), $"Issue reading after reset {type.Item1}"); } } File.Delete(fileName); } }
/// <summary> /// This class will make sure we go back to the beginning when starting in the middle of a stream and we reach the end. /// </summary> public ImprovedTextReaderPositionStore(ImprovedTextReader improvedTextReader) { m_ImprovedTextReader = improvedTextReader; m_ArrivedAtEndOnce = false; m_LineStarted = improvedTextReader.LineNumber; }