Added test cases for clean_file_name() function.
[MUtilities.git] / test / src / MUtilitiesTest.cpp
blobbaa116d8e4cb3585d03152f7ae5f6169dac11695
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
22 //Google Test
23 #include <gtest/gtest.h>
25 //MUtils
26 #include <MUtils/Global.h>
27 #include <MUtils/Version.h>
29 //CRT
30 #include <cstdio>
32 //Utilities
33 #define ASSERT_QSTR(X,Y) ASSERT_EQ((X).compare(QLatin1String(Y)), 0);
35 //===========================================================================
36 // GLOBAL
37 //===========================================================================
39 //-----------------------------------------------------------------
40 // Trim String
41 //-----------------------------------------------------------------
43 #define TEST_TRIM_STR(X,Y,Z) do \
44 { \
45 { \
46 QString test((Y)); \
47 MUtils::trim_##X(test); \
48 ASSERT_QSTR(test, (Z)); \
49 } \
50 { \
51 const QString test((Y)); \
52 ASSERT_QSTR(MUtils::trim_##X(test), (Z)); \
53 } \
54 } \
55 while(0)
57 TEST(Global, TrimStringLeft)
59 TEST_TRIM_STR(left, "", "");
60 TEST_TRIM_STR(left, " ", "");
61 TEST_TRIM_STR(left, "! test !", "! test !");
62 TEST_TRIM_STR(left, " test ", "test ");
63 TEST_TRIM_STR(left, " ! test ! ", "! test ! ");
66 TEST(Global, TrimStringRight)
68 TEST_TRIM_STR(right, "", "");
69 TEST_TRIM_STR(right, " ", "");
70 TEST_TRIM_STR(right, "! test !", "! test !");
71 TEST_TRIM_STR(right, " test ", " test");
72 TEST_TRIM_STR(right, " ! test ! ", " ! test !");
75 #undef TEST_TRIM_STR
77 //-----------------------------------------------------------------
78 // Clean File Path
79 //-----------------------------------------------------------------
81 #define TEST_CLEAN_FILE(X,Y,Z) do \
82 { \
83 ASSERT_QSTR(MUtils::clean_file_##X((Y)), (Z)); \
84 } \
85 while(0)
87 TEST(Global, CleanFileName)
89 static const char *const VALID_CHARS = "!#$%&'()+,-.0123456789;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{}~";
90 TEST_CLEAN_FILE(name, "", "");
91 TEST_CLEAN_FILE(name, VALID_CHARS, VALID_CHARS);
92 TEST_CLEAN_FILE(name, "example.txt", "example.txt");
93 TEST_CLEAN_FILE(name, " example.txt", " example.txt");
94 TEST_CLEAN_FILE(name, "example.txt ", "example.txt");
95 TEST_CLEAN_FILE(name, ".example.txt", ".example.txt");
96 TEST_CLEAN_FILE(name, "example.txt.", "example.txt");
97 TEST_CLEAN_FILE(name, "foo<>:\"/\\|?*\t\r\n.bar", "foo____________.bar");
98 TEST_CLEAN_FILE(name, "NUL", "___");
99 TEST_CLEAN_FILE(name, "NUL.txt", "___.txt");
100 TEST_CLEAN_FILE(name, "NULx.txt", "NULx.txt");
101 TEST_CLEAN_FILE(name, "xNUL.txt", "xNUL.txt");
104 //===========================================================================
105 // Main function
106 //===========================================================================
108 int main(int argc, char **argv)
110 printf("MuldeR's Utilities for Qt - Test Suite\n");
111 printf("Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>\n\n");
113 printf("Using MUtils v%u.%02u, built %s at %s, %s [%s]\n\n",
114 MUtils::Version::lib_version_major(), MUtils::Version::lib_version_minor(),
115 MUTILS_UTF8(MUtils::Version::lib_build_date().toString(Qt::ISODate)), MUTILS_UTF8(MUtils::Version::lib_build_time().toString(Qt::ISODate)),
116 MUtils::Version::compiler_version(), MUtils::Version::compiler_arch()
119 printf("This library is free software; you can redistribute it and/or\n");
120 printf("modify it under the terms of the GNU Lesser General Public\n");
121 printf("License as published by the Free Software Foundation; either\n");
122 printf("version 2.1 of the License, or (at your option) any later version.\n\n");
124 ::testing::InitGoogleTest(&argc, argv);
125 return RUN_ALL_TESTS();