コード例 #1
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest2()
        {
            // ["Bob said, ""Hey!""",2, 3 ]
            const string data = "\"Bob said, \"\"Hey!\"\"\",2, 3 ";

            using (CsvRecordReader csv = new CsvRecordReader(new StringReader(data), false))
            {
                CsvRecord record;

                record = csv.Read();
                Assert.NotNull(record);
                Assert.AreEqual(@"Bob said, ""Hey!""", record[0]);
                Assert.AreEqual("2", record[1]);
                Assert.AreEqual("3", record[2]);

                Assert.IsNull(csv.Read());
            }

            using (CsvRecordReader csv = new CsvRecordReader(new StringReader(data), false, ',', '"', '"', '#', ValueTrimmingOptions.None))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual(@"Bob said, ""Hey!""", record[0]);
                Assert.AreEqual("2", record[1]);
                Assert.AreEqual(" 3 ", record[2]);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #2
0
        public void TestCsvRecordReaderRead()
        {
            string filename = ".unittestscsvrecorreader" + nameof(TestCsvRecordReaderRead);

            CreateCsvTempFile(filename);

            FileSource source = new FileSource(filename, Path.GetTempPath());

            CsvRecordReader reader = new CsvRecordReader(source);

            Assert.Throws <InvalidOperationException>(() => reader.Read(3));

            reader.Prepare();

            string[][] lineparts = (string[][])reader.Read(2);

            Assert.AreEqual(2, lineparts.Length);
            Assert.AreEqual(5, lineparts[0].Length);
            Assert.AreEqual(new[] { "5.1", "3.5", "1.4", "0.2", "Iris-setosa" }, lineparts[0]);

            lineparts = (string[][])reader.Read(3);

            Assert.AreEqual(1, lineparts.Length);
            Assert.AreEqual(5, lineparts[0].Length);

            reader.Dispose();

            DeleteTempFile(filename);
        }
コード例 #3
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
 public void ParsingTest42()
 {
     using (var csv = new CsvRecordReader(new StringReader(" "), false))
     {
         record = csv.Read(); Assert.NotNull(record);
         Assert.AreEqual(1, csv.FieldCount);
         Assert.AreEqual(string.Empty, record[0]);
         Assert.IsNull(csv.Read());
     }
 }
コード例 #4
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
 public void ParsingTest7()
 {
     using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader("\r\n1\r\n"), false))
     {
         record = csv.Read(); Assert.NotNull(record);
         Assert.AreEqual(',', csv.Delimiter);
         Assert.AreEqual(0, csv.CurrentRecordIndex);
         Assert.AreEqual(1, csv.FieldCount);
         Assert.AreEqual("1", record[0]);
         Assert.IsNull(csv.Read());
     }
 }
