public void Constructor_ReadOnlySpan() { string file = FileGenerator("Column", "Data", 1, 3); var span = file.AsSpan(); var data = new DsvReader(span, System.Text.Encoding.UTF8, DsvOptions.DefaultCsvOptions); List <ReadOnlyMemory <char> > columns = null; List <List <ReadOnlyMemory <char> > > rows = new List <List <ReadOnlyMemory <char> > >(); while (data.MoveNext()) { if (!data.ColumnsFilled) { columns = data.ReadLine().ToList(); } else { rows.Add(data.ReadLine().ToList()); } } Assert.Equal(3, columns.Count); Assert.Single(rows); Assert.Equal("Column1", columns[0].ToString()); Assert.Equal("Column2", columns[1].ToString()); Assert.Equal("Column3", columns[2].ToString()); Assert.Equal("Data1", rows[0][0].ToString()); Assert.Equal("Data2", rows[0][1].ToString()); Assert.Equal("Data3", rows[0][2].ToString()); }
public void EndsWithNewLine() { string file = $"Column1,Column2,Column3{Environment.NewLine}Data1,Data2,Data3{Environment.NewLine}"; var data = new DsvReader(file, System.Text.Encoding.UTF8, DsvOptions.DefaultCsvOptions); List <ReadOnlyMemory <char> > columns = null; List <List <ReadOnlyMemory <char> > > rows = new List <List <ReadOnlyMemory <char> > >(); while (data.MoveNext()) { if (!data.ColumnsFilled) { columns = data.ReadLine().ToList(); } else { rows.Add(data.ReadLine().ToList()); } } Assert.Equal(3, columns.Count); Assert.Single(rows); Assert.Equal("Column1", columns[0].ToString()); Assert.Equal("Column2", columns[1].ToString()); Assert.Equal("Column3", columns[2].ToString()); Assert.Equal("Data1", rows[0][0].ToString()); Assert.Equal("Data2", rows[0][1].ToString()); Assert.Equal("Data3", rows[0][2].ToString()); }
public void MalformedData_Escapes() { string file = $"\"\"Column1\",Column2{Environment.NewLine}Data1,Data2"; Assert.Throws <FormatException>(() => { var data = new DsvReader(file, System.Text.Encoding.UTF8, DsvOptions.DefaultCsvOptions); List <ReadOnlyMemory <char> > columns = null; List <List <ReadOnlyMemory <char> > > rows = new List <List <ReadOnlyMemory <char> > >(); while (data.MoveNext()) { if (!data.ColumnsFilled) { columns = data.ReadLine().ToList(); } else { rows.Add(data.ReadLine().ToList()); } } }); }
public void SpecialCharacters() { string file = $"Column1,Column2,Column3{Environment.NewLine}Düsseldorf,Datüa2,ü123{Environment.NewLine}"; var bytes = Encoding.UTF8.GetBytes(file); var data = new DsvReader(bytes, System.Text.Encoding.UTF8, DsvOptions.DefaultCsvOptions); List <ReadOnlyMemory <char> > columns = null; List <List <ReadOnlyMemory <char> > > rows = new List <List <ReadOnlyMemory <char> > >(); while (data.MoveNext()) { if (!data.ColumnsFilled) { columns = data.ReadLine().ToList(); } else { rows.Add(data.ReadLine().ToList()); } } Assert.Equal(3, columns.Count); Assert.Single(rows); Assert.Equal("Column1", columns[0].ToString()); Assert.Equal("Column2", columns[1].ToString()); Assert.Equal("Column3", columns[2].ToString()); Assert.Equal("Düsseldorf", rows[0][0].ToString()); Assert.Equal("Datüa2", rows[0][1].ToString()); Assert.Equal("ü123", rows[0][2].ToString()); }
private void EscapeBomWithEncoding(Encoding encoding, string bomPrefix = null) { var BOM = bomPrefix ?? encoding.GetString(encoding.GetPreamble()); string file = $"{BOM}\"Column1\",\"Column2\",\"Column3\"{Environment.NewLine}\"\",Data2,Data3{Environment.NewLine},\"\","; var data = new DsvReader(file, encoding, DsvOptions.DefaultCsvOptions); List <ReadOnlyMemory <char> > columns = null; List <List <ReadOnlyMemory <char> > > rows = new List <List <ReadOnlyMemory <char> > >(); while (data.MoveNext()) { if (!data.ColumnsFilled) { columns = data.ReadLine().ToList(); } else { rows.Add(data.ReadLine().ToList()); } } Assert.Equal(3, columns.Count); Assert.Equal(2, rows.Count); Assert.Equal("Column1", columns[0].ToString()); Assert.Equal("Column2", columns[1].ToString()); Assert.Equal("Column3", columns[2].ToString()); Assert.Equal("", rows[0][0].ToString()); Assert.Equal("Data2", rows[0][1].ToString()); Assert.Equal("Data3", rows[0][2].ToString()); Assert.Equal("", rows[1][0].ToString()); Assert.Equal("", rows[1][1].ToString()); Assert.Equal("", rows[1][2].ToString()); }
public void ColumnResize() { string file = FileGenerator("Column", "Data", 3, 5); var data = new DsvReader(file, System.Text.Encoding.UTF8, DsvOptions.DefaultCsvOptions); List <ReadOnlyMemory <char> > columns = null; List <List <ReadOnlyMemory <char> > > rows = new List <List <ReadOnlyMemory <char> > >(); while (data.MoveNext()) { if (!data.ColumnsFilled) { columns = data.ReadLine().ToList(); } else { rows.Add(data.ReadLine().ToList()); } } Assert.Equal(5, columns.Count); Assert.Equal(3, rows.Count); Assert.Equal("Column1", columns[0].ToString()); Assert.Equal("Column2", columns[1].ToString()); Assert.Equal("Column3", columns[2].ToString()); Assert.Equal("Column4", columns[3].ToString()); Assert.Equal("Column5", columns[4].ToString()); for (int i = 0; i < 3; i++) { Assert.Equal("Data1", rows[i][0].ToString()); Assert.Equal("Data2", rows[i][1].ToString()); Assert.Equal("Data3", rows[i][2].ToString()); } }
public void Escaped_LastValue() { string file = $"\"Column1\",\"Column2\",\"Column3\"{Environment.NewLine}\"\",Data2,Data3{Environment.NewLine},\"\",\"HELLO\"{Environment.NewLine}"; var data = new DsvReader(file, System.Text.Encoding.UTF8, DsvOptions.DefaultCsvOptions); List <ReadOnlyMemory <char> > columns = null; List <List <ReadOnlyMemory <char> > > rows = new List <List <ReadOnlyMemory <char> > >(); while (data.MoveNext()) { if (!data.ColumnsFilled) { columns = data.ReadLine().ToList(); } else { rows.Add(data.ReadLine().ToList()); } } Assert.Equal(3, columns.Count); Assert.Equal(2, rows.Count); Assert.Equal("Column1", columns[0].ToString()); Assert.Equal("Column2", columns[1].ToString()); Assert.Equal("Column3", columns[2].ToString()); Assert.Equal("", rows[0][0].ToString()); Assert.Equal("Data2", rows[0][1].ToString()); Assert.Equal("Data3", rows[0][2].ToString()); Assert.Equal("", rows[1][0].ToString()); Assert.Equal("", rows[1][1].ToString()); Assert.Equal("HELLO", rows[1][2].ToString()); }