Mercurial > hg > svcore
comparison data/fileio/test/CSVStreamWriterTest.h @ 1834:735b0ccd3f4a sensible-delimited-data-strings
Test CSV quoting
| author | Chris Cannam |
|---|---|
| date | Mon, 06 Apr 2020 10:59:36 +0100 |
| parents | 21c792334c2e |
| children | bfdf0f7f9448 |
comparison
equal
deleted
inserted
replaced
| 1833:21c792334c2e | 1834:735b0ccd3f4a |
|---|---|
| 328 // qDebug("\n->>%s<<-\n", oss.str().c_str()); | 328 // qDebug("\n->>%s<<-\n", oss.str().c_str()); |
| 329 QVERIFY( wroteSparseModel == true ); | 329 QVERIFY( wroteSparseModel == true ); |
| 330 QVERIFY( oss.str() != std::string() ); | 330 QVERIFY( oss.str() != std::string() ); |
| 331 QVERIFY( oss.str() == expectedOutput.toStdString() ); | 331 QVERIFY( oss.str() == expectedOutput.toStdString() ); |
| 332 } | 332 } |
| 333 | |
| 334 void writeWithQuotingRequired() | |
| 335 { | |
| 336 QString commaLabel = | |
| 337 "This label contains punctuation, specifically, commas"; | |
| 338 QString quoteSpaceLabel = | |
| 339 "This label contains spaces and \"double quotes\""; | |
| 340 | |
| 341 NoteModel notes(8, 4); | |
| 342 notes.add({ 0, 64, 4, 1.f, commaLabel }); | |
| 343 notes.add({ 16, 64, 6, 1.f, quoteSpaceLabel }); | |
| 344 | |
| 345 QString expectedWithCommaSeparator = | |
| 346 QString("0.000000000,64,0.500000000,1,\"") + | |
| 347 commaLabel + | |
| 348 QString("\"\n") + | |
| 349 QString("2.000000000,64,0.750000000,1,") + | |
| 350 quoteSpaceLabel; | |
| 351 | |
| 352 QString expectedWithSpaceSeparator = | |
| 353 QString("0.000000000 64 0.500000000 1 \"") + | |
| 354 commaLabel + | |
| 355 QString("\"\n") + | |
| 356 QString("2.000000000 64 0.750000000 1 \"") + | |
| 357 QString("This label contains spaces and \"\"double quotes\"\"") + | |
| 358 QString("\""); | |
| 359 | |
| 360 StubReporter reporter { []() -> bool { return false; } }; | |
| 361 std::ostringstream oss; | |
| 362 auto wroteSparseModel = CSVStreamWriter::writeInChunks( | |
| 363 oss, | |
| 364 notes, | |
| 365 &reporter, | |
| 366 ",", | |
| 367 DataExportDefaults, | |
| 368 2 | |
| 369 ); | |
| 370 | |
| 371 QVERIFY( wroteSparseModel == true ); | |
| 372 QVERIFY( oss.str() != std::string() ); | |
| 373 | |
| 374 cerr << oss.str() << endl; | |
| 375 cerr << expectedWithCommaSeparator << endl; | |
| 376 | |
| 377 QVERIFY( oss.str() == expectedWithCommaSeparator.toStdString() ); | |
| 378 | |
| 379 std::ostringstream oss2; | |
| 380 wroteSparseModel = CSVStreamWriter::writeInChunks( | |
| 381 oss2, | |
| 382 notes, | |
| 383 &reporter, | |
| 384 " ", | |
| 385 DataExportDefaults, | |
| 386 2 | |
| 387 ); | |
| 388 | |
| 389 QVERIFY( wroteSparseModel == true ); | |
| 390 QVERIFY( oss2.str() != std::string() ); | |
| 391 | |
| 392 cerr << oss2.str() << endl; | |
| 393 cerr << expectedWithSpaceSeparator << endl; | |
| 394 | |
| 395 QVERIFY( oss2.str() == expectedWithSpaceSeparator.toStdString() ); | |
| 396 } | |
| 333 }; | 397 }; |
| 334 | 398 |
| 335 #endif | 399 #endif |
