CSVReader class is responsible for reading and parsing tabular data. Parsing is controlled by set of rules defined in Dialect. API exposes the following operations: Next() : reads and parses next record (returns true on success) Current : return current record as array of strings Headers : return headers as array of strings
Inheritance: ICSVReader
Beispiel #1
0
 public void Next_EmptyLine_NextReturnsFalse()
 {
     string input = "Header#1;Header#2;Header#3\r\n1;2;3\r\n \r\n";
     using (var dialect = new Dialect(true, ';', '"', '\0', false, "\r\n", QuoteStyle.QuoteMinimal, false, true))
     {
         using (var reader = new CSVReader(dialect, new StringReader(input)))
         {
             Assert.IsTrue(reader.Next());
             Assert.IsFalse(reader.Next());
         }
     }
 }
Beispiel #2
0
        public void Reader_DialectInternalError_ThrowException()
        {
            IList<IList<object>> results = new List<IList<object>>();

            using (Dialect dialect = new Dialect(true, '\0', '"', '\\', true, "\r\n", QuoteStyle.QUOTE_MINIMAL, true, false))
            {
                using (CSVReader reader = new CSVReader(dialect, new StringReader("1,2,3")))
                {
                    while (reader.NextRecord())
                    {
                        string[] record = reader.GetCurrentRecord();
                        if (record != null && record.Length > 0)
                            results.Add(record);
                        record = null;
                    }
                }
            }
        }
Beispiel #3
0
        public static void ReadTest(string input, IList<IList<object>> expect, Dialect dialect)
        {
            IList<IList<object>> results = new List<IList<object>>();
            using (var reader = new CSVReader(dialect, new StringReader(input)))
            {
                while (reader.Next())
                {
                    var record = reader.Current;
                    if (record != null && record.Length > 0)
                    {
                        results.Add (record);
                    }
                    record = null;
                }
            }

            Assert.AreEqual(expect, results);

            DisposeIListOfIListOfObjects(results);
            results = null;

            DisposeIListOfIListOfObjects(expect);
            expect = null;
        }
Beispiel #4
0
        public void Next_FirstLine_HeadersArePopulated()
        {
            using (var dialect = new Dialect(false, ',', '"', '\0', false, "\r\n", QuoteStyle.QuoteMinimal, true, true))
            {
                using (var reader = new CSVReader(dialect, new StringReader("a,b,c")))
                {
                    reader.Next();
                    var current = reader.Current;

                    Assert.IsNotNull(reader.Headers);
                    Assert.AreEqual(new[] { "a", "b", "c" }, reader.Headers);
                }
            }
        }
Beispiel #5
0
 public void Reader_NullStream_ThrowsException()
 {
     using (var dialect = new Dialect())
     {
         using (var reader = new CSVReader(dialect, null))
         {
         }
     }
 }
Beispiel #6
0
 public void Reader_NullDialect_ThrowsException()
 {
     using (var reader = new CSVReader(null, new StringReader("1,2,3")))
     {
     }
 }
Beispiel #7
0
 public void Reader_FileNameIsNullOrEmpty_ThrowsException(string fileName)
 {
     using (var dialect = new Dialect(true, ';', '\"', '\\', true, "\r\n", QuoteStyle.QuoteMinimal, false, false))
     {
         using (var csvReader = new CSVReader(dialect, fileName, "UTF-8"))
         {
         }
     }
 }
Beispiel #8
0
 public void Reader_FileIsLocked__ThrowsException()
 {
     using (var writer = new StreamWriter("test_write_file_locked.csv", false, Encoding.GetEncoding("utf-8")))
     {
         using (var dialect = new Dialect(true, ';', '\"', '\\', true, "\r\n", QuoteStyle.QuoteMinimal, false, false))
         {
             using (var csvReader = new CSVReader(dialect, "test_write_file_locked.csv", "UTF-8"))
             {
             }
         }
     }
 }
Beispiel #9
0
 public void Reader_DialectIsNull_ThrowsException()
 {
     using (var reader = new CSVReader(null, "VeryLongNonExistingFileNameForTesting", "UTF-8"))
     {
     }
 }
Beispiel #10
0
 public void Reader_CannotReadFromFile_ThrowsException()
 {
     using (var dialect = new Dialect())
     {
         using (var reader = new CSVReader(dialect, "VeryLongNonExistingFileNameForTesting", "utf-8"))
         {
         }
     }
 }
Beispiel #11
0
 public void Next_WithHeaders_HeadersAreInitialized()
 {
     string input = "Header#1;Header#2;Header#3\r\n1;2;3\r\n4;5;6\r\ntest1;234;test2";
     using (var dialect = new Dialect(true, ';', '"', '\0', false, "\r\n", QuoteStyle.QuoteMinimal, false, true))
     {
         using (var reader = new CSVReader(dialect, new StringReader(input)))
         {
             Assert.IsNotNull(reader.Headers);
         }
     }
 }
Beispiel #12
0
        public void Reader_NullStream_ThrowException()
        {
            IList<IList<object>> results = new List<IList<object>>();

            using (Dialect dialect = new Dialect())
            {
                using (CSVReader reader = new CSVReader(dialect, null))
                {
                    while (reader.NextRecord())
                    {
                        string[] record = reader.GetCurrentRecord();
                        if (record != null && record.Length > 0)
                            results.Add(record);
                        record = null;
                    }
                }
            }
        }
Beispiel #13
0
        public void Reader_NullDialect_ThrowException()
        {
            IList<IList<object>> results = new List<IList<object>>();

            using (CSVReader reader = new CSVReader(null, new StringReader("1,2,3")))
            {
                while (reader.NextRecord())
                {
                    string[] record = reader.GetCurrentRecord();
                    if (record != null && record.Length > 0)
                        results.Add(record);
                    record = null;
                }
            }
        }
Beispiel #14
0
        public void Reader_NotExistingFile_ThrowException()
        {
            IList<IList<object>> results = new List<IList<object>>();

            using (Dialect dialect = new Dialect())
            {
                using (CSVReader reader = new CSVReader(dialect, "__no_file.txt", "utf-8"))
                {
                    while (reader.NextRecord())
                    {
                        string[] record = reader.GetCurrentRecord();
                        if (record != null && record.Length > 0)
                            results.Add(record);
                        record = null;
                    }
                }
            }
        }