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