File indexing completed on 2024-05-12 05:09:59
0001 /*************************************************************************** 0002 Copyright (C) 2009-2020 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 #undef QT_NO_CAST_FROM_ASCII 0026 0027 #include "formattest.h" 0028 0029 #include "../fieldformat.h" 0030 #include "../config/tellico_config.h" 0031 0032 #include <KLocalizedString> 0033 0034 #include <QTest> 0035 0036 QTEST_GUILESS_MAIN( FormatTest ) 0037 0038 void FormatTest::initTestCase() { 0039 KLocalizedString::setApplicationDomain("tellico"); 0040 Tellico::Config::setArticlesString(QStringLiteral("the,l'")); 0041 Tellico::Config::setNoCapitalizationString(QStringLiteral("the,of,et,de")); 0042 } 0043 0044 void FormatTest::testCapitalization() { 0045 QFETCH(QString, string); 0046 QFETCH(QString, capitalized); 0047 0048 QCOMPARE(Tellico::FieldFormat::capitalize(string), capitalized); 0049 } 0050 0051 void FormatTest::testCapitalization_data() { 0052 QTest::addColumn<QString>("string"); 0053 QTest::addColumn<QString>("capitalized"); 0054 0055 QTest::newRow("test1") << "the title" << "The Title"; 0056 // right now, 'the' is in the no capitalization string 0057 QTest::newRow("test2") << "title, the" << "Title, the"; 0058 QTest::newRow("test3") << "the return of the king" << "The Return of the King"; 0059 QTest::newRow("test4") << QStringLiteral("école nationale supérieure de l'aéronautique et de l'espace") 0060 << QStringLiteral("École Nationale Supérieure de l'Aéronautique et de l'Espace"); 0061 QTest::newRow("test5") << "Appartement, L'" << "Appartement, L'"; 0062 } 0063 0064 void FormatTest::testTitle() { 0065 QFETCH(QString, string); 0066 QFETCH(QString, formatted); 0067 QFETCH(bool, capitalize); 0068 QFETCH(bool, format); 0069 0070 Tellico::FieldFormat::Options options; 0071 if(capitalize) { 0072 options |= Tellico::FieldFormat::FormatCapitalize; 0073 } 0074 if(format) { 0075 options |= Tellico::FieldFormat::FormatAuto; 0076 } 0077 0078 QCOMPARE(Tellico::FieldFormat::title(string, options), formatted); 0079 } 0080 0081 void FormatTest::testTitle_data() { 0082 QTest::addColumn<QString>("string"); 0083 QTest::addColumn<QString>("formatted"); 0084 QTest::addColumn<bool>("capitalize"); 0085 QTest::addColumn<bool>("format"); 0086 0087 QTest::newRow("test1") << "the title" << "the title" << false << false; 0088 QTest::newRow("test2") << "the title" << "The Title" << true << false; 0089 // right now, 'the' is in the no capitalization string 0090 QTest::newRow("test3") << "title, the" << "Title, the" << true << false; 0091 QTest::newRow("test4") << "the Title" << "the Title" << false << false; 0092 QTest::newRow("test5") << "the Title" << "The Title" << true << false; 0093 QTest::newRow("test6") << "the title" << "title, the" << false << true; 0094 QTest::newRow("test7") << "the title" << "Title, The" << true << true; 0095 // right now, 'the' is in the no capitalization string 0096 QTest::newRow("test8") << "title,the" << "Title, the" << true << true; 0097 QTest::newRow("test9") << "the return of the king" << "Return of the King, The" << true << true; 0098 } 0099 0100 void FormatTest::testName() { 0101 QFETCH(QString, string); 0102 QFETCH(QString, formatted); 0103 QFETCH(bool, capitalize); 0104 QFETCH(bool, format); 0105 0106 Tellico::FieldFormat::Options options; 0107 if(capitalize) { 0108 options |= Tellico::FieldFormat::FormatCapitalize; 0109 } 0110 if(format) { 0111 options |= Tellico::FieldFormat::FormatAuto; 0112 } 0113 0114 QCOMPARE(Tellico::FieldFormat::name(string, options), formatted); 0115 } 0116 0117 void FormatTest::testName_data() { 0118 QTest::addColumn<QString>("string"); 0119 QTest::addColumn<QString>("formatted"); 0120 QTest::addColumn<bool>("capitalize"); 0121 QTest::addColumn<bool>("format"); 0122 0123 QTest::newRow("test1") << "name" << "Name" << true << false; 0124 QTest::newRow("test2") << "first name" << "name, first" << false << true; 0125 QTest::newRow("test3") << "first Name" << "Name, First" << true << true; 0126 QTest::newRow("test4") << "tom swift, jr." << "Swift, Jr., Tom" << true << true; 0127 QTest::newRow("test4-capitalize") << "tom swift, Jr." << "Swift, Jr., Tom" << true << true; 0128 QTest::newRow("test5") << "swift, jr., tom" << "Swift, Jr., Tom" << true << false; 0129 QTest::newRow("test6") << "tom de swift, jr." << "de Swift, Jr., Tom" << true << true; 0130 QTest::newRow("test6-capitalize") << "tom de swift, Jr." << "de Swift, Jr., Tom" << true << true; 0131 QTest::newRow("test7") << "dr. tom de swift , jr." << "Dr. Tom de Swift, Jr." << true << false; 0132 QTest::newRow("test8") << "dr. tom de swift , jr." << "de Swift, Jr., Dr. Tom" << true << true; 0133 QTest::newRow("test9") << "hannibal lector smith" << "Smith, Hannibal Lector" << true << true; 0134 QTest::newRow("test10") << "hannibal lector-smith" << "Lector-Smith, Hannibal" << true << true; 0135 QTest::newRow("test11") << "lector smith, hannibal" << "Lector Smith, Hannibal" << true << true; 0136 QTest::newRow("test12") << "john van der graf" << "van der Graf, John" << true << true; 0137 QTest::newRow("test13") << "john Van Der graf" << "Van Der Graf, John" << true << true; 0138 QTest::newRow("single comma") << "," << "," << false << true; 0139 } 0140 0141 void FormatTest::testSplit() { 0142 QStringList list = QStringList() << QStringLiteral("one") << QStringLiteral("two") << QStringLiteral("three"); 0143 QCOMPARE(Tellico::FieldFormat::splitValue(list.join(Tellico::FieldFormat::delimiterString())), list); 0144 QCOMPARE(Tellico::FieldFormat::splitRow(list.join(Tellico::FieldFormat::columnDelimiterString())), list); 0145 QCOMPARE(Tellico::FieldFormat::splitTable(list.join(Tellico::FieldFormat::rowDelimiterString())), list); 0146 } 0147 0148 void FormatTest::testStripArticles() { 0149 QFETCH(QString, articles); 0150 QFETCH(QString, string); 0151 QFETCH(QString, stripped); 0152 0153 Tellico::Config::setArticlesString(articles); 0154 Tellico::FieldFormat::stripArticles(string); 0155 QCOMPARE(string, stripped); 0156 } 0157 0158 void FormatTest::testStripArticles_data() { 0159 QTest::addColumn<QString>("articles"); 0160 QTest::addColumn<QString>("string"); 0161 QTest::addColumn<QString>("stripped"); 0162 0163 QTest::newRow("test1") << "the,l'" << "name" << "name"; 0164 QTest::newRow("test2") << "the,l'" << "the name" << "name"; 0165 QTest::newRow("test3") << "the,l'" << "name, the" << "name"; 0166 QTest::newRow("test4") << "the,l'" << "thename" << "thename"; 0167 QTest::newRow("test5") << "the,l'" << "l'name" << "name"; 0168 QTest::newRow("test6") << "the,l'" << "lname" << "lname"; 0169 QTest::newRow("test7") << "the,l'" << "al'name" << "al'name"; 0170 QTest::newRow("test8") << "the" << "l'name" << "l'name"; 0171 QTest::newRow("test9") << "l'" << "the name" << "the name"; 0172 QTest::newRow("test10") << "l'" << "l'name," << "name"; 0173 }