Changeset View
Changeset View
Standalone View
Standalone View
autotests/batchrenametypestest.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2018 by Emirald Mateli <aldo.mateli@gmail.com> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or modify * | ||||
5 | * it under the terms of the GNU General Public License as published by * | ||||
6 | * the Free Software Foundation; either version 2 of the License, or * | ||||
7 | * (at your option) any later version. * | ||||
8 | * * | ||||
9 | * This program is distributed in the hope that it will be useful, * | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||||
12 | * GNU General Public License for more details. * | ||||
13 | * * | ||||
14 | * You should have received a copy of the GNU General Public License * | ||||
15 | * along with this program; if not, write to the * | ||||
16 | * Free Software Foundation, Inc., * | ||||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * | ||||
18 | ***************************************************************************/ | ||||
19 | | ||||
20 | #include <QTest> | ||||
21 | #include <QHash> | ||||
22 | #include <QString> | ||||
23 | #include <QVariant> | ||||
24 | #include <batchrenametypestest.h> | ||||
25 | #include <rename/batchrenametypes_p.h> | ||||
26 | | ||||
27 | typedef QHash<QString, QVariant> RenameTypeInput; | ||||
28 | Q_DECLARE_METATYPE(RenameTypeInput) | ||||
29 | | ||||
30 | void BatchRenameTypesTest::testMacroReplace_data() | ||||
31 | { | ||||
32 | QTest::addColumn<RenameTypeInput>("input"); | ||||
33 | QTest::addColumn<QString>("expected"); | ||||
34 | | ||||
35 | RenameTypeInput params; | ||||
36 | params["macroReplace.sequence"] = 15; | ||||
37 | params["macroReplace.padding"] = 3; | ||||
38 | params["timestamp"] = QDateTime::fromTime_t(1522504691); // 2018-03-31 15:58:11 CET | ||||
39 | params["name"] = "file.ext"; | ||||
40 | params["baseName"] = "file"; | ||||
41 | params["extension"] = ".ext"; | ||||
42 | | ||||
43 | params["macroReplace.text"] = "NewName {index} - (%y) %Y%M%D (Renamed from {name})"; | ||||
44 | QTest::newRow("Macro replace with sequence, date and name") | ||||
45 | << params << "NewName 015 - (18) 20180331 (Renamed from file.ext)"; | ||||
46 | | ||||
47 | params["macroReplace.text"] = "{baseName}_%H%m%S{suffix}"; | ||||
48 | QTest::newRow("Macro replace with time, basename and extension") | ||||
49 | << params << "file_155811.ext"; | ||||
50 | | ||||
51 | params["macroReplace.text"] = "hello"; | ||||
52 | QTest::newRow("Macro replace with no placeholders") | ||||
53 | << params << "hello"; | ||||
54 | } | ||||
55 | | ||||
56 | void BatchRenameTypesTest::testMacroReplace() | ||||
57 | { | ||||
58 | QFETCH(RenameTypeInput, input); | ||||
59 | QFETCH(QString, expected); | ||||
60 | | ||||
61 | QCOMPARE(KIO::BatchRenameTypes::macroRenamer(input), expected); | ||||
62 | } | ||||
63 | | ||||
64 | void BatchRenameTypesTest::testFindReplace_data() | ||||
65 | { | ||||
66 | QTest::addColumn<RenameTypeInput>("input"); | ||||
67 | QTest::addColumn<QString>("expected"); | ||||
68 | | ||||
69 | RenameTypeInput params; | ||||
70 | params["name"] = "Filename_for_find_and_replace.tar.gz"; | ||||
71 | params["findReplace.find"] = "find_"; | ||||
72 | params["findReplace.replace"] = ""; | ||||
73 | params["findReplace.useRegex"] = false; | ||||
74 | params["findReplace.caseInsensitive"] = false; | ||||
75 | | ||||
76 | QTest::newRow("Find & Replace no regex case sensitive") | ||||
77 | << params << "Filename_for_and_replace.tar.gz"; | ||||
78 | | ||||
79 | params["findReplace.find"] = "FIND_"; | ||||
80 | QTest::newRow("Find & Replace no regex case sensitive, should not replace anything") | ||||
81 | << params << "Filename_for_find_and_replace.tar.gz"; | ||||
82 | | ||||
83 | params["findReplace.find"] = "FIND_"; | ||||
84 | params["findReplace.caseInsensitive"] = true; | ||||
85 | QTest::newRow("Find & Replace no regex case insensitive") | ||||
86 | << params << "Filename_for_and_replace.tar.gz"; | ||||
87 | | ||||
88 | params["findReplace.useRegex"] = true; | ||||
89 | params["findReplace.caseInsensitive"] = false; | ||||
90 | params["findReplace.find"] = "[^a-z]"; | ||||
91 | QTest::newRow("Find & Replace regex, case sensitive. Remove all chars except a-z") | ||||
92 | << params << "ilenameforfindandreplacetargz"; | ||||
93 | | ||||
94 | params["findReplace.useRegex"] = true; | ||||
95 | params["findReplace.caseInsensitive"] = true; | ||||
96 | params["findReplace.find"] = "[^a-z]"; | ||||
97 | QTest::newRow("Find & Replace regex, case insensitive. Remove all non alpha chars") | ||||
98 | << params << "Filenameforfindandreplacetargz"; | ||||
99 | } | ||||
100 | | ||||
101 | void BatchRenameTypesTest::testFindReplace() | ||||
102 | { | ||||
103 | QFETCH(RenameTypeInput, input); | ||||
104 | QFETCH(QString, expected); | ||||
105 | | ||||
106 | QCOMPARE(KIO::BatchRenameTypes::findReplaceRenamer(input), expected); | ||||
107 | } | ||||
108 | | ||||
109 | void BatchRenameTypesTest::testCaptureGroups_data() | ||||
110 | { | ||||
111 | QTest::addColumn<QHash<QString, QVariant>>("input"); | ||||
112 | QTest::addColumn<QString>("expected"); | ||||
113 | | ||||
114 | RenameTypeInput params; | ||||
115 | params["name"] = "myfavpodcast1x03-Episode Title(192kbps).mp3"; | ||||
116 | params["newName.find"] = R"(.+(\d)x(\d+)-(.+?)\()"; | ||||
117 | params["newName.replace"] = R"(Favourite Podcast S0\1E\2 - \3.mp3)"; | ||||
118 | | ||||
119 | QTest::addRow("Capture groups") << params << "Favourite Podcast S01E03 - Episode Title.mp3"; | ||||
120 | } | ||||
121 | | ||||
122 | void BatchRenameTypesTest::testCaptureGroups() | ||||
123 | { | ||||
124 | QFETCH(RenameTypeInput, input); | ||||
125 | QFETCH(QString, expected); | ||||
126 | | ||||
127 | QCOMPARE(KIO::BatchRenameTypes::captureRegexGroups(input), expected); | ||||
128 | } | ||||
129 | | ||||
130 | QTEST_MAIN(BatchRenameTypesTest) | ||||
131 | | ||||
132 | #include "batchrenametypestest.moc" |