# HG changeset patch # User Chris Cannam # Date 1586167176 -3600 # Node ID 735b0ccd3f4ae758633950d7f53e897dd93156ed # Parent 21c792334c2e170bf4fa3a1d29d7596b9a953d86 Test CSV quoting diff -r 21c792334c2e -r 735b0ccd3f4a data/fileio/test/CSVStreamWriterTest.h --- a/data/fileio/test/CSVStreamWriterTest.h Fri Apr 03 17:11:05 2020 +0100 +++ b/data/fileio/test/CSVStreamWriterTest.h Mon Apr 06 10:59:36 2020 +0100 @@ -330,6 +330,70 @@ QVERIFY( oss.str() != std::string() ); QVERIFY( oss.str() == expectedOutput.toStdString() ); } + + void writeWithQuotingRequired() + { + QString commaLabel = + "This label contains punctuation, specifically, commas"; + QString quoteSpaceLabel = + "This label contains spaces and \"double quotes\""; + + NoteModel notes(8, 4); + notes.add({ 0, 64, 4, 1.f, commaLabel }); + notes.add({ 16, 64, 6, 1.f, quoteSpaceLabel }); + + QString expectedWithCommaSeparator = + QString("0.000000000,64,0.500000000,1,\"") + + commaLabel + + QString("\"\n") + + QString("2.000000000,64,0.750000000,1,") + + quoteSpaceLabel; + + QString expectedWithSpaceSeparator = + QString("0.000000000 64 0.500000000 1 \"") + + commaLabel + + QString("\"\n") + + QString("2.000000000 64 0.750000000 1 \"") + + QString("This label contains spaces and \"\"double quotes\"\"") + + QString("\""); + + StubReporter reporter { []() -> bool { return false; } }; + std::ostringstream oss; + auto wroteSparseModel = CSVStreamWriter::writeInChunks( + oss, + notes, + &reporter, + ",", + DataExportDefaults, + 2 + ); + + QVERIFY( wroteSparseModel == true ); + QVERIFY( oss.str() != std::string() ); + + cerr << oss.str() << endl; + cerr << expectedWithCommaSeparator << endl; + + QVERIFY( oss.str() == expectedWithCommaSeparator.toStdString() ); + + std::ostringstream oss2; + wroteSparseModel = CSVStreamWriter::writeInChunks( + oss2, + notes, + &reporter, + " ", + DataExportDefaults, + 2 + ); + + QVERIFY( wroteSparseModel == true ); + QVERIFY( oss2.str() != std::string() ); + + cerr << oss2.str() << endl; + cerr << expectedWithSpaceSeparator << endl; + + QVERIFY( oss2.str() == expectedWithSpaceSeparator.toStdString() ); + } }; #endif