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()); } }
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); }
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()); } }
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()); } }
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()); } }
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()); } }
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()); } }
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()); } }
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()); } }
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()); } }
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()); } }
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()); } }
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()); } }
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()); } }
public void ArgumentTestIndexer3() { using (CsvRecordReader csv = new CsvRecordReader(new StringReader(CsvReaderSampleData.SampleData1), false)) { string s = csv.Read()["asdf"]; } }
public void ArgumentTestIndexer1() { using (CsvRecordReader csv = new CsvRecordReader(new StringReader(CsvReaderSampleData.SampleData1), false)) { record = csv.Read(); string s = record[-1]; } }
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()); } }
public void ArgumentTestIndexer9() { using (CsvRecordReader csv = new CsvRecordReader(new StringReader(CsvReaderSampleData.SampleData1), true)) { var record = csv.Read(); string s = record["asdf"]; } }
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()); } }
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()); } }
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()); } }
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()); } }
public void ParsingTest19() { string data = String.Empty; using (CsvRecordReader csv = new CsvRecordReader(new System.IO.StringReader(data), false)) { Assert.IsNull(csv.Read()); } }
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()); } }
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()); } }
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()); } }
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()); } }
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()); } }
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()); } }
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]); } } }
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()); } }
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()); } }
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]); } }
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()); } }
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()); } }