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_EQ(GetAbsolutePathOf(FilePath("test_detail.xml")).string(),
     82             UnitTestOptions::GetAbsolutePathToOutputFile());
     83 }
     84 
     85 TEST(XmlOutputTest, GetOutputFileSingleFile) {
     86   GTEST_FLAG(output) = "xml:filename.abc";
     87   EXPECT_EQ(GetAbsolutePathOf(FilePath("filename.abc")).string(),
     88             UnitTestOptions::GetAbsolutePathToOutputFile());
     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().string() + ".xml")).string();
     97   const std::string& output_file =
     98       UnitTestOptions::GetAbsolutePathToOutputFile();
     99 #if GTEST_OS_WINDOWS
    100   EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
    101 #else
    102   EXPECT_EQ(expected_output_file, output_file.c_str());
    103 #endif
    104 }
    105 
    106 TEST(OutputFileHelpersTest, GetCurrentExecutableName) {
    107   const std::string exe_str = GetCurrentExecutableName().string();
    108 #if GTEST_OS_WINDOWS
    109   const bool success =
    110       _strcmpi("gtest-options_test", exe_str.c_str()) == 0 ||
    111       _strcmpi("gtest-options-ex_test", exe_str.c_str()) == 0 ||
    112       _strcmpi("gtest_all_test", exe_str.c_str()) == 0 ||
    113       _strcmpi("gtest_dll_test", exe_str.c_str()) == 0;
    114 #else
    115   // TODO(wan (at) google.com): remove the hard-coded "lt-" prefix when
    116   //   Chandler Carruth's libtool replacement is ready.
    117   const bool success =
    118       exe_str == "gtest-options_test" ||
    119       exe_str == "gtest_all_test" ||
    120       exe_str == "lt-gtest_all_test" ||
    121       exe_str == "gtest_dll_test";
    122 #endif  // GTEST_OS_WINDOWS
    123   if (!success)
    124     FAIL() << "GetCurrentExecutableName() returns " << exe_str;
    125 }
    126 
    127 class XmlOutputChangeDirTest : public Test {
    128  protected:
    129   virtual void SetUp() {
    130     original_working_dir_ = FilePath::GetCurrentDir();
    131     posix::ChDir("..");
    132     // This will make the test fail if run from the root directory.
    133     EXPECT_NE(original_working_dir_.string(),
    134               FilePath::GetCurrentDir().string());
    135   }
    136 
    137   virtual void TearDown() {
    138     posix::ChDir(original_working_dir_.string().c_str());
    139   }
    140 
    141   FilePath original_working_dir_;
    142 };
    143 
    144 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefault) {
    145   GTEST_FLAG(output) = "";
    146   EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_,
    147                                   FilePath("test_detail.xml")).string(),
    148             UnitTestOptions::GetAbsolutePathToOutputFile());
    149 }
    150 
    151 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefaultXML) {
    152   GTEST_FLAG(output) = "xml";
    153   EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_,
    154                                   FilePath("test_detail.xml")).string(),
    155             UnitTestOptions::GetAbsolutePathToOutputFile());
    156 }
    157 
    158 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativeFile) {
    159   GTEST_FLAG(output) = "xml:filename.abc";
    160   EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_,
    161                                   FilePath("filename.abc")).string(),
    162             UnitTestOptions::GetAbsolutePathToOutputFile());
    163 }
    164 
    165 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) {
    166   GTEST_FLAG(output) = "xml:path" GTEST_PATH_SEP_;
    167   const std::string expected_output_file =
    168       FilePath::ConcatPaths(
    169           original_working_dir_,
    170           FilePath(std::string("path") + GTEST_PATH_SEP_ +
    171                    GetCurrentExecutableName().string() + ".xml")).string();
    172   const std::string& output_file =
    173       UnitTestOptions::GetAbsolutePathToOutputFile();
    174 #if GTEST_OS_WINDOWS
    175   EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
    176 #else
    177   EXPECT_EQ(expected_output_file, output_file.c_str());
    178 #endif
    179 }
    180 
    181 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsoluteFile) {
    182 #if GTEST_OS_WINDOWS
    183   GTEST_FLAG(output) = "xml:c:\\tmp\\filename.abc";
    184   EXPECT_EQ(FilePath("c:\\tmp\\filename.abc").string(),
    185             UnitTestOptions::GetAbsolutePathToOutputFile());
    186 #else
    187   GTEST_FLAG(output) ="xml:/tmp/filename.abc";
    188   EXPECT_EQ(FilePath("/tmp/filename.abc").string(),
    189             UnitTestOptions::GetAbsolutePathToOutputFile());
    190 #endif
    191 }
    192 
    193 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsolutePath) {
    194 #if GTEST_OS_WINDOWS
    195   const std::string path = "c:\\tmp\\";
    196 #else
    197   const std::string path = "/tmp/";
    198 #endif
    199 
    200   GTEST_FLAG(output) = "xml:" + path;
    201   const std::string expected_output_file =
    202       path + GetCurrentExecutableName().string() + ".xml";
    203   const std::string& output_file =
    204       UnitTestOptions::GetAbsolutePathToOutputFile();
    205 
    206 #if GTEST_OS_WINDOWS
    207   EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
    208 #else
    209   EXPECT_EQ(expected_output_file, output_file.c_str());
    210 #endif
    211 }
    212 
    213 }  // namespace
    214 }  // namespace internal
    215 }  // namespace testing
    216