michael@0: // Copyright 2008, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // Authors: keith.ray@gmail.com (Keith Ray) michael@0: // michael@0: // Google Test UnitTestOptions tests michael@0: // michael@0: // This file tests classes and functions used internally by michael@0: // Google Test. They are subject to change without notice. michael@0: // michael@0: // This file is #included from gtest.cc, to avoid changing build or michael@0: // make-files on Windows and other platforms. Do not #include this file michael@0: // anywhere else! michael@0: michael@0: #include "gtest/gtest.h" michael@0: michael@0: #if GTEST_OS_WINDOWS_MOBILE michael@0: # include michael@0: #elif GTEST_OS_WINDOWS michael@0: # include michael@0: #endif // GTEST_OS_WINDOWS_MOBILE michael@0: michael@0: // Indicates that this translation unit is part of Google Test's michael@0: // implementation. It must come before gtest-internal-inl.h is michael@0: // included, or there will be a compiler error. This trick is to michael@0: // prevent a user from accidentally including gtest-internal-inl.h in michael@0: // his code. michael@0: #define GTEST_IMPLEMENTATION_ 1 michael@0: #include "src/gtest-internal-inl.h" michael@0: #undef GTEST_IMPLEMENTATION_ michael@0: michael@0: namespace testing { michael@0: namespace internal { michael@0: namespace { michael@0: michael@0: // Turns the given relative path into an absolute path. michael@0: FilePath GetAbsolutePathOf(const FilePath& relative_path) { michael@0: return FilePath::ConcatPaths(FilePath::GetCurrentDir(), relative_path); michael@0: } michael@0: michael@0: // Testing UnitTestOptions::GetOutputFormat/GetOutputFile. michael@0: michael@0: TEST(XmlOutputTest, GetOutputFormatDefault) { michael@0: GTEST_FLAG(output) = ""; michael@0: EXPECT_STREQ("", UnitTestOptions::GetOutputFormat().c_str()); michael@0: } michael@0: michael@0: TEST(XmlOutputTest, GetOutputFormat) { michael@0: GTEST_FLAG(output) = "xml:filename"; michael@0: EXPECT_STREQ("xml", UnitTestOptions::GetOutputFormat().c_str()); michael@0: } michael@0: michael@0: TEST(XmlOutputTest, GetOutputFileDefault) { michael@0: GTEST_FLAG(output) = ""; michael@0: EXPECT_STREQ(GetAbsolutePathOf(FilePath("test_detail.xml")).c_str(), michael@0: UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); michael@0: } michael@0: michael@0: TEST(XmlOutputTest, GetOutputFileSingleFile) { michael@0: GTEST_FLAG(output) = "xml:filename.abc"; michael@0: EXPECT_STREQ(GetAbsolutePathOf(FilePath("filename.abc")).c_str(), michael@0: UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); michael@0: } michael@0: michael@0: TEST(XmlOutputTest, GetOutputFileFromDirectoryPath) { michael@0: GTEST_FLAG(output) = "xml:path" GTEST_PATH_SEP_; michael@0: const std::string expected_output_file = michael@0: GetAbsolutePathOf( michael@0: FilePath(std::string("path") + GTEST_PATH_SEP_ + michael@0: GetCurrentExecutableName().c_str() + ".xml")).c_str(); michael@0: const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile(); michael@0: #if GTEST_OS_WINDOWS michael@0: EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str()); michael@0: #else michael@0: EXPECT_EQ(expected_output_file, output_file.c_str()); michael@0: #endif michael@0: } michael@0: michael@0: TEST(OutputFileHelpersTest, GetCurrentExecutableName) { michael@0: const std::string exe_str = GetCurrentExecutableName().c_str(); michael@0: #if GTEST_OS_WINDOWS michael@0: const bool success = michael@0: _strcmpi("gtest-options_test", exe_str.c_str()) == 0 || michael@0: _strcmpi("gtest-options-ex_test", exe_str.c_str()) == 0 || michael@0: _strcmpi("gtest_all_test", exe_str.c_str()) == 0 || michael@0: _strcmpi("gtest_dll_test", exe_str.c_str()) == 0; michael@0: #else michael@0: // TODO(wan@google.com): remove the hard-coded "lt-" prefix when michael@0: // Chandler Carruth's libtool replacement is ready. michael@0: const bool success = michael@0: exe_str == "gtest-options_test" || michael@0: exe_str == "gtest_all_test" || michael@0: exe_str == "lt-gtest_all_test" || michael@0: exe_str == "gtest_dll_test"; michael@0: #endif // GTEST_OS_WINDOWS michael@0: if (!success) michael@0: FAIL() << "GetCurrentExecutableName() returns " << exe_str; michael@0: } michael@0: michael@0: class XmlOutputChangeDirTest : public Test { michael@0: protected: michael@0: virtual void SetUp() { michael@0: original_working_dir_ = FilePath::GetCurrentDir(); michael@0: posix::ChDir(".."); michael@0: // This will make the test fail if run from the root directory. michael@0: EXPECT_STRNE(original_working_dir_.c_str(), michael@0: FilePath::GetCurrentDir().c_str()); michael@0: } michael@0: michael@0: virtual void TearDown() { michael@0: posix::ChDir(original_working_dir_.c_str()); michael@0: } michael@0: michael@0: FilePath original_working_dir_; michael@0: }; michael@0: michael@0: TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefault) { michael@0: GTEST_FLAG(output) = ""; michael@0: EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_, michael@0: FilePath("test_detail.xml")).c_str(), michael@0: UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); michael@0: } michael@0: michael@0: TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefaultXML) { michael@0: GTEST_FLAG(output) = "xml"; michael@0: EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_, michael@0: FilePath("test_detail.xml")).c_str(), michael@0: UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); michael@0: } michael@0: michael@0: TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativeFile) { michael@0: GTEST_FLAG(output) = "xml:filename.abc"; michael@0: EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_, michael@0: FilePath("filename.abc")).c_str(), michael@0: UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); michael@0: } michael@0: michael@0: TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) { michael@0: GTEST_FLAG(output) = "xml:path" GTEST_PATH_SEP_; michael@0: const std::string expected_output_file = michael@0: FilePath::ConcatPaths( michael@0: original_working_dir_, michael@0: FilePath(std::string("path") + GTEST_PATH_SEP_ + michael@0: GetCurrentExecutableName().c_str() + ".xml")).c_str(); michael@0: const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile(); michael@0: #if GTEST_OS_WINDOWS michael@0: EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str()); michael@0: #else michael@0: EXPECT_EQ(expected_output_file, output_file.c_str()); michael@0: #endif michael@0: } michael@0: michael@0: TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsoluteFile) { michael@0: #if GTEST_OS_WINDOWS michael@0: GTEST_FLAG(output) = "xml:c:\\tmp\\filename.abc"; michael@0: EXPECT_STREQ(FilePath("c:\\tmp\\filename.abc").c_str(), michael@0: UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); michael@0: #else michael@0: GTEST_FLAG(output) ="xml:/tmp/filename.abc"; michael@0: EXPECT_STREQ(FilePath("/tmp/filename.abc").c_str(), michael@0: UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); michael@0: #endif michael@0: } michael@0: michael@0: TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsolutePath) { michael@0: #if GTEST_OS_WINDOWS michael@0: const std::string path = "c:\\tmp\\"; michael@0: #else michael@0: const std::string path = "/tmp/"; michael@0: #endif michael@0: michael@0: GTEST_FLAG(output) = "xml:" + path; michael@0: const std::string expected_output_file = michael@0: path + GetCurrentExecutableName().c_str() + ".xml"; michael@0: const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile(); michael@0: michael@0: #if GTEST_OS_WINDOWS michael@0: EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str()); michael@0: #else michael@0: EXPECT_EQ(expected_output_file, output_file.c_str()); michael@0: #endif michael@0: } michael@0: michael@0: } // namespace michael@0: } // namespace internal michael@0: } // namespace testing