gtest-options_test.cc 7.8 KB

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