File indexing completed on 2024-05-12 05:10:02

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 #undef QT_NO_CAST_FROM_ASCII
0026 
0027 #include "isbntest.h"
0028 
0029 #include "../utils/isbnvalidator.h"
0030 
0031 #include <QTest>
0032 
0033 QTEST_APPLESS_MAIN( IsbnTest )
0034 
0035 Q_DECLARE_METATYPE(QValidator::State)
0036 
0037 #define QL1(x) QStringLiteral(x)
0038 
0039 void IsbnTest::initTestCase() {
0040   qRegisterMetaType<QValidator::State>();
0041 }
0042 
0043 void IsbnTest::testFixup() {
0044   QFETCH(QString, string);
0045   QFETCH(QString, expectedIsbn);
0046 
0047   Tellico::ISBNValidator val;
0048   QString qs = string;
0049   val.fixup(qs);
0050   QCOMPARE(qs, expectedIsbn);
0051 }
0052 
0053 void IsbnTest::testFixup_data() {
0054   QTest::addColumn<QString>("string");
0055   QTest::addColumn<QString>("expectedIsbn");
0056 
0057   // garbage
0058   QTest::newRow("My name is robby") << QL1("My name is robby") << QString();
0059   QTest::newRow("http://www.abclinuxu.cz/clanky/show/63080") << QL1("http://www.abclinuxu.cz/clanky/show/63080") << QL1("6-3080");
0060 
0061   // initial checks
0062   QTest::newRow("0-446-60098-9") << QL1("0-446-60098-9") << QL1("0-446-60098-9");
0063   // check sum value
0064   QTest::newRow("0-446-60098") << QL1("0-446-60098") << QL1("0-446-60098-9");
0065 
0066   // check EAN-13
0067   QTest::newRow("9780940016750") << QL1("9780940016750") << QL1("978-0-940016-75-0");
0068   QTest::newRow("978-0940016750") << QL1("978-0940016750") << QL1("978-0-940016-75-0");
0069   QTest::newRow("978-0-940016-75-0") << QL1("978-0-940016-75-0") << QL1("978-0-940016-75-0");
0070   QTest::newRow("978286274486") << QL1("978286274486") << QL1("978-2-86274-486-5");
0071   QTest::newRow("9788186119130") << QL1("9788186119130") << QL1("978-81-86-11913-6");
0072   QTest::newRow("9788186119137") << QL1("9788186119137") << QL1("978-81-86-11913-6");
0073   QTest::newRow("97881-8611-9-13-0") << QL1("97881-8611-9-13-0") << QL1("978-81-86-11913-6");
0074   QTest::newRow("97881-8611-9-13-7") << QL1("97881-8611-9-13-7") << QL1("978-81-86-11913-6");
0075 
0076   // don't add checksum for EAN that start with 978 or 979 and are less than 13 in length
0077   QTest::newRow("978059600") << QL1("978059600") << QL1("978-059600");
0078   QTest::newRow("978-0596000") << QL1("978-0596000") << QL1("978-059600-0");
0079 
0080   // normal english-language hyphenation
0081   QTest::newRow("0") << QL1("0") << QL1("0");
0082   QTest::newRow("05") << QL1("05") << QL1("0-5");
0083   QTest::newRow("059") << QL1("059") << QL1("0-59");
0084   QTest::newRow("0596") << QL1("0596") << QL1("0-596");
0085   QTest::newRow("05960") << QL1("05960") << QL1("0-596-0");
0086   QTest::newRow("059600") << QL1("059600") << QL1("0-596-00");
0087   QTest::newRow("0596000") << QL1("0596000") << QL1("0-596-000");
0088   QTest::newRow("05960005") << QL1("05960005") << QL1("0-596-0005");
0089   // checksum gets added
0090   QTest::newRow("059600053") << QL1("059600053") << QL1("0-596-00053-7");
0091   QTest::newRow("0-596-00053") << QL1("0-596-00053") << QL1("0-596-00053-7");
0092   QTest::newRow("044660098") << QL1("044660098") << QL1("0-446-60098-9");
0093   QTest::newRow("0446600989") << QL1("0446600989") << QL1("0-446-60098-9");
0094 
0095   // check french hyphenation
0096   QTest::newRow("2862744867") << QL1("2862744867") << QL1("2-86274-486-7");
0097 
0098   // check german hyphenation
0099   QTest::newRow("3423071516") << QL1("3423071516") << QL1("3-423-07151-6");
0100 
0101   // check polish hyphenation
0102   QTest::newRow("978-83-7436-170-5") << QL1("9788374361705") << QL1("978-83-7436-170-5");
0103 
0104   // check keeping middle hyphens
0105   QTest::newRow("6-18611913-0") << QL1("6-18611913-0") << QL1("6-18611913-0");
0106   QTest::newRow("6-186119-13-0") << QL1("6-186119-13-0") << QL1("6-186119-13-0");
0107   QTest::newRow("6-18611-9-13-0") << QL1("6-18611-9-13-0") << QL1("6-18611-913-0");
0108 }
0109 
0110 void IsbnTest::testIsbn10() {
0111   QFETCH(QString, string);
0112   QFETCH(QString, expectedIsbn);
0113 
0114   QCOMPARE(Tellico::ISBNValidator::isbn10(string), expectedIsbn);
0115 }
0116 
0117 void IsbnTest::testIsbn10_data() {
0118   QTest::addColumn<QString>("string");
0119   QTest::addColumn<QString>("expectedIsbn");
0120 
0121   QTest::newRow("0-06-087298-5") << QL1("0-06-087298-5") << QL1("0-06-087298-5");
0122   QTest::newRow("978-0-06-087298-4") << QL1("978-0-06-087298-4") << QL1("0-06-087298-5");
0123 }
0124 
0125 void IsbnTest::testIsbn13() {
0126   QFETCH(QString, string);
0127   QFETCH(QString, expectedIsbn);
0128 
0129   QCOMPARE(Tellico::ISBNValidator::isbn13(string), expectedIsbn);
0130 }
0131 
0132 void IsbnTest::testIsbn13_data() {
0133   QTest::addColumn<QString>("string");
0134   QTest::addColumn<QString>("expectedIsbn");
0135 
0136   QTest::newRow("0-06-087298-5") << QL1("0-06-087298-5") << QL1("978-0-06-087298-4");
0137   QTest::newRow("9780-06-087298-4") << QL1("9780-06-087298-4") << QL1("978-0-06-087298-4");
0138 }
0139 
0140 void IsbnTest::testComparison() {
0141   QFETCH(QString, value1);
0142   QFETCH(QString, value2);
0143   QFETCH(bool, equal);
0144 
0145   Tellico::ISBNComparison comp;
0146   QCOMPARE(comp(value1, value2), equal);
0147 }
0148 
0149 void IsbnTest::testComparison_data() {
0150   QTest::addColumn<QString>("value1");
0151   QTest::addColumn<QString>("value2");
0152   QTest::addColumn<bool>("equal");
0153 
0154   QTest::newRow("0446600989, 0-446-60098-9") << QL1("0446600989") << QL1("0-446-60098-9") << true;
0155   QTest::newRow("0940016753, 9780940016750") << QL1("0940016753") << QL1("9780940016750") << true;
0156   QTest::newRow("9780940016750, 0940016753") << QL1("9780940016750") << QL1("0940016753") << true;
0157   QTest::newRow("9780940016750, 978-0-940016-75-0") << QL1("9780940016750") << QL1("978-0-940016-75-0") << true;
0158   QTest::newRow("3-351-005296, 3351005296") << QL1("3-351-005296") <<  QL1("3351005296") << true;
0159   QTest::newRow("3-351-00529-6, 3351005296") << QL1("3-351-00529-6") <<  QL1("3351005296") << true;
0160 }
0161 
0162 void IsbnTest::testListDifference() {
0163   QFETCH(QStringList, list1);
0164   QFETCH(QStringList, list2);
0165   QFETCH(QStringList, result);
0166 
0167   QCOMPARE(Tellico::ISBNValidator::listDifference(list1, list2), result);
0168 }
0169 
0170 void IsbnTest::testListDifference_data() {
0171   QTest::addColumn<QStringList>("list1");
0172   QTest::addColumn<QStringList>("list2");
0173   QTest::addColumn<QStringList>("result");
0174 
0175   QStringList list1;
0176   list1 << QStringLiteral("0940016753") << QStringLiteral("9780940016750");
0177   QStringList list2;
0178 
0179   // comparing to empty list should return the first list
0180   QTest::newRow("list1") << list1 << list2 << list1;
0181 
0182   // comparing to a value that matches everything in the list should return empty list
0183   list2 << QStringLiteral("0-940016-75-0");
0184   QTest::newRow("list2") << list1 << list2 << QStringList();
0185 }
0186 
0187 void IsbnTest::testState() {
0188   QFETCH(QValidator::State, expectedState);
0189   QFETCH(QString, value);
0190   QFETCH(bool, changedValue);
0191 
0192   int pos = value.length() - 1;
0193   const QString original = value;
0194 
0195   Tellico::ISBNValidator val;
0196   QValidator::State state = val.validate(value, pos);
0197   if(!changedValue) {
0198     QCOMPARE(value, original);
0199   }
0200   QCOMPARE(state, expectedState);
0201 }
0202 
0203 void IsbnTest::testState_data() {
0204   QTest::addColumn<QValidator::State>("expectedState");
0205   QTest::addColumn<QString>("value");
0206   QTest::addColumn<bool>("changedValue");
0207 
0208   QTest::newRow("0") << QValidator::Intermediate << QL1("0") << false;
0209   QTest::newRow("0-") << QValidator::Intermediate << QL1("0-") << false;
0210   QTest::newRow("0-3") << QValidator::Intermediate << QL1("0-3") << false;
0211   QTest::newRow("0-32") << QValidator::Intermediate << QL1("0-32") << false;
0212   QTest::newRow("0-321") << QValidator::Intermediate << QL1("0-321") << false;
0213   QTest::newRow("0-321-") << QValidator::Intermediate << QL1("0-321-") << false;
0214   QTest::newRow("0-321-1") << QValidator::Intermediate << QL1("0-321-1") << false;
0215   QTest::newRow("0-321-11") << QValidator::Intermediate << QL1("0-321-11") << false;
0216   QTest::newRow("0-321-113") << QValidator::Intermediate << QL1("0-321-113") << false;
0217   QTest::newRow("0-321-1135") << QValidator::Intermediate << QL1("0-321-1135") << false;
0218   // checksum is added
0219   QTest::newRow("0-321-11358") << QValidator::Acceptable << QL1("0-321-11358") << true;
0220   QTest::newRow("0-321-11358-") << QValidator::Acceptable << QL1("0-321-11358-") << true;
0221   QTest::newRow("0-321-11358-6") << QValidator::Acceptable << QL1("0-321-11358-6") << false;
0222 
0223   QTest::newRow("0") << QValidator::Intermediate << QL1("0") << true;
0224   QTest::newRow("03") << QValidator::Intermediate << QL1("03") << true;
0225   QTest::newRow("032") << QValidator::Intermediate << QL1("032") << true;
0226   QTest::newRow("0321") << QValidator::Intermediate << QL1("0321") << true;
0227   QTest::newRow("03211") << QValidator::Intermediate << QL1("03211") << true;
0228   QTest::newRow("032111") << QValidator::Intermediate << QL1("032111") << true;
0229   QTest::newRow("0321113") << QValidator::Intermediate << QL1("0321113") << true;
0230   QTest::newRow("03211135") << QValidator::Intermediate << QL1("03211135") << true;
0231   // checksum is added
0232   QTest::newRow("032111358") << QValidator::Acceptable << QL1("032111358") << true;
0233   QTest::newRow("0321113586") << QValidator::Acceptable << QL1("0321113586") << true;
0234 
0235   // considered 10-digit ISBNs
0236   QTest::newRow("9") << QValidator::Intermediate << QL1("9") << false;
0237   QTest::newRow("97") << QValidator::Intermediate << QL1("97") << false;
0238   QTest::newRow("978") << QValidator::Intermediate << QL1("978") << false;
0239   QTest::newRow("978-") << QValidator::Intermediate << QL1("978-") << false;
0240   QTest::newRow("978-0") << QValidator::Intermediate << QL1("978-0") << false;
0241   QTest::newRow("978-0-") << QValidator::Intermediate << QL1("978-0-") << false;
0242   QTest::newRow("978-0-4") << QValidator::Intermediate << QL1("978-0-4") << false;
0243   QTest::newRow("978-0-47") << QValidator::Intermediate << QL1("978-0-47") << false;
0244   QTest::newRow("978-0-470") << QValidator::Intermediate << QL1("978-0-470") << false;
0245   QTest::newRow("978-0-470-") << QValidator::Intermediate << QL1("978-0-470-") << false;
0246   QTest::newRow("978-0-4701") << QValidator::Intermediate << QL1("978-0-4701") << false;
0247   QTest::newRow("978-0-470-1") << QValidator::Intermediate << QL1("978-0-470-1") << true;
0248   QTest::newRow("978-0-47014") << QValidator::Intermediate << QL1("978-0-47014") << false;
0249   QTest::newRow("978-0-470-14") << QValidator::Intermediate << QL1("978-0-470-14") << true;
0250   QTest::newRow("978-0-470-147") << QValidator::Intermediate << QL1("978-0-470-147") << true;
0251   QTest::newRow("978-0-470147") << QValidator::Intermediate << QL1("978-0-470147") << true;
0252   QTest::newRow("978-0-47014-7") << QValidator::Intermediate << QL1("978-0-47014-7") << false;
0253   // now considered 13-digit ISBN
0254   QTest::newRow("978-0-470-1476") << QValidator::Intermediate << QL1("978-0-470-1476") << false;
0255   // checksum is added
0256   QTest::newRow("978-0-470-14762") << QValidator::Acceptable << QL1("978-0-470-14762") << true;
0257   QTest::newRow("978-0-470-14762-") << QValidator::Acceptable << QL1("978-0-470-14762-") << true;
0258   QTest::newRow("978-0-470-14762-7") << QValidator::Acceptable << QL1("978-0-470-14762-7") << false;
0259 }