File indexing completed on 2024-03-24 17:25:10

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 "krenamefiletest.h"
0019 
0020 #include <QtTest>
0021 
0022 QTEST_GUILESS_MAIN(KRenameFileTest)
0023 
0024 void KRenameFileTest::testKRenameFile()
0025 {
0026     // Test a simple filename
0027     testKRenameFileInternal(QUrl("file:///home/test/simple.txt"), "/home/test",
0028                             "simple", "txt", eSplitMode_FirstDot, 0, false);
0029     testKRenameFileInternal(QUrl("file:///home/test/simple.txt"), "/home/test",
0030                             "simple", "txt", eSplitMode_LastDot, 0, false);
0031     testKRenameFileInternal(QUrl("file:///home/test/simple.txt"), "/home/test",
0032                             "simple.txt", "", eSplitMode_NoExtension, 0, false);
0033     testKRenameFileInternal(QUrl("file:///home/test/simple.txt"), "/home/test",
0034                             "simple.txt", "", eSplitMode_NoExtension, 1, false);
0035     testKRenameFileInternal(QUrl("file:///home/test/simple.txt"), "/home/test",
0036                             "simple.txt", "", eSplitMode_CustomDot, 0, false);
0037     testKRenameFileInternal(QUrl("file:///home/test/simple.txt"), "/home/test",
0038                             "simple", "txt", eSplitMode_CustomDot, 1, false);
0039 
0040     // Test a simple filename and a directory with spaces
0041     testKRenameFileInternal(QUrl("file:///home/dir with space/simple.txt"),
0042                             "/home/dir with space", "simple", "txt",
0043                             eSplitMode_FirstDot, 0, false);
0044     testKRenameFileInternal(QUrl("file:///home/dir with space/simple.txt"),
0045                             "/home/dir with space", "simple", "txt",
0046                             eSplitMode_LastDot, 0, false);
0047     testKRenameFileInternal(QUrl("file:///home/dir with space/simple.txt"),
0048                             "/home/dir with space", "simple.txt", "",
0049                             eSplitMode_CustomDot, 0, false);
0050     testKRenameFileInternal(QUrl("file:///home/dir with space/simple.txt"),
0051                             "/home/dir with space", "simple", "txt",
0052                             eSplitMode_CustomDot, 1, false);
0053 
0054     // test a more complicated file extension
0055     testKRenameFileInternal(QUrl("file:///home/test/complicated.tar.gz"),
0056                             "/home/test", "complicated", "tar.gz",
0057                             eSplitMode_FirstDot, 0, false);
0058     testKRenameFileInternal(QUrl("file:///home/test/complicated.tar.gz"),
0059                             "/home/test", "complicated.tar", "gz",
0060                             eSplitMode_LastDot, 0, false);
0061     testKRenameFileInternal(QUrl("file:///home/test/complicated.tar.gz"),
0062                             "/home/test", "complicated.tar.gz", "",
0063                             eSplitMode_CustomDot, 0, false);
0064     testKRenameFileInternal(QUrl("file:///home/test/complicated.tar.gz"),
0065                             "/home/test", "complicated", "tar.gz",
0066                             eSplitMode_CustomDot, 1, false);
0067     testKRenameFileInternal(QUrl("file:///home/test/complicated.tar.gz"),
0068                             "/home/test", "complicated.tar", "gz",
0069                             eSplitMode_CustomDot, 2, false);
0070     testKRenameFileInternal(QUrl("file:///home/test/complicated.tar.gz"),
0071                             "/home/test", "complicated.tar.gz", "",
0072                             eSplitMode_CustomDot, 3, false);
0073 
0074     // test a directory with dot
0075     testKRenameFileInternal(QUrl("file:///home/dir.with.dot/simple.txt"),
0076                             "/home/dir.with.dot", "simple", "txt",
0077                             eSplitMode_FirstDot, 0, false);
0078     testKRenameFileInternal(QUrl("file:///home/dir.with.dot/simple.txt"),
0079                             "/home/dir.with.dot", "simple", "txt",
0080                             eSplitMode_LastDot, 0, false);
0081     testKRenameFileInternal(QUrl("file:///home/dir.with.dot/simple.txt"),
0082                             "/home/dir.with.dot", "simple.txt", "",
0083                             eSplitMode_CustomDot, 0, false);
0084     testKRenameFileInternal(QUrl("file:///home/dir.with.dot/simple.txt"),
0085                             "/home/dir.with.dot", "simple", "txt",
0086                             eSplitMode_CustomDot, 1, false);
0087 
0088     // test a directory which ends with a slash
0089     testKRenameFileInternal(QUrl("file:///home/test/"), "/home",
0090                             "test", "", eSplitMode_FirstDot, 0, true);
0091     testKRenameFileInternal(QUrl("file:///home/test/"), "/home",
0092                             "test", "", eSplitMode_LastDot, 0, true);
0093     testKRenameFileInternal(QUrl("file:///home/test/"), "/home",
0094                             "test", "", eSplitMode_CustomDot, 0, true);
0095     testKRenameFileInternal(QUrl("file:///home/test/"), "/home",
0096                             "test", "", eSplitMode_CustomDot, 1, true);
0097 
0098     // test a directory which doesn't end with a slash
0099     testKRenameFileInternal(QUrl("file:///home/test"), "/home",
0100                             "test", "", eSplitMode_FirstDot, 0, true);
0101     testKRenameFileInternal(QUrl("file:///home/test"), "/home",
0102                             "test", "", eSplitMode_LastDot, 0, true);
0103     testKRenameFileInternal(QUrl("file:///home/test"), "/home",
0104                             "test", "", eSplitMode_CustomDot, 0, true);
0105     testKRenameFileInternal(QUrl("file:///home/test"), "/home",
0106                             "test", "", eSplitMode_CustomDot, 1, true);
0107 
0108     // test no file extension
0109     testKRenameFileInternal(QUrl("file:///home/test/simple"), "/home/test",
0110                             "simple", "", eSplitMode_FirstDot, 0, false);
0111     testKRenameFileInternal(QUrl("file:///home/test/simple"), "/home/test",
0112                             "simple", "", eSplitMode_LastDot, 0, false);
0113     testKRenameFileInternal(QUrl("file:///home/test/simple"), "/home/test",
0114                             "simple", "", eSplitMode_CustomDot, 0, false);
0115     testKRenameFileInternal(QUrl("file:///home/test/simple"), "/home/test",
0116                             "simple", "", eSplitMode_CustomDot, 1, false);
0117 
0118     // test very long complex file extension
0119     testKRenameFileInternal(QUrl("file:///home/test/simple.txt.long.ext.zip"),
0120                             "/home/test", "simple", "txt.long.ext.zip",
0121                             eSplitMode_FirstDot, 0, false);
0122     testKRenameFileInternal(QUrl("file:///home/test/simple.txt.long.ext.zip"),
0123                             "/home/test", "simple.txt.long.ext", "zip",
0124                             eSplitMode_LastDot, 0, false);
0125     testKRenameFileInternal(QUrl("file:///home/test/simple.txt.long.ext.zip"),
0126                             "/home/test", "simple.txt.long.ext.zip", "",
0127                             eSplitMode_CustomDot, 0, false);
0128     testKRenameFileInternal(QUrl("file:///home/test/simple.txt.long.ext.zip"),
0129                             "/home/test", "simple", "txt.long.ext.zip",
0130                             eSplitMode_CustomDot, 1, false);
0131     testKRenameFileInternal(QUrl("file:///home/test/simple.txt.long.ext.zip"),
0132                             "/home/test", "simple.txt", "long.ext.zip",
0133                             eSplitMode_CustomDot, 2, false);
0134     testKRenameFileInternal(QUrl("file:///home/test/simple.txt.long.ext.zip"),
0135                             "/home/test", "simple.txt.long", "ext.zip",
0136                             eSplitMode_CustomDot, 3, false);
0137     testKRenameFileInternal(QUrl("file:///home/test/simple.txt.long.ext.zip"),
0138                             "/home/test", "simple.txt.long.ext", "zip",
0139                             eSplitMode_CustomDot, 4, false);
0140     testKRenameFileInternal(QUrl("file:///home/test/simple.txt.long.ext.zip"),
0141                             "/home/test", "simple.txt.long.ext.zip", "",
0142                             eSplitMode_CustomDot, 5, false);
0143 }
0144 
0145 void KRenameFileTest::testKRenameFileInternal(const QUrl &url, const QString &directory,
0146         const QString &filename, const QString &extension,
0147         ESplitMode eSplitMode, int dot, bool isDir)
0148 {
0149     // if the expected filename and extension is empty expect a directory
0150     //bool        isDirectory = filename.isEmpty() && extension.isEmpty();
0151     KRenameFile file(url, isDir, eSplitMode, dot);
0152 
0153     QCOMPARE(file.srcDirectory(), directory);
0154     QCOMPARE(file.srcFilename(), filename);
0155     QCOMPARE(file.srcExtension(), extension);
0156 }