changeset 1834:735b0ccd3f4a sensible-delimited-data-strings

Test CSV quoting
author Chris Cannam
date Mon, 06 Apr 2020 10:59:36 +0100
parents 21c792334c2e
children 804dd0c06f0e
files data/fileio/test/CSVStreamWriterTest.h
diffstat 1 files changed, 64 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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