4343
4444using namespace yup ;
4545
46+ TEST (FileSearchPathTests, CopyConstructor)
47+ {
48+ #if YUP_WINDOWS
49+ const String prefix = " C:" ;
50+ #else
51+ const String prefix = " " ;
52+ #endif
53+
54+ FileSearchPath fsp1 { prefix + " /a/b;" + prefix + " /c/d" };
55+ FileSearchPath fsp2 (fsp1);
56+
57+ EXPECT_EQ (fsp1.toString (), fsp2.toString ());
58+ EXPECT_EQ (fsp2.getNumPaths (), 2 );
59+ }
60+
61+ TEST (FileSearchPathTests, CopyAssignment)
62+ {
63+ #if YUP_WINDOWS
64+ const String prefix = " C:" ;
65+ #else
66+ const String prefix = " " ;
67+ #endif
68+
69+ FileSearchPath fsp1 { prefix + " /a/b;" + prefix + " /c/d" };
70+ FileSearchPath fsp2;
71+ fsp2 = fsp1;
72+
73+ EXPECT_EQ (fsp1.toString (), fsp2.toString ());
74+ EXPECT_EQ (fsp2.getNumPaths (), 2 );
75+ }
76+
77+ TEST (FileSearchPathTests, StringAssignment)
78+ {
79+ #if YUP_WINDOWS
80+ const String prefix = " C:" ;
81+ #else
82+ const String prefix = " " ;
83+ #endif
84+
85+ FileSearchPath fsp;
86+ fsp = prefix + " /a/b;" + prefix + " /c/d" ;
87+
88+ EXPECT_EQ (fsp.getNumPaths (), 2 );
89+ EXPECT_EQ (fsp[0 ].getFullPathName (), prefix + " /a/b" );
90+ EXPECT_EQ (fsp[1 ].getFullPathName (), prefix + " /c/d" );
91+ }
92+
93+ TEST (FileSearchPathTests, GetNumPaths)
94+ {
95+ #if YUP_WINDOWS
96+ const String prefix = " C:" ;
97+ #else
98+ const String prefix = " " ;
99+ #endif
100+
101+ FileSearchPath empty;
102+ EXPECT_EQ (empty.getNumPaths (), 0 );
103+
104+ FileSearchPath fsp { prefix + " /a/b;" + prefix + " /c/d;" + prefix + " /e/f" };
105+ EXPECT_EQ (fsp.getNumPaths (), 3 );
106+ }
107+
108+ TEST (FileSearchPathTests, IndexOperator)
109+ {
110+ #if YUP_WINDOWS
111+ const String prefix = " C:" ;
112+ #else
113+ const String prefix = " " ;
114+ #endif
115+
116+ FileSearchPath fsp { prefix + " /a/b;" + prefix + " /c/d" };
117+ EXPECT_EQ (fsp[0 ].getFullPathName (), prefix + " /a/b" );
118+ EXPECT_EQ (fsp[1 ].getFullPathName (), prefix + " /c/d" );
119+ }
120+
121+ TEST (FileSearchPathTests, GetRawString)
122+ {
123+ #if YUP_WINDOWS
124+ const String prefix = " C:" ;
125+ #else
126+ const String prefix = " " ;
127+ #endif
128+
129+ FileSearchPath fsp { prefix + " /a/b;" + prefix + " /c/d" };
130+ EXPECT_EQ (fsp.getRawString (0 ), prefix + " /a/b" );
131+ EXPECT_EQ (fsp.getRawString (1 ), prefix + " /c/d" );
132+ }
133+
134+ TEST (FileSearchPathTests, Add)
135+ {
136+ #if YUP_WINDOWS
137+ const String prefix = " C:" ;
138+ #else
139+ const String prefix = " " ;
140+ #endif
141+
142+ FileSearchPath fsp;
143+ fsp.add (File (prefix + " /a/b" ));
144+ fsp.add (File (prefix + " /c/d" ));
145+
146+ EXPECT_EQ (fsp.getNumPaths (), 2 );
147+ EXPECT_EQ (fsp[0 ].getFullPathName (), prefix + " /a/b" );
148+ EXPECT_EQ (fsp[1 ].getFullPathName (), prefix + " /c/d" );
149+ }
150+
151+ TEST (FileSearchPathTests, AddWithIndex)
152+ {
153+ #if YUP_WINDOWS
154+ const String prefix = " C:" ;
155+ #else
156+ const String prefix = " " ;
157+ #endif
158+
159+ FileSearchPath fsp { prefix + " /a/b;" + prefix + " /c/d" };
160+ fsp.add (File (prefix + " /e/f" ), 1 );
161+
162+ EXPECT_EQ (fsp.getNumPaths (), 3 );
163+ EXPECT_EQ (fsp[0 ].getFullPathName (), prefix + " /a/b" );
164+ EXPECT_EQ (fsp[1 ].getFullPathName (), prefix + " /e/f" );
165+ EXPECT_EQ (fsp[2 ].getFullPathName (), prefix + " /c/d" );
166+ }
167+
168+ TEST (FileSearchPathTests, AddIfNotAlreadyThere)
169+ {
170+ #if YUP_WINDOWS
171+ const String prefix = " C:" ;
172+ #else
173+ const String prefix = " " ;
174+ #endif
175+
176+ FileSearchPath fsp { prefix + " /a/b;" + prefix + " /c/d" };
177+
178+ EXPECT_TRUE (fsp.addIfNotAlreadyThere (File (prefix + " /e/f" )));
179+ EXPECT_EQ (fsp.getNumPaths (), 3 );
180+
181+ EXPECT_FALSE (fsp.addIfNotAlreadyThere (File (prefix + " /a/b" )));
182+ EXPECT_EQ (fsp.getNumPaths (), 3 );
183+ }
184+
185+ TEST (FileSearchPathTests, Remove)
186+ {
187+ #if YUP_WINDOWS
188+ const String prefix = " C:" ;
189+ #else
190+ const String prefix = " " ;
191+ #endif
192+
193+ FileSearchPath fsp { prefix + " /a/b;" + prefix + " /c/d;" + prefix + " /e/f" };
194+ fsp.remove (1 );
195+
196+ EXPECT_EQ (fsp.getNumPaths (), 2 );
197+ EXPECT_EQ (fsp[0 ].getFullPathName (), prefix + " /a/b" );
198+ EXPECT_EQ (fsp[1 ].getFullPathName (), prefix + " /e/f" );
199+ }
200+
201+ TEST (FileSearchPathTests, AddPath)
202+ {
203+ #if YUP_WINDOWS
204+ const String prefix = " C:" ;
205+ #else
206+ const String prefix = " " ;
207+ #endif
208+
209+ FileSearchPath fsp1 { prefix + " /a/b;" + prefix + " /c/d" };
210+ FileSearchPath fsp2 { prefix + " /e/f;" + prefix + " /g/h" };
211+
212+ fsp1.addPath (fsp2);
213+
214+ EXPECT_EQ (fsp1.getNumPaths (), 4 );
215+ EXPECT_EQ (fsp1[2 ].getFullPathName (), prefix + " /e/f" );
216+ EXPECT_EQ (fsp1[3 ].getFullPathName (), prefix + " /g/h" );
217+ }
218+
219+ TEST (FileSearchPathTests, AddPathSkipsDuplicates)
220+ {
221+ #if YUP_WINDOWS
222+ const String prefix = " C:" ;
223+ #else
224+ const String prefix = " " ;
225+ #endif
226+
227+ FileSearchPath fsp1 { prefix + " /a/b;" + prefix + " /c/d" };
228+ FileSearchPath fsp2 { prefix + " /c/d;" + prefix + " /e/f" };
229+
230+ fsp1.addPath (fsp2);
231+
232+ EXPECT_EQ (fsp1.getNumPaths (), 3 );
233+ EXPECT_EQ (fsp1[0 ].getFullPathName (), prefix + " /a/b" );
234+ EXPECT_EQ (fsp1[1 ].getFullPathName (), prefix + " /c/d" );
235+ EXPECT_EQ (fsp1[2 ].getFullPathName (), prefix + " /e/f" );
236+ }
237+
238+ TEST (FileSearchPathTests, RemoveNonExistentPaths)
239+ {
240+ #if YUP_WINDOWS
241+ const String prefix = " C:" ;
242+ #else
243+ const String prefix = " " ;
244+ #endif
245+
246+ auto tempDir = File::getSpecialLocation (File::SpecialLocationType::tempDirectory);
247+ auto existingPath = tempDir.getChildFile (" test_existing_path" );
248+ existingPath.createDirectory ();
249+
250+ FileSearchPath fsp;
251+ fsp.add (existingPath);
252+ fsp.add (File (prefix + " /nonexistent/path/12345" ));
253+
254+ EXPECT_EQ (fsp.getNumPaths (), 2 );
255+
256+ fsp.removeNonExistentPaths ();
257+
258+ EXPECT_EQ (fsp.getNumPaths (), 1 );
259+ EXPECT_EQ (fsp[0 ].getFullPathName (), existingPath.getFullPathName ());
260+
261+ existingPath.deleteFile ();
262+ }
263+
264+ TEST (FileSearchPathTests, FindChildFilesArray)
265+ {
266+ auto tempDir = File::getSpecialLocation (File::SpecialLocationType::tempDirectory);
267+ auto testDir = tempDir.getChildFile (" test_find_child_files" );
268+ testDir.createDirectory ();
269+
270+ auto subDir = testDir.getChildFile (" subdir" );
271+ subDir.createDirectory ();
272+
273+ auto file1 = testDir.getChildFile (" test1.txt" );
274+ auto file2 = testDir.getChildFile (" test2.txt" );
275+ auto file3 = subDir.getChildFile (" test3.txt" );
276+
277+ file1.create ();
278+ file2.create ();
279+ file3.create ();
280+
281+ FileSearchPath fsp;
282+ fsp.add (testDir);
283+
284+ auto files = fsp.findChildFiles (File::findFiles, false , " *.txt" );
285+ EXPECT_EQ (files.size (), 2 );
286+
287+ auto filesRecursive = fsp.findChildFiles (File::findFiles, true , " *.txt" );
288+ EXPECT_EQ (filesRecursive.size (), 3 );
289+
290+ file1.deleteFile ();
291+ file2.deleteFile ();
292+ file3.deleteFile ();
293+ subDir.deleteFile ();
294+ testDir.deleteFile ();
295+ }
296+
297+ TEST (FileSearchPathTests, FindChildFilesWithResults)
298+ {
299+ auto tempDir = File::getSpecialLocation (File::SpecialLocationType::tempDirectory);
300+ auto testDir = tempDir.getChildFile (" test_find_child_files2" );
301+ testDir.createDirectory ();
302+
303+ auto file1 = testDir.getChildFile (" test1.txt" );
304+ auto file2 = testDir.getChildFile (" test2.txt" );
305+
306+ file1.create ();
307+ file2.create ();
308+
309+ FileSearchPath fsp;
310+ fsp.add (testDir);
311+
312+ Array<File> results;
313+ int count = fsp.findChildFiles (results, File::findFiles, false , " *.txt" );
314+
315+ EXPECT_EQ (count, 2 );
316+ EXPECT_EQ (results.size (), 2 );
317+
318+ file1.deleteFile ();
319+ file2.deleteFile ();
320+ testDir.deleteFile ();
321+ }
322+
323+ TEST (FileSearchPathTests, IsFileInPathNonRecursive)
324+ {
325+ auto tempDir = File::getSpecialLocation (File::SpecialLocationType::tempDirectory);
326+ auto testDir = tempDir.getChildFile (" test_is_file_in_path" );
327+ testDir.createDirectory ();
328+
329+ auto subDir = testDir.getChildFile (" subdir" );
330+ subDir.createDirectory ();
331+
332+ auto file1 = testDir.getChildFile (" test1.txt" );
333+ auto file2 = subDir.getChildFile (" test2.txt" );
334+
335+ file1.create ();
336+ file2.create ();
337+
338+ FileSearchPath fsp;
339+ fsp.add (testDir);
340+
341+ EXPECT_TRUE (fsp.isFileInPath (file1, false ));
342+ EXPECT_FALSE (fsp.isFileInPath (file2, false ));
343+
344+ file1.deleteFile ();
345+ file2.deleteFile ();
346+ subDir.deleteFile ();
347+ testDir.deleteFile ();
348+ }
349+
350+ TEST (FileSearchPathTests, IsFileInPathRecursive)
351+ {
352+ auto tempDir = File::getSpecialLocation (File::SpecialLocationType::tempDirectory);
353+ auto testDir = tempDir.getChildFile (" test_is_file_in_path_rec" );
354+ testDir.createDirectory ();
355+
356+ auto subDir = testDir.getChildFile (" subdir" );
357+ subDir.createDirectory ();
358+
359+ auto file1 = testDir.getChildFile (" test1.txt" );
360+ auto file2 = subDir.getChildFile (" test2.txt" );
361+
362+ file1.create ();
363+ file2.create ();
364+
365+ FileSearchPath fsp;
366+ fsp.add (testDir);
367+
368+ EXPECT_TRUE (fsp.isFileInPath (file1, true ));
369+ EXPECT_TRUE (fsp.isFileInPath (file2, true ));
370+
371+ file1.deleteFile ();
372+ file2.deleteFile ();
373+ subDir.deleteFile ();
374+ testDir.deleteFile ();
375+ }
376+
46377TEST (FileSearchPathTests, RemoveRedundantPaths)
47378{
48379#if YUP_WINDOWS
@@ -74,4 +405,4 @@ TEST (FileSearchPathTests, RemoveRedundantPaths)
74405 fsp.removeRedundantPaths ();
75406 EXPECT_EQ (fsp.toString (), " %FOO%;" + prefix + " /a/b/c" );
76407 }
77- }
408+ }
0 commit comments