Lab_1 0.1.1
Matrix Library
Loading...
Searching...
No Matches
googletest-filepath-test.cc
1// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Google Test filepath utilities
31//
32// This file tests classes and functions used internally by
33// Google Test. They are subject to change without notice.
34//
35// This file is #included from gtest-internal.h.
36// Do not #include this file anywhere else!
37
38#include "gtest/gtest.h"
39#include "gtest/internal/gtest-filepath.h"
40#include "src/gtest-internal-inl.h"
41
42#if GTEST_OS_WINDOWS_MOBILE
43#include <windows.h> // NOLINT
44#elif GTEST_OS_WINDOWS
45#include <direct.h> // NOLINT
46#endif // GTEST_OS_WINDOWS_MOBILE
47
48namespace testing {
49namespace internal {
50namespace {
51
52#if GTEST_OS_WINDOWS_MOBILE
53
54// Windows CE doesn't have the remove C function.
55int remove(const char* path) {
56 LPCWSTR wpath = String::AnsiToUtf16(path);
57 int ret = DeleteFile(wpath) ? 0 : -1;
58 delete[] wpath;
59 return ret;
60}
61// Windows CE doesn't have the _rmdir C function.
62int _rmdir(const char* path) {
63 FilePath filepath(path);
64 LPCWSTR wpath =
65 String::AnsiToUtf16(filepath.RemoveTrailingPathSeparator().c_str());
66 int ret = RemoveDirectory(wpath) ? 0 : -1;
67 delete[] wpath;
68 return ret;
69}
70
71#else
72
73TEST(GetCurrentDirTest, ReturnsCurrentDir) {
74 const FilePath original_dir = FilePath::GetCurrentDir();
75 EXPECT_FALSE(original_dir.IsEmpty());
76
77 posix::ChDir(GTEST_PATH_SEP_);
78 const FilePath cwd = FilePath::GetCurrentDir();
79 posix::ChDir(original_dir.c_str());
80
81#if GTEST_OS_WINDOWS || GTEST_OS_OS2
82
83 // Skips the ":".
84 const char* const cwd_without_drive = strchr(cwd.c_str(), ':');
85 ASSERT_TRUE(cwd_without_drive != NULL);
86 EXPECT_STREQ(GTEST_PATH_SEP_, cwd_without_drive + 1);
87
88#else
89
90 EXPECT_EQ(GTEST_PATH_SEP_, cwd.string());
91
92#endif
93}
94
95#endif // GTEST_OS_WINDOWS_MOBILE
96
97TEST(IsEmptyTest, ReturnsTrueForEmptyPath) {
98 EXPECT_TRUE(FilePath("").IsEmpty());
99}
100
101TEST(IsEmptyTest, ReturnsFalseForNonEmptyPath) {
102 EXPECT_FALSE(FilePath("a").IsEmpty());
103 EXPECT_FALSE(FilePath(".").IsEmpty());
104 EXPECT_FALSE(FilePath("a/b").IsEmpty());
105 EXPECT_FALSE(FilePath("a\\b\\").IsEmpty());
106}
107
108// RemoveDirectoryName "" -> ""
109TEST(RemoveDirectoryNameTest, WhenEmptyName) {
110 EXPECT_EQ("", FilePath("").RemoveDirectoryName().string());
111}
112
113// RemoveDirectoryName "afile" -> "afile"
114TEST(RemoveDirectoryNameTest, ButNoDirectory) {
115 EXPECT_EQ("afile", FilePath("afile").RemoveDirectoryName().string());
116}
117
118// RemoveDirectoryName "/afile" -> "afile"
119TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileName) {
120 EXPECT_EQ("afile",
121 FilePath(GTEST_PATH_SEP_ "afile").RemoveDirectoryName().string());
122}
123
124// RemoveDirectoryName "adir/" -> ""
125TEST(RemoveDirectoryNameTest, WhereThereIsNoFileName) {
126 EXPECT_EQ("",
127 FilePath("adir" GTEST_PATH_SEP_).RemoveDirectoryName().string());
128}
129
130// RemoveDirectoryName "adir/afile" -> "afile"
131TEST(RemoveDirectoryNameTest, ShouldGiveFileName) {
132 EXPECT_EQ(
133 "afile",
134 FilePath("adir" GTEST_PATH_SEP_ "afile").RemoveDirectoryName().string());
135}
136
137// RemoveDirectoryName "adir/subdir/afile" -> "afile"
138TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileName) {
139 EXPECT_EQ("afile",
140 FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile")
141 .RemoveDirectoryName()
142 .string());
143}
144
145#if GTEST_HAS_ALT_PATH_SEP_
146
147// Tests that RemoveDirectoryName() works with the alternate separator
148// on Windows.
149
150// RemoveDirectoryName("/afile") -> "afile"
151TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileNameForAlternateSeparator) {
152 EXPECT_EQ("afile", FilePath("/afile").RemoveDirectoryName().string());
153}
154
155// RemoveDirectoryName("adir/") -> ""
156TEST(RemoveDirectoryNameTest, WhereThereIsNoFileNameForAlternateSeparator) {
157 EXPECT_EQ("", FilePath("adir/").RemoveDirectoryName().string());
158}
159
160// RemoveDirectoryName("adir/afile") -> "afile"
161TEST(RemoveDirectoryNameTest, ShouldGiveFileNameForAlternateSeparator) {
162 EXPECT_EQ("afile", FilePath("adir/afile").RemoveDirectoryName().string());
163}
164
165// RemoveDirectoryName("adir/subdir/afile") -> "afile"
166TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileNameForAlternateSeparator) {
167 EXPECT_EQ("afile",
168 FilePath("adir/subdir/afile").RemoveDirectoryName().string());
169}
170
171#endif
172
173// RemoveFileName "" -> "./"
174TEST(RemoveFileNameTest, EmptyName) {
175#if GTEST_OS_WINDOWS_MOBILE
176 // On Windows CE, we use the root as the current directory.
177 EXPECT_EQ(GTEST_PATH_SEP_, FilePath("").RemoveFileName().string());
178#else
179 EXPECT_EQ("." GTEST_PATH_SEP_, FilePath("").RemoveFileName().string());
180#endif
181}
182
183// RemoveFileName "adir/" -> "adir/"
184TEST(RemoveFileNameTest, ButNoFile) {
185 EXPECT_EQ("adir" GTEST_PATH_SEP_,
186 FilePath("adir" GTEST_PATH_SEP_).RemoveFileName().string());
187}
188
189// RemoveFileName "adir/afile" -> "adir/"
190TEST(RemoveFileNameTest, GivesDirName) {
191 EXPECT_EQ("adir" GTEST_PATH_SEP_,
192 FilePath("adir" GTEST_PATH_SEP_ "afile").RemoveFileName().string());
193}
194
195// RemoveFileName "adir/subdir/afile" -> "adir/subdir/"
196TEST(RemoveFileNameTest, GivesDirAndSubDirName) {
197 EXPECT_EQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_,
198 FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile")
199 .RemoveFileName()
200 .string());
201}
202
203// RemoveFileName "/afile" -> "/"
204TEST(RemoveFileNameTest, GivesRootDir) {
205 EXPECT_EQ(GTEST_PATH_SEP_,
206 FilePath(GTEST_PATH_SEP_ "afile").RemoveFileName().string());
207}
208
209#if GTEST_HAS_ALT_PATH_SEP_
210
211// Tests that RemoveFileName() works with the alternate separator on
212// Windows.
213
214// RemoveFileName("adir/") -> "adir/"
215TEST(RemoveFileNameTest, ButNoFileForAlternateSeparator) {
216 EXPECT_EQ("adir" GTEST_PATH_SEP_,
217 FilePath("adir/").RemoveFileName().string());
218}
219
220// RemoveFileName("adir/afile") -> "adir/"
221TEST(RemoveFileNameTest, GivesDirNameForAlternateSeparator) {
222 EXPECT_EQ("adir" GTEST_PATH_SEP_,
223 FilePath("adir/afile").RemoveFileName().string());
224}
225
226// RemoveFileName("adir/subdir/afile") -> "adir/subdir/"
227TEST(RemoveFileNameTest, GivesDirAndSubDirNameForAlternateSeparator) {
228 EXPECT_EQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_,
229 FilePath("adir/subdir/afile").RemoveFileName().string());
230}
231
232// RemoveFileName("/afile") -> "\"
233TEST(RemoveFileNameTest, GivesRootDirForAlternateSeparator) {
234 EXPECT_EQ(GTEST_PATH_SEP_, FilePath("/afile").RemoveFileName().string());
235}
236
237#endif
238
239TEST(MakeFileNameTest, GenerateWhenNumberIsZero) {
240 FilePath actual =
241 FilePath::MakeFileName(FilePath("foo"), FilePath("bar"), 0, "xml");
242 EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
243}
244
245TEST(MakeFileNameTest, GenerateFileNameNumberGtZero) {
246 FilePath actual =
247 FilePath::MakeFileName(FilePath("foo"), FilePath("bar"), 12, "xml");
248 EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.string());
249}
250
251TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberIsZero) {
252 FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_),
253 FilePath("bar"), 0, "xml");
254 EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
255}
256
257TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberGtZero) {
258 FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_),
259 FilePath("bar"), 12, "xml");
260 EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.string());
261}
262
263TEST(MakeFileNameTest, GenerateWhenNumberIsZeroAndDirIsEmpty) {
264 FilePath actual =
265 FilePath::MakeFileName(FilePath(""), FilePath("bar"), 0, "xml");
266 EXPECT_EQ("bar.xml", actual.string());
267}
268
269TEST(MakeFileNameTest, GenerateWhenNumberIsNotZeroAndDirIsEmpty) {
270 FilePath actual =
271 FilePath::MakeFileName(FilePath(""), FilePath("bar"), 14, "xml");
272 EXPECT_EQ("bar_14.xml", actual.string());
273}
274
275TEST(ConcatPathsTest, WorksWhenDirDoesNotEndWithPathSep) {
276 FilePath actual = FilePath::ConcatPaths(FilePath("foo"), FilePath("bar.xml"));
277 EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
278}
279
280TEST(ConcatPathsTest, WorksWhenPath1EndsWithPathSep) {
281 FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_),
282 FilePath("bar.xml"));
283 EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
284}
285
286TEST(ConcatPathsTest, Path1BeingEmpty) {
287 FilePath actual = FilePath::ConcatPaths(FilePath(""), FilePath("bar.xml"));
288 EXPECT_EQ("bar.xml", actual.string());
289}
290
291TEST(ConcatPathsTest, Path2BeingEmpty) {
292 FilePath actual = FilePath::ConcatPaths(FilePath("foo"), FilePath(""));
293 EXPECT_EQ("foo" GTEST_PATH_SEP_, actual.string());
294}
295
296TEST(ConcatPathsTest, BothPathBeingEmpty) {
297 FilePath actual = FilePath::ConcatPaths(FilePath(""), FilePath(""));
298 EXPECT_EQ("", actual.string());
299}
300
301TEST(ConcatPathsTest, Path1ContainsPathSep) {
302 FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_ "bar"),
303 FilePath("foobar.xml"));
304 EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "foobar.xml",
305 actual.string());
306}
307
308TEST(ConcatPathsTest, Path2ContainsPathSep) {
309 FilePath actual =
310 FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_),
311 FilePath("bar" GTEST_PATH_SEP_ "bar.xml"));
312 EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "bar.xml",
313 actual.string());
314}
315
316TEST(ConcatPathsTest, Path2EndsWithPathSep) {
317 FilePath actual =
318 FilePath::ConcatPaths(FilePath("foo"), FilePath("bar" GTEST_PATH_SEP_));
319 EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_, actual.string());
320}
321
322// RemoveTrailingPathSeparator "" -> ""
323TEST(RemoveTrailingPathSeparatorTest, EmptyString) {
324 EXPECT_EQ("", FilePath("").RemoveTrailingPathSeparator().string());
325}
326
327// RemoveTrailingPathSeparator "foo" -> "foo"
328TEST(RemoveTrailingPathSeparatorTest, FileNoSlashString) {
329 EXPECT_EQ("foo", FilePath("foo").RemoveTrailingPathSeparator().string());
330}
331
332// RemoveTrailingPathSeparator "foo/" -> "foo"
333TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveTrailingSeparator) {
334 EXPECT_EQ(
335 "foo",
336 FilePath("foo" GTEST_PATH_SEP_).RemoveTrailingPathSeparator().string());
337#if GTEST_HAS_ALT_PATH_SEP_
338 EXPECT_EQ("foo", FilePath("foo/").RemoveTrailingPathSeparator().string());
339#endif
340}
341
342// RemoveTrailingPathSeparator "foo/bar/" -> "foo/bar/"
343TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveLastSeparator) {
344 EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
345 FilePath("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_)
346 .RemoveTrailingPathSeparator()
347 .string());
348}
349
350// RemoveTrailingPathSeparator "foo/bar" -> "foo/bar"
351TEST(RemoveTrailingPathSeparatorTest, ShouldReturnUnmodified) {
352 EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar", FilePath("foo" GTEST_PATH_SEP_ "bar")
353 .RemoveTrailingPathSeparator()
354 .string());
355}
356
357TEST(DirectoryTest, RootDirectoryExists) {
358#if GTEST_OS_WINDOWS // We are on Windows.
359 char current_drive[_MAX_PATH]; // NOLINT
360 current_drive[0] = static_cast<char>(_getdrive() + 'A' - 1);
361 current_drive[1] = ':';
362 current_drive[2] = '\\';
363 current_drive[3] = '\0';
364 EXPECT_TRUE(FilePath(current_drive).DirectoryExists());
365#else
366 EXPECT_TRUE(FilePath("/").DirectoryExists());
367#endif // GTEST_OS_WINDOWS
368}
369
370#if GTEST_OS_WINDOWS
371TEST(DirectoryTest, RootOfWrongDriveDoesNotExists) {
372 const int saved_drive_ = _getdrive();
373 // Find a drive that doesn't exist. Start with 'Z' to avoid common ones.
374 for (char drive = 'Z'; drive >= 'A'; drive--)
375 if (_chdrive(drive - 'A' + 1) == -1) {
376 char non_drive[_MAX_PATH]; // NOLINT
377 non_drive[0] = drive;
378 non_drive[1] = ':';
379 non_drive[2] = '\\';
380 non_drive[3] = '\0';
381 EXPECT_FALSE(FilePath(non_drive).DirectoryExists());
382 break;
383 }
384 _chdrive(saved_drive_);
385}
386#endif // GTEST_OS_WINDOWS
387
388#if !GTEST_OS_WINDOWS_MOBILE
389// Windows CE _does_ consider an empty directory to exist.
390TEST(DirectoryTest, EmptyPathDirectoryDoesNotExist) {
391 EXPECT_FALSE(FilePath("").DirectoryExists());
392}
393#endif // !GTEST_OS_WINDOWS_MOBILE
394
395TEST(DirectoryTest, CurrentDirectoryExists) {
396#if GTEST_OS_WINDOWS // We are on Windows.
397#ifndef _WIN32_CE // Windows CE doesn't have a current directory.
398
399 EXPECT_TRUE(FilePath(".").DirectoryExists());
400 EXPECT_TRUE(FilePath(".\\").DirectoryExists());
401
402#endif // _WIN32_CE
403#else
404 EXPECT_TRUE(FilePath(".").DirectoryExists());
405 EXPECT_TRUE(FilePath("./").DirectoryExists());
406#endif // GTEST_OS_WINDOWS
407}
408
409// "foo/bar" == foo//bar" == "foo///bar"
410TEST(NormalizeTest, MultipleConsecutiveSeparatorsInMidstring) {
411 EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
412 FilePath("foo" GTEST_PATH_SEP_ "bar").string());
413 EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
414 FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").string());
415 EXPECT_EQ(
416 "foo" GTEST_PATH_SEP_ "bar",
417 FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar")
418 .string());
419}
420
421// "/bar" == //bar" == "///bar"
422TEST(NormalizeTest, MultipleConsecutiveSeparatorsAtStringStart) {
423 EXPECT_EQ(GTEST_PATH_SEP_ "bar", FilePath(GTEST_PATH_SEP_ "bar").string());
424#if GTEST_OS_WINDOWS
425 EXPECT_EQ(GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar",
426 FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").string());
427#else
428 EXPECT_EQ(GTEST_PATH_SEP_ "bar",
429 FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").string());
430#endif
431 EXPECT_EQ(
432 GTEST_PATH_SEP_ "bar",
433 FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").string());
434}
435
436// "foo/" == foo//" == "foo///"
437TEST(NormalizeTest, MultipleConsecutiveSeparatorsAtStringEnd) {
438 EXPECT_EQ("foo" GTEST_PATH_SEP_, FilePath("foo" GTEST_PATH_SEP_).string());
439 EXPECT_EQ("foo" GTEST_PATH_SEP_,
440 FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_).string());
441 EXPECT_EQ(
442 "foo" GTEST_PATH_SEP_,
443 FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_).string());
444}
445
446#if GTEST_HAS_ALT_PATH_SEP_
447
448// Tests that separators at the end of the string are normalized
449// regardless of their combination (e.g. "foo\" =="foo/\" ==
450// "foo\\/").
451TEST(NormalizeTest, MixAlternateSeparatorAtStringEnd) {
452 EXPECT_EQ("foo" GTEST_PATH_SEP_, FilePath("foo/").string());
453 EXPECT_EQ("foo" GTEST_PATH_SEP_,
454 FilePath("foo" GTEST_PATH_SEP_ "/").string());
455 EXPECT_EQ("foo" GTEST_PATH_SEP_, FilePath("foo//" GTEST_PATH_SEP_).string());
456}
457
458#endif
459
460TEST(AssignmentOperatorTest, DefaultAssignedToNonDefault) {
461 FilePath default_path;
462 FilePath non_default_path("path");
463 non_default_path = default_path;
464 EXPECT_EQ("", non_default_path.string());
465 EXPECT_EQ("", default_path.string()); // RHS var is unchanged.
466}
467
468TEST(AssignmentOperatorTest, NonDefaultAssignedToDefault) {
469 FilePath non_default_path("path");
470 FilePath default_path;
471 default_path = non_default_path;
472 EXPECT_EQ("path", default_path.string());
473 EXPECT_EQ("path", non_default_path.string()); // RHS var is unchanged.
474}
475
476TEST(AssignmentOperatorTest, ConstAssignedToNonConst) {
477 const FilePath const_default_path("const_path");
478 FilePath non_default_path("path");
479 non_default_path = const_default_path;
480 EXPECT_EQ("const_path", non_default_path.string());
481}
482
483class DirectoryCreationTest : public Test {
484 protected:
485 void SetUp() override {
486 testdata_path_.Set(
487 FilePath(TempDir() + GetCurrentExecutableName().string() +
488 "_directory_creation" GTEST_PATH_SEP_ "test" GTEST_PATH_SEP_));
489 testdata_file_.Set(testdata_path_.RemoveTrailingPathSeparator());
490
491 unique_file0_.Set(
492 FilePath::MakeFileName(testdata_path_, FilePath("unique"), 0, "txt"));
493 unique_file1_.Set(
494 FilePath::MakeFileName(testdata_path_, FilePath("unique"), 1, "txt"));
495
496 remove(testdata_file_.c_str());
497 remove(unique_file0_.c_str());
498 remove(unique_file1_.c_str());
499 posix::RmDir(testdata_path_.c_str());
500 }
501
502 void TearDown() override {
503 remove(testdata_file_.c_str());
504 remove(unique_file0_.c_str());
505 remove(unique_file1_.c_str());
506 posix::RmDir(testdata_path_.c_str());
507 }
508
509 void CreateTextFile(const char* filename) {
510 FILE* f = posix::FOpen(filename, "w");
511 fprintf(f, "text\n");
512 fclose(f);
513 }
514
515 // Strings representing a directory and a file, with identical paths
516 // except for the trailing separator character that distinquishes
517 // a directory named 'test' from a file named 'test'. Example names:
518 FilePath testdata_path_; // "/tmp/directory_creation/test/"
519 FilePath testdata_file_; // "/tmp/directory_creation/test"
520 FilePath unique_file0_; // "/tmp/directory_creation/test/unique.txt"
521 FilePath unique_file1_; // "/tmp/directory_creation/test/unique_1.txt"
522};
523
524TEST_F(DirectoryCreationTest, CreateDirectoriesRecursively) {
525 EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.string();
526 EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
527 EXPECT_TRUE(testdata_path_.DirectoryExists());
528}
529
530TEST_F(DirectoryCreationTest, CreateDirectoriesForAlreadyExistingPath) {
531 EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.string();
532 EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
533 // Call 'create' again... should still succeed.
534 EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
535}
536
537TEST_F(DirectoryCreationTest, CreateDirectoriesAndUniqueFilename) {
538 FilePath file_path(FilePath::GenerateUniqueFileName(
539 testdata_path_, FilePath("unique"), "txt"));
540 EXPECT_EQ(unique_file0_.string(), file_path.string());
541 EXPECT_FALSE(file_path.FileOrDirectoryExists()); // file not there
542
543 testdata_path_.CreateDirectoriesRecursively();
544 EXPECT_FALSE(file_path.FileOrDirectoryExists()); // file still not there
545 CreateTextFile(file_path.c_str());
546 EXPECT_TRUE(file_path.FileOrDirectoryExists());
547
548 FilePath file_path2(FilePath::GenerateUniqueFileName(
549 testdata_path_, FilePath("unique"), "txt"));
550 EXPECT_EQ(unique_file1_.string(), file_path2.string());
551 EXPECT_FALSE(file_path2.FileOrDirectoryExists()); // file not there
552 CreateTextFile(file_path2.c_str());
553 EXPECT_TRUE(file_path2.FileOrDirectoryExists());
554}
555
556TEST_F(DirectoryCreationTest, CreateDirectoriesFail) {
557 // force a failure by putting a file where we will try to create a directory.
558 CreateTextFile(testdata_file_.c_str());
559 EXPECT_TRUE(testdata_file_.FileOrDirectoryExists());
560 EXPECT_FALSE(testdata_file_.DirectoryExists());
561 EXPECT_FALSE(testdata_file_.CreateDirectoriesRecursively());
562}
563
564TEST(NoDirectoryCreationTest, CreateNoDirectoriesForDefaultXmlFile) {
565 const FilePath test_detail_xml("test_detail.xml");
566 EXPECT_FALSE(test_detail_xml.CreateDirectoriesRecursively());
567}
568
569TEST(FilePathTest, DefaultConstructor) {
570 FilePath fp;
571 EXPECT_EQ("", fp.string());
572}
573
574TEST(FilePathTest, CharAndCopyConstructors) {
575 const FilePath fp("spicy");
576 EXPECT_EQ("spicy", fp.string());
577
578 const FilePath fp_copy(fp);
579 EXPECT_EQ("spicy", fp_copy.string());
580}
581
582TEST(FilePathTest, StringConstructor) {
583 const FilePath fp(std::string("cider"));
584 EXPECT_EQ("cider", fp.string());
585}
586
587TEST(FilePathTest, Set) {
588 const FilePath apple("apple");
589 FilePath mac("mac");
590 mac.Set(apple); // Implement Set() since overloading operator= is forbidden.
591 EXPECT_EQ("apple", mac.string());
592 EXPECT_EQ("apple", apple.string());
593}
594
595TEST(FilePathTest, ToString) {
596 const FilePath file("drink");
597 EXPECT_EQ("drink", file.string());
598}
599
600TEST(FilePathTest, RemoveExtension) {
601 EXPECT_EQ("app", FilePath("app.cc").RemoveExtension("cc").string());
602 EXPECT_EQ("app", FilePath("app.exe").RemoveExtension("exe").string());
603 EXPECT_EQ("APP", FilePath("APP.EXE").RemoveExtension("exe").string());
604}
605
606TEST(FilePathTest, RemoveExtensionWhenThereIsNoExtension) {
607 EXPECT_EQ("app", FilePath("app").RemoveExtension("exe").string());
608}
609
610TEST(FilePathTest, IsDirectory) {
611 EXPECT_FALSE(FilePath("cola").IsDirectory());
612 EXPECT_TRUE(FilePath("koala" GTEST_PATH_SEP_).IsDirectory());
613#if GTEST_HAS_ALT_PATH_SEP_
614 EXPECT_TRUE(FilePath("koala/").IsDirectory());
615#endif
616}
617
618TEST(FilePathTest, IsAbsolutePath) {
619 EXPECT_FALSE(FilePath("is" GTEST_PATH_SEP_ "relative").IsAbsolutePath());
620 EXPECT_FALSE(FilePath("").IsAbsolutePath());
621#if GTEST_OS_WINDOWS
622 EXPECT_TRUE(
623 FilePath("c:\\" GTEST_PATH_SEP_ "is_not" GTEST_PATH_SEP_ "relative")
624 .IsAbsolutePath());
625 EXPECT_FALSE(FilePath("c:foo" GTEST_PATH_SEP_ "bar").IsAbsolutePath());
626 EXPECT_TRUE(
627 FilePath("c:/" GTEST_PATH_SEP_ "is_not" GTEST_PATH_SEP_ "relative")
628 .IsAbsolutePath());
629 EXPECT_TRUE(FilePath("d:/Windows").IsAbsolutePath());
630 EXPECT_TRUE(FilePath("\\\\Host\\Share").IsAbsolutePath());
631 EXPECT_TRUE(FilePath("\\\\Host\\Share\\Folder").IsAbsolutePath());
632#else
633 EXPECT_TRUE(FilePath(GTEST_PATH_SEP_ "is_not" GTEST_PATH_SEP_ "relative")
634 .IsAbsolutePath());
635#endif // GTEST_OS_WINDOWS
636}
637
638TEST(FilePathTest, IsRootDirectory) {
639#if GTEST_OS_WINDOWS
640 EXPECT_TRUE(FilePath("a:\\").IsRootDirectory());
641 EXPECT_TRUE(FilePath("Z:/").IsRootDirectory());
642 EXPECT_TRUE(FilePath("e://").IsRootDirectory());
643 EXPECT_FALSE(FilePath("").IsRootDirectory());
644 EXPECT_FALSE(FilePath("b:").IsRootDirectory());
645 EXPECT_FALSE(FilePath("b:a").IsRootDirectory());
646 EXPECT_FALSE(FilePath("8:/").IsRootDirectory());
647 EXPECT_FALSE(FilePath("c|/").IsRootDirectory());
648 EXPECT_TRUE(FilePath("c:/").IsRootDirectory());
649 EXPECT_FALSE(FilePath("d:/Windows").IsRootDirectory());
650
651 // This is for backward compatibility, since callers (even in this library)
652 // have assumed IsRootDirectory() implies a trailing directory separator.
653 EXPECT_FALSE(FilePath("\\\\Host\\Share").IsRootDirectory());
654
655 EXPECT_TRUE(FilePath("\\\\Host\\Share\\").IsRootDirectory());
656 EXPECT_FALSE(FilePath("\\\\Host\\Share\\.").IsRootDirectory());
657 EXPECT_FALSE(FilePath("\\\\Host\\Share\\C$\\").IsRootDirectory());
658#else
659 EXPECT_TRUE(FilePath("/").IsRootDirectory());
660 EXPECT_TRUE(FilePath("//").IsRootDirectory());
661 EXPECT_FALSE(FilePath("").IsRootDirectory());
662 EXPECT_FALSE(FilePath("\\").IsRootDirectory());
663 EXPECT_FALSE(FilePath("/x").IsRootDirectory());
664#endif
665}
666
667} // namespace
668} // namespace internal
669} // namespace testing