1 /***************************************************************************
2 Copyright (C) 2009-2020 Robby Stephenson <robby@periapsis.org>
3 ***************************************************************************/
4
5 /***************************************************************************
6 * *
7 * This program is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU General Public License as *
9 * published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) version 3 or any later version *
11 * accepted by the membership of KDE e.V. (or its successor approved *
12 * by the membership of KDE e.V.), which shall act as a proxy *
13 * defined in Section 14 of version 3 of the license. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
22 * *
23 ***************************************************************************/
24
25 #undef QT_NO_CAST_FROM_ASCII
26
27 #include "formattest.h"
28
29 #include "../fieldformat.h"
30 #include "../config/tellico_config.h"
31
32 #include <QTest>
33
QTEST_GUILESS_MAIN(FormatTest)34 QTEST_GUILESS_MAIN( FormatTest )
35
36 void FormatTest::initTestCase() {
37 Tellico::Config::setArticlesString(QStringLiteral("the,l'"));
38 Tellico::Config::setNoCapitalizationString(QStringLiteral("the,of,et,de"));
39 }
40
testCapitalization()41 void FormatTest::testCapitalization() {
42 QFETCH(QString, string);
43 QFETCH(QString, capitalized);
44
45 QCOMPARE(Tellico::FieldFormat::capitalize(string), capitalized);
46 }
47
testCapitalization_data()48 void FormatTest::testCapitalization_data() {
49 QTest::addColumn<QString>("string");
50 QTest::addColumn<QString>("capitalized");
51
52 QTest::newRow("test1") << "the title" << "The Title";
53 // right now, 'the' is in the no capitalization string
54 QTest::newRow("test2") << "title, the" << "Title, the";
55 QTest::newRow("test3") << "the return of the king" << "The Return of the King";
56 QTest::newRow("test4") << QStringLiteral("école nationale supérieure de l'aéronautique et de l'espace")
57 << QStringLiteral("École Nationale Supérieure de l'Aéronautique et de l'Espace");
58 }
59
testTitle()60 void FormatTest::testTitle() {
61 QFETCH(QString, string);
62 QFETCH(QString, formatted);
63 QFETCH(bool, capitalize);
64 QFETCH(bool, format);
65
66 Tellico::FieldFormat::Options options;
67 if(capitalize) {
68 options |= Tellico::FieldFormat::FormatCapitalize;
69 }
70 if(format) {
71 options |= Tellico::FieldFormat::FormatAuto;
72 }
73
74 QCOMPARE(Tellico::FieldFormat::title(string, options), formatted);
75 }
76
testTitle_data()77 void FormatTest::testTitle_data() {
78 QTest::addColumn<QString>("string");
79 QTest::addColumn<QString>("formatted");
80 QTest::addColumn<bool>("capitalize");
81 QTest::addColumn<bool>("format");
82
83 QTest::newRow("test1") << "the title" << "the title" << false << false;
84 QTest::newRow("test2") << "the title" << "The Title" << true << false;
85 // right now, 'the' is in the no capitalization string
86 QTest::newRow("test3") << "title, the" << "Title, the" << true << false;
87 QTest::newRow("test4") << "the Title" << "the Title" << false << false;
88 QTest::newRow("test5") << "the Title" << "The Title" << true << false;
89 QTest::newRow("test6") << "the title" << "title, the" << false << true;
90 QTest::newRow("test7") << "the title" << "Title, The" << true << true;
91 // right now, 'the' is in the no capitalization string
92 QTest::newRow("test8") << "title,the" << "Title, the" << true << true;
93 QTest::newRow("test9") << "the return of the king" << "Return of the King, The" << true << true;
94 }
95
testName()96 void FormatTest::testName() {
97 QFETCH(QString, string);
98 QFETCH(QString, formatted);
99 QFETCH(bool, capitalize);
100 QFETCH(bool, format);
101
102 Tellico::FieldFormat::Options options;
103 if(capitalize) {
104 options |= Tellico::FieldFormat::FormatCapitalize;
105 }
106 if(format) {
107 options |= Tellico::FieldFormat::FormatAuto;
108 }
109
110 QCOMPARE(Tellico::FieldFormat::name(string, options), formatted);
111 }
112
testName_data()113 void FormatTest::testName_data() {
114 QTest::addColumn<QString>("string");
115 QTest::addColumn<QString>("formatted");
116 QTest::addColumn<bool>("capitalize");
117 QTest::addColumn<bool>("format");
118
119 QTest::newRow("test1") << "name" << "Name" << true << false;
120 QTest::newRow("test2") << "first name" << "name, first" << false << true;
121 QTest::newRow("test3") << "first Name" << "Name, First" << true << true;
122 QTest::newRow("test4") << "tom swift, jr." << "Swift, Jr., Tom" << true << true;
123 QTest::newRow("test4-capitalize") << "tom swift, Jr." << "Swift, Jr., Tom" << true << true;
124 QTest::newRow("test5") << "swift, jr., tom" << "Swift, Jr., Tom" << true << false;
125 QTest::newRow("test6") << "tom de swift, jr." << "de Swift, Jr., Tom" << true << true;
126 QTest::newRow("test6-capitalize") << "tom de swift, Jr." << "de Swift, Jr., Tom" << true << true;
127 QTest::newRow("test7") << "dr. tom de swift , jr." << "Dr. Tom de Swift, Jr." << true << false;
128 QTest::newRow("test8") << "dr. tom de swift , jr." << "de Swift, Jr., Dr. Tom" << true << true;
129 QTest::newRow("test9") << "hannibal lector smith" << "Smith, Hannibal Lector" << true << true;
130 QTest::newRow("test10") << "hannibal lector-smith" << "Lector-Smith, Hannibal" << true << true;
131 QTest::newRow("test11") << "lector smith, hannibal" << "Lector Smith, Hannibal" << true << true;
132 QTest::newRow("test12") << "john van der graf" << "van der Graf, John" << true << true;
133 QTest::newRow("test13") << "john Van Der graf" << "Van Der Graf, John" << true << true;
134 QTest::newRow("single comma") << "," << "," << false << true;
135 }
136
testSplit()137 void FormatTest::testSplit() {
138 QStringList list = QStringList() << QStringLiteral("one") << QStringLiteral("two") << QStringLiteral("three");
139 QCOMPARE(Tellico::FieldFormat::splitValue(list.join(Tellico::FieldFormat::delimiterString())), list);
140 QCOMPARE(Tellico::FieldFormat::splitRow(list.join(Tellico::FieldFormat::columnDelimiterString())), list);
141 QCOMPARE(Tellico::FieldFormat::splitTable(list.join(Tellico::FieldFormat::rowDelimiterString())), list);
142 }
143
testStripArticles()144 void FormatTest::testStripArticles() {
145 QFETCH(QString, articles);
146 QFETCH(QString, string);
147 QFETCH(QString, stripped);
148
149 Tellico::Config::setArticlesString(articles);
150 Tellico::FieldFormat::stripArticles(string);
151 QCOMPARE(string, stripped);
152 }
153
testStripArticles_data()154 void FormatTest::testStripArticles_data() {
155 QTest::addColumn<QString>("articles");
156 QTest::addColumn<QString>("string");
157 QTest::addColumn<QString>("stripped");
158
159 QTest::newRow("test1") << "the,l'" << "name" << "name";
160 QTest::newRow("test2") << "the,l'" << "the name" << "name";
161 QTest::newRow("test3") << "the,l'" << "name, the" << "name";
162 QTest::newRow("test4") << "the,l'" << "thename" << "thename";
163 QTest::newRow("test5") << "the,l'" << "l'name" << "name";
164 QTest::newRow("test6") << "the,l'" << "lname" << "lname";
165 QTest::newRow("test7") << "the,l'" << "al'name" << "al'name";
166 QTest::newRow("test8") << "the" << "l'name" << "l'name";
167 QTest::newRow("test9") << "l'" << "the name" << "the name";
168 QTest::newRow("test10") << "l'" << "l'name," << "name";
169 }
170