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 // their 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 #ifdef __ANDROID__
    123       || exe_str == "gtest-options_test_ndk_c++" ||
    124       exe_str == "gtest-options_test_ndk_gnustl" ||
    125       exe_str == "gtest-options_test_ndk_stlport"
    126 #endif
    127       ;
    128 #endif  // GTEST_OS_WINDOWS
    129   if (!success)
    130     FAIL() << "GetCurrentExecutableName() returns " << exe_str;
    131 }
    132 
    133 class XmlOutputChangeDirTest : public Test {
    134  protected:
    135   virtual void SetUp() {
    136     original_working_dir_ = FilePath::GetCurrentDir();
    137     posix::ChDir("..");
    138     // This will make the test fail if run from the root directory.
    139     EXPECT_NE(original_working_dir_.string(),
    140               FilePath::GetCurrentDir().string());
    141   }
    142 
    143   virtual void TearDown() {
    144     posix::ChDir(original_working_dir_.string().c_str());
    145   }
    146 
    147   FilePath original_working_dir_;
    148 };
    149 
    150 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefault) {
    151   GTEST_FLAG(output) = "";
    152   EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_,
    153                                   FilePath("test_detail.xml")).string(),
    154             UnitTestOptions::GetAbsolutePathToOutputFile());
    155 }
    156 
    157 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefaultXML) {
    158   GTEST_FLAG(output) = "xml";
    159   EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_,
    160                                   FilePath("test_detail.xml")).string(),
    161             UnitTestOptions::GetAbsolutePathToOutputFile());
    162 }
    163 
    164 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativeFile) {
    165   GTEST_FLAG(output) = "xml:filename.abc";
    166   EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_,
    167                                   FilePath("filename.abc")).string(),
    168             UnitTestOptions::GetAbsolutePathToOutputFile());
    169 }
    170 
    171 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) {
    172   GTEST_FLAG(output) = "xml:path" GTEST_PATH_SEP_;
    173   const std::string expected_output_file =
    174       FilePath::ConcatPaths(
    175           original_working_dir_,
    176           FilePath(std::string("path") + GTEST_PATH_SEP_ +
    177                    GetCurrentExecutableName().string() + ".xml")).string();
    178   const std::string& output_file =
    179       UnitTestOptions::GetAbsolutePathToOutputFile();
    180 #if GTEST_OS_WINDOWS
    181   EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
    182 #else
    183   EXPECT_EQ(expected_output_file, output_file.c_str());
    184 #endif
    185 }
    186 
    187 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsoluteFile) {
    188 #if GTEST_OS_WINDOWS
    189   GTEST_FLAG(output) = "xml:c:\\tmp\\filename.abc";
    190   EXPECT_EQ(FilePath("c:\\tmp\\filename.abc").string(),
    191             UnitTestOptions::GetAbsolutePathToOutputFile());
    192 #else
    193   GTEST_FLAG(output) ="xml:/tmp/filename.abc";
    194   EXPECT_EQ(FilePath("/tmp/filename.abc").string(),
    195             UnitTestOptions::GetAbsolutePathToOutputFile());
    196 #endif
    197 }
    198 
    199 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsolutePath) {
    200 #if GTEST_OS_WINDOWS
    201   const std::string path = "c:\\tmp\\";
    202 #else
    203   const std::string path = "/tmp/";
    204 #endif
    205 
    206   GTEST_FLAG(output) = "xml:" + path;
    207   const std::string expected_output_file =
    208       path + GetCurrentExecutableName().string() + ".xml";
    209   const std::string& output_file =
    210       UnitTestOptions::GetAbsolutePathToOutputFile();
    211 
    212 #if GTEST_OS_WINDOWS
    213   EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
    214 #else
    215   EXPECT_EQ(expected_output_file, output_file.c_str());
    216 #endif
    217 }
    218 
    219 }  // namespace
    220 }  // namespace internal
    221 }  // namespace testing
    222