Changeset View
Changeset View
Standalone View
Standalone View
src/tests/renametypestest.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 <views/rename/renametypes.h> | ||||
25 | | ||||
26 | typedef QHash<QString, QVariant> RenameTypeInput; | ||||
27 | Q_DECLARE_METATYPE(RenameTypeInput) | ||||
28 | | ||||
29 | class RenameTypesTest : public QObject | ||||
30 | { | ||||
31 | Q_OBJECT | ||||
32 | | ||||
33 | private slots: | ||||
34 | void testMacroReplace_data(); | ||||
35 | void testMacroReplace(); | ||||
36 | | ||||
37 | void testFindReplace_data(); | ||||
38 | void testFindReplace(); | ||||
39 | | ||||
40 | void testCaptureGroups_data(); | ||||
41 | void testCaptureGroups(); | ||||
42 | }; | ||||
43 | | ||||
44 | void RenameTypesTest::testMacroReplace_data() | ||||
45 | { | ||||
46 | QTest::addColumn<RenameTypeInput>("input"); | ||||
47 | QTest::addColumn<QString>("expected"); | ||||
48 | | ||||
49 | RenameTypeInput params; | ||||
50 | params["macroReplace.sequence"] = 15; | ||||
51 | params["macroReplace.padding"] = 3; | ||||
52 | params["timestamp"] = QDateTime::fromTime_t(1522504691); // 2018-03-31 15:58:11 CET | ||||
53 | params["name"] = "file.ext"; | ||||
54 | params["baseName"] = "file"; | ||||
55 | params["extension"] = ".ext"; | ||||
56 | | ||||
57 | params["macroReplace.text"] = "NewName # - (%y) %Y%M%D (Renamed from #name)"; | ||||
58 | QTest::newRow("Macro replace with sequence, date and name") | ||||
59 | << params << "NewName 015 - (18) 20180331 (Renamed from file.ext)"; | ||||
60 | | ||||
61 | params["macroReplace.text"] = "#bname_%H%m%S#ext"; | ||||
62 | QTest::newRow("Macro replace with time, basename and extension") | ||||
63 | << params << "file_155811.ext"; | ||||
64 | | ||||
65 | params["macroReplace.text"] = "hello"; | ||||
66 | QTest::newRow("Macro replace with no placeholders") | ||||
67 | << params << "hello"; | ||||
68 | } | ||||
69 | | ||||
70 | void RenameTypesTest::testMacroReplace() | ||||
71 | { | ||||
72 | QFETCH(RenameTypeInput, input); | ||||
73 | QFETCH(QString, expected); | ||||
74 | | ||||
75 | QCOMPARE(RenameTypes::macroRenamer(input), expected); | ||||
76 | } | ||||
77 | | ||||
78 | void RenameTypesTest::testFindReplace_data() | ||||
79 | { | ||||
80 | QTest::addColumn<RenameTypeInput>("input"); | ||||
81 | QTest::addColumn<QString>("expected"); | ||||
82 | | ||||
83 | RenameTypeInput params; | ||||
84 | params["name"] = "Filename_for_find_and_replace.tar.gz"; | ||||
85 | params["findReplace.find"] = "find_"; | ||||
86 | params["findReplace.replace"] = ""; | ||||
87 | params["findReplace.useRegex"] = false; | ||||
88 | params["findReplace.caseInsensitive"] = false; | ||||
89 | | ||||
90 | QTest::newRow("Find & Replace no regex case sensitive") | ||||
91 | << params << "Filename_for_and_replace.tar.gz"; | ||||
92 | | ||||
93 | params["findReplace.find"] = "FIND_"; | ||||
94 | QTest::newRow("Find & Replace no regex case sensitive, should not replace anything") | ||||
95 | << params << "Filename_for_find_and_replace.tar.gz"; | ||||
96 | | ||||
97 | params["findReplace.find"] = "FIND_"; | ||||
98 | params["findReplace.caseInsensitive"] = true; | ||||
99 | QTest::newRow("Find & Replace no regex case insensitive") | ||||
100 | << params << "Filename_for_and_replace.tar.gz"; | ||||
101 | | ||||
102 | params["findReplace.useRegex"] = true; | ||||
103 | params["findReplace.caseInsensitive"] = false; | ||||
104 | params["findReplace.find"] = "[^a-z]"; | ||||
105 | QTest::newRow("Find & Replace regex, case sensitive. Remove all chars except a-z") | ||||
106 | << params << "ilenameforfindandreplacetargz"; | ||||
107 | | ||||
108 | params["findReplace.useRegex"] = true; | ||||
109 | params["findReplace.caseInsensitive"] = true; | ||||
110 | params["findReplace.find"] = "[^a-z]"; | ||||
111 | QTest::newRow("Find & Replace regex, case insensitive. Remove all non alpha chars") | ||||
112 | << params << "Filenameforfindandreplacetargz"; | ||||
113 | } | ||||
114 | | ||||
115 | void RenameTypesTest::testFindReplace() | ||||
116 | { | ||||
117 | QFETCH(RenameTypeInput, input); | ||||
118 | QFETCH(QString, expected); | ||||
119 | | ||||
120 | QCOMPARE(RenameTypes::findReplaceRenamer(input), expected); | ||||
121 | } | ||||
122 | | ||||
123 | void RenameTypesTest::testCaptureGroups_data() | ||||
124 | { | ||||
125 | QTest::addColumn<QHash<QString, QVariant>>("input"); | ||||
126 | QTest::addColumn<QString>("expected"); | ||||
127 | | ||||
128 | RenameTypeInput params; | ||||
129 | params["name"] = "myfavpodcast1x03-Episode Title(192kbps).mp3"; | ||||
130 | params["newName.find"] = R"(.+(\d)x(\d+)-(.+?)\()"; | ||||
131 | params["newName.replace"] = R"(Favourite Podcast S0\1E\2 - \3.mp3)"; | ||||
132 | | ||||
133 | QTest::addRow("Capture groups") << params << "Favourite Podcast S01E03 - Episode Title.mp3"; | ||||
134 | } | ||||
135 | | ||||
136 | void RenameTypesTest::testCaptureGroups() | ||||
137 | { | ||||
138 | QFETCH(RenameTypeInput, input); | ||||
139 | QFETCH(QString, expected); | ||||
140 | | ||||
141 | QCOMPARE(RenameTypes::captureRegexGroups(input), expected); | ||||
142 | } | ||||
143 | | ||||
144 | QTEST_MAIN(RenameTypesTest) | ||||
145 | | ||||
146 | #include "renametypestest.moc" |