gtest-filepath_test.cc
Go to the documentation of this file.
00001 // Copyright 2008, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Authors: keith.ray@gmail.com (Keith Ray)
00031 //
00032 // Google Test filepath utilities
00033 //
00034 // This file tests classes and functions used internally by
00035 // Google Test.  They are subject to change without notice.
00036 //
00037 // This file is #included from gtest_unittest.cc, to avoid changing
00038 // build or make-files for some existing Google Test clients. Do not
00039 // #include this file anywhere else!
00040 
00041 #include "gtest/internal/gtest-filepath.h"
00042 #include "gtest/gtest.h"
00043 
00044 // Indicates that this translation unit is part of Google Test's
00045 // implementation.  It must come before gtest-internal-inl.h is
00046 // included, or there will be a compiler error.  This trick is to
00047 // prevent a user from accidentally including gtest-internal-inl.h in
00048 // his code.
00049 #define GTEST_IMPLEMENTATION_ 1
00050 #include "src/gtest-internal-inl.h"
00051 #undef GTEST_IMPLEMENTATION_
00052 
00053 #if GTEST_OS_WINDOWS_MOBILE
00054 # include <windows.h>  // NOLINT
00055 #elif GTEST_OS_WINDOWS
00056 # include <direct.h>  // NOLINT
00057 #endif  // GTEST_OS_WINDOWS_MOBILE
00058 
00059 namespace testing {
00060 namespace internal {
00061 namespace {
00062 
00063 #if GTEST_OS_WINDOWS_MOBILE
00064 // TODO(wan@google.com): Move these to the POSIX adapter section in
00065 // gtest-port.h.
00066 
00067 // Windows CE doesn't have the remove C function.
00068 int remove(const char* path) {
00069   LPCWSTR wpath = String::AnsiToUtf16(path);
00070   int ret = DeleteFile(wpath) ? 0 : -1;
00071   delete [] wpath;
00072   return ret;
00073 }
00074 // Windows CE doesn't have the _rmdir C function.
00075 int _rmdir(const char* path) {
00076   FilePath filepath(path);
00077   LPCWSTR wpath = String::AnsiToUtf16(
00078       filepath.RemoveTrailingPathSeparator().c_str());
00079   int ret = RemoveDirectory(wpath) ? 0 : -1;
00080   delete [] wpath;
00081   return ret;
00082 }
00083 
00084 #else
00085 
00086 TEST(GetCurrentDirTest, ReturnsCurrentDir) {
00087   const FilePath original_dir = FilePath::GetCurrentDir();
00088   EXPECT_FALSE(original_dir.IsEmpty());
00089 
00090   posix::ChDir(GTEST_PATH_SEP_);
00091   const FilePath cwd = FilePath::GetCurrentDir();
00092   posix::ChDir(original_dir.c_str());
00093 
00094 # if GTEST_OS_WINDOWS
00095 
00096   // Skips the ":".
00097   const char* const cwd_without_drive = strchr(cwd.c_str(), ':');
00098   ASSERT_TRUE(cwd_without_drive != NULL);
00099   EXPECT_STREQ(GTEST_PATH_SEP_, cwd_without_drive + 1);
00100 
00101 # else
00102 
00103   EXPECT_EQ(GTEST_PATH_SEP_, cwd.string());
00104 
00105 # endif
00106 }
00107 
00108 #endif  // GTEST_OS_WINDOWS_MOBILE
00109 
00110 TEST(IsEmptyTest, ReturnsTrueForEmptyPath) {
00111   EXPECT_TRUE(FilePath("").IsEmpty());
00112 }
00113 
00114 TEST(IsEmptyTest, ReturnsFalseForNonEmptyPath) {
00115   EXPECT_FALSE(FilePath("a").IsEmpty());
00116   EXPECT_FALSE(FilePath(".").IsEmpty());
00117   EXPECT_FALSE(FilePath("a/b").IsEmpty());
00118   EXPECT_FALSE(FilePath("a\\b\\").IsEmpty());
00119 }
00120 
00121 // RemoveDirectoryName "" -> ""
00122 TEST(RemoveDirectoryNameTest, WhenEmptyName) {
00123   EXPECT_EQ("", FilePath("").RemoveDirectoryName().string());
00124 }
00125 
00126 // RemoveDirectoryName "afile" -> "afile"
00127 TEST(RemoveDirectoryNameTest, ButNoDirectory) {
00128   EXPECT_EQ("afile",
00129       FilePath("afile").RemoveDirectoryName().string());
00130 }
00131 
00132 // RemoveDirectoryName "/afile" -> "afile"
00133 TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileName) {
00134   EXPECT_EQ("afile",
00135       FilePath(GTEST_PATH_SEP_ "afile").RemoveDirectoryName().string());
00136 }
00137 
00138 // RemoveDirectoryName "adir/" -> ""
00139 TEST(RemoveDirectoryNameTest, WhereThereIsNoFileName) {
00140   EXPECT_EQ("",
00141       FilePath("adir" GTEST_PATH_SEP_).RemoveDirectoryName().string());
00142 }
00143 
00144 // RemoveDirectoryName "adir/afile" -> "afile"
00145 TEST(RemoveDirectoryNameTest, ShouldGiveFileName) {
00146   EXPECT_EQ("afile",
00147       FilePath("adir" GTEST_PATH_SEP_ "afile").RemoveDirectoryName().string());
00148 }
00149 
00150 // RemoveDirectoryName "adir/subdir/afile" -> "afile"
00151 TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileName) {
00152   EXPECT_EQ("afile",
00153       FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile")
00154       .RemoveDirectoryName().string());
00155 }
00156 
00157 #if GTEST_HAS_ALT_PATH_SEP_
00158 
00159 // Tests that RemoveDirectoryName() works with the alternate separator
00160 // on Windows.
00161 
00162 // RemoveDirectoryName("/afile") -> "afile"
00163 TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileNameForAlternateSeparator) {
00164   EXPECT_EQ("afile", FilePath("/afile").RemoveDirectoryName().string());
00165 }
00166 
00167 // RemoveDirectoryName("adir/") -> ""
00168 TEST(RemoveDirectoryNameTest, WhereThereIsNoFileNameForAlternateSeparator) {
00169   EXPECT_EQ("", FilePath("adir/").RemoveDirectoryName().string());
00170 }
00171 
00172 // RemoveDirectoryName("adir/afile") -> "afile"
00173 TEST(RemoveDirectoryNameTest, ShouldGiveFileNameForAlternateSeparator) {
00174   EXPECT_EQ("afile", FilePath("adir/afile").RemoveDirectoryName().string());
00175 }
00176 
00177 // RemoveDirectoryName("adir/subdir/afile") -> "afile"
00178 TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileNameForAlternateSeparator) {
00179   EXPECT_EQ("afile",
00180             FilePath("adir/subdir/afile").RemoveDirectoryName().string());
00181 }
00182 
00183 #endif
00184 
00185 // RemoveFileName "" -> "./"
00186 TEST(RemoveFileNameTest, EmptyName) {
00187 #if GTEST_OS_WINDOWS_MOBILE
00188   // On Windows CE, we use the root as the current directory.
00189   EXPECT_EQ(GTEST_PATH_SEP_, FilePath("").RemoveFileName().string());
00190 #else
00191   EXPECT_EQ("." GTEST_PATH_SEP_, FilePath("").RemoveFileName().string());
00192 #endif
00193 }
00194 
00195 // RemoveFileName "adir/" -> "adir/"
00196 TEST(RemoveFileNameTest, ButNoFile) {
00197   EXPECT_EQ("adir" GTEST_PATH_SEP_,
00198       FilePath("adir" GTEST_PATH_SEP_).RemoveFileName().string());
00199 }
00200 
00201 // RemoveFileName "adir/afile" -> "adir/"
00202 TEST(RemoveFileNameTest, GivesDirName) {
00203   EXPECT_EQ("adir" GTEST_PATH_SEP_,
00204             FilePath("adir" GTEST_PATH_SEP_ "afile").RemoveFileName().string());
00205 }
00206 
00207 // RemoveFileName "adir/subdir/afile" -> "adir/subdir/"
00208 TEST(RemoveFileNameTest, GivesDirAndSubDirName) {
00209   EXPECT_EQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_,
00210       FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile")
00211       .RemoveFileName().string());
00212 }
00213 
00214 // RemoveFileName "/afile" -> "/"
00215 TEST(RemoveFileNameTest, GivesRootDir) {
00216   EXPECT_EQ(GTEST_PATH_SEP_,
00217       FilePath(GTEST_PATH_SEP_ "afile").RemoveFileName().string());
00218 }
00219 
00220 #if GTEST_HAS_ALT_PATH_SEP_
00221 
00222 // Tests that RemoveFileName() works with the alternate separator on
00223 // Windows.
00224 
00225 // RemoveFileName("adir/") -> "adir/"
00226 TEST(RemoveFileNameTest, ButNoFileForAlternateSeparator) {
00227   EXPECT_EQ("adir" GTEST_PATH_SEP_,
00228             FilePath("adir/").RemoveFileName().string());
00229 }
00230 
00231 // RemoveFileName("adir/afile") -> "adir/"
00232 TEST(RemoveFileNameTest, GivesDirNameForAlternateSeparator) {
00233   EXPECT_EQ("adir" GTEST_PATH_SEP_,
00234             FilePath("adir/afile").RemoveFileName().string());
00235 }
00236 
00237 // RemoveFileName("adir/subdir/afile") -> "adir/subdir/"
00238 TEST(RemoveFileNameTest, GivesDirAndSubDirNameForAlternateSeparator) {
00239   EXPECT_EQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_,
00240             FilePath("adir/subdir/afile").RemoveFileName().string());
00241 }
00242 
00243 // RemoveFileName("/afile") -> "\"
00244 TEST(RemoveFileNameTest, GivesRootDirForAlternateSeparator) {
00245   EXPECT_EQ(GTEST_PATH_SEP_, FilePath("/afile").RemoveFileName().string());
00246 }
00247 
00248 #endif
00249 
00250 TEST(MakeFileNameTest, GenerateWhenNumberIsZero) {
00251   FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"),
00252       0, "xml");
00253   EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
00254 }
00255 
00256 TEST(MakeFileNameTest, GenerateFileNameNumberGtZero) {
00257   FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"),
00258       12, "xml");
00259   EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.string());
00260 }
00261 
00262 TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberIsZero) {
00263   FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_),
00264       FilePath("bar"), 0, "xml");
00265   EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
00266 }
00267 
00268 TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberGtZero) {
00269   FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_),
00270       FilePath("bar"), 12, "xml");
00271   EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.string());
00272 }
00273 
00274 TEST(MakeFileNameTest, GenerateWhenNumberIsZeroAndDirIsEmpty) {
00275   FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"),
00276       0, "xml");
00277   EXPECT_EQ("bar.xml", actual.string());
00278 }
00279 
00280 TEST(MakeFileNameTest, GenerateWhenNumberIsNotZeroAndDirIsEmpty) {
00281   FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"),
00282       14, "xml");
00283   EXPECT_EQ("bar_14.xml", actual.string());
00284 }
00285 
00286 TEST(ConcatPathsTest, WorksWhenDirDoesNotEndWithPathSep) {
00287   FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
00288                                           FilePath("bar.xml"));
00289   EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
00290 }
00291 
00292 TEST(ConcatPathsTest, WorksWhenPath1EndsWithPathSep) {
00293   FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_),
00294                                           FilePath("bar.xml"));
00295   EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
00296 }
00297 
00298 TEST(ConcatPathsTest, Path1BeingEmpty) {
00299   FilePath actual = FilePath::ConcatPaths(FilePath(""),
00300                                           FilePath("bar.xml"));
00301   EXPECT_EQ("bar.xml", actual.string());
00302 }
00303 
00304 TEST(ConcatPathsTest, Path2BeingEmpty) {
00305   FilePath actual = FilePath::ConcatPaths(FilePath("foo"), FilePath(""));
00306   EXPECT_EQ("foo" GTEST_PATH_SEP_, actual.string());
00307 }
00308 
00309 TEST(ConcatPathsTest, BothPathBeingEmpty) {
00310   FilePath actual = FilePath::ConcatPaths(FilePath(""),
00311                                           FilePath(""));
00312   EXPECT_EQ("", actual.string());
00313 }
00314 
00315 TEST(ConcatPathsTest, Path1ContainsPathSep) {
00316   FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_ "bar"),
00317                                           FilePath("foobar.xml"));
00318   EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "foobar.xml",
00319             actual.string());
00320 }
00321 
00322 TEST(ConcatPathsTest, Path2ContainsPathSep) {
00323   FilePath actual = FilePath::ConcatPaths(
00324       FilePath("foo" GTEST_PATH_SEP_),
00325       FilePath("bar" GTEST_PATH_SEP_ "bar.xml"));
00326   EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "bar.xml",
00327             actual.string());
00328 }
00329 
00330 TEST(ConcatPathsTest, Path2EndsWithPathSep) {
00331   FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
00332                                           FilePath("bar" GTEST_PATH_SEP_));
00333   EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_, actual.string());
00334 }
00335 
00336 // RemoveTrailingPathSeparator "" -> ""
00337 TEST(RemoveTrailingPathSeparatorTest, EmptyString) {
00338   EXPECT_EQ("", FilePath("").RemoveTrailingPathSeparator().string());
00339 }
00340 
00341 // RemoveTrailingPathSeparator "foo" -> "foo"
00342 TEST(RemoveTrailingPathSeparatorTest, FileNoSlashString) {
00343   EXPECT_EQ("foo", FilePath("foo").RemoveTrailingPathSeparator().string());
00344 }
00345 
00346 // RemoveTrailingPathSeparator "foo/" -> "foo"
00347 TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveTrailingSeparator) {
00348   EXPECT_EQ("foo",
00349       FilePath("foo" GTEST_PATH_SEP_).RemoveTrailingPathSeparator().string());
00350 #if GTEST_HAS_ALT_PATH_SEP_
00351   EXPECT_EQ("foo", FilePath("foo/").RemoveTrailingPathSeparator().string());
00352 #endif
00353 }
00354 
00355 // RemoveTrailingPathSeparator "foo/bar/" -> "foo/bar/"
00356 TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveLastSeparator) {
00357   EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
00358             FilePath("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_)
00359                 .RemoveTrailingPathSeparator().string());
00360 }
00361 
00362 // RemoveTrailingPathSeparator "foo/bar" -> "foo/bar"
00363 TEST(RemoveTrailingPathSeparatorTest, ShouldReturnUnmodified) {
00364   EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
00365             FilePath("foo" GTEST_PATH_SEP_ "bar")
00366                 .RemoveTrailingPathSeparator().string());
00367 }
00368 
00369 TEST(DirectoryTest, RootDirectoryExists) {
00370 #if GTEST_OS_WINDOWS  // We are on Windows.
00371   char current_drive[_MAX_PATH];  // NOLINT
00372   current_drive[0] = static_cast<char>(_getdrive() + 'A' - 1);
00373   current_drive[1] = ':';
00374   current_drive[2] = '\\';
00375   current_drive[3] = '\0';
00376   EXPECT_TRUE(FilePath(current_drive).DirectoryExists());
00377 #else
00378   EXPECT_TRUE(FilePath("/").DirectoryExists());
00379 #endif  // GTEST_OS_WINDOWS
00380 }
00381 
00382 #if GTEST_OS_WINDOWS
00383 TEST(DirectoryTest, RootOfWrongDriveDoesNotExists) {
00384   const int saved_drive_ = _getdrive();
00385   // Find a drive that doesn't exist. Start with 'Z' to avoid common ones.
00386   for (char drive = 'Z'; drive >= 'A'; drive--)
00387     if (_chdrive(drive - 'A' + 1) == -1) {
00388       char non_drive[_MAX_PATH];  // NOLINT
00389       non_drive[0] = drive;
00390       non_drive[1] = ':';
00391       non_drive[2] = '\\';
00392       non_drive[3] = '\0';
00393       EXPECT_FALSE(FilePath(non_drive).DirectoryExists());
00394       break;
00395     }
00396   _chdrive(saved_drive_);
00397 }
00398 #endif  // GTEST_OS_WINDOWS
00399 
00400 #if !GTEST_OS_WINDOWS_MOBILE
00401 // Windows CE _does_ consider an empty directory to exist.
00402 TEST(DirectoryTest, EmptyPathDirectoryDoesNotExist) {
00403   EXPECT_FALSE(FilePath("").DirectoryExists());
00404 }
00405 #endif  // !GTEST_OS_WINDOWS_MOBILE
00406 
00407 TEST(DirectoryTest, CurrentDirectoryExists) {
00408 #if GTEST_OS_WINDOWS  // We are on Windows.
00409 # ifndef _WIN32_CE  // Windows CE doesn't have a current directory.
00410 
00411   EXPECT_TRUE(FilePath(".").DirectoryExists());
00412   EXPECT_TRUE(FilePath(".\\").DirectoryExists());
00413 
00414 # endif  // _WIN32_CE
00415 #else
00416   EXPECT_TRUE(FilePath(".").DirectoryExists());
00417   EXPECT_TRUE(FilePath("./").DirectoryExists());
00418 #endif  // GTEST_OS_WINDOWS
00419 }
00420 
00421 // "foo/bar" == foo//bar" == "foo///bar"
00422 TEST(NormalizeTest, MultipleConsecutiveSepaparatorsInMidstring) {
00423   EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
00424             FilePath("foo" GTEST_PATH_SEP_ "bar").string());
00425   EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
00426             FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").string());
00427   EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
00428             FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_
00429                      GTEST_PATH_SEP_ "bar").string());
00430 }
00431 
00432 // "/bar" == //bar" == "///bar"
00433 TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringStart) {
00434   EXPECT_EQ(GTEST_PATH_SEP_ "bar",
00435     FilePath(GTEST_PATH_SEP_ "bar").string());
00436   EXPECT_EQ(GTEST_PATH_SEP_ "bar",
00437     FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").string());
00438   EXPECT_EQ(GTEST_PATH_SEP_ "bar",
00439     FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").string());
00440 }
00441 
00442 // "foo/" == foo//" == "foo///"
00443 TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringEnd) {
00444   EXPECT_EQ("foo" GTEST_PATH_SEP_,
00445     FilePath("foo" GTEST_PATH_SEP_).string());
00446   EXPECT_EQ("foo" GTEST_PATH_SEP_,
00447     FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_).string());
00448   EXPECT_EQ("foo" GTEST_PATH_SEP_,
00449     FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_).string());
00450 }
00451 
00452 #if GTEST_HAS_ALT_PATH_SEP_
00453 
00454 // Tests that separators at the end of the string are normalized
00455 // regardless of their combination (e.g. "foo\" =="foo/\" ==
00456 // "foo\\/").
00457 TEST(NormalizeTest, MixAlternateSeparatorAtStringEnd) {
00458   EXPECT_EQ("foo" GTEST_PATH_SEP_,
00459             FilePath("foo/").string());
00460   EXPECT_EQ("foo" GTEST_PATH_SEP_,
00461             FilePath("foo" GTEST_PATH_SEP_ "/").string());
00462   EXPECT_EQ("foo" GTEST_PATH_SEP_,
00463             FilePath("foo//" GTEST_PATH_SEP_).string());
00464 }
00465 
00466 #endif
00467 
00468 TEST(AssignmentOperatorTest, DefaultAssignedToNonDefault) {
00469   FilePath default_path;
00470   FilePath non_default_path("path");
00471   non_default_path = default_path;
00472   EXPECT_EQ("", non_default_path.string());
00473   EXPECT_EQ("", default_path.string());  // RHS var is unchanged.
00474 }
00475 
00476 TEST(AssignmentOperatorTest, NonDefaultAssignedToDefault) {
00477   FilePath non_default_path("path");
00478   FilePath default_path;
00479   default_path = non_default_path;
00480   EXPECT_EQ("path", default_path.string());
00481   EXPECT_EQ("path", non_default_path.string());  // RHS var is unchanged.
00482 }
00483 
00484 TEST(AssignmentOperatorTest, ConstAssignedToNonConst) {
00485   const FilePath const_default_path("const_path");
00486   FilePath non_default_path("path");
00487   non_default_path = const_default_path;
00488   EXPECT_EQ("const_path", non_default_path.string());
00489 }
00490 
00491 class DirectoryCreationTest : public Test {
00492  protected:
00493   virtual void SetUp() {
00494     testdata_path_.Set(FilePath(
00495         TempDir() + GetCurrentExecutableName().string() +
00496         "_directory_creation" GTEST_PATH_SEP_ "test" GTEST_PATH_SEP_));
00497     testdata_file_.Set(testdata_path_.RemoveTrailingPathSeparator());
00498 
00499     unique_file0_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"),
00500         0, "txt"));
00501     unique_file1_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"),
00502         1, "txt"));
00503 
00504     remove(testdata_file_.c_str());
00505     remove(unique_file0_.c_str());
00506     remove(unique_file1_.c_str());
00507     posix::RmDir(testdata_path_.c_str());
00508   }
00509 
00510   virtual void TearDown() {
00511     remove(testdata_file_.c_str());
00512     remove(unique_file0_.c_str());
00513     remove(unique_file1_.c_str());
00514     posix::RmDir(testdata_path_.c_str());
00515   }
00516 
00517   std::string TempDir() const {
00518 #if GTEST_OS_WINDOWS_MOBILE
00519     return "\\temp\\";
00520 #elif GTEST_OS_WINDOWS
00521     const char* temp_dir = posix::GetEnv("TEMP");
00522     if (temp_dir == NULL || temp_dir[0] == '\0')
00523       return "\\temp\\";
00524     else if (temp_dir[strlen(temp_dir) - 1] == '\\')
00525       return temp_dir;
00526     else
00527       return std::string(temp_dir) + "\\";
00528 #elif GTEST_OS_LINUX_ANDROID
00529     return "/sdcard/";
00530 #else
00531     return "/tmp/";
00532 #endif  // GTEST_OS_WINDOWS_MOBILE
00533   }
00534 
00535   void CreateTextFile(const char* filename) {
00536     FILE* f = posix::FOpen(filename, "w");
00537     fprintf(f, "text\n");
00538     fclose(f);
00539   }
00540 
00541   // Strings representing a directory and a file, with identical paths
00542   // except for the trailing separator character that distinquishes
00543   // a directory named 'test' from a file named 'test'. Example names:
00544   FilePath testdata_path_;  // "/tmp/directory_creation/test/"
00545   FilePath testdata_file_;  // "/tmp/directory_creation/test"
00546   FilePath unique_file0_;  // "/tmp/directory_creation/test/unique.txt"
00547   FilePath unique_file1_;  // "/tmp/directory_creation/test/unique_1.txt"
00548 };
00549 
00550 TEST_F(DirectoryCreationTest, CreateDirectoriesRecursively) {
00551   EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.string();
00552   EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
00553   EXPECT_TRUE(testdata_path_.DirectoryExists());
00554 }
00555 
00556 TEST_F(DirectoryCreationTest, CreateDirectoriesForAlreadyExistingPath) {
00557   EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.string();
00558   EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
00559   // Call 'create' again... should still succeed.
00560   EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
00561 }
00562 
00563 TEST_F(DirectoryCreationTest, CreateDirectoriesAndUniqueFilename) {
00564   FilePath file_path(FilePath::GenerateUniqueFileName(testdata_path_,
00565       FilePath("unique"), "txt"));
00566   EXPECT_EQ(unique_file0_.string(), file_path.string());
00567   EXPECT_FALSE(file_path.FileOrDirectoryExists());  // file not there
00568 
00569   testdata_path_.CreateDirectoriesRecursively();
00570   EXPECT_FALSE(file_path.FileOrDirectoryExists());  // file still not there
00571   CreateTextFile(file_path.c_str());
00572   EXPECT_TRUE(file_path.FileOrDirectoryExists());
00573 
00574   FilePath file_path2(FilePath::GenerateUniqueFileName(testdata_path_,
00575       FilePath("unique"), "txt"));
00576   EXPECT_EQ(unique_file1_.string(), file_path2.string());
00577   EXPECT_FALSE(file_path2.FileOrDirectoryExists());  // file not there
00578   CreateTextFile(file_path2.c_str());
00579   EXPECT_TRUE(file_path2.FileOrDirectoryExists());
00580 }
00581 
00582 TEST_F(DirectoryCreationTest, CreateDirectoriesFail) {
00583   // force a failure by putting a file where we will try to create a directory.
00584   CreateTextFile(testdata_file_.c_str());
00585   EXPECT_TRUE(testdata_file_.FileOrDirectoryExists());
00586   EXPECT_FALSE(testdata_file_.DirectoryExists());
00587   EXPECT_FALSE(testdata_file_.CreateDirectoriesRecursively());
00588 }
00589 
00590 TEST(NoDirectoryCreationTest, CreateNoDirectoriesForDefaultXmlFile) {
00591   const FilePath test_detail_xml("test_detail.xml");
00592   EXPECT_FALSE(test_detail_xml.CreateDirectoriesRecursively());
00593 }
00594 
00595 TEST(FilePathTest, DefaultConstructor) {
00596   FilePath fp;
00597   EXPECT_EQ("", fp.string());
00598 }
00599 
00600 TEST(FilePathTest, CharAndCopyConstructors) {
00601   const FilePath fp("spicy");
00602   EXPECT_EQ("spicy", fp.string());
00603 
00604   const FilePath fp_copy(fp);
00605   EXPECT_EQ("spicy", fp_copy.string());
00606 }
00607 
00608 TEST(FilePathTest, StringConstructor) {
00609   const FilePath fp(std::string("cider"));
00610   EXPECT_EQ("cider", fp.string());
00611 }
00612 
00613 TEST(FilePathTest, Set) {
00614   const FilePath apple("apple");
00615   FilePath mac("mac");
00616   mac.Set(apple);  // Implement Set() since overloading operator= is forbidden.
00617   EXPECT_EQ("apple", mac.string());
00618   EXPECT_EQ("apple", apple.string());
00619 }
00620 
00621 TEST(FilePathTest, ToString) {
00622   const FilePath file("drink");
00623   EXPECT_EQ("drink", file.string());
00624 }
00625 
00626 TEST(FilePathTest, RemoveExtension) {
00627   EXPECT_EQ("app", FilePath("app.cc").RemoveExtension("cc").string());
00628   EXPECT_EQ("app", FilePath("app.exe").RemoveExtension("exe").string());
00629   EXPECT_EQ("APP", FilePath("APP.EXE").RemoveExtension("exe").string());
00630 }
00631 
00632 TEST(FilePathTest, RemoveExtensionWhenThereIsNoExtension) {
00633   EXPECT_EQ("app", FilePath("app").RemoveExtension("exe").string());
00634 }
00635 
00636 TEST(FilePathTest, IsDirectory) {
00637   EXPECT_FALSE(FilePath("cola").IsDirectory());
00638   EXPECT_TRUE(FilePath("koala" GTEST_PATH_SEP_).IsDirectory());
00639 #if GTEST_HAS_ALT_PATH_SEP_
00640   EXPECT_TRUE(FilePath("koala/").IsDirectory());
00641 #endif
00642 }
00643 
00644 TEST(FilePathTest, IsAbsolutePath) {
00645   EXPECT_FALSE(FilePath("is" GTEST_PATH_SEP_ "relative").IsAbsolutePath());
00646   EXPECT_FALSE(FilePath("").IsAbsolutePath());
00647 #if GTEST_OS_WINDOWS
00648   EXPECT_TRUE(FilePath("c:\\" GTEST_PATH_SEP_ "is_not"
00649                        GTEST_PATH_SEP_ "relative").IsAbsolutePath());
00650   EXPECT_FALSE(FilePath("c:foo" GTEST_PATH_SEP_ "bar").IsAbsolutePath());
00651   EXPECT_TRUE(FilePath("c:/" GTEST_PATH_SEP_ "is_not"
00652                        GTEST_PATH_SEP_ "relative").IsAbsolutePath());
00653 #else
00654   EXPECT_TRUE(FilePath(GTEST_PATH_SEP_ "is_not" GTEST_PATH_SEP_ "relative")
00655               .IsAbsolutePath());
00656 #endif  // GTEST_OS_WINDOWS
00657 }
00658 
00659 TEST(FilePathTest, IsRootDirectory) {
00660 #if GTEST_OS_WINDOWS
00661   EXPECT_TRUE(FilePath("a:\\").IsRootDirectory());
00662   EXPECT_TRUE(FilePath("Z:/").IsRootDirectory());
00663   EXPECT_TRUE(FilePath("e://").IsRootDirectory());
00664   EXPECT_FALSE(FilePath("").IsRootDirectory());
00665   EXPECT_FALSE(FilePath("b:").IsRootDirectory());
00666   EXPECT_FALSE(FilePath("b:a").IsRootDirectory());
00667   EXPECT_FALSE(FilePath("8:/").IsRootDirectory());
00668   EXPECT_FALSE(FilePath("c|/").IsRootDirectory());
00669 #else
00670   EXPECT_TRUE(FilePath("/").IsRootDirectory());
00671   EXPECT_TRUE(FilePath("//").IsRootDirectory());
00672   EXPECT_FALSE(FilePath("").IsRootDirectory());
00673   EXPECT_FALSE(FilePath("\\").IsRootDirectory());
00674   EXPECT_FALSE(FilePath("/x").IsRootDirectory());
00675 #endif
00676 }
00677 
00678 }  // namespace
00679 }  // namespace internal
00680 }  // namespace testing


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:44