File: batchrenamejobtest.cpp

package info (click to toggle)
kio 5.116.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,496 kB
  • sloc: cpp: 123,468; xml: 528; ansic: 466; ruby: 60; sh: 21; makefile: 13
file content (126 lines) | stat: -rw-r--r-- 4,855 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
    This file is part of the KDE libraries
    SPDX-FileCopyrightText: 2017 Chinmoy Ranjan Pradhan <chinmoyrp65@gmail.com>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#include <QSignalSpy>
#include <QTest>

#include <KIO/BatchRenameJob>

#include "kiotesthelper.h"

class BatchRenameJobTest : public QObject
{
    Q_OBJECT

private:
    void createTestFiles(const QStringList &fileList)
    {
        for (const QString &filename : fileList) {
            createTestFile(m_homeDir + filename);
        }
    }

    bool checkFileExistence(const QStringList &fileList)
    {
        for (const QString &filename : fileList) {
            const QString filePath = m_homeDir + filename;
            if (!QFile::exists(filePath)) {
                return false;
            }
        }
        return true;
    }

    QList<QUrl> createUrlList(const QStringList &fileList)
    {
        QList<QUrl> srcList;
        srcList.reserve(fileList.count());
        for (const QString &filename : fileList) {
            const QString filePath = m_homeDir + filename;
            srcList.append(QUrl::fromLocalFile(filePath));
        }
        return srcList;
    }

private Q_SLOTS:
    void initTestCase()
    {
        QStandardPaths::setTestModeEnabled(true);

        cleanupTestCase();

        // Create temporary home directory
        m_homeDir = homeTmpDir();
    }

    void cleanupTestCase()
    {
        QDir(homeTmpDir()).removeRecursively();
    }

    void batchRenameJobTest_data()
    {
        QTest::addColumn<QStringList>("oldFilenames");
        QTest::addColumn<QString>("baseName");
        QTest::addColumn<int>("index");
        QTest::addColumn<QChar>("indexPlaceholder");
        QTest::addColumn<QStringList>("newFilenames");

        /* clang-format off */
        QTest::newRow("different-extensions-single-placeholder") << (QStringList{"old_file_without_extension", "old_file.txt", "old_file.zip"})
                                                                 << "#-new_name"
                                                                 << 1
                                                                 << QChar('#')
                                                                 << QStringList{"1-new_name", "2-new_name.txt", "3-new_name.zip"};

        QTest::newRow("same-extensions-placeholder-sequence") << (QStringList{"first_source.cpp", "second_source.cpp", "third_source.java"})
                                                              << "new_source###"
                                                              << 8
                                                              << QChar('#')
                                                              << QStringList{"new_source008.cpp", "new_source009.cpp", "new_source010.java"};

        QTest::newRow("different-extensions-invalid-placeholder") << (QStringList{"audio.mp3", "video.mp4", "movie.mkv"})
                                                                  << "me#d#ia"
                                                                  << 0
                                                                  << QChar('#')
                                                                  << QStringList{"me#d#ia.mp3", "me#d#ia.mp4", "me#d#ia.mkv"};

        QTest::newRow("same-extensions-invalid-placeholder") << (QStringList{"random_headerfile.h", "another_headerfile.h", "random_sourcefile.c"})
                                                             << "##file#"
                                                             << 4
                                                             << QChar('#')
                                                             << QStringList{"##file#4.h", "##file#5.h", "##file#6.c"};
        /* clang-format on */
    }

    void batchRenameJobTest()
    {
        QFETCH(QStringList, oldFilenames);
        QFETCH(QString, baseName);
        QFETCH(int, index);
        QFETCH(QChar, indexPlaceholder);
        QFETCH(QStringList, newFilenames);
        createTestFiles(oldFilenames);
        QVERIFY(checkFileExistence(oldFilenames));
        KIO::BatchRenameJob *job = KIO::batchRename(createUrlList(oldFilenames), baseName, index, indexPlaceholder);
        job->setUiDelegate(nullptr);
        QSignalSpy spy(job, &KIO::BatchRenameJob::fileRenamed);
        QVERIFY2(job->exec(), qPrintable(job->errorString()));
        QCOMPARE(spy.count(), oldFilenames.count());
        QCOMPARE(job->processedAmount(KJob::Items), oldFilenames.count());
        QCOMPARE(job->totalAmount(KJob::Items), oldFilenames.count());
        QVERIFY(!checkFileExistence(oldFilenames));
        QVERIFY(checkFileExistence(newFilenames));
    }

private:
    QString m_homeDir;
};

QTEST_MAIN(BatchRenameJobTest)

#include "batchrenamejobtest.moc"