gtest/test/gtest-filepath_test.cc
Go to the documentation of this file.
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 // Authors: keith.ray@gmail.com (Keith Ray)
31 //
32 // Google Test filepath utilities
33 //
34 // This file tests classes and functions used internally by
35 // Google Test. They are subject to change without notice.
36 //
37 // This file is #included from gtest_unittest.cc, to avoid changing
38 // build or make-files for some existing Google Test clients. Do not
39 // #include this file anywhere else!
40 
41 #include "gtest/internal/gtest-filepath.h"
42 #include "gtest/gtest.h"
43 
44 // Indicates that this translation unit is part of Google Test's
45 // implementation. It must come before gtest-internal-inl.h is
46 // included, or there will be a compiler error. This trick is to
47 // prevent a user from accidentally including gtest-internal-inl.h in
48 // his code.
49 #define GTEST_IMPLEMENTATION_ 1
50 #include "src/gtest-internal-inl.h"
51 #undef GTEST_IMPLEMENTATION_
52 
53 #if GTEST_OS_WINDOWS_MOBILE
54 # include <windows.h> // NOLINT
55 #elif GTEST_OS_WINDOWS
56 # include <direct.h> // NOLINT
57 #endif // GTEST_OS_WINDOWS_MOBILE
58 
59 namespace testing {
60 namespace internal {
61 namespace {
62 
63 #if GTEST_OS_WINDOWS_MOBILE
64 // TODO(wan@google.com): Move these to the POSIX adapter section in
65 // gtest-port.h.
66 
67 // Windows CE doesn't have the remove C function.
68 int remove(const char* path) {
69  LPCWSTR wpath = String::AnsiToUtf16(path);
70  int ret = DeleteFile(wpath) ? 0 : -1;
71  delete [] wpath;
72  return ret;
73 }
74 // Windows CE doesn't have the _rmdir C function.
75 int _rmdir(const char* path) {
76  FilePath filepath(path);
77  LPCWSTR wpath = String::AnsiToUtf16(
78  filepath.RemoveTrailingPathSeparator().c_str());
79  int ret = RemoveDirectory(wpath) ? 0 : -1;
80  delete [] wpath;
81  return ret;
82 }
83 
84 #else
85 
86 TEST(GetCurrentDirTest, ReturnsCurrentDir) {
87  const FilePath original_dir = FilePath::GetCurrentDir();
88  EXPECT_FALSE(original_dir.IsEmpty());
89 
91  const FilePath cwd = FilePath::GetCurrentDir();
92  posix::ChDir(original_dir.c_str());
93 
94 # if GTEST_OS_WINDOWS
95 
96  // Skips the ":".
97  const char* const cwd_without_drive = strchr(cwd.c_str(), ':');
98  ASSERT_TRUE(cwd_without_drive != NULL);
99  EXPECT_STREQ(GTEST_PATH_SEP_, cwd_without_drive + 1);
100 
101 # else
102 
103  EXPECT_EQ(GTEST_PATH_SEP_, cwd.string());
104 
105 # endif
106 }
107 
108 #endif // GTEST_OS_WINDOWS_MOBILE
109 
110 TEST(IsEmptyTest, ReturnsTrueForEmptyPath) {
111  EXPECT_TRUE(FilePath("").IsEmpty());
112 }
113 
114 TEST(IsEmptyTest, ReturnsFalseForNonEmptyPath) {
115  EXPECT_FALSE(FilePath("a").IsEmpty());
116  EXPECT_FALSE(FilePath(".").IsEmpty());
117  EXPECT_FALSE(FilePath("a/b").IsEmpty());
118  EXPECT_FALSE(FilePath("a\\b\\").IsEmpty());
119 }
120 
121 // RemoveDirectoryName "" -> ""
122 TEST(RemoveDirectoryNameTest, WhenEmptyName) {
123  EXPECT_EQ("", FilePath("").RemoveDirectoryName().string());
124 }
125 
126 // RemoveDirectoryName "afile" -> "afile"
127 TEST(RemoveDirectoryNameTest, ButNoDirectory) {
128  EXPECT_EQ("afile",
129  FilePath("afile").RemoveDirectoryName().string());
130 }
131 
132 // RemoveDirectoryName "/afile" -> "afile"
133 TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileName) {
134  EXPECT_EQ("afile",
135  FilePath(GTEST_PATH_SEP_ "afile").RemoveDirectoryName().string());
136 }
137 
138 // RemoveDirectoryName "adir/" -> ""
139 TEST(RemoveDirectoryNameTest, WhereThereIsNoFileName) {
140  EXPECT_EQ("",
141  FilePath("adir" GTEST_PATH_SEP_).RemoveDirectoryName().string());
142 }
143 
144 // RemoveDirectoryName "adir/afile" -> "afile"
145 TEST(RemoveDirectoryNameTest, ShouldGiveFileName) {
146  EXPECT_EQ("afile",
147  FilePath("adir" GTEST_PATH_SEP_ "afile").RemoveDirectoryName().string());
148 }
149 
150 // RemoveDirectoryName "adir/subdir/afile" -> "afile"
151 TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileName) {
152  EXPECT_EQ("afile",
153  FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile")
154  .RemoveDirectoryName().string());
155 }
156 
157 #if GTEST_HAS_ALT_PATH_SEP_
158 
159 // Tests that RemoveDirectoryName() works with the alternate separator
160 // on Windows.
161 
162 // RemoveDirectoryName("/afile") -> "afile"
163 TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileNameForAlternateSeparator) {
164  EXPECT_EQ("afile", FilePath("/afile").RemoveDirectoryName().string());
165 }
166 
167 // RemoveDirectoryName("adir/") -> ""
168 TEST(RemoveDirectoryNameTest, WhereThereIsNoFileNameForAlternateSeparator) {
169  EXPECT_EQ("", FilePath("adir/").RemoveDirectoryName().string());
170 }
171 
172 // RemoveDirectoryName("adir/afile") -> "afile"
173 TEST(RemoveDirectoryNameTest, ShouldGiveFileNameForAlternateSeparator) {
174  EXPECT_EQ("afile", FilePath("adir/afile").RemoveDirectoryName().string());
175 }
176 
177 // RemoveDirectoryName("adir/subdir/afile") -> "afile"
178 TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileNameForAlternateSeparator) {
179  EXPECT_EQ("afile",
180  FilePath("adir/subdir/afile").RemoveDirectoryName().string());
181 }
182 
183 #endif
184 
185 // RemoveFileName "" -> "./"
186 TEST(RemoveFileNameTest, EmptyName) {
187 #if GTEST_OS_WINDOWS_MOBILE
188  // On Windows CE, we use the root as the current directory.
189  EXPECT_EQ(GTEST_PATH_SEP_, FilePath("").RemoveFileName().string());
190 #else
191  EXPECT_EQ("." GTEST_PATH_SEP_, FilePath("").RemoveFileName().string());
192 #endif
193 }
194 
195 // RemoveFileName "adir/" -> "adir/"
196 TEST(RemoveFileNameTest, ButNoFile) {
197  EXPECT_EQ("adir" GTEST_PATH_SEP_,
198  FilePath("adir" GTEST_PATH_SEP_).RemoveFileName().string());
199 }
200 
201 // RemoveFileName "adir/afile" -> "adir/"
202 TEST(RemoveFileNameTest, GivesDirName) {
203  EXPECT_EQ("adir" GTEST_PATH_SEP_,
204  FilePath("adir" GTEST_PATH_SEP_ "afile").RemoveFileName().string());
205 }
206 
207 // RemoveFileName "adir/subdir/afile" -> "adir/subdir/"
208 TEST(RemoveFileNameTest, GivesDirAndSubDirName) {
209  EXPECT_EQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_,
210  FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile")
211  .RemoveFileName().string());
212 }
213 
214 // RemoveFileName "/afile" -> "/"
215 TEST(RemoveFileNameTest, GivesRootDir) {
217  FilePath(GTEST_PATH_SEP_ "afile").RemoveFileName().string());
218 }
219 
220 #if GTEST_HAS_ALT_PATH_SEP_
221 
222 // Tests that RemoveFileName() works with the alternate separator on
223 // Windows.
224 
225 // RemoveFileName("adir/") -> "adir/"
226 TEST(RemoveFileNameTest, ButNoFileForAlternateSeparator) {
227  EXPECT_EQ("adir" GTEST_PATH_SEP_,
228  FilePath("adir/").RemoveFileName().string());
229 }
230 
231 // RemoveFileName("adir/afile") -> "adir/"
232 TEST(RemoveFileNameTest, GivesDirNameForAlternateSeparator) {
233  EXPECT_EQ("adir" GTEST_PATH_SEP_,
234  FilePath("adir/afile").RemoveFileName().string());
235 }
236 
237 // RemoveFileName("adir/subdir/afile") -> "adir/subdir/"
238 TEST(RemoveFileNameTest, GivesDirAndSubDirNameForAlternateSeparator) {
239  EXPECT_EQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_,
240  FilePath("adir/subdir/afile").RemoveFileName().string());
241 }
242 
243 // RemoveFileName("/afile") -> "\"
244 TEST(RemoveFileNameTest, GivesRootDirForAlternateSeparator) {
245  EXPECT_EQ(GTEST_PATH_SEP_, FilePath("/afile").RemoveFileName().string());
246 }
247 
248 #endif
249 
250 TEST(MakeFileNameTest, GenerateWhenNumberIsZero) {
251  FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"),
252  0, "xml");
253  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
254 }
255 
256 TEST(MakeFileNameTest, GenerateFileNameNumberGtZero) {
257  FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"),
258  12, "xml");
259  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.string());
260 }
261 
262 TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberIsZero) {
263  FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_),
264  FilePath("bar"), 0, "xml");
265  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
266 }
267 
268 TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberGtZero) {
269  FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_),
270  FilePath("bar"), 12, "xml");
271  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.string());
272 }
273 
274 TEST(MakeFileNameTest, GenerateWhenNumberIsZeroAndDirIsEmpty) {
275  FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"),
276  0, "xml");
277  EXPECT_EQ("bar.xml", actual.string());
278 }
279 
280 TEST(MakeFileNameTest, GenerateWhenNumberIsNotZeroAndDirIsEmpty) {
281  FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"),
282  14, "xml");
283  EXPECT_EQ("bar_14.xml", actual.string());
284 }
285 
286 TEST(ConcatPathsTest, WorksWhenDirDoesNotEndWithPathSep) {
287  FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
288  FilePath("bar.xml"));
289  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
290 }
291 
292 TEST(ConcatPathsTest, WorksWhenPath1EndsWithPathSep) {
293  FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_),
294  FilePath("bar.xml"));
295  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
296 }
297 
298 TEST(ConcatPathsTest, Path1BeingEmpty) {
299  FilePath actual = FilePath::ConcatPaths(FilePath(""),
300  FilePath("bar.xml"));
301  EXPECT_EQ("bar.xml", actual.string());
302 }
303 
304 TEST(ConcatPathsTest, Path2BeingEmpty) {
305  FilePath actual = FilePath::ConcatPaths(FilePath("foo"), FilePath(""));
306  EXPECT_EQ("foo" GTEST_PATH_SEP_, actual.string());
307 }
308 
309 TEST(ConcatPathsTest, BothPathBeingEmpty) {
310  FilePath actual = FilePath::ConcatPaths(FilePath(""),
311  FilePath(""));
312  EXPECT_EQ("", actual.string());
313 }
314 
315 TEST(ConcatPathsTest, Path1ContainsPathSep) {
316  FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_ "bar"),
317  FilePath("foobar.xml"));
318  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "foobar.xml",
319  actual.string());
320 }
321 
322 TEST(ConcatPathsTest, Path2ContainsPathSep) {
323  FilePath actual = FilePath::ConcatPaths(
324  FilePath("foo" GTEST_PATH_SEP_),
325  FilePath("bar" GTEST_PATH_SEP_ "bar.xml"));
326  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "bar.xml",
327  actual.string());
328 }
329 
330 TEST(ConcatPathsTest, Path2EndsWithPathSep) {
331  FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
332  FilePath("bar" GTEST_PATH_SEP_));
333  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_, actual.string());
334 }
335 
336 // RemoveTrailingPathSeparator "" -> ""
337 TEST(RemoveTrailingPathSeparatorTest, EmptyString) {
338  EXPECT_EQ("", FilePath("").RemoveTrailingPathSeparator().string());
339 }
340 
341 // RemoveTrailingPathSeparator "foo" -> "foo"
342 TEST(RemoveTrailingPathSeparatorTest, FileNoSlashString) {
343  EXPECT_EQ("foo", FilePath("foo").RemoveTrailingPathSeparator().string());
344 }
345 
346 // RemoveTrailingPathSeparator "foo/" -> "foo"
347 TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveTrailingSeparator) {
348  EXPECT_EQ("foo",
349  FilePath("foo" GTEST_PATH_SEP_).RemoveTrailingPathSeparator().string());
350 #if GTEST_HAS_ALT_PATH_SEP_
351  EXPECT_EQ("foo", FilePath("foo/").RemoveTrailingPathSeparator().string());
352 #endif
353 }
354 
355 // RemoveTrailingPathSeparator "foo/bar/" -> "foo/bar/"
356 TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveLastSeparator) {
357  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
358  FilePath("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_)
359  .RemoveTrailingPathSeparator().string());
360 }
361 
362 // RemoveTrailingPathSeparator "foo/bar" -> "foo/bar"
363 TEST(RemoveTrailingPathSeparatorTest, ShouldReturnUnmodified) {
364  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
365  FilePath("foo" GTEST_PATH_SEP_ "bar")
366  .RemoveTrailingPathSeparator().string());
367 }
368 
369 TEST(DirectoryTest, RootDirectoryExists) {
370 #if GTEST_OS_WINDOWS // We are on Windows.
371  char current_drive[_MAX_PATH]; // NOLINT
372  current_drive[0] = static_cast<char>(_getdrive() + 'A' - 1);
373  current_drive[1] = ':';
374  current_drive[2] = '\\';
375  current_drive[3] = '\0';
376  EXPECT_TRUE(FilePath(current_drive).DirectoryExists());
377 #else
378  EXPECT_TRUE(FilePath("/").DirectoryExists());
379 #endif // GTEST_OS_WINDOWS
380 }
381 
382 #if GTEST_OS_WINDOWS
383 TEST(DirectoryTest, RootOfWrongDriveDoesNotExists) {
384  const int saved_drive_ = _getdrive();
385  // Find a drive that doesn't exist. Start with 'Z' to avoid common ones.
386  for (char drive = 'Z'; drive >= 'A'; drive--)
387  if (_chdrive(drive - 'A' + 1) == -1) {
388  char non_drive[_MAX_PATH]; // NOLINT
389  non_drive[0] = drive;
390  non_drive[1] = ':';
391  non_drive[2] = '\\';
392  non_drive[3] = '\0';
393  EXPECT_FALSE(FilePath(non_drive).DirectoryExists());
394  break;
395  }
396  _chdrive(saved_drive_);
397 }
398 #endif // GTEST_OS_WINDOWS
399 
400 #if !GTEST_OS_WINDOWS_MOBILE
401 // Windows CE _does_ consider an empty directory to exist.
402 TEST(DirectoryTest, EmptyPathDirectoryDoesNotExist) {
403  EXPECT_FALSE(FilePath("").DirectoryExists());
404 }
405 #endif // !GTEST_OS_WINDOWS_MOBILE
406 
407 TEST(DirectoryTest, CurrentDirectoryExists) {
408 #if GTEST_OS_WINDOWS // We are on Windows.
409 # ifndef _WIN32_CE // Windows CE doesn't have a current directory.
410 
411  EXPECT_TRUE(FilePath(".").DirectoryExists());
412  EXPECT_TRUE(FilePath(".\\").DirectoryExists());
413 
414 # endif // _WIN32_CE
415 #else
416  EXPECT_TRUE(FilePath(".").DirectoryExists());
417  EXPECT_TRUE(FilePath("./").DirectoryExists());
418 #endif // GTEST_OS_WINDOWS
419 }
420 
421 // "foo/bar" == foo//bar" == "foo///bar"
422 TEST(NormalizeTest, MultipleConsecutiveSepaparatorsInMidstring) {
423  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
424  FilePath("foo" GTEST_PATH_SEP_ "bar").string());
425  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
426  FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").string());
427  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
428  FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_
429  GTEST_PATH_SEP_ "bar").string());
430 }
431 
432 // "/bar" == //bar" == "///bar"
433 TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringStart) {
435  FilePath(GTEST_PATH_SEP_ "bar").string());
437  FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").string());
438  EXPECT_EQ(GTEST_PATH_SEP_ "bar",
439  FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").string());
440 }
441 
442 // "foo/" == foo//" == "foo///"
443 TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringEnd) {
445  FilePath("foo" GTEST_PATH_SEP_).string());
447  FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_).string());
448  EXPECT_EQ("foo" GTEST_PATH_SEP_,
449  FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_).string());
450 }
451 
452 #if GTEST_HAS_ALT_PATH_SEP_
453 
454 // Tests that separators at the end of the string are normalized
455 // regardless of their combination (e.g. "foo\" =="foo/\" ==
456 // "foo\\/").
457 TEST(NormalizeTest, MixAlternateSeparatorAtStringEnd) {
459  FilePath("foo/").string());
461  FilePath("foo" GTEST_PATH_SEP_ "/").string());
463  FilePath("foo//" GTEST_PATH_SEP_).string());
464 }
465 
466 #endif
467 
468 TEST(AssignmentOperatorTest, DefaultAssignedToNonDefault) {
469  FilePath default_path;
470  FilePath non_default_path("path");
471  non_default_path = default_path;
472  EXPECT_EQ("", non_default_path.string());
473  EXPECT_EQ("", default_path.string()); // RHS var is unchanged.
474 }
475 
476 TEST(AssignmentOperatorTest, NonDefaultAssignedToDefault) {
477  FilePath non_default_path("path");
478  FilePath default_path;
479  default_path = non_default_path;
480  EXPECT_EQ("path", default_path.string());
481  EXPECT_EQ("path", non_default_path.string()); // RHS var is unchanged.
482 }
483 
484 TEST(AssignmentOperatorTest, ConstAssignedToNonConst) {
485  const FilePath const_default_path("const_path");
486  FilePath non_default_path("path");
487  non_default_path = const_default_path;
488  EXPECT_EQ("const_path", non_default_path.string());
489 }
490 
491 class DirectoryCreationTest : public Test {
492  protected:
493  virtual void SetUp() {
494  testdata_path_.Set(FilePath(
495  TempDir() + GetCurrentExecutableName().string() +
496  "_directory_creation" GTEST_PATH_SEP_ "test" GTEST_PATH_SEP_));
497  testdata_file_.Set(testdata_path_.RemoveTrailingPathSeparator());
498 
499  unique_file0_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"),
500  0, "txt"));
501  unique_file1_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"),
502  1, "txt"));
503 
504  remove(testdata_file_.c_str());
505  remove(unique_file0_.c_str());
506  remove(unique_file1_.c_str());
507  posix::RmDir(testdata_path_.c_str());
508  }
509 
510  virtual void TearDown() {
511  remove(testdata_file_.c_str());
512  remove(unique_file0_.c_str());
513  remove(unique_file1_.c_str());
514  posix::RmDir(testdata_path_.c_str());
515  }
516 
517  std::string TempDir() const {
518 #if GTEST_OS_WINDOWS_MOBILE
519  return "\\temp\\";
520 #elif GTEST_OS_WINDOWS
521  const char* temp_dir = posix::GetEnv("TEMP");
522  if (temp_dir == NULL || temp_dir[0] == '\0')
523  return "\\temp\\";
524  else if (temp_dir[strlen(temp_dir) - 1] == '\\')
525  return temp_dir;
526  else
527  return std::string(temp_dir) + "\\";
528 #elif GTEST_OS_LINUX_ANDROID
529  return "/sdcard/";
530 #else
531  return "/tmp/";
532 #endif // GTEST_OS_WINDOWS_MOBILE
533  }
534 
535  void CreateTextFile(const char* filename) {
536  FILE* f = posix::FOpen(filename, "w");
537  fprintf(f, "text\n");
538  fclose(f);
539  }
540 
541  // Strings representing a directory and a file, with identical paths
542  // except for the trailing separator character that distinquishes
543  // a directory named 'test' from a file named 'test'. Example names:
544  FilePath testdata_path_; // "/tmp/directory_creation/test/"
545  FilePath testdata_file_; // "/tmp/directory_creation/test"
546  FilePath unique_file0_; // "/tmp/directory_creation/test/unique.txt"
547  FilePath unique_file1_; // "/tmp/directory_creation/test/unique_1.txt"
548 };
549 
550 TEST_F(DirectoryCreationTest, CreateDirectoriesRecursively) {
551  EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.string();
552  EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
553  EXPECT_TRUE(testdata_path_.DirectoryExists());
554 }
555 
556 TEST_F(DirectoryCreationTest, CreateDirectoriesForAlreadyExistingPath) {
557  EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.string();
558  EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
559  // Call 'create' again... should still succeed.
560  EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
561 }
562 
563 TEST_F(DirectoryCreationTest, CreateDirectoriesAndUniqueFilename) {
565  FilePath("unique"), "txt"));
566  EXPECT_EQ(unique_file0_.string(), file_path.string());
567  EXPECT_FALSE(file_path.FileOrDirectoryExists()); // file not there
568 
569  testdata_path_.CreateDirectoriesRecursively();
570  EXPECT_FALSE(file_path.FileOrDirectoryExists()); // file still not there
571  CreateTextFile(file_path.c_str());
572  EXPECT_TRUE(file_path.FileOrDirectoryExists());
573 
575  FilePath("unique"), "txt"));
576  EXPECT_EQ(unique_file1_.string(), file_path2.string());
577  EXPECT_FALSE(file_path2.FileOrDirectoryExists()); // file not there
578  CreateTextFile(file_path2.c_str());
579  EXPECT_TRUE(file_path2.FileOrDirectoryExists());
580 }
581 
582 TEST_F(DirectoryCreationTest, CreateDirectoriesFail) {
583  // force a failure by putting a file where we will try to create a directory.
584  CreateTextFile(testdata_file_.c_str());
585  EXPECT_TRUE(testdata_file_.FileOrDirectoryExists());
586  EXPECT_FALSE(testdata_file_.DirectoryExists());
587  EXPECT_FALSE(testdata_file_.CreateDirectoriesRecursively());
588 }
589 
590 TEST(NoDirectoryCreationTest, CreateNoDirectoriesForDefaultXmlFile) {
591  const FilePath test_detail_xml("test_detail.xml");
592  EXPECT_FALSE(test_detail_xml.CreateDirectoriesRecursively());
593 }
594 
595 TEST(FilePathTest, DefaultConstructor) {
596  FilePath fp;
597  EXPECT_EQ("", fp.string());
598 }
599 
600 TEST(FilePathTest, CharAndCopyConstructors) {
601  const FilePath fp("spicy");
602  EXPECT_EQ("spicy", fp.string());
603 
604  const FilePath fp_copy(fp);
605  EXPECT_EQ("spicy", fp_copy.string());
606 }
607 
608 TEST(FilePathTest, StringConstructor) {
609  const FilePath fp(std::string("cider"));
610  EXPECT_EQ("cider", fp.string());
611 }
612 
613 TEST(FilePathTest, Set) {
614  const FilePath apple("apple");
615  FilePath mac("mac");
616  mac.Set(apple); // Implement Set() since overloading operator= is forbidden.
617  EXPECT_EQ("apple", mac.string());
618  EXPECT_EQ("apple", apple.string());
619 }
620 
621 TEST(FilePathTest, ToString) {
622  const FilePath file("drink");
623  EXPECT_EQ("drink", file.string());
624 }
625 
626 TEST(FilePathTest, RemoveExtension) {
627  EXPECT_EQ("app", FilePath("app.cc").RemoveExtension("cc").string());
628  EXPECT_EQ("app", FilePath("app.exe").RemoveExtension("exe").string());
629  EXPECT_EQ("APP", FilePath("APP.EXE").RemoveExtension("exe").string());
630 }
631 
632 TEST(FilePathTest, RemoveExtensionWhenThereIsNoExtension) {
633  EXPECT_EQ("app", FilePath("app").RemoveExtension("exe").string());
634 }
635 
636 TEST(FilePathTest, IsDirectory) {
637  EXPECT_FALSE(FilePath("cola").IsDirectory());
638  EXPECT_TRUE(FilePath("koala" GTEST_PATH_SEP_).IsDirectory());
639 #if GTEST_HAS_ALT_PATH_SEP_
640  EXPECT_TRUE(FilePath("koala/").IsDirectory());
641 #endif
642 }
643 
644 TEST(FilePathTest, IsAbsolutePath) {
645  EXPECT_FALSE(FilePath("is" GTEST_PATH_SEP_ "relative").IsAbsolutePath());
646  EXPECT_FALSE(FilePath("").IsAbsolutePath());
647 #if GTEST_OS_WINDOWS
648  EXPECT_TRUE(FilePath("c:\\" GTEST_PATH_SEP_ "is_not"
649  GTEST_PATH_SEP_ "relative").IsAbsolutePath());
650  EXPECT_FALSE(FilePath("c:foo" GTEST_PATH_SEP_ "bar").IsAbsolutePath());
651  EXPECT_TRUE(FilePath("c:/" GTEST_PATH_SEP_ "is_not"
652  GTEST_PATH_SEP_ "relative").IsAbsolutePath());
653 #else
654  EXPECT_TRUE(FilePath(GTEST_PATH_SEP_ "is_not" GTEST_PATH_SEP_ "relative")
655  .IsAbsolutePath());
656 #endif // GTEST_OS_WINDOWS
657 }
658 
659 TEST(FilePathTest, IsRootDirectory) {
660 #if GTEST_OS_WINDOWS
661  EXPECT_TRUE(FilePath("a:\\").IsRootDirectory());
662  EXPECT_TRUE(FilePath("Z:/").IsRootDirectory());
663  EXPECT_TRUE(FilePath("e://").IsRootDirectory());
664  EXPECT_FALSE(FilePath("").IsRootDirectory());
665  EXPECT_FALSE(FilePath("b:").IsRootDirectory());
666  EXPECT_FALSE(FilePath("b:a").IsRootDirectory());
667  EXPECT_FALSE(FilePath("8:/").IsRootDirectory());
668  EXPECT_FALSE(FilePath("c|/").IsRootDirectory());
669 #else
670  EXPECT_TRUE(FilePath("/").IsRootDirectory());
671  EXPECT_TRUE(FilePath("//").IsRootDirectory());
672  EXPECT_FALSE(FilePath("").IsRootDirectory());
673  EXPECT_FALSE(FilePath("\\").IsRootDirectory());
674  EXPECT_FALSE(FilePath("/x").IsRootDirectory());
675 #endif
676 }
677 
678 } // namespace
679 } // namespace internal
680 } // namespace testing
static FilePath ConcatPaths(const FilePath &directory, const FilePath &relative_path)
f
FILE * FOpen(const char *path, const char *mode)
FilePath unique_file1_
FilePath testdata_file_
#define GTEST_PATH_SEP_
#define EXPECT_TRUE(condition)
FilePath testdata_path_
#define EXPECT_STREQ(expected, actual)
#define EXPECT_FALSE(condition)
FilePath unique_file0_
GTEST_API_ FilePath GetCurrentExecutableName()
#define ASSERT_TRUE(condition)
static FilePath GenerateUniqueFileName(const FilePath &directory, const FilePath &base_name, const char *extension)
static FilePath MakeFileName(const FilePath &directory, const FilePath &base_name, int number, const char *extension)
std::string ToString(const AttributeId &value)
FMT_API int fprintf(std::FILE *f, CStringRef format, ArgList args)
Definition: format.cc:891
const char * GetEnv(const char *name)
#define EXPECT_EQ(expected, actual)
TEST(IsXDigitTest, WorksForNarrowAscii)


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:12