public byte[] ToCsvArray() { var dialect = new Dialect(); byte[] result; using (var memoryStream = new MemoryStream()) { using (var textWriter = new StreamWriter(memoryStream)) using (var csvWriter = new CSVWriter(dialect, textWriter)) { csvWriter.WriteRow(new object[] { "Team", "Player", "Eligible?", "Draft Round", "Auction Cost" }); foreach (var keeper in _keepers) { csvWriter.WriteRow(new object[] { keeper.TeamName, keeper.PlayerName, keeper.IsEligible ? "Yes" : "No", keeper.DraftRound, keeper.Cost }); } } result = memoryStream.ToArray(); } return result; }
public void Writer_WriteRow_Ok() { CultureInfo oldCulture = System.Threading.Thread.CurrentThread.CurrentCulture; Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; object[] row = new object[] { 123, 123.45, 10M, "This is string", new DateTime(2010, 9, 3, 0, 0, 0), null, 1 }; string results = null; using (StringWriter stringWriter = new StringWriter()) { using (CSVWriter writer = new CSVWriter( new Dialect(true, ';', '\"', '\\', true, "\r\n", QuoteStyle.QUOTE_MINIMAL, false, false), stringWriter)) { writer.WriteRow(row); } results = stringWriter.ToString(); } Assert.AreEqual("123;123.45;10;\"This is string\";09/03/2010 00:00:00;;1\r\n", results); Thread.CurrentThread.CurrentCulture = oldCulture; }
public void Constructor_FileNotExists_ThrowsCannotWriteToFileException() { using (var dialect = new Dialect(true, ';', '\"', '\\', true, "\r\n", QuoteStyle.QuoteMinimal, false, false)) { using (var writer = new CSVWriter(dialect, "VeryLongNonExistingFileNameForTesting", "UTF-8")) { } } }
public void Constructor_FileNameIsNullOrEmpty_ThrowsFileNameIsNullOrEmptyException(string fileName) { using (var dialect = new Dialect(true, ';', '\"', '\\', true, "\r\n", QuoteStyle.QuoteMinimal, false, false)) { using (var writer = new CSVWriter(dialect, fileName, "UTF-8")) { } } }
public void ConstructorSecond_DialectIsNull_ThrowsDialectIsNullException() { using (var stringWriter = new StringWriter()) { using (var writer = new CSVWriter(null, stringWriter)) { } } }
public void Constructor_FileIsLocked_ThrowsCannotWriteToFileException() { 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 csvWriter = new CSVWriter(dialect, "test_write_file_locked.csv", "UTF-8")) { } } } }
public void ConstructorFirst_DialectIsNull_ThrowsDialectIsNullException() { using (var writer = new CSVWriter(null, Guid.NewGuid().ToString(), "UTF-8")) { } }
private void WriteAndTestRow(object[] input, string output, Dialect dialect, CultureInfo culture = null) { dialect = dialect ?? new Dialect( true, ';', '\"', '\\', true, "\r\n", QuoteStyle.QuoteMinimal, false, false); var oldCulture = Thread.CurrentThread.CurrentCulture; try { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; string results; using (var writer = new StringWriter()) { using (var csvWriter = new CSVWriter(dialect, writer, culture)) { csvWriter.WriteRow(input); } results = writer.ToString(); } Assert.AreEqual(output, results); } finally { Thread.CurrentThread.CurrentCulture = oldCulture; } }
public void WriteRow_RowIsNull_ThrowsRowIsNullOrEmptyException() { using (var stringWriter = new StringWriter()) { using (var dialect = new Dialect(true, ';', '\"', '\\', true, "\r\n", QuoteStyle.QuoteMinimal, false, false)) { using (var writer = new CSVWriter(dialect, stringWriter)) { writer.WriteRow(null); } } } }
public void WriteRow_QuoteAll_Quoted() { var row = new object[] { 1, 2, 3, new DumyObject(4) }; string results; using (var writer = new StringWriter()) { var dialect = new Dialect(true, ';', '\"', '\\', true, "\r\n", QuoteStyle.QuoteAll, false, false); using (var csvWriter = new CSVWriter(dialect, writer)) { csvWriter.WriteRow(row); } results = writer.ToString(); } Assert.AreEqual("\"1\";\"2\";\"3\";\"4\"\r\n", results); }
public void WriteRow_EscapeStrings_Escaped() { var row = new object[] { "\"" }; string results; using (var writer = new StringWriter()) { var dialect = new Dialect(true, ';', '\"', '\\', true, "\r\n", QuoteStyle.QuoteAll, false, false); using (var csvWriter = new CSVWriter(dialect, writer)) { csvWriter.WriteRow(row); } results = writer.ToString(); } Assert.AreEqual("\"\\\"\"\r\n", results); }