File indexing completed on 2025-01-05 03:58:01

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2009-06-09
0007  * Description : a test for the AdvancedRename utility
0008  *
0009  * SPDX-FileCopyrightText: 2009-2011 by Andi Clemens <andi dot clemens at gmail dot com>
0010  * SPDX-FileCopyrightText: 2015      by Mohamed_Anwer <m_dot_anwer at gmx dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "advancedrename_utest.h"
0017 
0018 // C++ includes
0019 
0020 #include <algorithm>
0021 
0022 // Qt includes
0023 
0024 #include <QFileInfo>
0025 #include <QTest>
0026 #include <QDir>
0027 
0028 // Local includes
0029 
0030 #include "digikam_debug.h"
0031 #include "dtestdatadir.h"
0032 
0033 using namespace Digikam;
0034 
0035 QTEST_MAIN(AdvancedRenameTest)
0036 
0037 QString createFilePath(const QString& file)
0038 {
0039     const QString filePath = DTestDataDir::TestData(QString::fromUtf8("core/tests/advancerename"))
0040                                    .root().path() + QLatin1Char('/') + file;
0041     qCDebug(DIGIKAM_TESTS_LOG) << "Test Data File:" << filePath;
0042 
0043     return filePath;
0044 }
0045 
0046 Q_DECLARE_METATYPE(QList<int>)
0047 
0048 const QString fileName  = QLatin1String("advancedrename_testimage.jpg");
0049 const QString fileName2 = QLatin1String("advancedrename_testimage2.jpg");
0050 const QString fileName3 = QLatin1String("001a.jpg");
0051 const QString fileName4 = QLatin1String("test.png");
0052 const QString fileName5 = QLatin1String("myfile.jpg");
0053 const QString fileName6 = QLatin1String("my_file.jpg");
0054 const QString fileName7 = QLatin1String("holiday_spain_2011_img001.jpg");
0055 const QString fileName8 = QLatin1String("my images.jpg");
0056 const QString fileName9 = QLatin1String("holiday_spain_2011_001img.jpg");
0057 
0058 const QString filePath  = createFilePath(fileName);
0059 const QString filePath2 = createFilePath(fileName2);
0060 const QString filePath3 = createFilePath(fileName3);
0061 const QString filePath4 = createFilePath(fileName4);
0062 const QString filePath5 = createFilePath(fileName5);
0063 const QString filePath6 = createFilePath(fileName6);
0064 const QString filePath7 = createFilePath(fileName7);
0065 const QString filePath8 = createFilePath(fileName8);
0066 const QString filePath9 = createFilePath(fileName9);
0067 
0068 void AdvancedRenameTest::testFileNameToken()
0069 {
0070     QList<ParseSettings> files;
0071     ParseSettings ps;
0072     ps.fileUrl     = QUrl::fromLocalFile(filePath);
0073     files << ps;
0074     AdvancedRenameManager manager(files);
0075     manager.parseFiles(QLatin1String("[file]"));
0076 
0077     QString parsed = manager.newName(filePath);
0078     QCOMPARE(parsed, fileName);
0079 }
0080 
0081 void AdvancedRenameTest::testFileExtensionToken_data()
0082 {
0083     QTest::addColumn<QString>("parseString");
0084     QTest::addColumn<QString>("result");
0085 
0086     QTest::newRow("[ext]")
0087                << "[ext]"
0088                << "jpg.jpg";
0089 
0090     QTest::newRow(".[ext]")
0091                << ".[ext]"
0092                << ".jpg";
0093 
0094     QTest::newRow("[ext].[ext]")
0095                << "[ext].[ext]"
0096                << "jpg.jpg";
0097 
0098     QTest::newRow("[ext].[ext]{upper}")
0099                << "[ext].[ext]{upper}"
0100                << "jpg.JPG";
0101 
0102     QTest::newRow("[ext]{upper}.[ext]{upper}")
0103                << "[ext]{upper}.[ext]{upper}"
0104                << "JPG.JPG";
0105 
0106     QTest::newRow("[ext]_lala_####")
0107                << "[ext]_lala_####"
0108                << "jpg_lala_0001.jpg";
0109 
0110     QTest::newRow("[ext]_lala_####[ext]")
0111                << "[ext]_lala_####[ext]"
0112                << "jpg_lala_0001jpg.jpg";
0113 
0114     QTest::newRow("[ext]_lala_####.[ext]")
0115                << "[ext]_lala_####.[ext]"
0116                << "jpg_lala_0001.jpg";
0117 
0118     QTest::newRow("[ext]_lala_####.[ext]{upper}")
0119                << "[ext]_lala_####.[ext]{upper}"
0120                << "jpg_lala_0001.JPG";
0121 
0122     QTest::newRow("[ext]_lala_####[ext]{upper}")
0123                << "[ext]_lala_####[ext]{upper}"
0124                << "jpg_lala_0001JPG.jpg";
0125 }
0126 
0127 void AdvancedRenameTest::testFileExtensionToken()
0128 {
0129     QFETCH(QString,   parseString);
0130     QFETCH(QString,   result);
0131 
0132     QList<ParseSettings> files;
0133     ParseSettings ps;
0134     ps.fileUrl     = QUrl::fromLocalFile(filePath);
0135     files << ps;
0136     AdvancedRenameManager manager(files);
0137     manager.parseFiles(parseString);
0138 
0139     QString parsed = manager.newName(filePath);
0140     QCOMPARE(parsed, result);
0141 }
0142 
0143 void AdvancedRenameTest::testFileOwnerToken()
0144 {
0145     QList<ParseSettings> files;
0146     ParseSettings ps;
0147     ps.fileUrl       = QUrl::fromLocalFile(filePath4);
0148     files << ps;
0149     AdvancedRenameManager manager(files);
0150     manager.parseFiles(QLatin1String("[user]"));
0151 
0152     QFileInfo fi(ps.fileUrl.toLocalFile());
0153     QVERIFY(fi.exists());
0154     QVERIFY(fi.isReadable());
0155 
0156     QString userName = fi.owner();
0157     QVERIFY(!userName.isEmpty());
0158 
0159     QString result   = userName + QLatin1String(".png");
0160     QString parsed   = manager.newName(filePath4);
0161 
0162     QCOMPARE(parsed, result);
0163 }
0164 
0165 void AdvancedRenameTest::testFileGroupToken()
0166 {
0167     QList<ParseSettings> files;
0168     ParseSettings ps;
0169     ps.fileUrl        = QUrl::fromLocalFile(filePath4);
0170     files << ps;
0171     AdvancedRenameManager manager(files);
0172     manager.parseFiles(QLatin1String("[group]"));
0173 
0174     QFileInfo fi(ps.fileUrl.toLocalFile());
0175     QVERIFY(fi.exists());
0176     QVERIFY(fi.isReadable());
0177 
0178     QString groupName = fi.group();
0179     QVERIFY(!groupName.isEmpty());
0180 
0181     QString result    = groupName + QLatin1String(".png");
0182     QString parsed    = manager.newName(filePath4);
0183 
0184     QCOMPARE(parsed, result);
0185 }
0186 
0187 void AdvancedRenameTest::testDirectoryNameToken_data()
0188 {
0189     QTest::addColumn<QString>("parseString");
0190     QTest::addColumn<QString>("result");
0191 
0192     // The main directory of digiKam can have different names, depending on how the
0193     // user named it. Therefore we have to detect the name here.
0194 
0195     QDir dir = DTestDataDir::TestData(QString::fromUtf8("core/tests/advancerename")).root();
0196     qCDebug(DIGIKAM_TESTS_LOG) << "Test Data Dir:" << dir;
0197 
0198     dir.cdUp();
0199     const QString dir2up = dir.path();
0200     QDir dir2            = dir;
0201     dir2.cdUp();
0202     QString digikamDir   = dir2up.right(dir2up.size() - dir2.path().size() -1) +
0203                            QLatin1Char('/');
0204     qCDebug(DIGIKAM_TESTS_LOG) << "digikamDir: " << digikamDir;
0205 
0206     digikamDir.chop(1);
0207 
0208     QTest::newRow("[dir]")
0209             << "[dir]"
0210             << "advancerename.jpg";
0211 
0212     QTest::newRow("[dir.]")
0213             << "[dir.]"
0214             << QString::fromUtf8("%1.jpg").arg(digikamDir).toLatin1().constData();
0215 
0216     QTest::newRow("[dir.]_[dir]")
0217             << "[dir.]_[dir]"
0218             << QString::fromUtf8("%1_advancerename.jpg").arg(digikamDir).toLatin1().constData();
0219 
0220     QTest::newRow("[dir......................................................................]")
0221             << "[dir......................................................................]"
0222             << fileName;
0223 }
0224 
0225 void AdvancedRenameTest::testDirectoryNameToken()
0226 {
0227     QFETCH(QString,   parseString);
0228     QFETCH(QString,   result);
0229 
0230     QList<ParseSettings> files;
0231     ParseSettings ps;
0232     ps.fileUrl     = QUrl::fromLocalFile(filePath);
0233     files << ps;
0234     AdvancedRenameManager manager(files);
0235     manager.parseFiles(parseString);
0236 
0237     QString parsed = manager.newName(filePath);
0238     QCOMPARE(parsed, result);
0239 }
0240 
0241 void AdvancedRenameTest::testNumberToken_data()
0242 {
0243     QTest::addColumn<QString>("parseString");
0244     QTest::addColumn<QString>("result");
0245 
0246     QTest::newRow("#")
0247             << "#"
0248             << "1.jpg";
0249 
0250     QTest::newRow("####[2,3]")
0251             << "####[2,3]"
0252             << "0002.jpg";
0253 
0254     QTest::newRow("####[2,3]_bla_## ###")
0255             << "####[2,3]_bla_## ###"
0256             << "0002_bla_01 001.jpg";
0257 
0258     QTest::newRow("####[2,3]_bla_## ###")
0259             << "####[2,3]_bla_## ###"
0260             << "0002_bla_01 001.jpg";
0261 
0262     QTest::newRow("####[2,3]_bla_## ###")
0263             << "####[2,3]_bla_## ###"
0264             << "0002_bla_01 001.jpg";
0265 
0266     QTest::newRow("###[100]_bla")
0267             << "###[100]_bla"
0268             << "100_bla.jpg";
0269 
0270     QTest::newRow("###[e,1,100]_bla")
0271             << "###[e,1,100]_bla"
0272             << "001_bla.jpg";
0273 }
0274 
0275 void AdvancedRenameTest::testNumberToken()
0276 {
0277     QFETCH(QString,   parseString);
0278     QFETCH(QString,   result);
0279 
0280     QList<ParseSettings> files;
0281     ParseSettings ps;
0282     ps.fileUrl     = QUrl::fromLocalFile(filePath);
0283     files << ps;
0284     AdvancedRenameManager manager(files);
0285     manager.parseFiles(parseString);
0286 
0287     QString parsed = manager.newName(filePath);
0288     QCOMPARE(parsed, result);
0289 }
0290 
0291 void AdvancedRenameTest::testFirstLetterOfEachWordUppercaseModifier_data()
0292 {
0293     QTest::addColumn<QString>("parseString");
0294     QTest::addColumn<QString>("file");
0295     QTest::addColumn<QString>("result");
0296 
0297     QTest::newRow("myfile")
0298             << "[file]{firstupper}"
0299             << filePath5
0300             << "Myfile.jpg";
0301 
0302     QTest::newRow("my_file")
0303             << "[file]{firstupper}"
0304             << filePath6
0305             << "My_File.jpg";
0306 
0307     QTest::newRow("holiday_spain_2011_img001")
0308             << "[file]{firstupper}"
0309             << filePath7
0310             << "Holiday_Spain_2011_Img001.jpg";
0311 
0312     QTest::newRow("holiday_spain_2011_001img")
0313             << "[file]{firstupper}"
0314             << filePath9
0315             << "Holiday_Spain_2011_001Img.jpg";
0316 
0317     QTest::newRow("001a")
0318             << "[file]{firstupper}"
0319             << filePath3
0320             << "001A.jpg";
0321 
0322     QTest::newRow("my images")
0323             << "[file]{firstupper}"
0324             << filePath8
0325             << "My Images.jpg";
0326 
0327     QTest::newRow("<empty>")
0328             << "[file]{firstupper}"
0329             << ""
0330             << "";
0331 
0332     QTest::newRow(fileName.toLatin1().constData())
0333             << "[file]{firstupper}"
0334             << filePath
0335             << "Advancedrename_Testimage.jpg";
0336 }
0337 
0338 void AdvancedRenameTest::testFirstLetterOfEachWordUppercaseModifier()
0339 {
0340     QFETCH(QString, parseString);
0341     QFETCH(QString, file);
0342     QFETCH(QString, result);
0343 
0344     QList<ParseSettings> files;
0345     ParseSettings ps;
0346     ps.fileUrl     = QUrl::fromLocalFile(file);
0347     files << ps;
0348     AdvancedRenameManager manager(files);
0349     manager.parseFiles(parseString);
0350 
0351     QString parsed = manager.newName(file);
0352     QCOMPARE(parsed, result);
0353 }
0354 
0355 void AdvancedRenameTest::testChainedModifiers_data()
0356 {
0357     QTest::addColumn<QString>("parseString");
0358     QTest::addColumn<QString>("result");
0359 
0360     QTest::newRow("[file]*{upper}")
0361             << "[file]{firstupper}{upper}"
0362             << "ADVANCEDRENAME_TESTIMAGE.jpg";
0363 
0364     QTest::newRow("[file]{range:3,}*")
0365             << "[file]{range:3,}{firstupper}"
0366             << "Vancedrename_Testimage.jpg";
0367 
0368     QTest::newRow("[file]{range:3,}{replace:\"name\",\"age\"}{firstupper}")
0369             << "[file]{range:3,}{replace:\"name\",\"age\"}{firstupper}"
0370             << "Vancedreage_Testimage.jpg";
0371 }
0372 
0373 void AdvancedRenameTest::testChainedModifiers()
0374 {
0375     QFETCH(QString,   parseString);
0376     QFETCH(QString,   result);
0377 
0378     QList<ParseSettings> files;
0379     ParseSettings ps;
0380     ps.fileUrl     = QUrl::fromLocalFile(filePath);
0381     files << ps;
0382     AdvancedRenameManager manager(files);
0383     manager.parseFiles(parseString);
0384 
0385     QString parsed = manager.newName(filePath);
0386     QCOMPARE(parsed, result);
0387 }
0388 
0389 void AdvancedRenameTest::testUppercaseModifier()
0390 {
0391     QList<ParseSettings> files;
0392     ParseSettings ps;
0393     ps.fileUrl     = QUrl::fromLocalFile(filePath);
0394     files << ps;
0395     AdvancedRenameManager manager(files);
0396     manager.parseFiles(QLatin1String("[file]{upper}"));
0397 
0398     QString parsed = manager.newName(filePath);
0399     QFileInfo fi(filePath);
0400     QString tmp    = fi.baseName().toUpper() + QLatin1Char('.') + fi.suffix();
0401     QCOMPARE(parsed, tmp);
0402 }
0403 
0404 void AdvancedRenameTest::testUniqueModifier()
0405 {
0406     QList<ParseSettings> files;
0407     ParseSettings ps;
0408     ps.fileUrl = QUrl::fromLocalFile(filePath);
0409     files << ps;
0410     ps.fileUrl = QUrl::fromLocalFile(filePath2);
0411     files << ps;
0412     AdvancedRenameManager manager(files);
0413     manager.parseFiles(QLatin1String("[file]_[dir]{unique}"));
0414 
0415     QString parsed  = manager.newName(filePath);
0416     QString parsed2 = manager.newName(filePath2);
0417 
0418     // parse again, unique tokens should not be modified
0419 
0420     manager.parseFiles(QLatin1String("[file]_[dir]{unique}"));
0421 
0422     QString parsed3 = manager.newName(filePath);
0423     QString parsed4 = manager.newName(filePath2);
0424 
0425     QCOMPARE(parsed,  parsed3);
0426     QCOMPARE(parsed2, parsed4);
0427 }
0428 
0429 void AdvancedRenameTest::addFiles_should_only_add_files()
0430 {
0431     QList<ParseSettings> files;
0432     ParseSettings ps;
0433     ps.fileUrl = QUrl::fromLocalFile(filePath);
0434     files << ps;
0435     ps.fileUrl = QUrl::fromLocalFile(filePath2);
0436     files << ps;
0437     ps.fileUrl = QUrl::fromLocalFile(filePath3);
0438     files << ps;
0439     ps.fileUrl = QUrl::fromLocalFile(filePath4);
0440     files << ps;
0441     ps.fileUrl = QUrl::fromLocalFile(filePath5);
0442     files << ps;
0443     AdvancedRenameManager manager(files);
0444     QCOMPARE(manager.fileList().count(), 5);
0445 
0446     QList<ParseSettings> additionalFiles;
0447     ps.fileUrl = QUrl::fromLocalFile(filePath6);
0448     additionalFiles << ps;
0449     ps.fileUrl = QUrl::fromLocalFile(filePath7);
0450     additionalFiles << ps;
0451     ps.fileUrl = QUrl::fromLocalFile(filePath8);
0452     additionalFiles << ps;
0453     ps.fileUrl = QUrl::fromLocalFile(filePath9);
0454     additionalFiles << ps;
0455     manager.addFiles(additionalFiles);
0456     QCOMPARE(manager.fileList().count(), 9);
0457 }
0458 
0459 void AdvancedRenameTest::addFiles_should_only_add_files2()
0460 {
0461     QList<ParseSettings> files;
0462     ParseSettings ps;
0463     ps.fileUrl = QUrl::fromLocalFile(filePath);
0464     files << ps;
0465     ps.fileUrl = QUrl::fromLocalFile(filePath2);
0466     files << ps;
0467     ps.fileUrl = QUrl::fromLocalFile(filePath3);
0468     files << ps;
0469     ps.fileUrl = QUrl::fromLocalFile(filePath4);
0470     files << ps;
0471     ps.fileUrl = QUrl::fromLocalFile(filePath5);
0472     files << ps;
0473     AdvancedRenameManager manager;
0474     manager.addFiles(files);
0475     QCOMPARE(manager.fileList().count(), 5);
0476 }
0477 
0478 void AdvancedRenameTest::reset_removes_everything()
0479 {
0480     QList<ParseSettings> files;
0481     ParseSettings ps;
0482     ps.fileUrl = QUrl::fromLocalFile(filePath);
0483     files << ps;
0484     ps.fileUrl = QUrl::fromLocalFile(filePath2);
0485     files << ps;
0486     ps.fileUrl = QUrl::fromLocalFile(filePath3);
0487     files << ps;
0488     ps.fileUrl = QUrl::fromLocalFile(filePath4);
0489     files << ps;
0490     ps.fileUrl = QUrl::fromLocalFile(filePath5);
0491     files << ps;
0492     AdvancedRenameManager manager;
0493     manager.addFiles(files);
0494     QCOMPARE(manager.fileList().count(), 5);
0495 
0496     manager.reset();
0497     QCOMPARE(manager.fileList().count(), 0);
0498     QCOMPARE(manager.newFileList().count(), 0);
0499 }
0500 
0501 void AdvancedRenameTest::parseFiles_does_nothing_without_assigned_widget()
0502 {
0503     QList<ParseSettings> files;
0504     ParseSettings ps;
0505     ps.fileUrl = QUrl::fromLocalFile(filePath);
0506     files << ps;
0507     ps.fileUrl = QUrl::fromLocalFile(filePath2);
0508     files << ps;
0509     ps.fileUrl = QUrl::fromLocalFile(filePath3);
0510     files << ps;
0511     AdvancedRenameManager manager(files);
0512     manager.parseFiles();
0513 
0514     QCOMPARE(manager.newName(filePath), filePath);
0515     QCOMPARE(manager.newName(filePath2), filePath2);
0516     QCOMPARE(manager.newName(filePath3), filePath3);
0517 }
0518 
0519 void AdvancedRenameTest::setStartIndex_invalid_index()
0520 {
0521     QList<ParseSettings> files;
0522     ParseSettings ps;
0523     ps.fileUrl = QUrl::fromLocalFile(filePath);
0524     files << ps;
0525     AdvancedRenameManager manager(files);
0526     manager.setStartIndex(-1);
0527     manager.parseFiles(QLatin1String("####"));
0528 
0529     QCOMPARE(manager.newName(filePath), QLatin1String("0001.jpg"));
0530 }
0531 
0532 void AdvancedRenameTest::setStartIndex_sequencenumber_no_custom_start()
0533 {
0534     QList<ParseSettings> files;
0535     ParseSettings ps;
0536     ps.fileUrl = QUrl::fromLocalFile(filePath);
0537     files << ps;
0538 
0539     QString parseString(QLatin1String("####"));
0540 
0541     AdvancedRenameManager manager(files);
0542     manager.parseFiles(parseString);
0543 
0544     QCOMPARE(manager.newName(filePath), QLatin1String("0001.jpg"));
0545 
0546     manager.setStartIndex(12);
0547     manager.parseFiles(parseString);
0548     QCOMPARE(manager.newName(filePath), QLatin1String("0012.jpg"));
0549 
0550     manager.setStartIndex(-1000);
0551     manager.parseFiles(parseString);
0552     QCOMPARE(manager.newName(filePath), QLatin1String("0001.jpg"));
0553 }
0554 
0555 void AdvancedRenameTest::setStartIndex_sequencenumber_with_custom_start()
0556 {
0557     QList<ParseSettings> files;
0558     ParseSettings ps;
0559     ps.fileUrl = QUrl::fromLocalFile(filePath);
0560     files << ps;
0561 
0562     QString parseString(QLatin1String("####[666]"));
0563 
0564     AdvancedRenameManager manager(files);
0565     manager.parseFiles(parseString);
0566 
0567     QCOMPARE(manager.newName(filePath), QLatin1String("0666.jpg"));
0568 
0569     manager.setStartIndex(12);
0570     manager.parseFiles(parseString);
0571     QCOMPARE(manager.newName(filePath), QLatin1String("0666.jpg"));
0572 
0573     manager.setStartIndex(-1000);
0574     manager.parseFiles(parseString);
0575     QCOMPARE(manager.newName(filePath), QLatin1String("0666.jpg"));
0576 }
0577 
0578 void AdvancedRenameTest::sequencenumber_tests_data()
0579 {
0580     QStringList files;
0581     files << filePath << filePath2 << filePath3;
0582 
0583     QTest::addColumn<QString>("parseString");
0584     QTest::addColumn<QStringList>("files");
0585     QTest::addColumn<QStringList>("results");
0586 
0587     QTest::newRow("####")
0588             << "####"
0589             << files
0590             << (QStringList() << QLatin1String("0001.jpg") << QLatin1String("0002.jpg") << QLatin1String("0003.jpg"));
0591 
0592     QTest::newRow("###[-2]")
0593             << "###[-2]"
0594             << files
0595             << (QStringList() << QLatin1String("001.jpg") << QLatin1String("002.jpg") << QLatin1String("003.jpg"));
0596 
0597     QTest::newRow("###[2]")
0598             << "###[2]"
0599             << files
0600             << (QStringList() << QLatin1String("002.jpg") << QLatin1String("003.jpg") << QLatin1String("004.jpg"));
0601 
0602     QTest::newRow("##[3,3]")
0603             << "##[3,3]"
0604             << files
0605             << (QStringList() << QLatin1String("03.jpg") << QLatin1String("06.jpg") << QLatin1String("09.jpg"));
0606 
0607     QTest::newRow("#[4,4]")
0608             << "#[4,4]"
0609             << files
0610             << (QStringList() << QLatin1String("4.jpg") << QLatin1String("8.jpg") << QLatin1String("12.jpg"));
0611 
0612     QTest::newRow("#[4,-4]")
0613             << "#[4,-4]"
0614             << files
0615             << (QStringList() << QLatin1String("4.jpg") << QLatin1String("5.jpg") << QLatin1String("6.jpg"));
0616 }
0617 
0618 void AdvancedRenameTest::sequencenumber_tests()
0619 {
0620     QFETCH(QString,       parseString);
0621     QFETCH(QStringList,   files);
0622     QFETCH(QStringList,   results);
0623 
0624     ParseSettings ps;
0625 
0626     QList<ParseSettings> files2;
0627 
0628     Q_FOREACH (const QString& file, files)
0629     {
0630         ps.fileUrl = QUrl::fromLocalFile(file);
0631         files2 << ps;
0632     }
0633 
0634     AdvancedRenameManager manager(files2);
0635     manager.parseFiles(parseString);
0636 
0637     QVERIFY(files.count() == results.count());
0638 
0639     for (int i = 0 ; i < files.count() ; ++i)
0640     {
0641         QCOMPARE(manager.newName(files.at(i)), results.at(i));
0642     }
0643 }
0644 
0645 void AdvancedRenameTest::newFileList_tests_data()
0646 {
0647     QStringList files;
0648     files << filePath << filePath2 << filePath3;
0649 
0650     QTest::addColumn<QString>("parseString");
0651     QTest::addColumn<QStringList>("files");
0652     QTest::addColumn<QStringList>("results");
0653 
0654     QTest::newRow("####")
0655             << "####"
0656             << files
0657             << (QStringList() << QLatin1String("0001.jpg") << QLatin1String("0002.jpg") << QLatin1String("0003.jpg"));
0658 
0659     QTest::newRow("###[-2]")
0660             << "###[-2]"
0661             << files
0662             << (QStringList() << QLatin1String("001.jpg") << QLatin1String("002.jpg") << QLatin1String("003.jpg"));
0663 
0664     QTest::newRow("###[2]")
0665             << "###[2]"
0666             << files
0667             << (QStringList() << QLatin1String("002.jpg") << QLatin1String("003.jpg") << QLatin1String("004.jpg"));
0668 
0669     QTest::newRow("##[3,3]")
0670             << "##[3,3]"
0671             << files
0672             << (QStringList() << QLatin1String("03.jpg") << QLatin1String("06.jpg") << QLatin1String("09.jpg"));
0673 
0674     QTest::newRow("#[4,4]")
0675             << "#[4,4]"
0676             << files
0677             << (QStringList() << QLatin1String("4.jpg") << QLatin1String("8.jpg") << QLatin1String("12.jpg"));
0678 
0679     QTest::newRow("#[4,-4]")
0680             << "#[4,-4]"
0681             << files
0682             << (QStringList() << QLatin1String("4.jpg") << QLatin1String("5.jpg") << QLatin1String("6.jpg"));
0683 }
0684 
0685 void AdvancedRenameTest::newFileList_tests()
0686 {
0687     QFETCH(QString,       parseString);
0688     QFETCH(QStringList,   files);
0689     QFETCH(QStringList,   results);
0690 
0691     ParseSettings ps;
0692 
0693     QList<ParseSettings> files2;
0694 
0695     Q_FOREACH (const QString& file, files)
0696     {
0697         ps.fileUrl = QUrl::fromLocalFile(file);
0698         files2 << ps;
0699     }
0700 
0701     AdvancedRenameManager manager(files2);
0702     manager.parseFiles(parseString);
0703 
0704     QVERIFY(files.count() == results.count());
0705 
0706     QMap<QString, QString> newFileList = manager.newFileList();
0707 
0708     for (int i = 0 ; i < files.count() ; ++i)
0709     {
0710         QCOMPARE(newFileList.value(files.at(i)), results.at(i));
0711     }
0712 }
0713 
0714 void AdvancedRenameTest::indexOfFile_sorting_data()
0715 {
0716     QStringList files;
0717     files << filePath << filePath2 << filePath3;
0718 
0719     QTest::addColumn<int>("sortAction");
0720     QTest::addColumn<int>("sortDirection");
0721     QTest::addColumn<QStringList>("files");
0722     QTest::addColumn<QList<int> >("results");
0723 
0724     QTest::newRow("custom_asc")
0725             << static_cast<int>(AdvancedRenameManager::SortCustom)
0726             << static_cast<int>(AdvancedRenameManager::SortAscending)
0727             << files
0728             << (QList<int>() << 1 << 2 << 3);
0729 
0730     QTest::newRow("custom_desc")
0731             << static_cast<int>(AdvancedRenameManager::SortCustom)
0732             << static_cast<int>(AdvancedRenameManager::SortDescending)
0733             << files
0734             << (QList<int>() << 1 << 2 << 3);
0735 
0736     QTest::newRow("name_asc")
0737             << static_cast<int>(AdvancedRenameManager::SortName)
0738             << static_cast<int>(AdvancedRenameManager::SortAscending)
0739             << files
0740             << (QList<int>() << 2 << 3 << 1);
0741 
0742     QTest::newRow("name_desc")
0743             << static_cast<int>(AdvancedRenameManager::SortName)
0744             << static_cast<int>(AdvancedRenameManager::SortDescending)
0745             << files
0746             << (QList<int>() << 2 << 1 << 3);
0747 }
0748 
0749 void AdvancedRenameTest::indexOfFile_sorting()
0750 {
0751     QFETCH(int,           sortAction);
0752     QFETCH(int,           sortDirection);
0753     QFETCH(QStringList,   files);
0754     QFETCH(QList<int>,    results);
0755 
0756     ParseSettings ps;
0757 
0758     QList<ParseSettings> files2;
0759 
0760     Q_FOREACH (const QString& file, files)
0761     {
0762         ps.fileUrl = QUrl::fromLocalFile(file);
0763         files2 << ps;
0764     }
0765 
0766     AdvancedRenameManager manager(files2);
0767     manager.setSortAction(static_cast<AdvancedRenameManager::SortAction>(sortAction));
0768     manager.setSortDirection(static_cast<AdvancedRenameManager::SortDirection>(sortDirection));
0769 
0770     QVERIFY(files.count() == results.count());
0771 
0772     for (int i = 0 ; i < files.count() ; ++i)
0773     {
0774         QCOMPARE(manager.indexOfFile(files.at(i)), results.at(i));
0775     }
0776 }
0777 
0778 void AdvancedRenameTest::indexOfFile_invalid_input_returns_minus_one()
0779 {
0780     QList<ParseSettings> files;
0781     ParseSettings ps;
0782     ps.fileUrl = QUrl::fromLocalFile(filePath);
0783     files << ps;
0784 
0785     AdvancedRenameManager manager(files);
0786     QCOMPARE(manager.indexOfFile(QLatin1String("none_existent_file.png")), -1);
0787 }
0788 
0789 void AdvancedRenameTest::indexOfFolder_invalid_input_returns_minus_one()
0790 {
0791     QList<ParseSettings> files;
0792     ParseSettings ps;
0793     ps.fileUrl = QUrl::fromLocalFile(filePath);
0794     files << ps;
0795 
0796     AdvancedRenameManager manager(files);
0797     QCOMPARE(manager.indexOfFolder(QLatin1String("none_existent_file.png")), -1);
0798 }
0799 
0800 void AdvancedRenameTest::indexOfFileGroup_invalid_input_returns_minus_one()
0801 {
0802     QList<ParseSettings> files;
0803     ParseSettings ps;
0804     ps.fileUrl = QUrl::fromLocalFile(filePath);
0805     files << ps;
0806 
0807     AdvancedRenameManager manager(files);
0808     QCOMPARE(manager.indexOfFileGroup(QLatin1String("none_existent_file.png")), -1);
0809 }
0810 
0811 void AdvancedRenameTest::sequencenumber_tests_startIndex_data()
0812 {
0813     QStringList files;
0814     files << filePath << filePath2 << filePath3;
0815 
0816     QTest::addColumn<QString>("parseString");
0817     QTest::addColumn<QStringList>("files");
0818     QTest::addColumn<QStringList>("results");
0819 
0820     QTest::newRow("####")
0821             << "####"
0822             << files
0823             << (QStringList() << QLatin1String("0025.jpg") << QLatin1String("0026.jpg") << QLatin1String("0027.jpg"));
0824 
0825     QTest::newRow("###[-2]")
0826             << "###[-2]"
0827             << files
0828             << (QStringList() << QLatin1String("025.jpg") << QLatin1String("026.jpg") << QLatin1String("027.jpg"));
0829 
0830     QTest::newRow("###[2]")
0831             << "###[2]"
0832             << files
0833             << (QStringList() << QLatin1String("002.jpg") << QLatin1String("003.jpg") << QLatin1String("004.jpg"));
0834 
0835     QTest::newRow("##[3,3]")
0836             << "##[3,3]"
0837             << files
0838             << (QStringList() << QLatin1String("03.jpg") << QLatin1String("06.jpg") << QLatin1String("09.jpg"));
0839 }
0840 
0841 void AdvancedRenameTest::sequencenumber_tests_startIndex()
0842 {
0843     QFETCH(QString,       parseString);
0844     QFETCH(QStringList,   files);
0845     QFETCH(QStringList,   results);
0846 
0847     ParseSettings ps;
0848 
0849     QList<ParseSettings> files2;
0850 
0851     Q_FOREACH (const QString& file, files)
0852     {
0853         ps.fileUrl = QUrl::fromLocalFile(file);
0854         files2 << ps;
0855     }
0856 
0857     AdvancedRenameManager manager(files2);
0858     manager.setStartIndex(25);
0859     manager.parseFiles(parseString);
0860 
0861     QVERIFY(files.count() == results.count());
0862 
0863     for (int i = 0 ; i < files.count() ; ++i)
0864     {
0865         QCOMPARE(manager.newName(files.at(i)), results.at(i));
0866     }
0867 }
0868 
0869 void AdvancedRenameTest::sortAction_custom_asc_should_not_sort()
0870 {
0871     QList<ParseSettings> files;
0872     ParseSettings ps;
0873 
0874     QStringList filePaths;
0875     filePaths << filePath  << filePath2 << filePath3
0876               << filePath4 << filePath5 << filePath6
0877               << filePath7 << filePath8 << filePath9;
0878 
0879     Q_FOREACH (const QString& fpath, filePaths)
0880     {
0881         ps.fileUrl = QUrl::fromLocalFile(fpath);
0882         files << ps;
0883     }
0884 
0885     AdvancedRenameManager manager(files);
0886 
0887     QStringList managedFiles = manager.fileList();
0888     QVERIFY(managedFiles.count() == filePaths.count());
0889 
0890     for (int i = 0 ; i < managedFiles.count() ; ++i)
0891     {
0892         QCOMPARE(managedFiles.at(i), filePaths.at(i));
0893     }
0894 }
0895 
0896 void AdvancedRenameTest::sortAction_custom_desc_should_not_sort()
0897 {
0898     QList<ParseSettings> files;
0899     ParseSettings ps;
0900 
0901     QStringList filePaths;
0902     filePaths << filePath  << filePath2 << filePath3
0903               << filePath4 << filePath5 << filePath6
0904               << filePath7 << filePath8 << filePath9;
0905 
0906     Q_FOREACH (const QString& fpath, filePaths)
0907     {
0908         ps.fileUrl = QUrl::fromLocalFile(fpath);
0909         files << ps;
0910     }
0911 
0912     AdvancedRenameManager manager(files);
0913     manager.setSortDirection(AdvancedRenameManager::SortDescending);
0914 
0915     QStringList managedFiles = manager.fileList();
0916     QVERIFY(managedFiles.count() == filePaths.count());
0917 
0918     for (int i = 0 ; i < managedFiles.count() ; ++i)
0919     {
0920         QCOMPARE(managedFiles.at(i), filePaths.at(i));
0921     }
0922 }
0923 
0924 void AdvancedRenameTest::sortAction_name_asc()
0925 {
0926     QList<ParseSettings> files;
0927     ParseSettings ps;
0928 
0929     QStringList filePaths;
0930     filePaths << filePath  << filePath2 << filePath3
0931               << filePath4 << filePath5 << filePath6
0932               << filePath7 << filePath8 << filePath9;
0933 
0934     Q_FOREACH (const QString& fpath, filePaths)
0935     {
0936         ps.fileUrl = QUrl::fromLocalFile(fpath);
0937         files << ps;
0938     }
0939 
0940     filePaths.sort();
0941 
0942     AdvancedRenameManager manager(files);
0943     manager.setSortAction(AdvancedRenameManager::SortName);
0944 
0945     QStringList managedFiles = manager.fileList();
0946     QVERIFY(managedFiles.count() == filePaths.count());
0947 
0948     for (int i = 0 ; i < managedFiles.count() ; ++i)
0949     {
0950         QCOMPARE(managedFiles.at(i), filePaths.at(i));
0951     }
0952 }
0953 
0954 void AdvancedRenameTest::sortAction_name_desc()
0955 {
0956     QList<ParseSettings> files;
0957     ParseSettings ps;
0958 
0959     QStringList filePaths;
0960     filePaths << filePath  << filePath2 << filePath3
0961               << filePath4 << filePath5 << filePath6
0962               << filePath7 << filePath8 << filePath9;
0963 
0964     Q_FOREACH (const QString& fpath, filePaths)
0965     {
0966         ps.fileUrl = QUrl::fromLocalFile(fpath);
0967         files << ps;
0968     }
0969 
0970     filePaths.sort();
0971     std::reverse(filePaths.begin(), filePaths.end());
0972 
0973     AdvancedRenameManager manager(files);
0974     manager.setSortAction(AdvancedRenameManager::SortName);
0975     manager.setSortDirection(AdvancedRenameManager::SortDescending);
0976 
0977     QStringList managedFiles = manager.fileList();
0978     QVERIFY(managedFiles.count() == filePaths.count());
0979 
0980     for (int i = 0 ; i < managedFiles.count() ; ++i)
0981     {
0982         QCOMPARE(managedFiles.at(i), filePaths.at(i));
0983     }
0984 }
0985 
0986 void AdvancedRenameTest::sortAction_size_asc()
0987 {
0988     QList<ParseSettings> files;
0989     ParseSettings ps;
0990 
0991     QStringList filePaths;
0992     filePaths << filePath7 << filePath8 << filePath9
0993               << filePath3 << filePath << filePath2
0994               << filePath5 << filePath4 << filePath6;
0995 
0996     Q_FOREACH (const QString& fpath, filePaths)
0997     {
0998         ps.fileUrl = QUrl::fromLocalFile(fpath);
0999         files << ps;
1000     }
1001 
1002     AdvancedRenameManager manager(files);
1003     manager.setSortAction(AdvancedRenameManager::SortSize);
1004 
1005     QStringList managedFiles = manager.fileList();
1006     QVERIFY(managedFiles.count() == filePaths.count());
1007 
1008     for (int i = 0 ; i < managedFiles.count() ; ++i)
1009     {
1010         QCOMPARE(managedFiles.at(i), filePaths.at(i));
1011     }
1012 }
1013 
1014 void AdvancedRenameTest::sortAction_size_desc()
1015 {
1016     QList<ParseSettings> files;
1017     ParseSettings ps;
1018 
1019     QStringList filePaths;
1020     filePaths << filePath6 << filePath4 << filePath5
1021               << filePath2 << filePath  << filePath3
1022               << filePath9 << filePath8 << filePath7;
1023 
1024     Q_FOREACH (const QString& fpath, filePaths)
1025     {
1026         ps.fileUrl = QUrl::fromLocalFile(fpath);
1027         files << ps;
1028     }
1029 
1030     AdvancedRenameManager manager(files);
1031     manager.setSortAction(AdvancedRenameManager::SortSize);
1032     manager.setSortDirection(AdvancedRenameManager::SortDescending);
1033 
1034     QStringList managedFiles = manager.fileList();
1035     QVERIFY(managedFiles.count() == filePaths.count());
1036 
1037     for (int i = 0 ; i < managedFiles.count() ; ++i)
1038     {
1039         QCOMPARE(managedFiles.at(i), filePaths.at(i));
1040     }
1041 }
1042 
1043 /*
1044 void AdvancedRenameTest::sortAction_date_asc()
1045 {
1046     QList<ParseSettings> files;
1047     ParseSettings ps;
1048 
1049     QStringList filePaths;
1050     filePaths << filePath4 << filePath3 << filePath
1051               << filePath2 << filePath9 << filePath7
1052               << filePath8 << filePath6 << filePath5;
1053 
1054     Q_FOREACH (const QString& fpath, filePaths)
1055     {
1056         ps.fileUrl = QUrl::fromLocalFile(fpath);
1057         files << ps;
1058     }
1059 
1060     AdvancedRenameManager manager(files);
1061     manager.setSortAction(AdvancedRenameManager::SortDate);
1062 
1063     QStringList managedFiles = manager.fileList();
1064     QVERIFY(managedFiles.count() == filePaths.count());
1065 
1066     for (int i = 0 ; i < managedFiles.count() ; ++i)
1067     {
1068         QCOMPARE(managedFiles.at(i), filePaths.at(i));
1069     }
1070 }
1071 
1072 void AdvancedRenameTest::sortAction_date_desc()
1073 {
1074     QList<ParseSettings> files;
1075     ParseSettings ps;
1076 
1077     QStringList filePaths;
1078     filePaths << filePath5 << filePath6 << filePath8
1079               << filePath7 << filePath9 << filePath2
1080               << filePath  << filePath3 << filePath4;
1081 
1082     Q_FOREACH (const QString& fpath, filePaths)
1083     {
1084         ps.fileUrl = QUrl::fromLocalFile(fpath);
1085         files << ps;
1086     }
1087 
1088     AdvancedRenameManager manager(files);
1089     manager.setSortAction(AdvancedRenameManager::SortDate);
1090     manager.setSortDirection(AdvancedRenameManager::SortDescending);
1091 
1092     QStringList managedFiles = manager.fileList();
1093     QVERIFY(managedFiles.count() == filePaths.count());
1094 
1095     for (int i = 0 ; i < managedFiles.count() ; ++i)
1096     {
1097         QCOMPARE(managedFiles.at(i), filePaths.at(i));
1098     }
1099 }
1100 */
1101 
1102 void AdvancedRenameTest::testReplaceModifier_data()
1103 {
1104     QTest::addColumn<QString>("parseString");
1105     QTest::addColumn<QString>("result");
1106 
1107     QTest::newRow("[file]{replace:\"adv\",\"AAA\"}")
1108             << "[file]{replace:\"adv\",\"AAA\"}"
1109             << "AAAancedrename_testimage.jpg";
1110 
1111     QTest::newRow("[file]{replace:\"Adv\",\"AAA\"}")
1112             << "[file]{replace:\"Adv\",\"AAA\"}"
1113             << "advancedrename_testimage.jpg";
1114 
1115     QTest::newRow("[file]{replace:\"Adv\",\"AAA\",i}")
1116             << "[file]{replace:\"Adv\",\"AAA\",i}"
1117             << "AAAancedrename_testimage.jpg";
1118 
1119     QTest::newRow("[file]{replace:\"Adv\",\"AAA\",ri}")
1120             << "[file]{replace:\"Adv\",\"AAA\",ri}"
1121             << "AAAancedrename_testimage.jpg";
1122 
1123     QTest::newRow("[file]{replace:\"Adv\",\"AAA\",ir}")
1124             << "[file]{replace:\"Adv\",\"AAA\",ir}"
1125             << "AAAancedrename_testimage.jpg";
1126 
1127     QTest::newRow("[file]{replace:\"a.v\",\"AAA\"}")
1128             << "[file]{replace:\"a.v\",\"AAA\"}"
1129             << "advancedrename_testimage.jpg";
1130 
1131     QTest::newRow("[file]{replace:\"a.v\",\"AAA\",r}")
1132             << "[file]{replace:\"a.v\",\"AAA\",r}"
1133             << "AAAancedrename_testimage.jpg";
1134 }
1135 
1136 void AdvancedRenameTest::testReplaceModifier()
1137 {
1138     QFETCH(QString,   parseString);
1139     QFETCH(QString,   result);
1140 
1141     QList<ParseSettings> files;
1142     ParseSettings ps;
1143     ps.fileUrl     = QUrl::fromLocalFile(filePath);
1144     files << ps;
1145     AdvancedRenameManager manager(files);
1146     manager.parseFiles(parseString);
1147 
1148     QString parsed = manager.newName(filePath);
1149     QCOMPARE(parsed, result);
1150 }
1151 
1152 void AdvancedRenameTest::testRangeModifier_data()
1153 {
1154     QTest::addColumn<QString>("parseString");
1155     QTest::addColumn<QString>("result");
1156 
1157     QDateTime curdate = QDateTime::currentDateTime();
1158 
1159     QTest::newRow("[file]{range:1}")
1160             << "[file]{range:1}"
1161             << "a.jpg";
1162 
1163     QTest::newRow("[file]{range:3}")
1164             << "[file]{range:3}"
1165             << "v.jpg";
1166 
1167     QTest::newRow("[file]{range:1,3}")
1168             << "[file]{range:1,3}"
1169             << "adv.jpg";
1170 
1171     QTest::newRow("[file]{range:3,}")
1172             << "[file]{range:3,}"
1173             << "vancedrename_testimage.jpg";
1174 
1175     QTest::newRow("[file]{range:0}")
1176             << "[file]{range:0}"
1177             << "advancedrename_testimage.jpg";
1178 
1179     QTest::newRow("[file]{range:-100}")
1180             << "[file]{range:-100}"
1181             << "a.jpg";
1182 
1183     QTest::newRow("[file]{range:-100,}")
1184             << "[file]{range:-100,}"
1185             << "advancedrename_testimage.jpg";
1186 
1187     QTest::newRow("[file]{range:-100,2}")
1188             << "[file]{range:-100,2}"
1189             << "ad.jpg";
1190 }
1191 
1192 void AdvancedRenameTest::testRangeModifier()
1193 {
1194     QFETCH(QString,   parseString);
1195     QFETCH(QString,   result);
1196 
1197     QList<ParseSettings> files;
1198     ParseSettings ps;
1199     ps.fileUrl     = QUrl::fromLocalFile(filePath);
1200     files << ps;
1201     AdvancedRenameManager manager(files);
1202     manager.parseFiles(parseString);
1203 
1204     QString parsed = manager.newName(filePath);
1205     QCOMPARE(parsed, result);
1206 }
1207 
1208 void AdvancedRenameTest::testDefaultValueModifier_data()
1209 {
1210     QTest::addColumn<QString>("parseString");
1211     QTest::addColumn<QString>("result");
1212 
1213     QDateTime curdate = QDateTime::currentDateTime();
1214 
1215     QTest::newRow("[meta:Iptc.Application2.Keywords]_[file]")
1216             << "[meta:Iptc.Application2.Keywords]{default:\"Unknown\"}_[file]"
1217             << "Colca Canyon_advancedrename_testimage.jpg";
1218 
1219     QTest::newRow("[meta:Exif.GPSInfo.GPSAltitude]_[file]")
1220             << "[meta:Exif.GPSInfo.GPSAltitude]{default:\"Unknown\"}_[file]"
1221             << "Unknown_advancedrename_testimage.jpg";
1222 }
1223 
1224 void AdvancedRenameTest::testDefaultValueModifier()
1225 {
1226     QFETCH(QString,   parseString);
1227     QFETCH(QString,   result);
1228 
1229     QList<ParseSettings> files;
1230     ParseSettings ps;
1231     ps.fileUrl     = QUrl::fromLocalFile(filePath);
1232     files << ps;
1233     AdvancedRenameManager manager(files);
1234     manager.parseFiles(parseString);
1235 
1236     QString parsed = manager.newName(filePath);
1237     QCOMPARE(parsed, result);
1238 }
1239 
1240 void AdvancedRenameTest::testLowercaseModifier()
1241 {
1242     QList<ParseSettings> files;
1243     ParseSettings ps;
1244     ps.fileUrl     = QUrl::fromLocalFile(filePath);
1245     files << ps;
1246     AdvancedRenameManager manager(files);
1247     manager.parseFiles(QLatin1String("[file]{lower}"));
1248 
1249     QString parsed = manager.newName(filePath);
1250     QCOMPARE(parsed, fileName.toLower());
1251 }
1252 
1253 void AdvancedRenameTest::testEmptyParseString()
1254 {
1255     QList<ParseSettings> files;
1256     ParseSettings ps;
1257     ps.fileUrl     = QUrl::fromLocalFile(filePath);
1258     files << ps;
1259     AdvancedRenameManager manager(files);
1260 
1261     // test for empty parser string
1262 
1263     manager.parseFiles(QLatin1String(""));
1264 
1265     QString parsed = manager.newName(filePath);
1266     QCOMPARE(parsed, fileName);
1267 
1268     manager.parseFiles(QLatin1String("      "));
1269     parsed         = manager.newName(filePath);
1270     QCOMPARE(parsed, fileName);
1271 }
1272 
1273 #include "moc_advancedrename_utest.cpp"