testfilesearchpath.cpp
1 //------------------------------------------------------------------------------
2 // testfilesearchpath.cpp
3 //------------------------------------------------------------------------------
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.
14 //
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
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2023 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "testfilesearchpath.h"
24 
25 #include "pathfinder/filesearchpath.h"
26 
27 #define ASSERT(x) { if (!(x)) { testLog << "Failed: ASSERT(" #x ")"; return false; }}
28 #define ASSERT_FALSE(x) { if (x) { testLog << "Failed: ASSERT_FALSE(" #x ")"; return false;}}
29 
30 bool TestFileSearchPathContains::executeTest()
31 {
32  FileSearchPath simplePath("avo/ca");
33  ASSERT(simplePath.contains("avo/ca"));
34  ASSERT(simplePath.contains("avo/ca/"));
35  ASSERT(simplePath.contains("avo//ca//"));
36  ASSERT_FALSE(simplePath.contains("avo/c"));
37  ASSERT_FALSE(simplePath.contains("avo/cado"));
38  ASSERT_FALSE(simplePath.contains("avo/ca/do"));
39  ASSERT_FALSE(simplePath.contains("avo"));
40  #ifdef Q_OS_WIN
41  ASSERT(simplePath.contains("avo\\ca"));
42  ASSERT(simplePath.contains("avo\\ca\\"));
43  ASSERT_FALSE(simplePath.contains("avo\\ca\\do"));
44  ASSERT_FALSE(simplePath.contains("avo\\cado"));
45  #endif
46 
47  FileSearchPath recursivePath("avo/ca");
48  recursivePath.setRecursive(true);
49  ASSERT(recursivePath.contains("avo/ca"));
50  ASSERT(recursivePath.contains("avo/ca/"));
51  ASSERT(recursivePath.contains("avo//ca//"));
52  ASSERT_FALSE(recursivePath.contains("avo/c"));
53  ASSERT_FALSE(recursivePath.contains("avo/cado"));
54  ASSERT(recursivePath.contains("avo/ca/do"));
55  ASSERT_FALSE(recursivePath.contains("avo"));
56  #ifdef Q_OS_WIN
57  ASSERT(recursivePath.contains("avo\\ca"));
58  ASSERT(recursivePath.contains("avo\\ca\\"));
59  ASSERT(recursivePath.contains("avo\\ca\\do"));
60  ASSERT_FALSE(recursivePath.contains("avo\\cado"));
61  #endif
62 
63  return true;
64 }
65 
66 bool TestFileSearchPathMerge::executeTest()
67 {
68  // Given
69  QList<FileSearchPath> paths;
70  paths << FileSearchPath("avo//ca");
71  paths << FileSearchPath("avo/ca/");
72  paths << FileSearchPath("avo/ca/do");
73  paths << FileSearchPath("ba/na/na");
74  paths << []() { FileSearchPath p("ba/na"); p.setRecursive(true); return p; }();
75  paths << []() { FileSearchPath p("ap"); p.setRecursive(true); return p; }();
76  paths << []() { FileSearchPath p("ap/ple"); p.setRecursive(true); return p; }();
77  paths << FileSearchPath("recuA");
78  paths << []() { FileSearchPath p("recuA"); p.setRecursive(true); return p; }();
79  paths << []() { FileSearchPath p("recuB"); p.setRecursive(true); return p; }();
80  paths << FileSearchPath("recuB");
81 
82  // When
83  FileSearchPath::merge(paths);
84 
85  // Then
86  testLog << QString("paths.length() == %1").arg(paths.length());
87  for (int idx = 0; idx < paths.length(); ++idx)
88  {
89  testLog << QString("paths[%1]: %2%3")
90  .arg(idx)
91  .arg(paths[idx].path())
92  .arg(paths[idx].isRecursive() ? " (R)" : "");
93  }
94  ASSERT(paths.length() == 6);
95  ASSERT(paths[0].path() == "avo/ca/");
96  ASSERT_FALSE(paths[0].isRecursive());
97  ASSERT(paths[1].path() == "avo/ca/do");
98  ASSERT_FALSE(paths[1].isRecursive());
99  ASSERT(paths[2].path() == "ba/na");
100  ASSERT(paths[2].isRecursive());
101  ASSERT(paths[3].path() == "ap");
102  ASSERT(paths[3].isRecursive());
103  ASSERT(paths[4].path() == "recuA");
104  ASSERT(paths[4].isRecursive());
105  ASSERT(paths[5].path() == "recuB");
106  ASSERT(paths[5].isRecursive());
107 
108  return true;
109 }