File indexing completed on 2024-05-12 05:09:56
0001 /*************************************************************************** 0002 Copyright (C) 2010 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 "comparisontest.h" 0028 #include "../models/stringcomparison.h" 0029 #include "../models/fieldcomparison.h" 0030 #include "../images/imagefactory.h" 0031 #include "../config/tellico_config.h" 0032 0033 #include <KLocalizedString> 0034 0035 #include <QTest> 0036 0037 QTEST_GUILESS_MAIN( ComparisonTest ) 0038 0039 void ComparisonTest::initTestCase() { 0040 KLocalizedString::setApplicationDomain("tellico"); 0041 Tellico::Config::setArticlesString(QStringLiteral("the,l'")); 0042 Tellico::ImageFactory::init(); 0043 } 0044 0045 void ComparisonTest::testNumber() { 0046 QFETCH(QString, string1); 0047 QFETCH(QString, string2); 0048 QFETCH(int, res); 0049 0050 Tellico::NumberComparison comp; 0051 0052 QCOMPARE(comp.compare(string1, string2), res); 0053 } 0054 0055 void ComparisonTest::testNumber_data() { 0056 QTest::addColumn<QString>("string1"); 0057 QTest::addColumn<QString>("string2"); 0058 QTest::addColumn<int>("res"); 0059 0060 QTest::newRow("null") << QString() << QString() << 0; 0061 QTest::newRow("< 0") << QString() << QStringLiteral("0") << -1; 0062 QTest::newRow("> 0") << QStringLiteral("0") << QString() << 1; 0063 QTest::newRow("=1 1") << QStringLiteral("1") << QStringLiteral("1") << 0; 0064 QTest::newRow("< 1") << QStringLiteral("0") << QStringLiteral("1") << -1; 0065 QTest::newRow("> 1") << QStringLiteral("1") << QStringLiteral("0") << 1; 0066 QTest::newRow("> -1") << QStringLiteral("0") << QStringLiteral("-1") << 1; 0067 QTest::newRow("< -1") << QStringLiteral("-1") << QStringLiteral("0") << -1; 0068 QTest::newRow("> 10") << QStringLiteral("10") << QStringLiteral("5") << 5; 0069 QTest::newRow("< 10") << QStringLiteral("5") << QStringLiteral("10") << -5; 0070 QTest::newRow("multiple1") << QStringLiteral("1; 2") << QStringLiteral("2") << -1; 0071 QTest::newRow("multiple2") << QStringLiteral("3; 2") << QStringLiteral("2") << 1; 0072 QTest::newRow("multiple3") << QStringLiteral("2") << QStringLiteral("2; 3") << -1; 0073 QTest::newRow("multiple4") << QStringLiteral("1; 2") << QStringLiteral("1; 3") << -1; 0074 QTest::newRow("float1") << QStringLiteral("5.1") << QStringLiteral("6.9") << -2; 0075 QTest::newRow("float2") << QStringLiteral("5.1") << QStringLiteral("5.2") << -1; 0076 QTest::newRow("float3") << QStringLiteral("5.2") << QStringLiteral("5.1") << 1; 0077 QTest::newRow("float4") << QStringLiteral("5.1") << QStringLiteral("5.1") << 0; 0078 } 0079 0080 void ComparisonTest::testLCC() { 0081 QFETCH(QString, string1); 0082 QFETCH(QString, string2); 0083 QFETCH(int, res); 0084 0085 Tellico::LCCComparison comp; 0086 0087 QCOMPARE(comp.compare(string1, string2), res); 0088 } 0089 0090 void ComparisonTest::testLCC_data() { 0091 QTest::addColumn<QString>("string1"); 0092 QTest::addColumn<QString>("string2"); 0093 QTest::addColumn<int>("res"); 0094 0095 QTest::newRow("null") << QString() << QString() << 0; 0096 QTest::newRow("null1") << QString() << QStringLiteral("B") << -1; 0097 QTest::newRow("null2") << QStringLiteral("B") << QString() << 1; 0098 QTest::newRow("test1") << QStringLiteral("BX932 .C53 1993") << QStringLiteral("BX2230.3") << -1; 0099 QTest::newRow("test2") << QStringLiteral("BX932 .C53 1993") << QStringLiteral("BX2380 .R67 2002") << -1; 0100 QTest::newRow("test3") << QStringLiteral("AE25 E3 2002") << QStringLiteral("AE5 E333 2003") << 1; 0101 } 0102 0103 void ComparisonTest::testDate() { 0104 QFETCH(QString, string1); 0105 QFETCH(QString, string2); 0106 QFETCH(int, res); 0107 0108 Tellico::ISODateComparison comp; 0109 0110 QCOMPARE(comp.compare(string1, string2), res); 0111 } 0112 0113 void ComparisonTest::testDate_data() { 0114 QTest::addColumn<QString>("string1"); 0115 QTest::addColumn<QString>("string2"); 0116 QTest::addColumn<int>("res"); 0117 0118 QTest::newRow("null") << QString() << QString() << 0; 0119 QTest::newRow("null1") << QString() << QStringLiteral("2001") << -1; 0120 QTest::newRow("null2") << QStringLiteral("2001") << QString() << 1; 0121 QTest::newRow("test1") << QStringLiteral("2001-9-8") << QStringLiteral("2001-10-12") << -1; 0122 QTest::newRow("test2") << QStringLiteral("1998") << QStringLiteral("2020-01-01") << -1; 0123 QTest::newRow("test3") << QStringLiteral("2008--") << QStringLiteral("2008-1-1") << 0; 0124 QTest::newRow("test5") << QStringLiteral("2008-2-2") << QStringLiteral("2008-02-02") << 0; 0125 // non-integers get converted to current date 0126 QTest::newRow("words") << QStringLiteral("all-by-myself") << QStringLiteral("---") << 0; 0127 // no longer in 2020, so all dashes converts to current year, after 2020 0128 QTest::newRow("words") << QStringLiteral("2020--") << QStringLiteral("---") << -1; 0129 } 0130 0131 void ComparisonTest::testTitle() { 0132 QFETCH(QString, string1); 0133 QFETCH(QString, string2); 0134 QFETCH(int, res); 0135 0136 Tellico::TitleComparison comp; 0137 0138 QCOMPARE(comp.compare(string1, string2), res); 0139 } 0140 0141 void ComparisonTest::testTitle_data() { 0142 QTest::addColumn<QString>("string1"); 0143 QTest::addColumn<QString>("string2"); 0144 QTest::addColumn<int>("res"); 0145 0146 QTest::newRow("null") << QString() << QString() << 0; 0147 QTest::newRow("null1") << QString() << QStringLiteral("The One") << -1; 0148 QTest::newRow("null2") << QStringLiteral("The One") << QString() << 1; 0149 // not that they're equal, but that "The One" should be sorted under "One" 0150 QTest::newRow("test3") << QStringLiteral("The One") << QStringLiteral("One, The") << -1; 0151 QTest::newRow("test4") << QStringLiteral("The One") << QStringLiteral("one, the") << -1; 0152 QTest::newRow("test5") << QStringLiteral("l'One") << QStringLiteral("one, the") << -1; 0153 QTest::newRow("test6") << QStringLiteral("l'One") << QStringLiteral("the one") << 0; 0154 QTest::newRow("test7") << QStringLiteral("All One") << QStringLiteral("the one") << -1; 0155 } 0156 0157 void ComparisonTest::testString() { 0158 QFETCH(QString, string1); 0159 QFETCH(QString, string2); 0160 QFETCH(int, res); 0161 0162 Tellico::StringComparison comp; 0163 0164 QCOMPARE(comp.compare(string1, string2), res); 0165 } 0166 0167 void ComparisonTest::testString_data() { 0168 QTest::addColumn<QString>("string1"); 0169 QTest::addColumn<QString>("string2"); 0170 QTest::addColumn<int>("res"); 0171 0172 QTest::newRow("null") << QString() << QString() << 0; 0173 QTest::newRow("null1") << QString() << QStringLiteral("string") << -1; 0174 QTest::newRow("null2") << QStringLiteral("string") << QString() << 1; 0175 QTest::newRow("test1") << QStringLiteral("string1") << QStringLiteral("string1") << 0; 0176 QTest::newRow("test2") << QStringLiteral("string1") << QStringLiteral("string2") << -1; 0177 } 0178 0179 void ComparisonTest::testBool() { 0180 QFETCH(QString, string1); 0181 QFETCH(QString, string2); 0182 QFETCH(int, res); 0183 0184 Tellico::BoolComparison comp; 0185 0186 QCOMPARE(comp.compare(string1, string2), res); 0187 } 0188 0189 void ComparisonTest::testBool_data() { 0190 QTest::addColumn<QString>("string1"); 0191 QTest::addColumn<QString>("string2"); 0192 QTest::addColumn<int>("res"); 0193 0194 QTest::newRow("null") << QString() << QString() << 0; 0195 QTest::newRow("null1") << QString() << QStringLiteral("true") << -1; 0196 QTest::newRow("null2") << QStringLiteral("true") << QString() << 1; 0197 QTest::newRow("truetrue") << QStringLiteral("true") << QStringLiteral("true") << 0; 0198 QTest::newRow("truefalse") << QStringLiteral("true") << QStringLiteral("false") << 1; 0199 QTest::newRow("falsetrue") << QStringLiteral("false") << QStringLiteral("true") << -1; 0200 } 0201 0202 void ComparisonTest::testChoiceField() { 0203 Tellico::Data::CollPtr coll(new Tellico::Data::Collection(true)); // add default field 0204 0205 QStringList allowed; 0206 allowed << QStringLiteral("choice2") << QStringLiteral("choice1"); 0207 QVERIFY(allowed.size() > 1); 0208 Tellico::Data::FieldPtr field(new Tellico::Data::Field(QStringLiteral("choice"), QStringLiteral("Choice"), allowed)); 0209 coll->addField(field); 0210 0211 Tellico::Data::EntryPtr entry1(new Tellico::Data::Entry(coll)); 0212 entry1->setField(field->name(), allowed.at(0)); 0213 Tellico::Data::EntryPtr entry2(new Tellico::Data::Entry(coll)); 0214 entry2->setField(field->name(), allowed.at(1)); 0215 0216 Tellico::FieldComparison* comp = Tellico::FieldComparison::create(field); 0217 // even though the second allowed value would sort first, it comes second in the list 0218 QCOMPARE(comp->compare(entry1, entry2), -1); 0219 } 0220 0221 void ComparisonTest::testImage() { 0222 Tellico::Data::CollPtr coll(new Tellico::Data::Collection(true)); 0223 Tellico::Data::FieldPtr field(new Tellico::Data::Field(QStringLiteral("image"), QStringLiteral("Image"), Tellico::Data::Field::Image)); 0224 coll->addField(field); 0225 0226 Tellico::Data::EntryPtr entry1(new Tellico::Data::Entry(coll)); 0227 Tellico::Data::EntryPtr entry2(new Tellico::Data::Entry(coll)); 0228 0229 Tellico::FieldComparison* comp = Tellico::FieldComparison::create(field); 0230 QCOMPARE(comp->compare(entry1, entry2), 0); 0231 0232 QUrl u1 = QUrl::fromLocalFile(QFINDTESTDATA("data/img1.jpg")); 0233 QString id1 = Tellico::ImageFactory::addImage(u1); 0234 QUrl u2 = QUrl::fromLocalFile(QFINDTESTDATA("../../icons/128-apps-tellico.png")); 0235 QString id2 = Tellico::ImageFactory::addImage(u2); 0236 0237 entry1->setField(QLatin1String("image"), id1); 0238 QCOMPARE(comp->compare(entry1, entry2), 1); 0239 entry2->setField(QLatin1String("image"), id1); 0240 QCOMPARE(comp->compare(entry1, entry2), 0); 0241 entry2->setField(QLatin1String("image"), id2); 0242 QVERIFY(comp->compare(entry1, entry2) < 0); 0243 }