Home | History | Annotate | Download | only in test
      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 (at) gmail.com (Keith Ray)
     31 //
     32 // Google Test UnitTestOptions tests
     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.cc, to avoid changing build or
     38 // make-files on Windows and other platforms. Do not #include this file
     39 // anywhere else!
     40 
     41 #include "gtest/gtest.h"
     42 
     43 #if GTEST_OS_WINDOWS_MOBILE
     44 # include <windows.h>
     45 #elif GTEST_OS_WINDOWS
     46 # include <direct.h>
     47 #endif  // GTEST_OS_WINDOWS_MOBILE
     48 
     49 // Indicates that this translation unit is part of Google Test's
     50 // implementation.  It must come before gtest-internal-inl.h is
     51 // included, or there will be a compiler error.  This trick is to
     52 // prevent a user from accidentally including gtest-internal-inl.h in
     53 // his code.
     54 #define GTEST_IMPLEMENTATION_ 1
     55 #include "src/gtest-internal-inl.h"
     56 #undef GTEST_IMPLEMENTATION_
     57 
     58 namespace testing {
     59 namespace internal {
     60 namespace {
     61 
     62 // Turns the given relative path into an absolute path.
     63 FilePath GetAbsolutePathOf(const FilePath& relative_path) {
     64   return FilePath::ConcatPaths(FilePath::GetCurrentDir(), relative_path);
     65 }
     66 
     67 // Testing UnitTestOptions::GetOutputFormat/GetOutputFile.
     68 
     69 TEST(XmlOutputTest, GetOutputFormatDefault) {
     70   GTEST_FLAG(output) = "";
     71   EXPECT_STREQ("", UnitTestOptions::GetOutputFormat().c_str());
     72 }
     73 
     74 TEST(XmlOutputTest, GetOutputFormat) {
     75   GTEST_FLAG(output) = "xml:filename";
     76   EXPECT_STREQ("xml", UnitTestOptions::GetOutputFormat().c_str());
     77 }
     78 
     79 TEST(XmlOutputTest, GetOutputFileDefault) {
     80   GTEST_FLAG(output) = "";
     81   EXPECT_STREQ(GetAbsolutePathOf(FilePath("test_detail.xml")).c_str(),
     82                UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
     83 }
     84 
     85 TEST(XmlOutputTest, GetOutputFileSingleFile) {
     86   GTEST_FLAG(output) = "xml:filename.abc";
     87   EXPECT_STREQ(GetAbsolutePathOf(FilePath("filename.abc")).c_str(),
     88                UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
     89 }
     90 
     91 TEST(XmlOutputTest, GetOutputFileFromDirectoryPath) {
     92   GTEST_FLAG(output) = "xml:path" GTEST_PATH_SEP_;
     93   const std::string expected_output_file =
     94       GetAbsolutePathOf(
     95           FilePath(std::string("path") + GTEST_PATH_SEP_ +
     96                    GetCurrentExecutableName().c_str() + ".xml")).c_str();
     97   const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
     98 #if GTEST_OS_WINDOWS
     99   EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
    100 #else
    101   EXPECT_EQ(expected_output_file, output_file.c_str());
    102 #endif
    103 }
    104 
    105 TEST(OutputFileHelpersTest, GetCurrentExecutableName) {
    106   const std::string exe_str = GetCurrentExecutableName().c_str();
    107 #if GTEST_OS_WINDOWS
    108   const bool success =
    109       _strcmpi("gtest-options_test", exe_str.c_str()) == 0 ||
    110       _strcmpi("gtest-options-ex_test", exe_str.c_str()) == 0 ||
    111       _strcmpi("gtest_all_test", exe_str.c_str()) == 0 ||
    112       _strcmpi("gtest_dll_test", exe_str.c_str()) == 0;
    113 #else
    114   // TODO(wan (at) google.com): remove the hard-coded "lt-" prefix when
    115   //   Chandler Carruth's libtool replacement is ready.
    116   const bool success =
    117       exe_str == "gtest-options_test" ||
    118       exe_str == "gtest_all_test" ||
    119       exe_str == "lt-gtest_all_test" ||
    120       exe_str == "gtest_dll_test";
    121 #endif  // GTEST_OS_WINDOWS
    122   if (!success)
    123     FAIL() << "GetCurrentExecutableName() returns " << exe_str;
    124 }
    125 
    126 class XmlOutputChangeDirTest : public Test {
    127  protected:
    128   virtual void SetUp() {
    129     original_working_dir_ = FilePath::GetCurrentDir();
    130     posix::ChDir("..");
    131     // This will make the test fail if run from the root directory.
    132     EXPECT_STRNE(original_working_dir_.c_str(),
    133                  FilePath::GetCurrentDir().c_str());
    134   }
    135 
    136   virtual void TearDown() {
    137     posix::ChDir(original_working_dir_.c_str());
    138   }
    139 
    140   FilePath original_working_dir_;
    141 };
    142 
    143 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefault) {
    144   GTEST_FLAG(output) = "";
    145   EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_,
    146                                      FilePath("test_detail.xml")).c_str(),
    147                UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
    148 }
    149 
    150 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefaultXML) {
    151   GTEST_FLAG(output) = "xml";
    152   EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_,
    153                                      FilePath("test_detail.xml")).c_str(),
    154                UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
    155 }
    156 
    157 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativeFile) {
    158   GTEST_FLAG(output) = "xml:filename.abc";
    159   EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_,
    160                                      FilePath("filename.abc")).c_str(),
    161                UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
    162 }
    163 
    164 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) {
    165   GTEST_FLAG(output) = "xml:path" GTEST_PATH_SEP_;
    166   const std::string expected_output_file =
    167       FilePath::ConcatPaths(
    168           original_working_dir_,
    169           FilePath(std::string("path") + GTEST_PATH_SEP_ +
    170                    GetCurrentExecutableName().c_str() + ".xml")).c_str();
    171   const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
    172 #if GTEST_OS_WINDOWS
    173   EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
    174 #else
    175   EXPECT_EQ(expected_output_file, output_file.c_str());
    176 #endif
    177 }
    178 
    179 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsoluteFile) {
    180 #if GTEST_OS_WINDOWS
    181   GTEST_FLAG(output) = "xml:c:\\tmp\\filename.abc";
    182   EXPECT_STREQ(FilePath("c:\\tmp\\filename.abc").c_str(),
    183                UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
    184 #else
    185   GTEST_FLAG(output) ="xml:/tmp/filename.abc";
    186   EXPECT_STREQ(FilePath("/tmp/filename.abc").c_str(),
    187                UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
    188 #endif
    189 }
    190 
    191 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsolutePath) {
    192 #if GTEST_OS_WINDOWS
    193   const std::string path = "c:\\tmp\\";
    194 #else
    195   const std::string path = "/tmp/";
    196 #endif
    197 
    198   GTEST_FLAG(output) = "xml:" + path;
    199   const std::string expected_output_file =
    200       path + GetCurrentExecutableName().c_str() + ".xml";
    201   const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
    202 
    203 #if GTEST_OS_WINDOWS
    204   EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
    205 #else
    206   EXPECT_EQ(expected_output_file, output_file.c_str());
    207 #endif
    208 }
    209 
    210 }  // namespace
    211 }  // namespace internal
    212 }  // namespace testing
    213