File indexing completed on 2022-11-29 20:25:14

0001 /***************************************************************************
0002                           krenametest.cpp  -  description
0003                              -------------------
0004     begin                : Sat Apr 20 2007
0005     copyright            : (C) 2007 by Dominik Seichter
0006     email                : domseichter@web.de
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #include "krenametest.h"
0019 
0020 #include "krenamefile.h"
0021 #include "batchrenamer.h"
0022 
0023 #include <stdio.h>
0024 
0025 #include <QTextEdit>
0026 #include <QVBoxLayout>
0027 
0028 #include <QUrl>
0029 
0030 // Macros to simplify the test code
0031 
0032 #define PRINT_RESULT( name ) writeTestMessage(" %04i Running test: %20s = %s at line %i", \
0033         ++m_counter, name, m_result ? "[OK]" : "[FAILED]", __LINE__ ); \
0034 if( !m_result ) { \
0035     ++m_failed; \
0036     writeTestMessage( "<b>ERROR: %04i Test %s failed!</b>\n", m_counter, name ); \
0037 } else \
0038     ++m_success;
0039 
0040 #define RUN_KRENAMEFILE_TEST( name, url, dir, file, ext, mode, dot, isDir )   \
0041     m_result = testKRenameFileInternal( url, dir, file, ext, mode, dot, isDir ); \
0042     PRINT_RESULT( name );
0043 
0044 #define RUN_TOKEN_TEST( name, templ, filename, expect ) m_result = tokenTest( templ, filename, expect ); \
0045     PRINT_RESULT( name );
0046 
0047 #define RUN_NUMBER_TEST( name, length, start, step, skip ) m_result = numberingTest( length, start, step, skip, 100 ); \
0048     PRINT_RESULT( name );
0049 
0050 #define RUN_NUMBER_TESTS( name, start, step, skip ) m_result = numberingTest( 1, start, step, skip, 100 ); \
0051     PRINT_RESULT( name ); \
0052     m_result = numberingTest( 2, start, step, skip, 100 ); \
0053     PRINT_RESULT( name ); \
0054     m_result = numberingTest( 3, start, step, skip, 100 ); \
0055     PRINT_RESULT( name ); \
0056     m_result = numberingTest( 4, start, step, skip, 100 ); \
0057     PRINT_RESULT( name ); \
0058     m_result = numberingTest( 10, start, step, skip, 100 ); \
0059     PRINT_RESULT( name ); \
0060 
0061 #define RUN_REPLACE_TEST( name, token, file, expect, replace, with, regExp ) \
0062     m_result = replaceTest( token, file, expect, replace, with, regExp); \
0063     PRINT_RESULT( name );
0064 
0065 #define RUN_KRENAME_MULTI_FILE_TEST( files, expected, expectedPaths, token, name ) \
0066     m_result = testMultipleFiles( files, expected, expectedPaths, token ); \
0067     PRINT_RESULT( name );
0068 
0069     KRenameTest::KRenameTest()
0070         : QWidget(),
0071           m_counter(0), m_verbose(false), m_success(0), m_failed(0)
0072     {
0073         QVBoxLayout *layout = new QVBoxLayout(this);
0074 
0075         m_text = new QTextEdit(this);
0076         m_text->setReadOnly(true);
0077         m_text->setFont(QFont("Courier"));
0078 
0079         layout->addWidget(m_text);
0080 
0081         this->setWindowTitle("KRename Test");
0082         this->resize(640, 480);
0083         this->show();
0084     }
0085 
0086     KRenameTest::~KRenameTest()
0087     {
0088 
0089     }
0090 
0091     void KRenameTest::startTest()
0092     {
0093         testKRenameFile();
0094         testBatchRenamer();
0095 
0096         this->writeTestMessage("<b>Results:</b>\n\nSuccessful: %i Failed %i", m_success, m_failed);
0097     }
0098 
0099     void KRenameTest::writeTestHeader(const QString &text)
0100     {
0101         QString str("<b>Testing class: <i>%1</i></b>");
0102         m_text->append(str.arg(text));
0103     }
0104 
0105     void KRenameTest::writeTestMessage(const char *format, ...)
0106     {
0107         va_list  args;
0108 
0109         va_start(args, format);
0110         vsnprintf(m_buffer, KRenameTest::BUFFER_LENGTH, format, args);
0111         va_end(args);
0112 
0113         m_text->append(m_buffer);
0114     }
0115 
0116     void KRenameTest::testKRenameFile()
0117     {
0118         writeTestHeader("KRenameFile");
0119 
0120         // Test a simple filename
0121         RUN_KRENAMEFILE_TEST("file:///home/test/simple.txt (first dot)", QUrl("file:///home/test/simple.txt"),
0122                              "/home/test", "simple", "txt", eSplitMode_FirstDot, 0, false);
0123         RUN_KRENAMEFILE_TEST("file:///home/test/simple.txt (last dot)", QUrl("file:///home/test/simple.txt"),
0124                              "/home/test", "simple", "txt", eSplitMode_LastDot, 0, false);
0125         RUN_KRENAMEFILE_TEST("file:///home/test/simple.txt (no extension)", QUrl("file:///home/test/simple.txt"),
0126                              "/home/test", "simple.txt", "", eSplitMode_NoExtension, 0, false);
0127         RUN_KRENAMEFILE_TEST("file:///home/test/simple.txt (no extension)", QUrl("file:///home/test/simple.txt"),
0128                              "/home/test", "simple.txt", "", eSplitMode_NoExtension, 1, false);
0129         RUN_KRENAMEFILE_TEST("file:///home/test/simple.txt (custom dot 0)", QUrl("file:///home/test/simple.txt"),
0130                              "/home/test", "simple.txt", "", eSplitMode_CustomDot, 0, false);
0131         RUN_KRENAMEFILE_TEST("file:///home/test/simple.txt (custom dot 1)", QUrl("file:///home/test/simple.txt"),
0132                              "/home/test", "simple", "txt", eSplitMode_CustomDot, 1, false);
0133 
0134         // Test a simple filename and a directory with spaces
0135         RUN_KRENAMEFILE_TEST("file:///home/dir with space/simple.txt (first dot)", QUrl("file:///home/dir with space/simple.txt"),
0136                              "/home/dir with space", "simple", "txt", eSplitMode_FirstDot, 0, false);
0137         RUN_KRENAMEFILE_TEST("file:///home/dir with space/simple.txt (last dot)", QUrl("file:///home/dir with space/simple.txt"),
0138                              "/home/dir with space", "simple", "txt", eSplitMode_LastDot, 0, false);
0139         RUN_KRENAMEFILE_TEST("file:///home/dir with space/simple.txt (custom dot 0)", QUrl("file:///home/dir with space/simple.txt"),
0140                              "/home/dir with space", "simple.txt", "", eSplitMode_CustomDot, 0, false);
0141         RUN_KRENAMEFILE_TEST("file:///home/dir with space/simple.txt (custom dot 1)", QUrl("file:///home/dir with space/simple.txt"),
0142                              "/home/dir with space", "simple", "txt", eSplitMode_CustomDot, 1, false);
0143 
0144         // test a more complicated file extension
0145         RUN_KRENAMEFILE_TEST("file:///home/test/complicated.tar.gz (first dot)", QUrl("file:///home/test/complicated.tar.gz"),
0146                              "/home/test", "complicated", "tar.gz", eSplitMode_FirstDot, 0, false);
0147         RUN_KRENAMEFILE_TEST("file:///home/test/complicated.tar.gz (last dot)", QUrl("file:///home/test/complicated.tar.gz"),
0148                              "/home/test", "complicated.tar", "gz", eSplitMode_LastDot, 0, false);
0149         RUN_KRENAMEFILE_TEST("file:///home/test/complicated.tar.gz (custom dot 0)", QUrl("file:///home/test/complicated.tar.gz"),
0150                              "/home/test", "complicated.tar.gz", "", eSplitMode_CustomDot, 0, false);
0151         RUN_KRENAMEFILE_TEST("file:///home/test/complicated.tar.gz (custom dot 1)", QUrl("file:///home/test/complicated.tar.gz"),
0152                              "/home/test", "complicated", "tar.gz", eSplitMode_CustomDot, 1, false);
0153         RUN_KRENAMEFILE_TEST("file:///home/test/complicated.tar.gz (custom dot 2)", QUrl("file:///home/test/complicated.tar.gz"),
0154                              "/home/test", "complicated.tar", "gz", eSplitMode_CustomDot, 2, false);
0155         RUN_KRENAMEFILE_TEST("file:///home/test/complicated.tar.gz (custom dot 3)", QUrl("file:///home/test/complicated.tar.gz"),
0156                              "/home/test", "complicated.tar.gz", "", eSplitMode_CustomDot, 3, false);
0157 
0158         // test a directory with dot
0159         RUN_KRENAMEFILE_TEST("file:///home/dir.with.dot/simple.txt (first dot)", QUrl("file:///home/dir.with.dot/simple.txt"),
0160                              "/home/dir.with.dot", "simple", "txt", eSplitMode_FirstDot, 0, false);
0161         RUN_KRENAMEFILE_TEST("file:///home/dir.with.dot/simple.txt (last dot)", QUrl("file:///home/dir.with.dot/simple.txt"),
0162                              "/home/dir.with.dot", "simple", "txt", eSplitMode_LastDot, 0, false);
0163         RUN_KRENAMEFILE_TEST("file:///home/dir.with.dot/simple.txt (custom dot 0)", QUrl("file:///home/dir.with.dot/simple.txt"),
0164                              "/home/dir.with.dot", "simple.txt", "", eSplitMode_CustomDot, 0, false);
0165         RUN_KRENAMEFILE_TEST("file:///home/dir.with.dot/simple.txt (custom dot 1)", QUrl("file:///home/dir.with.dot/simple.txt"),
0166                              "/home/dir.with.dot", "simple", "txt", eSplitMode_CustomDot, 1, false);
0167 
0168         // test a directory which ends with a slash
0169         RUN_KRENAMEFILE_TEST("file:///home/test/ (first dot)", QUrl("file:///home/test/"),
0170                              "/home", "test", "", eSplitMode_FirstDot, 0, true);
0171         RUN_KRENAMEFILE_TEST("file:///home/test/ (last dot)", QUrl("file:///home/test/"),
0172                              "/home", "test", "", eSplitMode_LastDot, 0, true);
0173         RUN_KRENAMEFILE_TEST("file:///home/test/ (custom dot 0)", QUrl("file:///home/test/"),
0174                              "/home", "test", "", eSplitMode_CustomDot, 0, true);
0175         RUN_KRENAMEFILE_TEST("file:///home/test/ (custom dot 1)", QUrl("file:///home/test/"),
0176                              "/home", "test", "", eSplitMode_CustomDot, 1, true);
0177 
0178         // test a directory which doesn't end with a slash
0179         RUN_KRENAMEFILE_TEST("file:///home/test (first dot)", QUrl("file:///home/test"),
0180                              "/home", "test", "", eSplitMode_FirstDot, 0, true);
0181         RUN_KRENAMEFILE_TEST("file:///home/test (last dot)", QUrl("file:///home/test"),
0182                              "/home", "test", "", eSplitMode_LastDot, 0, true);
0183         RUN_KRENAMEFILE_TEST("file:///home/test (custom dot 0)", QUrl("file:///home/test"),
0184                              "/home", "test", "", eSplitMode_CustomDot, 0, true);
0185         RUN_KRENAMEFILE_TEST("file:///home/test (custom dot 1)", QUrl("file:///home/test"),
0186                              "/home", "test", "", eSplitMode_CustomDot, 1, true);
0187 
0188         // test no file extension
0189         RUN_KRENAMEFILE_TEST("file:///home/test/simple (first dot)", QUrl("file:///home/test/simple"),
0190                              "/home/test", "simple", "", eSplitMode_FirstDot, 0, false);
0191         RUN_KRENAMEFILE_TEST("file:///home/test/simple (last dot)", QUrl("file:///home/test/simple"),
0192                              "/home/test", "simple", "", eSplitMode_LastDot, 0, false);
0193         RUN_KRENAMEFILE_TEST("file:///home/test/simple (custom dot 0)", QUrl("file:///home/test/simple"),
0194                              "/home/test", "simple", "", eSplitMode_CustomDot, 0, false);
0195         RUN_KRENAMEFILE_TEST("file:///home/test/simple (custom dot 1)", QUrl("file:///home/test/simple"),
0196                              "/home/test", "simple", "", eSplitMode_CustomDot, 1, false);
0197 
0198         // test very long complex file extension
0199         RUN_KRENAMEFILE_TEST("file:///home/test/simple.txt.long.ext.zip (first dot)", QUrl("file:///home/test/simple.txt.long.ext.zip"),
0200                              "/home/test", "simple", "txt.long.ext.zip", eSplitMode_FirstDot, 0, false);
0201         RUN_KRENAMEFILE_TEST("file:///home/test/simple.txt.long.ext.zip (last dot)", QUrl("file:///home/test/simple.txt.long.ext.zip"),
0202                              "/home/test", "simple.txt.long.ext", "zip", eSplitMode_LastDot, 0, false);
0203         RUN_KRENAMEFILE_TEST("file:///home/test/simple.txt.long.ext.zip (custom dot 0)", QUrl("file:///home/test/simple.txt.long.ext.zip"),
0204                              "/home/test", "simple.txt.long.ext.zip", "", eSplitMode_CustomDot, 0, false);
0205         RUN_KRENAMEFILE_TEST("file:///home/test/simple.txt.long.ext.zip (custom dot 1)", QUrl("file:///home/test/simple.txt.long.ext.zip"),
0206                              "/home/test", "simple", "txt.long.ext.zip", eSplitMode_CustomDot, 1, false);
0207         RUN_KRENAMEFILE_TEST("file:///home/test/simple.txt.long.ext.zip (custom dot 2)", QUrl("file:///home/test/simple.txt.long.ext.zip"),
0208                              "/home/test", "simple.txt", "long.ext.zip", eSplitMode_CustomDot, 2, false);
0209         RUN_KRENAMEFILE_TEST("file:///home/test/simple.txt.long.ext.zip (custom dot 3)", QUrl("file:///home/test/simple.txt.long.ext.zip"),
0210                              "/home/test", "simple.txt.long", "ext.zip", eSplitMode_CustomDot, 3, false);
0211         RUN_KRENAMEFILE_TEST("file:///home/test/simple.txt.long.ext.zip (custom dot 4)", QUrl("file:///home/test/simple.txt.long.ext.zip"),
0212                              "/home/test", "simple.txt.long.ext", "zip", eSplitMode_CustomDot, 4, false);
0213         RUN_KRENAMEFILE_TEST("file:///home/test/simple.txt.long.ext.zip (custom dot 5)", QUrl("file:///home/test/simple.txt.long.ext.zip"),
0214                              "/home/test", "simple.txt.long.ext.zip", "", eSplitMode_CustomDot, 5, false);
0215     }
0216 
0217     bool KRenameTest::testKRenameFileInternal(const QUrl &url, const QString &directory,
0218             const QString &filename, const QString &extension,
0219             ESplitMode eSplitMode, int dot, bool isDir)
0220     {
0221         // if the expected filename and extension is empty expect a directory
0222         //bool        isDirectory = filename.isEmpty() && extension.isEmpty();
0223         KRenameFile file(url, isDir, eSplitMode, dot);
0224 
0225         if (file.srcDirectory() != directory) {
0226             writeTestMessage("Directory=(%s) expected (%s).\n",
0227                              file.srcDirectory().toLatin1().data(),
0228                              directory.toLatin1().data());
0229             return false;
0230         }
0231 
0232         if (file.srcFilename() != filename) {
0233             writeTestMessage("Filename=(%s) expected (%s).\n",
0234                              file.srcFilename().toLatin1().data(),
0235                              filename.toLatin1().data());
0236             return false;
0237         }
0238 
0239         if (file.srcExtension() != extension) {
0240             writeTestMessage("Extension=(%s) expected (%s).\n",
0241                              file.srcExtension().toLatin1().data(),
0242                              extension.toLatin1().data());
0243             return false;
0244         }
0245 
0246         return true;
0247     }
0248 
0249     void KRenameTest::testBatchRenamer()
0250     {
0251         writeTestHeader("BatchRenamer");
0252 
0253         // Testing the basic KRename tokens
0254         QString filename(" Test File name ");
0255         QString directory1("krename");
0256         QString directory2("home");
0257 
0258         RUN_TOKEN_TEST("$ Test", "$", filename, filename);
0259         RUN_TOKEN_TEST("& Test", "&", filename, filename.toUpper());
0260         RUN_TOKEN_TEST("% Test", "%", filename, filename.toLower());
0261         RUN_TOKEN_TEST("* Test", "*", filename, " Test File Name ");
0262         RUN_TOKEN_TEST("# Test", "#", filename, QString::number(0));
0263         RUN_TOKEN_TEST("## Test", "##", filename, QString().sprintf("%02i", 0));
0264         RUN_TOKEN_TEST("### Test", "###", filename, QString().sprintf("%03i", 0));
0265         RUN_TOKEN_TEST("#### Test", "####", filename, QString().sprintf("%04i", 0));
0266         RUN_TOKEN_TEST("##### Test", "#####", filename, QString().sprintf("%05i", 0));
0267         RUN_TOKEN_TEST("#{100;2} Test", "#{100;2}", filename, QString::number(100));
0268         RUN_TOKEN_TEST("####{100;2} Test", "####{100;2}", filename, QString().sprintf("%04i", 100));
0269         RUN_TOKEN_TEST("####{2;2}## Test", "####{2;2}##", filename,
0270                        QString().sprintf("%04i", 2) + QString().sprintf("%02i", 0));
0271         RUN_TOKEN_TEST("[1] Test", "[1]", filename, QString(filename[0]));
0272         RUN_TOKEN_TEST("[2] Test", "[2]", filename, QString(filename[1]));
0273         RUN_TOKEN_TEST("[3] Test", "[3]", filename, QString(filename[2]));
0274         RUN_TOKEN_TEST("[&4] Test", "[&4]", filename, QString(filename[3].toUpper()));
0275         RUN_TOKEN_TEST("[$4] Test", "[$4]", filename, QString(filename[3]));
0276         RUN_TOKEN_TEST("[%4] Test", "[%4]", filename, QString(filename[3].toLower()));
0277         RUN_TOKEN_TEST("[*4] Test", "[*4]", filename, QString(filename[3].toUpper()));
0278         RUN_TOKEN_TEST("[4-] Test", "[4-]", filename, filename.right(filename.length() - 3));
0279         RUN_TOKEN_TEST("[&4-] Test", "[&4-]", filename, filename.right(filename.length() - 3).toUpper());
0280         RUN_TOKEN_TEST("[$4-] Test", "[$4-]", filename, filename.right(filename.length() - 3));
0281         RUN_TOKEN_TEST("[%4-] Test", "[%4-]", filename, filename.right(filename.length() - 3).toLower());
0282         RUN_TOKEN_TEST("[*4-] Test", "[*4-]", filename, "St File Name ");
0283         RUN_TOKEN_TEST("[4-] Test", "[4-]", filename, filename.right(filename.length() - 3));
0284         RUN_TOKEN_TEST("[&4-[length]] Test", "[&4-[length]]", filename, filename.right(filename.length() - 3).toUpper());
0285         RUN_TOKEN_TEST("[$4-[length]] Test", "[$4-[length]]", filename, filename.right(filename.length() - 3));
0286         RUN_TOKEN_TEST("[%4-[length]] Test", "[%4-[length]]", filename, filename.right(filename.length() - 3).toLower());
0287         RUN_TOKEN_TEST("[*4-[length]] Test", "[*4-[length]]", filename, "St File Name ");
0288         RUN_TOKEN_TEST("[trimmed;[4-]] Test", "[trimmed;[4-]]", filename, filename.right(filename.length() - 3).trimmed());
0289         RUN_TOKEN_TEST("[trimmed] Test", "[trimmed]", filename, filename.trimmed());
0290         RUN_TOKEN_TEST("[length] Test", "[length]", filename, QString::number(filename.length()));
0291         RUN_TOKEN_TEST("[length-0] Test", "[length-0]", filename, QString::number(filename.length()));
0292         RUN_TOKEN_TEST("[length-1] Test", "[length-1]", filename, QString::number(filename.length() - 1));
0293         RUN_TOKEN_TEST("[length-2] Test", "[length-2]", filename, QString::number(filename.length() - 2));
0294         RUN_TOKEN_TEST("[#length] Test", "[#length]", filename, QString::number(filename.length()));
0295         RUN_TOKEN_TEST("[#length-0] Test", "[#length-0]", filename, QString::number(filename.length()));
0296         RUN_TOKEN_TEST("[#length-1] Test", "[#length-1]", filename, QString::number(filename.length() - 1));
0297         RUN_TOKEN_TEST("[#length-2] Test", "[#length-2]", filename, QString::number(filename.length() - 2));
0298         RUN_TOKEN_TEST("[####length] Test", "[####length]", filename, QString().sprintf("%04i", filename.length()));
0299         RUN_TOKEN_TEST("[####length-0] Test", "[####length-0]", filename, QString().sprintf("%04i", filename.length()));
0300         RUN_TOKEN_TEST("[####length-1] Test", "[####length-1]", filename, QString().sprintf("%04i", filename.length() - 1));
0301         RUN_TOKEN_TEST("[####length-2] Test", "[####length-2]", filename, QString().sprintf("%04i", filename.length() - 2));
0302         RUN_TOKEN_TEST("[6-9] Test", "[6-9]", filename, filename.mid(5, 4));
0303         RUN_TOKEN_TEST("[&6-9] Test", "[&6-9]", filename, filename.mid(5, 4).toUpper());
0304         RUN_TOKEN_TEST("[$6-9] Test", "[$6-9]", filename, filename.mid(5, 4));
0305         RUN_TOKEN_TEST("[%6-9] Test", "[%6-9]", filename, filename.mid(5, 4).toLower());
0306         RUN_TOKEN_TEST("[*6-9] Test", "[*6-9]", filename, filename.mid(5, 4));
0307         RUN_TOKEN_TEST("[trimmed;[6-9]] Test", "[trimmed;[6-9]]", filename, filename.mid(5, 4).trimmed());
0308         RUN_TOKEN_TEST("[6;4] Test", "[6;4]", filename, filename.mid(5, 4));
0309         RUN_TOKEN_TEST("[&6;4] Test", "[&6;4]", filename, filename.mid(5, 4).toUpper());
0310         RUN_TOKEN_TEST("[$6;4] Test", "[$6;4]", filename, filename.mid(5, 4));
0311         RUN_TOKEN_TEST("[%6;4] Test", "[%6;4]", filename, filename.mid(5, 4).toLower());
0312         RUN_TOKEN_TEST("[*6;4] Test", "[*6;4]", filename, filename.mid(5, 4));
0313         RUN_TOKEN_TEST("[1;1{[length]}] Test", "[1;1{[length]}]", filename,  "1");
0314         RUN_TOKEN_TEST("[trimmed;[6;4]] Test", "[trimmed;[6;4]]", filename, filename.mid(5, 4).trimmed());
0315         RUN_TOKEN_TEST("[trimmed; Hallo ] Test", "[trimmed; Hallo ]", filename, "Hallo");
0316         RUN_TOKEN_TEST("[dirname] Test", "[dirname]", filename, directory1);
0317         RUN_TOKEN_TEST("[&dirname] Test", "[&dirname]", filename, directory1.toUpper());
0318         RUN_TOKEN_TEST("[$dirname] Test", "[$dirname]", filename, directory1);
0319         RUN_TOKEN_TEST("[%dirname] Test", "[%dirname]", filename, directory1.toLower());
0320         RUN_TOKEN_TEST("[*dirname] Test", "[*dirname]", filename, "KRename");
0321         RUN_TOKEN_TEST("[trimmed;[dirname]] Test", "[trimmed;[dirname]]", filename, directory1);
0322         RUN_TOKEN_TEST("[dirname.] Test", "[dirname.]", filename, directory2);
0323         RUN_TOKEN_TEST("[&dirname.] Test", "[&dirname.]", filename, directory2.toUpper());
0324         RUN_TOKEN_TEST("[$dirname.] Test", "[$dirname.]", filename, directory2);
0325         RUN_TOKEN_TEST("[%dirname.] Test", "[%dirname.]", filename, directory2.toLower());
0326         RUN_TOKEN_TEST("[*dirname.] Test", "[*dirname.]", filename, "Home");
0327         RUN_TOKEN_TEST("[trimmed;[dirname.]] Test", "[trimmed;[dirname.]]", filename, directory2);
0328         RUN_TOKEN_TEST("[dirname..] Test", "[dirname..]", filename, "");
0329         RUN_TOKEN_TEST("[&dirname..] Test", "[&dirname..]", filename, "");
0330         RUN_TOKEN_TEST("[$dirname..] Test", "[$dirname..]", filename, "");
0331         RUN_TOKEN_TEST("[%dirname..] Test", "[%dirname..]", filename, "");
0332         RUN_TOKEN_TEST("[*dirname..] Test", "[*dirname..]", filename, "");
0333         // TODO: This test has strange effects that only occur if [dirname..] is QString::null
0334         //RUN_TOKEN_TEST( "[trimmed;[dirname..]] Test", "[trimmed;[dirname..]]", filename, filename.trimmed() );
0335         RUN_TOKEN_TEST("Complex Test1", "&[2-5]", filename, filename.toUpper() + "Test");
0336         RUN_TOKEN_TEST("Complex Test2", "%[2-5]", filename, filename.toLower() + "Test");
0337         RUN_TOKEN_TEST("Complex Test3", "$[2-5]", filename, filename + "Test");
0338         RUN_TOKEN_TEST("Complex Test4", "*[2-5]", filename, " Test File Name Test");
0339         RUN_TOKEN_TEST("Complex Test5", "[trimmed][2-5]", filename, filename.trimmed() + "Test");
0340         RUN_TOKEN_TEST("Complex Test6", "[&2-5]\\&[length-2]\\&[1;1{Hallo}]", filename, "TEST&14&H");
0341 
0342         // Testing all special Characters in KRename
0343         RUN_TOKEN_TEST("\\/ Test", "\\/", filename, "%2f");   // this is displayed as a slash,
0344         // unix filenames are not allowed
0345         // to contain a slash
0346         RUN_TOKEN_TEST("\\[ Test", "\\[", filename, "[");
0347         RUN_TOKEN_TEST("\\] Test", "\\]", filename, "]");
0348         RUN_TOKEN_TEST("\\$ Test", "\\$", filename, "$");
0349         RUN_TOKEN_TEST("\\* Test", "\\*", filename, "*");
0350         RUN_TOKEN_TEST("\\\\ Test", "\\\\", filename, "\\");
0351         RUN_TOKEN_TEST("\\& Test", "\\&", filename, "&");
0352         RUN_TOKEN_TEST("\\% Test", "\\%", filename, "%");
0353         RUN_TOKEN_TEST("\\# Test", "\\#", filename, "#");
0354 
0355         // Testing filenames with special characters
0356         QString specialname("Test %1 File");
0357         RUN_TOKEN_TEST("File [ Test", "$", specialname.arg("["), specialname.arg("["));
0358         RUN_TOKEN_TEST("File ] Test", "$", specialname.arg("]"), specialname.arg("]"));
0359         RUN_TOKEN_TEST("File $ Test", "$", specialname.arg("$"), specialname.arg("$"));
0360         RUN_TOKEN_TEST("File * Test", "$", specialname.arg("*"), specialname.arg("*"));
0361         RUN_TOKEN_TEST("File \\ Test", "$", specialname.arg("\\"), specialname.arg("\\"));
0362         RUN_TOKEN_TEST("File & Test", "$", specialname.arg("&"), specialname.arg("&"));
0363         RUN_TOKEN_TEST("File % Test", "$", specialname.arg("%"), specialname.arg("%"));
0364         RUN_TOKEN_TEST("File # Test", "$", specialname.arg("#"), specialname.arg("#"));
0365 
0366         // load all plugins now
0367         //PluginLoader::instance()->loadPlugins( false ); // file plugins are not required
0368 
0369         // Testing system functions
0370         RUN_TOKEN_TEST("Date Test", "[date]", filename, QDateTime::currentDateTime().toString("dd-MM-yyyy"));
0371         RUN_TOKEN_TEST("dd-MM-yyyy Date Test", "[date;dd-MM-yyyy]",
0372                        filename, QDateTime::currentDateTime().toString("dd-MM-yyyy"));
0373         RUN_TOKEN_TEST("dd:MM:yyyy Date Test", "[date;dd:MM:yyyy]",
0374                        filename, QDateTime::currentDateTime().toString("dd:MM:yyyy"));
0375         RUN_TOKEN_TEST("yy.mm.dd Date Test", "[date;yy.mm.dd]",
0376                        filename, QDateTime::currentDateTime().toString("yy.mm.dd"));
0377         RUN_TOKEN_TEST("d Date Test", "[date;d]", filename, QDateTime::currentDateTime().toString("d"));
0378         RUN_TOKEN_TEST("dd Date Test", "[date;dd]", filename, QDateTime::currentDateTime().toString("dd"));
0379         RUN_TOKEN_TEST("ddd Date Test", "[date;ddd]", filename, QDateTime::currentDateTime().toString("ddd"));
0380         RUN_TOKEN_TEST("dddd Date Test", "[date;dddd]", filename, QDateTime::currentDateTime().toString("dddd"));
0381         RUN_TOKEN_TEST("M Date Test", "[date;M]", filename, QDateTime::currentDateTime().toString("M"));
0382         RUN_TOKEN_TEST("MM Date Test", "[date;MM]", filename, QDateTime::currentDateTime().toString("MM"));
0383         RUN_TOKEN_TEST("MMM Date Test", "[date;MMM]", filename, QDateTime::currentDateTime().toString("MMM"));
0384         RUN_TOKEN_TEST("MMMM Date Test", "[date;MMMM]", filename, QDateTime::currentDateTime().toString("MMMM"));
0385         RUN_TOKEN_TEST("yy Date Test", "[date;yy]", filename, QDateTime::currentDateTime().toString("yy"));
0386         RUN_TOKEN_TEST("yyyy Date Test", "[date;yyyy]", filename, QDateTime::currentDateTime().toString("yyyy"));
0387 
0388         RUN_TOKEN_TEST("h Date Test", "[date;h]", filename, QDateTime::currentDateTime().toString("h"));
0389         RUN_TOKEN_TEST("hh Date Test", "[date;hh]", filename, QDateTime::currentDateTime().toString("hh"));
0390         RUN_TOKEN_TEST("m Date Test", "[date;m]", filename, QDateTime::currentDateTime().toString("m"));
0391         RUN_TOKEN_TEST("mm Date Test", "[date;mm]", filename, QDateTime::currentDateTime().toString("mm"));
0392         RUN_TOKEN_TEST("s Date Test", "[date;s]", filename, QDateTime::currentDateTime().toString("s"));
0393         RUN_TOKEN_TEST("ss Date Test", "[date;ss]", filename, QDateTime::currentDateTime().toString("ss"));
0394         // Current computers are to slow to compare two milliseconds as the instruction is longer than
0395         // a millisecond.
0396         //
0397         // RUN_TOKEN_TEST( "z Date Test", "[date;z]", filename, QDateTime::currentDateTime().toString( "z") );
0398         // RUN_TOKEN_TEST( "zzz Date Test", "[date;zzz]", filename, QDateTime::currentDateTime().toString( "zzz") );
0399         RUN_TOKEN_TEST("ap Date Test", "[date;ap]", filename, QDateTime::currentDateTime().toString("ap"));
0400         RUN_TOKEN_TEST("AP Date Test", "[date;AP]", filename, QDateTime::currentDateTime().toString("AP"));
0401         RUN_TOKEN_TEST("Day Test", "[day]", filename, QDateTime::currentDateTime().toString("dd"));
0402         RUN_TOKEN_TEST("Month Test", "[month]", filename, QDateTime::currentDateTime().toString("MM"));
0403         RUN_TOKEN_TEST("Year Test", "[year]", filename, QDateTime::currentDateTime().toString("yyyy"));
0404         RUN_TOKEN_TEST("Hour Test", "[hour]", filename, QDateTime::currentDateTime().toString("hh"));
0405         RUN_TOKEN_TEST("Minute Test", "[minute]", filename, QDateTime::currentDateTime().toString("mm"));
0406         RUN_TOKEN_TEST("Second Test", "[second]", filename, QDateTime::currentDateTime().toString("ss"));
0407         RUN_TOKEN_TEST("Time Test", "[time]", filename, QDateTime::currentDateTime().toString("hh-mm-ss"));
0408 
0409         // Testing numbering name, start, step, skip
0410         RUN_NUMBER_TESTS("Numbers 0- Step 1", 0, 1, QList<int>());
0411         RUN_NUMBER_TESTS("Numbers 1- Step 1", 1, 1, QList<int>());
0412         RUN_NUMBER_TESTS("Numbers 2- Step 1", 2, 1, QList<int>());
0413         RUN_NUMBER_TESTS("Numbers -2- Step 1", -2, 1, QList<int>());
0414 
0415         RUN_NUMBER_TESTS("Numbers 0- Step 2", 0, 2, QList<int>());
0416         RUN_NUMBER_TESTS("Numbers 1- Step 2", 1, 2, QList<int>());
0417         RUN_NUMBER_TESTS("Numbers 2- Step 2", 2, 2, QList<int>());
0418         RUN_NUMBER_TESTS("Numbers -2- Step 2", -2, 2, QList<int>());
0419 
0420         RUN_NUMBER_TESTS("Numbers 0- Step 7", 0, 7, QList<int>());
0421         RUN_NUMBER_TESTS("Numbers 1- Step 7", 1, 7, QList<int>());
0422         RUN_NUMBER_TESTS("Numbers 2- Step 7", 2, 7, QList<int>());
0423         RUN_NUMBER_TESTS("Numbers -2- Step 7", -2, 7, QList<int>());
0424 
0425         RUN_NUMBER_TESTS("Numbers 0- Step -3", 0, -3, QList<int>());
0426         RUN_NUMBER_TESTS("Numbers 1- Step -3", 1, -3, QList<int>());
0427         RUN_NUMBER_TESTS("Numbers 2- Step -3", 2, -3, QList<int>());
0428         RUN_NUMBER_TESTS("Numbers -2- Step -3", -2, -3, QList<int>());
0429 
0430         RUN_NUMBER_TESTS("Skip 0- Step 1", 0, 1, QList<int>() << 1 << 2 << 3 << 4 << 89);
0431         RUN_NUMBER_TESTS("Skip 1- Step 1", 1, 1, QList<int>() << 1 << 2 << 3 << 4 << 89);
0432         RUN_NUMBER_TESTS("Skip 2- Step 1", 2, 1, QList<int>() << 1 << 2 << 3 << 4 << 89);
0433         RUN_NUMBER_TESTS("Skip -2- Step 1", -2, 1, QList<int>() << 1 << 2 << 3 << 4 << 89);
0434 
0435         RUN_NUMBER_TESTS("Skip 10- Step 79", 10, 79, QList<int>() << 1 << 2 << 3 << 4 << 89);
0436         RUN_NUMBER_TESTS("Skip 10- Step -2", 10, -2, QList<int>() << 1 << 2 << 3 << 4 << 89);
0437         RUN_NUMBER_TESTS("Skip 10- Step -1", 10, -1, QList<int>() << 1 << 2 << 3 << 4 << 89);
0438 
0439         // Test the find and replace feature of KRename
0440         RUN_REPLACE_TEST("Replace: Spaces", "$", "Filename with spaces", "Filename_with_spaces", " ", "_", false);
0441         RUN_REPLACE_TEST("Replace: Nothing", "$", "Filename", "Filename", " ", "_", false);
0442         RUN_REPLACE_TEST("Replace: Word", "$", "Filename with spaces", "Filename HAS spaces", "with", "HAS", false);
0443         RUN_REPLACE_TEST("Replace: $", "$", "Filename with $ and spaces", "Filename with ! and spaces", "$", "!", false);
0444         RUN_REPLACE_TEST("Replace: &", "$", "Filename with & and spaces", "Filename with ! and spaces", "&", "!", false);
0445         RUN_REPLACE_TEST("Replace: %", "$", "Filename with % and spaces", "Filename with ! and spaces", "%", "!", false);
0446         RUN_REPLACE_TEST("Replace: *", "$", "Filename with * and spaces", "Filename with ! and spaces", "*", "!", false);
0447         RUN_REPLACE_TEST("Replace: [", "$", "Filename with [ and spaces", "Filename with ! and spaces", "[", "!", false);
0448         RUN_REPLACE_TEST("Replace: ]", "$", "Filename with ] and spaces", "Filename with ! and spaces", "]", "!", false);
0449         RUN_REPLACE_TEST("Replace: #", "$", "Filename with # and spaces", "Filename with ! and spaces", "#", "!", false);
0450         RUN_REPLACE_TEST("Replace: to $", "$", "Filename with spaces", "Filename $ spaces", "with", "$", false);
0451         RUN_REPLACE_TEST("Replace: to &", "$", "Filename with spaces", "Filename & spaces", "with", "&", false);
0452         RUN_REPLACE_TEST("Replace: to %", "$", "Filename with spaces", "Filename % spaces", "with", "%", false);
0453         RUN_REPLACE_TEST("Replace: to *", "$", "Filename with spaces", "Filename * spaces", "with", "*", false);
0454         RUN_REPLACE_TEST("Replace: to [", "$", "Filename with spaces", "Filename [ spaces", "with", "[", false);
0455         RUN_REPLACE_TEST("Replace: to ]", "$", "Filename with spaces", "Filename ] spaces", "with", "]", false);
0456         RUN_REPLACE_TEST("Replace: to #", "$", "Filename with spaces", "Filename # spaces", "with", "#", false);
0457 
0458         RUN_REPLACE_TEST("RegExp: ?", "$", "Filename", "AAAAAAAAA", "[a-zA-z]?", "A", true);
0459         RUN_REPLACE_TEST("RegExp: {1}", "$", "Filename", "AAAAAAAA", "[a-zA-z]{1}", "A", true);
0460         RUN_REPLACE_TEST("RegExp: +", "$", "Filename", "A", "[a-zA-z]+", "A", true);
0461         RUN_REPLACE_TEST("RegExp: \\d", "$", "Filename 123", "Filename NumberNumberNumber", "\\d", "Number", true);
0462         RUN_REPLACE_TEST("RegExp: \\d+", "$", "Filename 123", "Filename Number", "\\d+", "Number", true);
0463         RUN_REPLACE_TEST("RegExp: Match", "$", "Filename 123", "MATCHING", "[a-zA-z]* \\d{3}", "MATCHING", true);
0464 
0465         // TODO:
0466         // TODO: Manual Change Test
0467 
0468         // Regression tests
0469         // Caused an infinite loop
0470         RUN_TOKEN_TEST("[1-2 [4-] Test", "[1-2 [4-]", filename, ' ' + filename.right(filename.length() - 3));
0471         const char *regTemplate = "###{329;13";
0472         RUN_TOKEN_TEST(regTemplate, regTemplate, filename, "329");
0473         RUN_TOKEN_TEST("ABC {1", "ABC {1", filename, "ABC {1");
0474 
0475         // Brackets appeared as \[ in the final result
0476         QString regressionName = "1-07 Take Flight (Wings) [Pocketman]";
0477         QString regressionExpect = "100-Take Flight (Wings) [Pocketman]";
0478         RUN_TOKEN_TEST("1##-[$6-] Test", "1##-[$6-]", regressionName, regressionExpect);
0479 
0480         KRenameFile::List files;
0481         QStringList expected;
0482         QStringList expectedPaths;
0483 
0484         files << KRenameFile(QUrl("/home/foo/bar/subdir"), true, eSplitMode_FirstDot, 1)
0485               << KRenameFile(QUrl("/home/foo"), true, eSplitMode_FirstDot, 1)
0486               << KRenameFile(QUrl("/home/foo/bar"), true, eSplitMode_FirstDot, 1)
0487               << KRenameFile(QUrl("/home/foo/bar/baz"), true, eSplitMode_FirstDot, 1);
0488         expected << "SUBDIR" << "FOO" << "BAR" << "BAZ";
0489         expectedPaths << "/home/foo/bar" << "/home" << "/home/FOO" << "/home/FOO/BAR";
0490 
0491         RUN_KRENAME_MULTI_FILE_TEST(files, expected, expectedPaths,
0492                                     "&", "Testing a directory hierarchy");
0493     }
0494 
0495     bool KRenameTest::tokenTest(const char *token, const QString &filename, const QString &expected)
0496     {
0497         QString directory("/home/krename/");
0498         KRenameFile::List list;
0499         KRenameFile file(QUrl(directory + filename), filename.isEmpty(), eSplitMode_FirstDot, 1);
0500 
0501         list.push_back(file);
0502 
0503         BatchRenamer b;
0504         b.setFilenameTemplate(token);
0505         b.setFiles(&list);
0506         b.processFilenames();
0507 
0508         QString str = list[0].dstFilename();
0509 
0510         bool result = (str == expected);
0511         if (m_verbose || !result)
0512             writeTestMessage(" ---> Expected: (%s) Got: (%s) Token: (%s)",
0513                              expected.toLatin1().data(),
0514                              str.toLatin1().data(), token);
0515 
0516         return result;
0517     }
0518 
0519     bool KRenameTest::numberingTest(int length, int start, int step, QList<int> skip, int num)
0520     {
0521         QString directory("/home/krename/");
0522         KRenameFile::List list;
0523         QString token;
0524         token.fill('#', length);
0525 
0526         for (int i = 0; i < num; i++) {
0527             QString filename = "any" + QString::number(i);
0528             KRenameFile file(QUrl(directory + filename), filename.isEmpty(), eSplitMode_FirstDot, 1);
0529 
0530             list.push_back(file);
0531         }
0532 
0533         BatchRenamer b;
0534         b.setFilenameTemplate(token);
0535         b.setFiles(&list);
0536         b.setNumberStepping(step);
0537         b.setNumberStartIndex(start);
0538         b.setNumberSkipList(skip);
0539         b.processFilenames();
0540 
0541         int cur = start;
0542         bool result = true;
0543         QString expected;
0544 
0545         while (skip.contains(cur)) {
0546             cur += step;
0547         }
0548 
0549         KRenameFile::List::ConstIterator it = list.constBegin();
0550 
0551         while (it != list.end()  && result) {
0552             expected.sprintf("%0*i", length, cur);
0553             result = ((*it).dstFilename() == expected);
0554             if (m_verbose || !result)
0555                 qDebug(" ---> Expected: (%s) Got: (%s) Start: %i Step: %i Token: (%s)",
0556                        expected.toLatin1().data(), (*it).dstFilename().toLatin1().data(), start, step, token.toLatin1().data());
0557 
0558             do {
0559                 cur += step;
0560             } while (skip.contains(cur));
0561 
0562             ++it;
0563         }
0564 
0565         return result;
0566     }
0567 
0568     bool KRenameTest::replaceTest(const QString &token, const QString &filename, const QString &expected,
0569                                   const QString &replace, const QString &with, bool regExp)
0570     {
0571         QString directory("/home/krename/");
0572         KRenameFile::List list;
0573         KRenameFile file(QUrl(directory + filename), filename.isEmpty(), eSplitMode_FirstDot, 1);
0574         list.push_back(file);
0575 
0576         QList<TReplaceItem> replaceList;
0577 
0578         TReplaceItem strings;
0579         strings.find    = replace;
0580         strings.replace = with;
0581         strings.reg     = regExp;
0582 
0583         replaceList.append(strings);
0584 
0585         BatchRenamer b;
0586         b.setFilenameTemplate(token);
0587         b.setFiles(&list);
0588         b.setReplaceList(replaceList);
0589         b.processFilenames();
0590 
0591         QString str = list[0].dstFilename();
0592         bool result = (str == expected);
0593         if (m_verbose || !result)
0594             writeTestMessage(" ---> Expected: (%s) Got: (%s) Token: (%s)",
0595                              expected.toLatin1().data(),
0596                              str.toLatin1().data(), token.toLatin1().data());
0597 
0598         return result;
0599 
0600     }
0601 
0602     bool KRenameTest::testMultipleFiles(KRenameFile::List &files, const QStringList &expected, const QStringList &expectedPath, const QString &token)
0603     {
0604         BatchRenamer b;
0605         b.setFilenameTemplate(token);
0606         b.setFiles(&files);
0607         b.processFilenames();
0608 
0609         bool result = true;
0610         for (int i = 0; i < files.count(); i++) {
0611             QString str = files[i].dstFilename();
0612             QString strPath = files[i].dstDirectory();
0613             bool strResult = (str == expected[i]);
0614             bool pathResult = (strPath == expectedPath[i]);
0615             if (m_verbose || !strResult) {
0616                 writeTestMessage(" ---> Expected: (%s) Got: (%s) Token: (%s)",
0617                                  expected[i].toLatin1().data(),
0618                                  str.toLatin1().data(), token.toLatin1().data());
0619             }
0620             if (m_verbose || !pathResult) {
0621                 writeTestMessage(" ---> Expected: (%s) Got: (%s) Token: (%s)",
0622                                  expectedPath[i].toLatin1().data(),
0623                                  strPath.toLatin1().data(), token.toLatin1().data());
0624             }
0625 
0626             result = result && strResult && pathResult;
0627         }
0628 
0629         return result;
0630     }
0631