1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/webrtc/trunk/testing/gtest/test/gtest-filepath_test.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,696 @@ 1.4 +// Copyright 2008, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 +// 1.33 +// Authors: keith.ray@gmail.com (Keith Ray) 1.34 +// 1.35 +// Google Test filepath utilities 1.36 +// 1.37 +// This file tests classes and functions used internally by 1.38 +// Google Test. They are subject to change without notice. 1.39 +// 1.40 +// This file is #included from gtest_unittest.cc, to avoid changing 1.41 +// build or make-files for some existing Google Test clients. Do not 1.42 +// #include this file anywhere else! 1.43 + 1.44 +#include "gtest/internal/gtest-filepath.h" 1.45 +#include "gtest/gtest.h" 1.46 + 1.47 +// Indicates that this translation unit is part of Google Test's 1.48 +// implementation. It must come before gtest-internal-inl.h is 1.49 +// included, or there will be a compiler error. This trick is to 1.50 +// prevent a user from accidentally including gtest-internal-inl.h in 1.51 +// his code. 1.52 +#define GTEST_IMPLEMENTATION_ 1 1.53 +#include "src/gtest-internal-inl.h" 1.54 +#undef GTEST_IMPLEMENTATION_ 1.55 + 1.56 +#if GTEST_OS_WINDOWS_MOBILE 1.57 +# include <windows.h> // NOLINT 1.58 +#elif GTEST_OS_WINDOWS 1.59 +# include <direct.h> // NOLINT 1.60 +#endif // GTEST_OS_WINDOWS_MOBILE 1.61 + 1.62 +namespace testing { 1.63 +namespace internal { 1.64 +namespace { 1.65 + 1.66 +#if GTEST_OS_WINDOWS_MOBILE 1.67 +// TODO(wan@google.com): Move these to the POSIX adapter section in 1.68 +// gtest-port.h. 1.69 + 1.70 +// Windows CE doesn't have the remove C function. 1.71 +int remove(const char* path) { 1.72 + LPCWSTR wpath = String::AnsiToUtf16(path); 1.73 + int ret = DeleteFile(wpath) ? 0 : -1; 1.74 + delete [] wpath; 1.75 + return ret; 1.76 +} 1.77 +// Windows CE doesn't have the _rmdir C function. 1.78 +int _rmdir(const char* path) { 1.79 + FilePath filepath(path); 1.80 + LPCWSTR wpath = String::AnsiToUtf16( 1.81 + filepath.RemoveTrailingPathSeparator().c_str()); 1.82 + int ret = RemoveDirectory(wpath) ? 0 : -1; 1.83 + delete [] wpath; 1.84 + return ret; 1.85 +} 1.86 + 1.87 +#else 1.88 + 1.89 +TEST(GetCurrentDirTest, ReturnsCurrentDir) { 1.90 + const FilePath original_dir = FilePath::GetCurrentDir(); 1.91 + EXPECT_FALSE(original_dir.IsEmpty()); 1.92 + 1.93 + posix::ChDir(GTEST_PATH_SEP_); 1.94 + const FilePath cwd = FilePath::GetCurrentDir(); 1.95 + posix::ChDir(original_dir.c_str()); 1.96 + 1.97 +# if GTEST_OS_WINDOWS 1.98 + 1.99 + // Skips the ":". 1.100 + const char* const cwd_without_drive = strchr(cwd.c_str(), ':'); 1.101 + ASSERT_TRUE(cwd_without_drive != NULL); 1.102 + EXPECT_STREQ(GTEST_PATH_SEP_, cwd_without_drive + 1); 1.103 + 1.104 +# else 1.105 + 1.106 + EXPECT_STREQ(GTEST_PATH_SEP_, cwd.c_str()); 1.107 + 1.108 +# endif 1.109 +} 1.110 + 1.111 +#endif // GTEST_OS_WINDOWS_MOBILE 1.112 + 1.113 +TEST(IsEmptyTest, ReturnsTrueForEmptyPath) { 1.114 + EXPECT_TRUE(FilePath("").IsEmpty()); 1.115 + EXPECT_TRUE(FilePath(NULL).IsEmpty()); 1.116 +} 1.117 + 1.118 +TEST(IsEmptyTest, ReturnsFalseForNonEmptyPath) { 1.119 + EXPECT_FALSE(FilePath("a").IsEmpty()); 1.120 + EXPECT_FALSE(FilePath(".").IsEmpty()); 1.121 + EXPECT_FALSE(FilePath("a/b").IsEmpty()); 1.122 + EXPECT_FALSE(FilePath("a\\b\\").IsEmpty()); 1.123 +} 1.124 + 1.125 +// RemoveDirectoryName "" -> "" 1.126 +TEST(RemoveDirectoryNameTest, WhenEmptyName) { 1.127 + EXPECT_STREQ("", FilePath("").RemoveDirectoryName().c_str()); 1.128 +} 1.129 + 1.130 +// RemoveDirectoryName "afile" -> "afile" 1.131 +TEST(RemoveDirectoryNameTest, ButNoDirectory) { 1.132 + EXPECT_STREQ("afile", 1.133 + FilePath("afile").RemoveDirectoryName().c_str()); 1.134 +} 1.135 + 1.136 +// RemoveDirectoryName "/afile" -> "afile" 1.137 +TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileName) { 1.138 + EXPECT_STREQ("afile", 1.139 + FilePath(GTEST_PATH_SEP_ "afile").RemoveDirectoryName().c_str()); 1.140 +} 1.141 + 1.142 +// RemoveDirectoryName "adir/" -> "" 1.143 +TEST(RemoveDirectoryNameTest, WhereThereIsNoFileName) { 1.144 + EXPECT_STREQ("", 1.145 + FilePath("adir" GTEST_PATH_SEP_).RemoveDirectoryName().c_str()); 1.146 +} 1.147 + 1.148 +// RemoveDirectoryName "adir/afile" -> "afile" 1.149 +TEST(RemoveDirectoryNameTest, ShouldGiveFileName) { 1.150 + EXPECT_STREQ("afile", 1.151 + FilePath("adir" GTEST_PATH_SEP_ "afile").RemoveDirectoryName().c_str()); 1.152 +} 1.153 + 1.154 +// RemoveDirectoryName "adir/subdir/afile" -> "afile" 1.155 +TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileName) { 1.156 + EXPECT_STREQ("afile", 1.157 + FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile") 1.158 + .RemoveDirectoryName().c_str()); 1.159 +} 1.160 + 1.161 +#if GTEST_HAS_ALT_PATH_SEP_ 1.162 + 1.163 +// Tests that RemoveDirectoryName() works with the alternate separator 1.164 +// on Windows. 1.165 + 1.166 +// RemoveDirectoryName("/afile") -> "afile" 1.167 +TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileNameForAlternateSeparator) { 1.168 + EXPECT_STREQ("afile", 1.169 + FilePath("/afile").RemoveDirectoryName().c_str()); 1.170 +} 1.171 + 1.172 +// RemoveDirectoryName("adir/") -> "" 1.173 +TEST(RemoveDirectoryNameTest, WhereThereIsNoFileNameForAlternateSeparator) { 1.174 + EXPECT_STREQ("", 1.175 + FilePath("adir/").RemoveDirectoryName().c_str()); 1.176 +} 1.177 + 1.178 +// RemoveDirectoryName("adir/afile") -> "afile" 1.179 +TEST(RemoveDirectoryNameTest, ShouldGiveFileNameForAlternateSeparator) { 1.180 + EXPECT_STREQ("afile", 1.181 + FilePath("adir/afile").RemoveDirectoryName().c_str()); 1.182 +} 1.183 + 1.184 +// RemoveDirectoryName("adir/subdir/afile") -> "afile" 1.185 +TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileNameForAlternateSeparator) { 1.186 + EXPECT_STREQ("afile", 1.187 + FilePath("adir/subdir/afile").RemoveDirectoryName().c_str()); 1.188 +} 1.189 + 1.190 +#endif 1.191 + 1.192 +// RemoveFileName "" -> "./" 1.193 +TEST(RemoveFileNameTest, EmptyName) { 1.194 +#if GTEST_OS_WINDOWS_MOBILE 1.195 + // On Windows CE, we use the root as the current directory. 1.196 + EXPECT_STREQ(GTEST_PATH_SEP_, 1.197 + FilePath("").RemoveFileName().c_str()); 1.198 +#else 1.199 + EXPECT_STREQ("." GTEST_PATH_SEP_, 1.200 + FilePath("").RemoveFileName().c_str()); 1.201 +#endif 1.202 +} 1.203 + 1.204 +// RemoveFileName "adir/" -> "adir/" 1.205 +TEST(RemoveFileNameTest, ButNoFile) { 1.206 + EXPECT_STREQ("adir" GTEST_PATH_SEP_, 1.207 + FilePath("adir" GTEST_PATH_SEP_).RemoveFileName().c_str()); 1.208 +} 1.209 + 1.210 +// RemoveFileName "adir/afile" -> "adir/" 1.211 +TEST(RemoveFileNameTest, GivesDirName) { 1.212 + EXPECT_STREQ("adir" GTEST_PATH_SEP_, 1.213 + FilePath("adir" GTEST_PATH_SEP_ "afile") 1.214 + .RemoveFileName().c_str()); 1.215 +} 1.216 + 1.217 +// RemoveFileName "adir/subdir/afile" -> "adir/subdir/" 1.218 +TEST(RemoveFileNameTest, GivesDirAndSubDirName) { 1.219 + EXPECT_STREQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_, 1.220 + FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile") 1.221 + .RemoveFileName().c_str()); 1.222 +} 1.223 + 1.224 +// RemoveFileName "/afile" -> "/" 1.225 +TEST(RemoveFileNameTest, GivesRootDir) { 1.226 + EXPECT_STREQ(GTEST_PATH_SEP_, 1.227 + FilePath(GTEST_PATH_SEP_ "afile").RemoveFileName().c_str()); 1.228 +} 1.229 + 1.230 +#if GTEST_HAS_ALT_PATH_SEP_ 1.231 + 1.232 +// Tests that RemoveFileName() works with the alternate separator on 1.233 +// Windows. 1.234 + 1.235 +// RemoveFileName("adir/") -> "adir/" 1.236 +TEST(RemoveFileNameTest, ButNoFileForAlternateSeparator) { 1.237 + EXPECT_STREQ("adir" GTEST_PATH_SEP_, 1.238 + FilePath("adir/").RemoveFileName().c_str()); 1.239 +} 1.240 + 1.241 +// RemoveFileName("adir/afile") -> "adir/" 1.242 +TEST(RemoveFileNameTest, GivesDirNameForAlternateSeparator) { 1.243 + EXPECT_STREQ("adir" GTEST_PATH_SEP_, 1.244 + FilePath("adir/afile").RemoveFileName().c_str()); 1.245 +} 1.246 + 1.247 +// RemoveFileName("adir/subdir/afile") -> "adir/subdir/" 1.248 +TEST(RemoveFileNameTest, GivesDirAndSubDirNameForAlternateSeparator) { 1.249 + EXPECT_STREQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_, 1.250 + FilePath("adir/subdir/afile").RemoveFileName().c_str()); 1.251 +} 1.252 + 1.253 +// RemoveFileName("/afile") -> "\" 1.254 +TEST(RemoveFileNameTest, GivesRootDirForAlternateSeparator) { 1.255 + EXPECT_STREQ(GTEST_PATH_SEP_, 1.256 + FilePath("/afile").RemoveFileName().c_str()); 1.257 +} 1.258 + 1.259 +#endif 1.260 + 1.261 +TEST(MakeFileNameTest, GenerateWhenNumberIsZero) { 1.262 + FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"), 1.263 + 0, "xml"); 1.264 + EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str()); 1.265 +} 1.266 + 1.267 +TEST(MakeFileNameTest, GenerateFileNameNumberGtZero) { 1.268 + FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"), 1.269 + 12, "xml"); 1.270 + EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.c_str()); 1.271 +} 1.272 + 1.273 +TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberIsZero) { 1.274 + FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_), 1.275 + FilePath("bar"), 0, "xml"); 1.276 + EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str()); 1.277 +} 1.278 + 1.279 +TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberGtZero) { 1.280 + FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_), 1.281 + FilePath("bar"), 12, "xml"); 1.282 + EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.c_str()); 1.283 +} 1.284 + 1.285 +TEST(MakeFileNameTest, GenerateWhenNumberIsZeroAndDirIsEmpty) { 1.286 + FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"), 1.287 + 0, "xml"); 1.288 + EXPECT_STREQ("bar.xml", actual.c_str()); 1.289 +} 1.290 + 1.291 +TEST(MakeFileNameTest, GenerateWhenNumberIsNotZeroAndDirIsEmpty) { 1.292 + FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"), 1.293 + 14, "xml"); 1.294 + EXPECT_STREQ("bar_14.xml", actual.c_str()); 1.295 +} 1.296 + 1.297 +TEST(ConcatPathsTest, WorksWhenDirDoesNotEndWithPathSep) { 1.298 + FilePath actual = FilePath::ConcatPaths(FilePath("foo"), 1.299 + FilePath("bar.xml")); 1.300 + EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str()); 1.301 +} 1.302 + 1.303 +TEST(ConcatPathsTest, WorksWhenPath1EndsWithPathSep) { 1.304 + FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_), 1.305 + FilePath("bar.xml")); 1.306 + EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str()); 1.307 +} 1.308 + 1.309 +TEST(ConcatPathsTest, Path1BeingEmpty) { 1.310 + FilePath actual = FilePath::ConcatPaths(FilePath(""), 1.311 + FilePath("bar.xml")); 1.312 + EXPECT_STREQ("bar.xml", actual.c_str()); 1.313 +} 1.314 + 1.315 +TEST(ConcatPathsTest, Path2BeingEmpty) { 1.316 + FilePath actual = FilePath::ConcatPaths(FilePath("foo"), 1.317 + FilePath("")); 1.318 + EXPECT_STREQ("foo" GTEST_PATH_SEP_, actual.c_str()); 1.319 +} 1.320 + 1.321 +TEST(ConcatPathsTest, BothPathBeingEmpty) { 1.322 + FilePath actual = FilePath::ConcatPaths(FilePath(""), 1.323 + FilePath("")); 1.324 + EXPECT_STREQ("", actual.c_str()); 1.325 +} 1.326 + 1.327 +TEST(ConcatPathsTest, Path1ContainsPathSep) { 1.328 + FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_ "bar"), 1.329 + FilePath("foobar.xml")); 1.330 + EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "foobar.xml", 1.331 + actual.c_str()); 1.332 +} 1.333 + 1.334 +TEST(ConcatPathsTest, Path2ContainsPathSep) { 1.335 + FilePath actual = FilePath::ConcatPaths( 1.336 + FilePath("foo" GTEST_PATH_SEP_), 1.337 + FilePath("bar" GTEST_PATH_SEP_ "bar.xml")); 1.338 + EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "bar.xml", 1.339 + actual.c_str()); 1.340 +} 1.341 + 1.342 +TEST(ConcatPathsTest, Path2EndsWithPathSep) { 1.343 + FilePath actual = FilePath::ConcatPaths(FilePath("foo"), 1.344 + FilePath("bar" GTEST_PATH_SEP_)); 1.345 + EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_, actual.c_str()); 1.346 +} 1.347 + 1.348 +// RemoveTrailingPathSeparator "" -> "" 1.349 +TEST(RemoveTrailingPathSeparatorTest, EmptyString) { 1.350 + EXPECT_STREQ("", 1.351 + FilePath("").RemoveTrailingPathSeparator().c_str()); 1.352 +} 1.353 + 1.354 +// RemoveTrailingPathSeparator "foo" -> "foo" 1.355 +TEST(RemoveTrailingPathSeparatorTest, FileNoSlashString) { 1.356 + EXPECT_STREQ("foo", 1.357 + FilePath("foo").RemoveTrailingPathSeparator().c_str()); 1.358 +} 1.359 + 1.360 +// RemoveTrailingPathSeparator "foo/" -> "foo" 1.361 +TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveTrailingSeparator) { 1.362 + EXPECT_STREQ( 1.363 + "foo", 1.364 + FilePath("foo" GTEST_PATH_SEP_).RemoveTrailingPathSeparator().c_str()); 1.365 +#if GTEST_HAS_ALT_PATH_SEP_ 1.366 + EXPECT_STREQ("foo", 1.367 + FilePath("foo/").RemoveTrailingPathSeparator().c_str()); 1.368 +#endif 1.369 +} 1.370 + 1.371 +// RemoveTrailingPathSeparator "foo/bar/" -> "foo/bar/" 1.372 +TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveLastSeparator) { 1.373 + EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", 1.374 + FilePath("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_) 1.375 + .RemoveTrailingPathSeparator().c_str()); 1.376 +} 1.377 + 1.378 +// RemoveTrailingPathSeparator "foo/bar" -> "foo/bar" 1.379 +TEST(RemoveTrailingPathSeparatorTest, ShouldReturnUnmodified) { 1.380 + EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", 1.381 + FilePath("foo" GTEST_PATH_SEP_ "bar") 1.382 + .RemoveTrailingPathSeparator().c_str()); 1.383 +} 1.384 + 1.385 +TEST(DirectoryTest, RootDirectoryExists) { 1.386 +#if GTEST_OS_WINDOWS // We are on Windows. 1.387 + char current_drive[_MAX_PATH]; // NOLINT 1.388 + current_drive[0] = static_cast<char>(_getdrive() + 'A' - 1); 1.389 + current_drive[1] = ':'; 1.390 + current_drive[2] = '\\'; 1.391 + current_drive[3] = '\0'; 1.392 + EXPECT_TRUE(FilePath(current_drive).DirectoryExists()); 1.393 +#else 1.394 + EXPECT_TRUE(FilePath("/").DirectoryExists()); 1.395 +#endif // GTEST_OS_WINDOWS 1.396 +} 1.397 + 1.398 +#if GTEST_OS_WINDOWS 1.399 +TEST(DirectoryTest, RootOfWrongDriveDoesNotExists) { 1.400 + const int saved_drive_ = _getdrive(); 1.401 + // Find a drive that doesn't exist. Start with 'Z' to avoid common ones. 1.402 + for (char drive = 'Z'; drive >= 'A'; drive--) 1.403 + if (_chdrive(drive - 'A' + 1) == -1) { 1.404 + char non_drive[_MAX_PATH]; // NOLINT 1.405 + non_drive[0] = drive; 1.406 + non_drive[1] = ':'; 1.407 + non_drive[2] = '\\'; 1.408 + non_drive[3] = '\0'; 1.409 + EXPECT_FALSE(FilePath(non_drive).DirectoryExists()); 1.410 + break; 1.411 + } 1.412 + _chdrive(saved_drive_); 1.413 +} 1.414 +#endif // GTEST_OS_WINDOWS 1.415 + 1.416 +#if !GTEST_OS_WINDOWS_MOBILE 1.417 +// Windows CE _does_ consider an empty directory to exist. 1.418 +TEST(DirectoryTest, EmptyPathDirectoryDoesNotExist) { 1.419 + EXPECT_FALSE(FilePath("").DirectoryExists()); 1.420 +} 1.421 +#endif // !GTEST_OS_WINDOWS_MOBILE 1.422 + 1.423 +TEST(DirectoryTest, CurrentDirectoryExists) { 1.424 +#if GTEST_OS_WINDOWS // We are on Windows. 1.425 +# ifndef _WIN32_CE // Windows CE doesn't have a current directory. 1.426 + 1.427 + EXPECT_TRUE(FilePath(".").DirectoryExists()); 1.428 + EXPECT_TRUE(FilePath(".\\").DirectoryExists()); 1.429 + 1.430 +# endif // _WIN32_CE 1.431 +#else 1.432 + EXPECT_TRUE(FilePath(".").DirectoryExists()); 1.433 + EXPECT_TRUE(FilePath("./").DirectoryExists()); 1.434 +#endif // GTEST_OS_WINDOWS 1.435 +} 1.436 + 1.437 +TEST(NormalizeTest, NullStringsEqualEmptyDirectory) { 1.438 + EXPECT_STREQ("", FilePath(NULL).c_str()); 1.439 + EXPECT_STREQ("", FilePath(String(NULL)).c_str()); 1.440 +} 1.441 + 1.442 +// "foo/bar" == foo//bar" == "foo///bar" 1.443 +TEST(NormalizeTest, MultipleConsecutiveSepaparatorsInMidstring) { 1.444 + EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", 1.445 + FilePath("foo" GTEST_PATH_SEP_ "bar").c_str()); 1.446 + EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", 1.447 + FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str()); 1.448 + EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", 1.449 + FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ 1.450 + GTEST_PATH_SEP_ "bar").c_str()); 1.451 +} 1.452 + 1.453 +// "/bar" == //bar" == "///bar" 1.454 +TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringStart) { 1.455 + EXPECT_STREQ(GTEST_PATH_SEP_ "bar", 1.456 + FilePath(GTEST_PATH_SEP_ "bar").c_str()); 1.457 + EXPECT_STREQ(GTEST_PATH_SEP_ "bar", 1.458 + FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str()); 1.459 + EXPECT_STREQ(GTEST_PATH_SEP_ "bar", 1.460 + FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str()); 1.461 +} 1.462 + 1.463 +// "foo/" == foo//" == "foo///" 1.464 +TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringEnd) { 1.465 + EXPECT_STREQ("foo" GTEST_PATH_SEP_, 1.466 + FilePath("foo" GTEST_PATH_SEP_).c_str()); 1.467 + EXPECT_STREQ("foo" GTEST_PATH_SEP_, 1.468 + FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_).c_str()); 1.469 + EXPECT_STREQ("foo" GTEST_PATH_SEP_, 1.470 + FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_).c_str()); 1.471 +} 1.472 + 1.473 +#if GTEST_HAS_ALT_PATH_SEP_ 1.474 + 1.475 +// Tests that separators at the end of the string are normalized 1.476 +// regardless of their combination (e.g. "foo\" =="foo/\" == 1.477 +// "foo\\/"). 1.478 +TEST(NormalizeTest, MixAlternateSeparatorAtStringEnd) { 1.479 + EXPECT_STREQ("foo" GTEST_PATH_SEP_, 1.480 + FilePath("foo/").c_str()); 1.481 + EXPECT_STREQ("foo" GTEST_PATH_SEP_, 1.482 + FilePath("foo" GTEST_PATH_SEP_ "/").c_str()); 1.483 + EXPECT_STREQ("foo" GTEST_PATH_SEP_, 1.484 + FilePath("foo//" GTEST_PATH_SEP_).c_str()); 1.485 +} 1.486 + 1.487 +#endif 1.488 + 1.489 +TEST(AssignmentOperatorTest, DefaultAssignedToNonDefault) { 1.490 + FilePath default_path; 1.491 + FilePath non_default_path("path"); 1.492 + non_default_path = default_path; 1.493 + EXPECT_STREQ("", non_default_path.c_str()); 1.494 + EXPECT_STREQ("", default_path.c_str()); // RHS var is unchanged. 1.495 +} 1.496 + 1.497 +TEST(AssignmentOperatorTest, NonDefaultAssignedToDefault) { 1.498 + FilePath non_default_path("path"); 1.499 + FilePath default_path; 1.500 + default_path = non_default_path; 1.501 + EXPECT_STREQ("path", default_path.c_str()); 1.502 + EXPECT_STREQ("path", non_default_path.c_str()); // RHS var is unchanged. 1.503 +} 1.504 + 1.505 +TEST(AssignmentOperatorTest, ConstAssignedToNonConst) { 1.506 + const FilePath const_default_path("const_path"); 1.507 + FilePath non_default_path("path"); 1.508 + non_default_path = const_default_path; 1.509 + EXPECT_STREQ("const_path", non_default_path.c_str()); 1.510 +} 1.511 + 1.512 +class DirectoryCreationTest : public Test { 1.513 + protected: 1.514 + virtual void SetUp() { 1.515 + testdata_path_.Set(FilePath(String::Format("%s%s%s", 1.516 + TempDir().c_str(), GetCurrentExecutableName().c_str(), 1.517 + "_directory_creation" GTEST_PATH_SEP_ "test" GTEST_PATH_SEP_))); 1.518 + testdata_file_.Set(testdata_path_.RemoveTrailingPathSeparator()); 1.519 + 1.520 + unique_file0_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"), 1.521 + 0, "txt")); 1.522 + unique_file1_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"), 1.523 + 1, "txt")); 1.524 + 1.525 + remove(testdata_file_.c_str()); 1.526 + remove(unique_file0_.c_str()); 1.527 + remove(unique_file1_.c_str()); 1.528 + posix::RmDir(testdata_path_.c_str()); 1.529 + } 1.530 + 1.531 + virtual void TearDown() { 1.532 + remove(testdata_file_.c_str()); 1.533 + remove(unique_file0_.c_str()); 1.534 + remove(unique_file1_.c_str()); 1.535 + posix::RmDir(testdata_path_.c_str()); 1.536 + } 1.537 + 1.538 + String TempDir() const { 1.539 +#if GTEST_OS_WINDOWS_MOBILE 1.540 + return String("\\temp\\"); 1.541 +#elif GTEST_OS_WINDOWS 1.542 + const char* temp_dir = posix::GetEnv("TEMP"); 1.543 + if (temp_dir == NULL || temp_dir[0] == '\0') 1.544 + return String("\\temp\\"); 1.545 + else if (String(temp_dir).EndsWith("\\")) 1.546 + return String(temp_dir); 1.547 + else 1.548 + return String::Format("%s\\", temp_dir); 1.549 +#else 1.550 + return String("/tmp/"); 1.551 +#endif // GTEST_OS_WINDOWS_MOBILE 1.552 + } 1.553 + 1.554 + void CreateTextFile(const char* filename) { 1.555 + FILE* f = posix::FOpen(filename, "w"); 1.556 + fprintf(f, "text\n"); 1.557 + fclose(f); 1.558 + } 1.559 + 1.560 + // Strings representing a directory and a file, with identical paths 1.561 + // except for the trailing separator character that distinquishes 1.562 + // a directory named 'test' from a file named 'test'. Example names: 1.563 + FilePath testdata_path_; // "/tmp/directory_creation/test/" 1.564 + FilePath testdata_file_; // "/tmp/directory_creation/test" 1.565 + FilePath unique_file0_; // "/tmp/directory_creation/test/unique.txt" 1.566 + FilePath unique_file1_; // "/tmp/directory_creation/test/unique_1.txt" 1.567 +}; 1.568 + 1.569 +TEST_F(DirectoryCreationTest, CreateDirectoriesRecursively) { 1.570 + EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.c_str(); 1.571 + EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively()); 1.572 + EXPECT_TRUE(testdata_path_.DirectoryExists()); 1.573 +} 1.574 + 1.575 +TEST_F(DirectoryCreationTest, CreateDirectoriesForAlreadyExistingPath) { 1.576 + EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.c_str(); 1.577 + EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively()); 1.578 + // Call 'create' again... should still succeed. 1.579 + EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively()); 1.580 +} 1.581 + 1.582 +TEST_F(DirectoryCreationTest, CreateDirectoriesAndUniqueFilename) { 1.583 + FilePath file_path(FilePath::GenerateUniqueFileName(testdata_path_, 1.584 + FilePath("unique"), "txt")); 1.585 + EXPECT_STREQ(unique_file0_.c_str(), file_path.c_str()); 1.586 + EXPECT_FALSE(file_path.FileOrDirectoryExists()); // file not there 1.587 + 1.588 + testdata_path_.CreateDirectoriesRecursively(); 1.589 + EXPECT_FALSE(file_path.FileOrDirectoryExists()); // file still not there 1.590 + CreateTextFile(file_path.c_str()); 1.591 + EXPECT_TRUE(file_path.FileOrDirectoryExists()); 1.592 + 1.593 + FilePath file_path2(FilePath::GenerateUniqueFileName(testdata_path_, 1.594 + FilePath("unique"), "txt")); 1.595 + EXPECT_STREQ(unique_file1_.c_str(), file_path2.c_str()); 1.596 + EXPECT_FALSE(file_path2.FileOrDirectoryExists()); // file not there 1.597 + CreateTextFile(file_path2.c_str()); 1.598 + EXPECT_TRUE(file_path2.FileOrDirectoryExists()); 1.599 +} 1.600 + 1.601 +TEST_F(DirectoryCreationTest, CreateDirectoriesFail) { 1.602 + // force a failure by putting a file where we will try to create a directory. 1.603 + CreateTextFile(testdata_file_.c_str()); 1.604 + EXPECT_TRUE(testdata_file_.FileOrDirectoryExists()); 1.605 + EXPECT_FALSE(testdata_file_.DirectoryExists()); 1.606 + EXPECT_FALSE(testdata_file_.CreateDirectoriesRecursively()); 1.607 +} 1.608 + 1.609 +TEST(NoDirectoryCreationTest, CreateNoDirectoriesForDefaultXmlFile) { 1.610 + const FilePath test_detail_xml("test_detail.xml"); 1.611 + EXPECT_FALSE(test_detail_xml.CreateDirectoriesRecursively()); 1.612 +} 1.613 + 1.614 +TEST(FilePathTest, DefaultConstructor) { 1.615 + FilePath fp; 1.616 + EXPECT_STREQ("", fp.c_str()); 1.617 +} 1.618 + 1.619 +TEST(FilePathTest, CharAndCopyConstructors) { 1.620 + const FilePath fp("spicy"); 1.621 + EXPECT_STREQ("spicy", fp.c_str()); 1.622 + 1.623 + const FilePath fp_copy(fp); 1.624 + EXPECT_STREQ("spicy", fp_copy.c_str()); 1.625 +} 1.626 + 1.627 +TEST(FilePathTest, StringConstructor) { 1.628 + const FilePath fp(String("cider")); 1.629 + EXPECT_STREQ("cider", fp.c_str()); 1.630 +} 1.631 + 1.632 +TEST(FilePathTest, Set) { 1.633 + const FilePath apple("apple"); 1.634 + FilePath mac("mac"); 1.635 + mac.Set(apple); // Implement Set() since overloading operator= is forbidden. 1.636 + EXPECT_STREQ("apple", mac.c_str()); 1.637 + EXPECT_STREQ("apple", apple.c_str()); 1.638 +} 1.639 + 1.640 +TEST(FilePathTest, ToString) { 1.641 + const FilePath file("drink"); 1.642 + String str(file.ToString()); 1.643 + EXPECT_STREQ("drink", str.c_str()); 1.644 +} 1.645 + 1.646 +TEST(FilePathTest, RemoveExtension) { 1.647 + EXPECT_STREQ("app", FilePath("app.exe").RemoveExtension("exe").c_str()); 1.648 + EXPECT_STREQ("APP", FilePath("APP.EXE").RemoveExtension("exe").c_str()); 1.649 +} 1.650 + 1.651 +TEST(FilePathTest, RemoveExtensionWhenThereIsNoExtension) { 1.652 + EXPECT_STREQ("app", FilePath("app").RemoveExtension("exe").c_str()); 1.653 +} 1.654 + 1.655 +TEST(FilePathTest, IsDirectory) { 1.656 + EXPECT_FALSE(FilePath("cola").IsDirectory()); 1.657 + EXPECT_TRUE(FilePath("koala" GTEST_PATH_SEP_).IsDirectory()); 1.658 +#if GTEST_HAS_ALT_PATH_SEP_ 1.659 + EXPECT_TRUE(FilePath("koala/").IsDirectory()); 1.660 +#endif 1.661 +} 1.662 + 1.663 +TEST(FilePathTest, IsAbsolutePath) { 1.664 + EXPECT_FALSE(FilePath("is" GTEST_PATH_SEP_ "relative").IsAbsolutePath()); 1.665 + EXPECT_FALSE(FilePath("").IsAbsolutePath()); 1.666 +#if GTEST_OS_WINDOWS 1.667 + EXPECT_TRUE(FilePath("c:\\" GTEST_PATH_SEP_ "is_not" 1.668 + GTEST_PATH_SEP_ "relative").IsAbsolutePath()); 1.669 + EXPECT_FALSE(FilePath("c:foo" GTEST_PATH_SEP_ "bar").IsAbsolutePath()); 1.670 + EXPECT_TRUE(FilePath("c:/" GTEST_PATH_SEP_ "is_not" 1.671 + GTEST_PATH_SEP_ "relative").IsAbsolutePath()); 1.672 +#else 1.673 + EXPECT_TRUE(FilePath(GTEST_PATH_SEP_ "is_not" GTEST_PATH_SEP_ "relative") 1.674 + .IsAbsolutePath()); 1.675 +#endif // GTEST_OS_WINDOWS 1.676 +} 1.677 + 1.678 +TEST(FilePathTest, IsRootDirectory) { 1.679 +#if GTEST_OS_WINDOWS 1.680 + EXPECT_TRUE(FilePath("a:\\").IsRootDirectory()); 1.681 + EXPECT_TRUE(FilePath("Z:/").IsRootDirectory()); 1.682 + EXPECT_TRUE(FilePath("e://").IsRootDirectory()); 1.683 + EXPECT_FALSE(FilePath("").IsRootDirectory()); 1.684 + EXPECT_FALSE(FilePath("b:").IsRootDirectory()); 1.685 + EXPECT_FALSE(FilePath("b:a").IsRootDirectory()); 1.686 + EXPECT_FALSE(FilePath("8:/").IsRootDirectory()); 1.687 + EXPECT_FALSE(FilePath("c|/").IsRootDirectory()); 1.688 +#else 1.689 + EXPECT_TRUE(FilePath("/").IsRootDirectory()); 1.690 + EXPECT_TRUE(FilePath("//").IsRootDirectory()); 1.691 + EXPECT_FALSE(FilePath("").IsRootDirectory()); 1.692 + EXPECT_FALSE(FilePath("\\").IsRootDirectory()); 1.693 + EXPECT_FALSE(FilePath("/x").IsRootDirectory()); 1.694 +#endif 1.695 +} 1.696 + 1.697 +} // namespace 1.698 +} // namespace internal 1.699 +} // namespace testing