コード例 #5
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest33()
        {
            const string data = "\"double\\\\\\\\double backslash\"";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false, ',', '\"', '\\', '#', ValueTrimmingOptions.None))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("double\\\\double backslash", record[0]);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(1, csv.FieldCount);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #6
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest26()
        {
            const string data = "1\n";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("1", record[0]);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(1, csv.FieldCount);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #7
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest41()
        {
            using (CsvRecordReader csv = new CsvRecordReader(new StringReader("First record          ,Second record"), false, CsvReader.DefaultDelimiter, CsvReader.DefaultQuote, CsvReader.DefaultEscape, CsvReader.DefaultComment, ValueTrimmingOptions.UnquotedOnly, 16))
            {
                Assert.AreEqual(2, csv.FieldCount);

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("First record", record[0]);
                Assert.AreEqual("Second record", record[1]);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #8
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest38()
        {
            using (CsvRecordReader csv = new CsvRecordReader(new StringReader("abc,def,ghi\n"), false))
            {
                int fieldCount = csv.FieldCount;

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("abc", record[0]);
                Assert.AreEqual("def", record[1]);
                Assert.AreEqual("ghi", record[2]);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #9
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest29()
        {
            string data = new String('a', 75) + "," + new String('b', 75);

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual(new String('a', 75), record[0]);
                Assert.AreEqual(new String('b', 75), record[1]);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(2, csv.FieldCount);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #10
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest35()
        {
            using (CsvRecordReader csv = new CsvRecordReader(new StringReader("\t"), false, '\t'))
            {
                Assert.AreEqual(2, csv.FieldCount);

                record = csv.Read(); Assert.NotNull(record);

                Assert.AreEqual(string.Empty, record[0]);
                Assert.AreEqual(string.Empty, record[1]);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #11
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest16()
        {
            const string data = "1\r2\n";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false, '\r', '"', '\"', '#', ValueTrimmingOptions.UnquotedOnly))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("1", record[0]);
                Assert.AreEqual("2", record[1]);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(2, csv.FieldCount);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #12
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest9()
        {
            const string data = ",";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual(String.Empty, record[0]);
                Assert.AreEqual(String.Empty, record[1]);
                Assert.AreEqual(',', csv.Delimiter);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(2, csv.FieldCount);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #13
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest3()
        {
            const string data = "1\r2\n";

            using (CsvRecordReader csv = new CsvRecordReader(new StringReader(data), false))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("1", record[0]);

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("2", record[0]);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #14
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest27()
        {
            const string data = "'bob said, ''Hey!''',2, 3 ";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false, ',', '\'', '\'', '#', ValueTrimmingOptions.UnquotedOnly))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("bob said, 'Hey!'", record[0]);
                Assert.AreEqual("2", record[1]);
                Assert.AreEqual("3", record[2]);
                Assert.AreEqual(',', csv.Delimiter);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(3, csv.FieldCount);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #15
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
 public void ArgumentTestIndexer3()
 {
     using (CsvRecordReader csv = new CsvRecordReader(new StringReader(CsvReaderSampleData.SampleData1), false))
     {
         string s = csv.Read()["asdf"];
     }
 }
コード例 #16
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
 public void ArgumentTestIndexer1()
 {
     using (CsvRecordReader csv = new CsvRecordReader(new StringReader(CsvReaderSampleData.SampleData1), false))
     {
         record = csv.Read();
         string s = record[-1];
     }
 }
コード例 #17
0
 public void ArgumentTestIndexer1()
 {
     using (CsvRecordReader csv = new CsvRecordReader(new StringReader(CsvReaderSampleData.SampleData1), false))
     {
         record = csv.Read();
         string s = record[-1];
     }
 }
コード例 #18
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest20()
        {
            const string data = "user_id,name\r\n1,Bruce";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), true))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("1", record[0]);
                Assert.AreEqual("Bruce", record[1]);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(2, csv.FieldCount);
                Assert.AreEqual("1", record["user_id"]);
                Assert.AreEqual("Bruce", record["name"]);
                Assert.IsNull(csv.Read());
                Assert.IsNull(csv.Read());
            }
        }
コード例 #19
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
 public void ArgumentTestIndexer9()
 {
     using (CsvRecordReader csv = new CsvRecordReader(new StringReader(CsvReaderSampleData.SampleData1), true))
     {
         var    record = csv.Read();
         string s      = record["asdf"];
     }
 }
コード例 #20
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void SkipEmptyLinesTest2()
        {
            using (CsvRecordReader csv = new CsvRecordReader(new StringReader("00\n\n10"), false))
            {
                csv.SkipEmptyLines = true;

                Assert.AreEqual(1, csv.FieldCount);

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("00", record[0]);

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("10", record[0]);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #21
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest31()
        {
            const string data = "1\r\n# bunch of crazy stuff here\r\n1";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false, ',', '\"', '\"', '#', ValueTrimmingOptions.None))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("1", record[0]);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(1, csv.FieldCount);
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("1", record[0]);
                Assert.AreEqual(1, csv.CurrentRecordIndex);
                Assert.AreEqual(1, csv.FieldCount);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #22
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest40()
        {
            using (CsvRecordReader csv = new CsvRecordReader(new StringReader("\"00\",\n\"10\","), false))
            {
                Assert.AreEqual(2, csv.FieldCount);

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("00", record[0]);
                Assert.AreEqual(string.Empty, record[1]);

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("10", record[0]);
                Assert.AreEqual(string.Empty, record[1]);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #23
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest4()
        {
            const string data = "\"\n\r\n\n\r\r\",,\t,\n";

            using (CsvRecordReader csv = new CsvRecordReader(new StringReader(data), false))
            {
                record = csv.Read(); Assert.NotNull(record);

                Assert.AreEqual(4, csv.FieldCount);

                Assert.AreEqual("\n\r\n\n\r\r", record[0]);
                Assert.AreEqual("", record[1]);
                Assert.AreEqual("", record[2]);
                Assert.AreEqual("", record[3]);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #24
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest19()
        {
            string data = String.Empty;

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false))
            {
                Assert.IsNull(csv.Read());
            }
        }
コード例 #25
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest39()
        {
            using (CsvRecordReader csv = new CsvRecordReader(new StringReader("00,01,   \n10,11,   "), false, CsvReader.DefaultDelimiter, CsvReader.DefaultQuote, CsvReader.DefaultEscape, CsvReader.DefaultComment, ValueTrimmingOptions.UnquotedOnly, 1))
            {
                int fieldCount = csv.FieldCount;

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("00", record[0]);
                Assert.AreEqual("01", record[1]);
                Assert.AreEqual("", record[2]);

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("10", record[0]);
                Assert.AreEqual("11", record[1]);
                Assert.AreEqual("", record[2]);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #26
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void UnicodeParsingTest1()
        {
            // control characters and comma are skipped

            char[] raw = new char[65536 - 13];

            for (int i = 0; i < raw.Length; i++)
            {
                raw[i] = (char)(i + 14);
            }

            raw[44 - 14] = ' '; // skip comma

            string data = new string(raw);

            using (CsvRecordReader csv = new CsvRecordReader(new StringReader(data), false))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual(data, record[0]);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #27
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest22()
        {
            const string data = "\r\r\n1\r";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false, '\r', '\"', '\"', '#', ValueTrimmingOptions.None))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual(3, csv.FieldCount);

                Assert.AreEqual(String.Empty, record[0]);
                Assert.AreEqual(String.Empty, record[1]);
                Assert.AreEqual(String.Empty, record[2]);
                Assert.AreEqual(0, csv.CurrentRecordIndex);

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("1", record[0]);
                Assert.AreEqual(String.Empty, record[1]);
                Assert.AreEqual(1, csv.CurrentRecordIndex);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #28
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void UnicodeParsingTest3()
        {
            byte[] buffer;

            string test = "München";

            using (MemoryStream stream = new MemoryStream())
            {
                using (TextWriter writer = new StreamWriter(stream, Encoding.Unicode))
                {
                    writer.Write(test);
                }

                buffer = stream.ToArray();
            }

            using (CsvRecordReader csv = new CsvRecordReader(new StreamReader(new MemoryStream(buffer), Encoding.Unicode, false), false))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual(test, record[0]);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #29
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest32()
        {
            const string data = "\"1\",Bruce\r\n\"2\n\",Toni\r\n\"3\",Brian\r\n";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false, ',', '\"', '\"', '#', ValueTrimmingOptions.None))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("1", record[0]);
                Assert.AreEqual("Bruce", record[1]);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(2, csv.FieldCount);
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("2\n", record[0]);
                Assert.AreEqual("Toni", record[1]);
                Assert.AreEqual(1, csv.CurrentRecordIndex);
                Assert.AreEqual(2, csv.FieldCount);
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("3", record[0]);
                Assert.AreEqual("Brian", record[1]);
                Assert.AreEqual(2, csv.CurrentRecordIndex);
                Assert.AreEqual(2, csv.FieldCount);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #30
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void ParsingTest34()
        {
            const string data = "\"Chicane\", \"Love on the Run\", \"Knight Rider\", \"This field contains a comma, but it doesn't matter as the field is quoted\"\r\n" +
                                "\"Samuel Barber\", \"Adagio for Strings\", \"Classical\", \"This field contains a double quote character, \"\", but it doesn't matter as it is escaped\"";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false, ',', '\"', '\"', '#', ValueTrimmingOptions.UnquotedOnly))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("Chicane", record[0]);
                Assert.AreEqual("Love on the Run", record[1]);
                Assert.AreEqual("Knight Rider", record[2]);
                Assert.AreEqual("This field contains a comma, but it doesn't matter as the field is quoted", record[3]);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(4, csv.FieldCount);
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("Samuel Barber", record[0]);
                Assert.AreEqual("Adagio for Strings", record[1]);
                Assert.AreEqual("Classical", record[2]);
                Assert.AreEqual("This field contains a double quote character, \", but it doesn't matter as it is escaped", record[3]);
                Assert.AreEqual(1, csv.CurrentRecordIndex);
                Assert.AreEqual(4, csv.FieldCount);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #31
0
ファイル: CsvReaderTest.cs プロジェクト: smithbower/CsvReader
        public void GetFieldHeaders_WithEmptyHeaderNames(string defaultHeaderName)
        {
            if (defaultHeaderName == null)
            {
                defaultHeaderName = "Column";
            }

            using (var csv = new CsvRecordReader(new StringReader(",  ,,aaa,\"   \",,,"), true))
            {
                csv.DefaultHeaderName = defaultHeaderName;

                Assert.IsNull(csv.Read());
                Assert.AreEqual(8, csv.FieldCount);

                var headers = csv.GetFieldHeaders();
                Assert.AreEqual(csv.FieldCount, headers.Length);

                Assert.AreEqual("aaa", headers[3]);
                foreach (var index in new int[] { 0, 1, 2, 4, 5, 6, 7 })
                {
                    Assert.AreEqual(defaultHeaderName + index.ToString(), headers[index]);
                }
            }
        }
コード例 #32
0
        public void ParsingTest33()
        {
            const string data = "\"double\\\\\\\\double backslash\"";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false, ',', '\"', '\\', '#', ValueTrimmingOptions.None))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("double\\\\double backslash", record[0]);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(1, csv.FieldCount);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #33
0
        public void ParsingTest8()
        {
            const string data = "\"bob said, \"\"Hey!\"\"\",2, 3 ";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false, ',', '\"', '\"', '#', ValueTrimmingOptions.UnquotedOnly))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("bob said, \"Hey!\"", record[0]);
                Assert.AreEqual("2", record[1]);
                Assert.AreEqual("3", record[2]);
                Assert.AreEqual(',', csv.Delimiter);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(3, csv.FieldCount);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #34
0
        public void UnicodeParsingTest3()
        {
            byte[] buffer;

            string test = "München";

            using (MemoryStream stream = new MemoryStream())
            {
                using (TextWriter writer = new StreamWriter(stream, Encoding.Unicode))
                {
                    writer.Write(test);
                }

                buffer = stream.ToArray();
            }

            using (CsvRecordReader csv = new CsvRecordReader(new StreamReader(new MemoryStream(buffer), Encoding.Unicode, false), false))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual(test, record[0]);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #35
0
        public void ParsingTest41()
        {
            using (CsvRecordReader csv = new CsvRecordReader(new StringReader("First record          ,Second record"), false, CsvReader.DefaultDelimiter, CsvReader.DefaultQuote, CsvReader.DefaultEscape, CsvReader.DefaultComment, ValueTrimmingOptions.UnquotedOnly, 16))
            {
                Assert.AreEqual(2, csv.FieldCount);

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("First record", record[0]);
                Assert.AreEqual("Second record", record[1]);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #36
0
 public void ParsingTest42()
 {
     using (var csv = new CsvRecordReader(new StringReader(" "), false))
     {
         record = csv.Read(); Assert.NotNull(record);
         Assert.AreEqual(1, csv.FieldCount);
         Assert.AreEqual(string.Empty, record[0]);
         Assert.IsNull(csv.Read());
     }
 }
コード例 #37
0
        public void ParsingTest38()
        {
            using (CsvRecordReader csv = new CsvRecordReader(new StringReader("abc,def,ghi\n"), false))
            {
                int fieldCount = csv.FieldCount;

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("abc", record[0]);
                Assert.AreEqual("def", record[1]);
                Assert.AreEqual("ghi", record[2]);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #38
0
        public void ParsingTest4()
        {
            const string data = "\"\n\r\n\n\r\r\",,\t,\n";

            using (CsvRecordReader csv = new CsvRecordReader(new StringReader(data), false))
            {
                record = csv.Read(); Assert.NotNull(record);

                Assert.AreEqual(4, csv.FieldCount);

                Assert.AreEqual("\n\r\n\n\r\r", record[0]);
                Assert.AreEqual("", record[1]);
                Assert.AreEqual("", record[2]);
                Assert.AreEqual("", record[3]);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #39
0
        public void ParsingTest20()
        {
            const string data = "user_id,name\r\n1,Bruce";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), true))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("1", record[0]);
                Assert.AreEqual("Bruce", record[1]);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(2, csv.FieldCount);
                Assert.AreEqual("1", record["user_id"]);
                Assert.AreEqual("Bruce", record["name"]);
                Assert.IsNull(csv.Read());
                Assert.IsNull(csv.Read());
            }
        }
コード例 #40
0
        public void ParsingTest22()
        {
            const string data = "\r\r\n1\r\r";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false, '\r', '\"', '\"', '#', ValueTrimmingOptions.None))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual(3, csv.FieldCount);

                Assert.AreEqual(String.Empty, record[0]);
                Assert.AreEqual(String.Empty, record[1]);
                Assert.AreEqual(String.Empty, record[2]);
                Assert.AreEqual(0, csv.CurrentRecordIndex);

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("1", record[0]);
                Assert.AreEqual(String.Empty, record[1]);
                Assert.AreEqual(1, csv.CurrentRecordIndex);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #41
0
        public void ParsingTest19()
        {
            string data = String.Empty;

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false))
            {
                Assert.IsNull(csv.Read());
            }
        }
コード例 #42
0
        public void ParsingTest2()
        {
            // ["Bob said, ""Hey!""",2, 3 ]
            const string data = "\"Bob said, \"\"Hey!\"\"\",2, 3 ";

            using (CsvRecordReader csv = new CsvRecordReader(new StringReader(data), false))
            {
                CsvRecord record;

                record = csv.Read();
                Assert.NotNull(record);
                Assert.AreEqual(@"Bob said, ""Hey!""", record[0]);
                Assert.AreEqual("2", record[1]);
                Assert.AreEqual("3", record[2]);

                Assert.IsNull(csv.Read());
            }

            using (CsvRecordReader csv = new CsvRecordReader(new StringReader(data), false, ',', '"', '"', '#', ValueTrimmingOptions.None))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual(@"Bob said, ""Hey!""", record[0]);
                Assert.AreEqual("2", record[1]);
                Assert.AreEqual(" 3 ", record[2]);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #43
0
        public void ParsingTest16()
        {
            const string data = "1\r2\n";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false, '\r', '"', '\"', '#', ValueTrimmingOptions.UnquotedOnly))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("1", record[0]);
                Assert.AreEqual("2", record[1]);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(2, csv.FieldCount);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #44
0
        public void GetFieldHeaders_WithEmptyHeaderNames(string defaultHeaderName)
        {
            if (defaultHeaderName == null)
                defaultHeaderName = "Column";

            using (var csv = new CsvRecordReader(new StringReader(",  ,,aaa,\"   \",,,"), true))
            {
                csv.DefaultHeaderName = defaultHeaderName;

                Assert.IsNull(csv.Read());
                Assert.AreEqual(8, csv.FieldCount);

                var headers = csv.GetFieldHeaders();
                Assert.AreEqual(csv.FieldCount, headers.Length);

                Assert.AreEqual("aaa", headers[3]);
                foreach (var index in new int[] { 0, 1, 2, 4, 5, 6, 7 })
                    Assert.AreEqual(defaultHeaderName + index.ToString(), headers[index]);
            }
        }
コード例 #45
0
 public void ArgumentTestIndexer9()
 {
     using (CsvRecordReader csv = new CsvRecordReader(new StringReader(CsvReaderSampleData.SampleData1), true))
     {
         var record = csv.Read();
         string s = record["asdf"];
     }
 }
コード例 #46
0
        public void ParsingTest34()
        {
            const string data = "\"Chicane\", \"Love on the Run\", \"Knight Rider\", \"This field contains a comma, but it doesn't matter as the field is quoted\"\r\n" +
                      "\"Samuel Barber\", \"Adagio for Strings\", \"Classical\", \"This field contains a double quote character, \"\", but it doesn't matter as it is escaped\"";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false, ',', '\"', '\"', '#', ValueTrimmingOptions.UnquotedOnly))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("Chicane", record[0]);
                Assert.AreEqual("Love on the Run", record[1]);
                Assert.AreEqual("Knight Rider", record[2]);
                Assert.AreEqual("This field contains a comma, but it doesn't matter as the field is quoted", record[3]);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(4, csv.FieldCount);
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("Samuel Barber", record[0]);
                Assert.AreEqual("Adagio for Strings", record[1]);
                Assert.AreEqual("Classical", record[2]);
                Assert.AreEqual("This field contains a double quote character, \", but it doesn't matter as it is escaped", record[3]);
                Assert.AreEqual(1, csv.CurrentRecordIndex);
                Assert.AreEqual(4, csv.FieldCount);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #47
0
        public void ParsingTest29()
        {
            string data = new String('a', 75) + "," + new String('b', 75);

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual(new String('a', 75), record[0]);
                Assert.AreEqual(new String('b', 75), record[1]);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(2, csv.FieldCount);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #48
0
        public void ParsingTest35()
        {
            using (CsvRecordReader csv = new CsvRecordReader(new StringReader("\t"), false, '\t'))
            {
                Assert.AreEqual(2, csv.FieldCount);

                record = csv.Read(); Assert.NotNull(record);

                Assert.AreEqual(string.Empty, record[0]);
                Assert.AreEqual(string.Empty, record[1]);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #49
0
        public void ParsingTest3()
        {
            const string data = "1\r2\n";

            using (CsvRecordReader csv = new CsvRecordReader(new StringReader(data), false))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("1", record[0]);

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("2", record[0]);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #50
0
        public void ParsingTest39()
        {
            using (CsvRecordReader csv = new CsvRecordReader(new StringReader("00,01,   \n10,11,   "), false, CsvReader.DefaultDelimiter, CsvReader.DefaultQuote, CsvReader.DefaultEscape, CsvReader.DefaultComment, ValueTrimmingOptions.UnquotedOnly, 1))
            {
                int fieldCount = csv.FieldCount;

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("00", record[0]);
                Assert.AreEqual("01", record[1]);
                Assert.AreEqual("", record[2]);

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("10", record[0]);
                Assert.AreEqual("11", record[1]);
                Assert.AreEqual("", record[2]);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #51
0
        public void ParsingTest30()
        {
            const string data = "1\r\n\r\n1";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("1", record[0]);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(1, csv.FieldCount);
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("1", record[0]);
                Assert.AreEqual(1, csv.CurrentRecordIndex);
                Assert.AreEqual(1, csv.FieldCount);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #52
0
        public void ParsingTest40()
        {
            using (CsvRecordReader csv = new CsvRecordReader(new StringReader("\"00\",\n\"10\","), false))
            {
                Assert.AreEqual(2, csv.FieldCount);

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("00", record[0]);
                Assert.AreEqual(string.Empty, record[1]);

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("10", record[0]);
                Assert.AreEqual(string.Empty, record[1]);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #53
0
        public void ParsingTest31()
        {
            const string data = "1\r\n# bunch of crazy stuff here\r\n1";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false, ',', '\"', '\"', '#', ValueTrimmingOptions.None))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("1", record[0]);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(1, csv.FieldCount);
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("1", record[0]);
                Assert.AreEqual(1, csv.CurrentRecordIndex);
                Assert.AreEqual(1, csv.FieldCount);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #54
0
 public void ArgumentTestIndexer3()
 {
     using (CsvRecordReader csv = new CsvRecordReader(new StringReader(CsvReaderSampleData.SampleData1), false))
     {
         string s = csv.Read()["asdf"];
     }
 }
コード例 #55
0
        public void SkipEmptyLinesTest2()
        {
            using (CsvRecordReader csv = new CsvRecordReader(new StringReader("00\n\n10"), false))
            {
                csv.SkipEmptyLines = true;

                Assert.AreEqual(1, csv.FieldCount);

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("00", record[0]);

                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("10", record[0]);

                Assert.IsNull(csv.Read());
            }
        }
コード例 #56
0
 public void ParsingTest7()
 {
     using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader("\r\n1\r\n"), false))
     {
         record = csv.Read(); Assert.NotNull(record);
         Assert.AreEqual(',', csv.Delimiter);
         Assert.AreEqual(0, csv.CurrentRecordIndex);
         Assert.AreEqual(1, csv.FieldCount);
         Assert.AreEqual("1", record[0]);
         Assert.IsNull(csv.Read());
     }
 }
コード例 #57
0
        public void ParsingTest32()
        {
            const string data = "\"1\",Bruce\r\n\"2\n\",Toni\r\n\"3\",Brian\r\n";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false, ',', '\"', '\"', '#', ValueTrimmingOptions.None))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("1", record[0]);
                Assert.AreEqual("Bruce", record[1]);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(2, csv.FieldCount);
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("2\n", record[0]);
                Assert.AreEqual("Toni", record[1]);
                Assert.AreEqual(1, csv.CurrentRecordIndex);
                Assert.AreEqual(2, csv.FieldCount);
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual("3", record[0]);
                Assert.AreEqual("Brian", record[1]);
                Assert.AreEqual(2, csv.CurrentRecordIndex);
                Assert.AreEqual(2, csv.FieldCount);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #58
0
        public void ParsingTest9()
        {
            const string data = ",";

            using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual(String.Empty, record[0]);
                Assert.AreEqual(String.Empty, record[1]);
                Assert.AreEqual(',', csv.Delimiter);
                Assert.AreEqual(0, csv.CurrentRecordIndex);
                Assert.AreEqual(2, csv.FieldCount);
                Assert.IsNull(csv.Read());
            }
        }
コード例 #59
0
        public void UnicodeParsingTest1()
        {
            // control characters and comma are skipped

            char[] raw = new char[65536 - 13];

            for (int i = 0; i < raw.Length; i++)
                raw[i] = (char)(i + 14);

            raw[44 - 14] = ' '; // skip comma

            string data = new string(raw);

            using (CsvRecordReader csv = new CsvRecordReader(new StringReader(data), false))
            {
                record = csv.Read(); Assert.NotNull(record);
                Assert.AreEqual(data, record[0]);
                Assert.IsNull(csv.Read());
            }
        }