public void ReadSingleDataRow()
        {
            RowData             rowData = new RowData();
            IFileReader         reader  = FileReaderTest.GetReader("1,2,3");
            DelimitedFileParser parser  = new DelimitedFileParser(",", "\r\n");

            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "1", "2", "3" });
        }
        public void ParseDelimitedRowsWithQualifiersTest()
        {
            IFileReader reader = FileReaderTest.GetReader("\"A,B,C\r\n1\",2,3\r\n1,2,3,\"4\"");
            FieldParser parser = FieldParser.BuildParserWithSingleDelimiterAndQualifier("\r\n", "\"");

            VerifySuccessfulFieldParsing(parser, reader, "A,B,C\r\n1,2,3");
            VerifyFieldParsing(parser, reader, "1,2,3,\"4\"");

            Assert.AreEqual(reader.IsEOF, true);
        }
        public void ParseSimpleRowsTest()
        {
            IFileReader reader = FileReaderTest.GetReader("A,B,C\r\n1,2,3");
            FieldParser parser = FieldParser.BuildParserWithSingleDelimiter("\r\n");

            VerifySuccessfulFieldParsing(parser, reader, "A,B,C");
            VerifyFieldParsing(parser, reader, "1,2,3");

            Assert.AreEqual(reader.IsEOF, true);
        }
        public void ReadColumnNamesTest()
        {
            RowData             rowData = new RowData();
            IFileReader         reader  = FileReaderTest.GetReader("A,B,C\r\n1,2,3");
            DelimitedFileParser parser  = new DelimitedFileParser(",", "\r\n");

            parser.ColumnNameInFirstRow = true;

            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "A", "B", "C" });
        }
        public void ParseSingleQualifiedColumn()
        {
            RowData     rowData   = new RowData();
            IFileReader reader    = FileReaderTest.GetReader("\"A,B\r\n\",C\r\n1,2,3");
            RowParser   rowParser = new RowParser(string.Empty, "\r\n", "\"");

            rowParser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, "\"A,B\r\n\",C\r\n", new string[] { "A,B\r\n,C" });
            rowParser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, "1,2,3", new string[] { "1,2,3" });
        }
        public void ParseSimpleRows()
        {
            RowData     rowData   = new RowData();
            IFileReader reader    = FileReaderTest.GetReader("A,B,C\r\n1,2,3");
            RowParser   rowParser = new RowParser(",", "\r\n", string.Empty);

            rowParser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, "A,B,C\r\n", new string[] { "A", "B", "C" });
            rowParser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, "1,2,3", new string[] { "1", "2", "3" });
        }
        public void ParseRowsWithUnevenFields()
        {
            RowData     rowData   = new RowData();
            IFileReader reader    = FileReaderTest.GetReader("A,B,C,D\r\n1,2,3");
            RowParser   rowParser = new RowParser(",", "\r\n", "\"");

            rowParser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "A", "B", "C", "D" });
            rowParser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "1", "2", "3" });
        }
        public void ParseQualifiedRowsWithError()
        {
            RowData     rowData   = new RowData();
            IFileReader reader    = FileReaderTest.GetReader("A,\"B\" \r\n1,2,3");
            RowParser   rowParser = new RowParser(",", "\r\n", "\"");

            rowParser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "A", "B " });
            rowParser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "1", "2", "3" });
        }
        private static void TestEmbeddedQualifiers(string textToParse, string[] expectedFields)
        {
            IFileReader reader = FileReaderTest.GetReader(textToParse);
            FieldParser parser = FieldParser.BuildParserWithTwoDelimitersAndQualifier(",", "\r\n", "\"");

            foreach (string fieldData in expectedFields)
            {
                VerifySuccessfulFieldParsing(parser, reader, fieldData);
            }

            Assert.AreEqual(reader.IsEOF, true);
        }
        public void ParseDelimitedFieldsMultiCharacterDelimiterQualifiersError()
        {
            IFileReader reader = FileReaderTest.GetReader("A,.,\"B,C,D\",.1,2,3,.,4,\r\na,.,b");
            FieldParser parser = FieldParser.BuildParserWithTwoDelimitersAndQualifier(",.,", "\r\n", "\"");

            VerifySuccessfulFieldParsing(parser, reader, "A");
            VerifySuccessfulFieldParsing(parser, reader, "B,C,D,.1,2,3");
            VerifySuccessfulLastRowFieldParsing(parser, reader, "4,");
            VerifySuccessfulFieldParsing(parser, reader, "a");
            VerifyFieldParsing(parser, reader, "b");

            Assert.AreEqual(reader.IsEOF, true);
        }
        public void ReadSingleColumnRows()
        {
            RowData             rowData = new RowData();
            IFileReader         reader  = FileReaderTest.GetReader("1,2,3");
            DelimitedFileParser parser  = new DelimitedFileParser(string.Empty, ",");

            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "1" });
            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "2" });
            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "3" });
        }
        public void ReadSingleColumnRows2()
        {
            RowData             rowData = new RowData();
            IFileReader         reader  = FileReaderTest.GetReader("ABC\r\nDEF\r\nGHI");
            DelimitedFileParser parser  = new DelimitedFileParser(string.Empty, "\r\n");

            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "ABC" });
            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "DEF" });
            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "GHI" });
        }
        public void ReadColumnNamesAfterHeaderTest()
        {
            RowData             rowData = new RowData();
            IFileReader         reader  = FileReaderTest.GetReader("\r\n\r\nHeader text\r\nA,B,C\r\n1,2,3");
            DelimitedFileParser parser  = new DelimitedFileParser(",", "\r\n");

            parser.ColumnNameInFirstRow = true;
            parser.HeaderRowDelimiter   = "\r\n";
            parser.HeaderRowsToSkip     = 3;

            parser.SkipHeaderRows(reader);
            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "A", "B", "C" });
        }
        public void ParseSimpleTextOneDelimiterTest()
        {
            IFileReader reader = FileReaderTest.GetReader("A,B,C,1,2,3");
            FieldParser parser = FieldParser.BuildParserWithSingleDelimiter(",");

            VerifySuccessfulFieldParsing(parser, reader, "A");
            VerifySuccessfulFieldParsing(parser, reader, "B");
            VerifySuccessfulFieldParsing(parser, reader, "C");
            VerifySuccessfulFieldParsing(parser, reader, "1");
            VerifySuccessfulFieldParsing(parser, reader, "2");
            VerifyFieldParsing(parser, reader, "3");

            Assert.AreEqual(reader.IsEOF, true);
        }
        public void ParseSimpleTextTwoDelimitersTest()
        {
            IFileReader reader = FileReaderTest.GetReader("A,B,C\r\n1,2,3");
            FieldParser parser = FieldParser.BuildParserWithTwoDelimiters(",", "\r\n");

            VerifySuccessfulFieldParsing(parser, reader, "A");
            VerifySuccessfulFieldParsing(parser, reader, "B");
            VerifySuccessfulLastRowFieldParsing(parser, reader, "C");
            VerifySuccessfulFieldParsing(parser, reader, "1");
            VerifySuccessfulFieldParsing(parser, reader, "2");
            VerifyFieldParsing(parser, reader, "3");

            Assert.AreEqual(reader.IsEOF, true);
        }
        public void DelimitedFieldsWithQualifiersErrorTest()
        {
            IFileReader reader = FileReaderTest.GetReader("\"A\",\"B\",\"C\" \r\n\"1\",\"2\" ,3");
            FieldParser parser = FieldParser.BuildParserWithTwoDelimitersAndQualifier(",", "\r\n", "\"");

            VerifySuccessfulFieldParsing(parser, reader, "A");
            VerifySuccessfulFieldParsing(parser, reader, "B");
            VerifySuccessfulFieldParsing(parser, reader, "C ");
            VerifySuccessfulFieldParsing(parser, reader, "1");
            VerifySuccessfulFieldParsing(parser, reader, "2 ");
            VerifyFieldParsing(parser, reader, "3");

            Assert.AreEqual(reader.IsEOF, true);
        }
        public void ParseDelimitedFieldsWithEscapedMultiCharQualifiersTest()
        {
            IFileReader reader = FileReaderTest.GetReader("A,:\"::\"B,:\"::\":C:\"::\":,D:\":,1,:\"2,3,:\":4:\":");
            FieldParser parser = FieldParser.BuildParserWithSingleDelimiterAndQualifier(",", ":\":");

            VerifySuccessfulFieldParsing(parser, reader, "A");
            VerifySuccessfulFieldParsing(parser, reader, ":\"B,:\":C:\":,D");
            VerifySuccessfulFieldParsing(parser, reader, "1");
            VerifySuccessfulFieldParsing(parser, reader, ":\"2");
            VerifySuccessfulFieldParsing(parser, reader, "3");
            VerifyFieldParsing(parser, reader, "4");

            Assert.AreEqual(reader.IsEOF, true);
        }
        public void ParseDelimitedFieldsWithEscapedQualifiersTest()
        {
            IFileReader reader = FileReaderTest.GetReader("A,\"B,\"\"C\"\",D\",1,2,3,\"4\"");
            FieldParser parser = FieldParser.BuildParserWithSingleDelimiterAndQualifier(",", "\"");

            VerifySuccessfulFieldParsing(parser, reader, "A");
            VerifySuccessfulFieldParsing(parser, reader, "B,\"C\",D");
            VerifySuccessfulFieldParsing(parser, reader, "1");
            VerifySuccessfulFieldParsing(parser, reader, "2");
            VerifySuccessfulFieldParsing(parser, reader, "3");
            VerifyFieldParsing(parser, reader, "4");

            Assert.AreEqual(reader.IsEOF, true);
        }
        public void TestSkippingTooManyHeaderRows()
        {
            RowData             rowData = new RowData();
            IFileReader         reader  = FileReaderTest.GetReader("\r\n\r\nHeader\r\nA,B,C,D\r\n1,1,1,1\r\n2,2,2,2\r\n3,3,3,3\r\n4,4,4,4\r\n");
            DelimitedFileParser parser  = new DelimitedFileParser(",", "\r\n");

            parser.HeaderRowDelimiter   = "\r\n";
            parser.HeaderRowsToSkip     = 12;
            parser.DataRowsToSkip       = 15;
            parser.ColumnNameInFirstRow = true;

            parser.SkipInitialRows(reader);

            Assert.AreEqual <bool>(true, reader.IsEOF);
        }
        public void ReadMultipleDataRows()
        {
            RowData             rowData = new RowData();
            IFileReader         reader  = FileReaderTest.GetReader("1,1,1,1\r\n2,2,2,2\r\n3,3,3,3\r\n4,4,4,4");
            DelimitedFileParser parser  = new DelimitedFileParser(",", "\r\n");

            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "1", "1", "1", "1" });
            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "2", "2", "2", "2" });
            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "3", "3", "3", "3" });
            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "4", "4", "4", "4" });
        }
        public void ReadSingleColumnRows3()
        {
            RowData             rowData = new RowData();
            IFileReader         reader  = FileReaderTest.GetReader("\"ABC\"\r\n\"DE\"F\r\n\"G\"HI");
            DelimitedFileParser parser  = new DelimitedFileParser(string.Empty, "\r\n");

            parser.TextQualifier = "\"";

            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "ABC" });
            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "DEF" });
            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "GHI" });
        }
        public void ReadMultipleDataRowsAfterHeaderWithMessyParsing()
        {
            RowData             rowData = new RowData();
            IFileReader         reader  = FileReaderTest.GetReader("\r\n\r\nHeader text\r\n A)::-)brr\"-:-)\r\n-\r\nasdfghj\"-\"\"-\":-)\"-\":-)\"-\"\"-\"\"-\":--\r");
            DelimitedFileParser parser  = new DelimitedFileParser(":-)", "-\r\n");

            parser.HeaderRowDelimiter = "\r\n";
            parser.HeaderRowsToSkip   = 3;
            parser.TextQualifier      = "\"-\"";

            parser.SkipHeaderRows(reader);
            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { " A):", "brr\"-", "\r\n" });
            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "asdfghj\"-\"\"-\"", ":-)\"-\":-" });
        }
        public void ParseDelimitedQualifiedFieldsTest()
        {
            IFileReader reader = FileReaderTest.GetReader("A,\"B,C\r\n1\",2,3\r\n1,2,3,\"4\"");
            FieldParser parser = FieldParser.BuildParserWithTwoDelimitersAndQualifier(",", "\r\n", "\"");

            VerifySuccessfulFieldParsing(parser, reader, "A");
            VerifySuccessfulFieldParsing(parser, reader, "B,C\r\n1");
            VerifySuccessfulFieldParsing(parser, reader, "2");
            VerifySuccessfulLastRowFieldParsing(parser, reader, "3");
            VerifySuccessfulFieldParsing(parser, reader, "1");
            VerifySuccessfulFieldParsing(parser, reader, "2");
            VerifySuccessfulFieldParsing(parser, reader, "3");
            VerifyFieldParsing(parser, reader, "4");

            Assert.AreEqual(reader.IsEOF, true);
        }
        public void TestSkippingRows()
        {
            RowData             rowData = new RowData();
            IFileReader         reader  = FileReaderTest.GetReader("\r\n\r\nHeader\r\nA,B,C,D\r\n1,1,1,1\r\n2,2,2,2\r\n3,3,3,3\r\n4,4,4,4\r\n");
            DelimitedFileParser parser  = new DelimitedFileParser(",", "\r\n");

            parser.HeaderRowDelimiter   = "\r\n";
            parser.HeaderRowsToSkip     = 3;
            parser.DataRowsToSkip       = 3;
            parser.ColumnNameInFirstRow = true;

            parser.SkipInitialRows(reader);
            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[] { "4", "4", "4", "4" });
            parser.ParseNextRow(reader, rowData);
            RowDataTest.VerifyParsedRow(rowData, new string[0]);

            Assert.AreEqual <bool>(true, reader.IsEOF);
        }