public void Write_OneRowWithEuroSymbol_CorrectlyWritten() { var table = new DataTable(); Load(table, new string[] { "symbol € EUR" }, "alpha1"); var csvWriter = new CsvWriter(false); csvWriter.Write(table, "test.csv"); using (Stream stream = new FileStream("test.csv", FileMode.Open)) { stream.Position = 0; using (StreamReader streamReader = new StreamReader(stream)) { var text = streamReader.ReadToEnd(); var firstCell = text.Split(new string[] { Csv.RecordSeparator }, StringSplitOptions.RemoveEmptyEntries)[0]; Assert.That(firstCell, Is.StringContaining("€")); } } }
public void Write_OneRowNeedQuoting_CorrectlyQuoted() { var table = new DataTable(); Load(table, new string[] { "a;11" }, "alpha1"); var csvWriter = new CsvWriter(false); using (MemoryStream stream = new MemoryStream()) { StreamWriter streamWriter = new StreamWriter(stream); csvWriter.Write(table, streamWriter); stream.Position = 0; using (StreamReader streamReader = new StreamReader(stream)) { var text = streamReader.ReadToEnd(); var firstCell= text.Split(new string[] { Csv.RecordSeparator }, StringSplitOptions.RemoveEmptyEntries)[0]; Assert.That(firstCell, Is.StringStarting(Csv.TextQualifier.ToString())); Assert.That(firstCell, Is.StringEnding(Csv.TextQualifier.ToString())); Assert.That(firstCell, Is.StringContaining(Csv.FieldSeparator.ToString())); } } }
public void Write_TwoRowsWithHeader_HeaderIsCorrect() { var table = new DataTable(); var columnNames = "alpha1,alpha2,alpha3"; Load(table, new string[] { "a11,a12,a13", "a21,a22,a23" }, columnNames); var csvWriter = new CsvWriter(true); using (MemoryStream stream = new MemoryStream()) { StreamWriter streamWriter = new StreamWriter(stream); csvWriter.Write(table, streamWriter); stream.Position = 0; using (StreamReader streamReader = new StreamReader(stream)) { var text = streamReader.ReadToEnd(); var lines = text.Split(new string[] { Csv.RecordSeparator }, StringSplitOptions.RemoveEmptyEntries); var lineHeader = lines[0]; var fields = lineHeader.Split(Csv.FieldSeparator); Assert.That(fields, Is.EqualTo(columnNames.Split(','))); } } }
public void Save(string filename) { var csvWriter = new CsvWriter(true); csvWriter.Write(Content, filename); }
public void Write_TwoRowsWithHeader_ThreeLines() { var table = new DataTable(); Load(table, new string[] { "a11,a12", "a21,a22" }, "alpha1,alpha2"); var csvWriter = new CsvWriter(true); using (MemoryStream stream = new MemoryStream()) { StreamWriter streamWriter = new StreamWriter(stream); csvWriter.Write(table, streamWriter); stream.Position = 0; using (StreamReader streamReader = new StreamReader(stream)) { var text = streamReader.ReadToEnd(); text.Remove(text.Length - 2); //Avoid miscount if last line as a record separator or not var countLine = text.Count(c => c == Csv.RecordSeparator[0]); Assert.That(countLine, Is.EqualTo(3)); } } }
public void Write_TwoRowsWithoutHeader_AllLinesHaveTwoFieldSeparator() { var table = new DataTable(); Load(table, new string[] { "a11,a12,a13", "a21,a22,a23" }, "alpha1,alpha2,alpha3"); var csvWriter = new CsvWriter(false); using (MemoryStream stream = new MemoryStream()) { StreamWriter streamWriter = new StreamWriter(stream); csvWriter.Write(table, streamWriter); stream.Position = 0; using (StreamReader streamReader = new StreamReader(stream)) { var text = streamReader.ReadToEnd(); var lines = text.Split(new string[] {Csv.RecordSeparator}, StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { var countLine = line.Count(c => c == Csv.FieldSeparator); Assert.That(countLine, Is.EqualTo(2)); } } } }