File indexing completed on 2024-05-12 16:46:15
0001 /*************************************************************************** 0002 Copyright (C) 2009 Robby Stephenson <robby@periapsis.org> 0003 ***************************************************************************/ 0004 0005 /*************************************************************************** 0006 * * 0007 * This program is free software; you can redistribute it and/or * 0008 * modify it under the terms of the GNU General Public License as * 0009 * published by the Free Software Foundation; either version 2 of * 0010 * the License or (at your option) version 3 or any later version * 0011 * accepted by the membership of KDE e.V. (or its successor approved * 0012 * by the membership of KDE e.V.), which shall act as a proxy * 0013 * defined in Section 14 of version 3 of the license. * 0014 * * 0015 * This program is distributed in the hope that it will be useful, * 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0018 * GNU General Public License for more details. * 0019 * * 0020 * You should have received a copy of the GNU General Public License * 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 0022 * * 0023 ***************************************************************************/ 0024 0025 #include "csvtest.h" 0026 0027 #include "../translators/csvparser.h" 0028 #include "../translators/csvexporter.h" 0029 0030 #include <QTest> 0031 0032 QTEST_MAIN( CsvTest ) 0033 0034 #define QL1(x) QStringLiteral(x) 0035 0036 void CsvTest::initTestCase() { 0037 } 0038 0039 void CsvTest::cleanupTestCase() { 0040 } 0041 0042 void CsvTest::testAll() { 0043 QFETCH(QString, line); 0044 QFETCH(QStringList, tokens); 0045 QFETCH(QString, delim); 0046 0047 Tellico::CSVParser p(line); 0048 p.setDelimiter(delim); 0049 0050 QStringList tokensNew = p.nextTokens(); 0051 0052 QCOMPARE(tokensNew, tokens); 0053 } 0054 0055 void CsvTest::testAll_data() { 0056 QTest::addColumn<QString>("line"); 0057 QTest::addColumn<QString>("delim"); 0058 QTest::addColumn<QStringList>("tokens"); 0059 0060 QTest::newRow("basic") << "robby,stephenson is cool\t,," << "," 0061 << (QStringList() << QL1("robby") << QL1("stephenson is cool") << QString() << QString()); 0062 QTest::newRow("space") << "robby,stephenson is cool\t,," << " " 0063 << (QStringList() << QL1("robby,stephenson") << QL1("is") << QL1("cool\t,,")); 0064 QTest::newRow("tab") << "robby\t\tstephenson" << "\t" 0065 << (QStringList() << QL1("robby") << QString() << QL1("stephenson")); 0066 // quotes get swallowed 0067 QTest::newRow("quotes") << "robby,\"stephenson,is,cool\"" << "," 0068 << (QStringList() << QL1("robby") << QL1("stephenson,is,cool")); 0069 QTest::newRow("newline") << "robby,\"stephenson\n,is,cool\"" << "," 0070 << (QStringList() << QL1("robby") << QL1("stephenson\n,is,cool")); 0071 } 0072 0073 void CsvTest::testEntry() { 0074 Tellico::Data::CollPtr coll(new Tellico::Data::Collection(true)); 0075 Tellico::Data::EntryPtr entry(new Tellico::Data::Entry(coll)); 0076 entry->setField(QStringLiteral("title"), QStringLiteral("title, with comma")); 0077 coll->addEntries(entry); 0078 0079 Tellico::Export::CSVExporter exporter(coll); 0080 exporter.setEntries(coll->entries()); 0081 exporter.setFields(Tellico::Data::FieldList() << coll->fieldByName(QStringLiteral("title"))); 0082 0083 QString output = exporter.text(); 0084 // the header line has the field titles, skip that 0085 output = output.section(QLatin1Char('\n'), 1); 0086 output.chop(1); 0087 QCOMPARE(output, QStringLiteral("\"title, with comma\"")); 0088 }