You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
7.8 KiB

  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_STREQ(GetAbsolutePathOf(FilePath("test_detail.xml")).c_str(),
  73. UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
  74. }
  75. TEST(XmlOutputTest, GetOutputFileSingleFile) {
  76. GTEST_FLAG(output) = "xml:filename.abc";
  77. EXPECT_STREQ(GetAbsolutePathOf(FilePath("filename.abc")).c_str(),
  78. UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
  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().c_str() + ".xml")).c_str();
  86. const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
  87. #if GTEST_OS_WINDOWS
  88. EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
  89. #else
  90. EXPECT_EQ(expected_output_file, output_file.c_str());
  91. #endif
  92. }
  93. TEST(OutputFileHelpersTest, GetCurrentExecutableName) {
  94. const std::string exe_str = GetCurrentExecutableName().c_str();
  95. #if GTEST_OS_WINDOWS
  96. const bool success =
  97. _strcmpi("gtest-options_test", exe_str.c_str()) == 0 ||
  98. _strcmpi("gtest-options-ex_test", exe_str.c_str()) == 0 ||
  99. _strcmpi("gtest_all_test", exe_str.c_str()) == 0 ||
  100. _strcmpi("gtest_dll_test", exe_str.c_str()) == 0;
  101. #else
  102. // TODO(wan@google.com): remove the hard-coded "lt-" prefix when
  103. // Chandler Carruth's libtool replacement is ready.
  104. const bool success =
  105. exe_str == "gtest-options_test" ||
  106. exe_str == "gtest_all_test" ||
  107. exe_str == "lt-gtest_all_test" ||
  108. exe_str == "gtest_dll_test";
  109. #endif // GTEST_OS_WINDOWS
  110. if (!success)
  111. FAIL() << "GetCurrentExecutableName() returns " << exe_str;
  112. }
  113. class XmlOutputChangeDirTest : public Test {
  114. protected:
  115. virtual void SetUp() {
  116. original_working_dir_ = FilePath::GetCurrentDir();
  117. posix::ChDir("..");
  118. // This will make the test fail if run from the root directory.
  119. EXPECT_STRNE(original_working_dir_.c_str(),
  120. FilePath::GetCurrentDir().c_str());
  121. }
  122. virtual void TearDown() {
  123. posix::ChDir(original_working_dir_.c_str());
  124. }
  125. FilePath original_working_dir_;
  126. };
  127. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefault) {
  128. GTEST_FLAG(output) = "";
  129. EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_,
  130. FilePath("test_detail.xml")).c_str(),
  131. UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
  132. }
  133. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefaultXML) {
  134. GTEST_FLAG(output) = "xml";
  135. EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_,
  136. FilePath("test_detail.xml")).c_str(),
  137. UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
  138. }
  139. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativeFile) {
  140. GTEST_FLAG(output) = "xml:filename.abc";
  141. EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_,
  142. FilePath("filename.abc")).c_str(),
  143. UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
  144. }
  145. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) {
  146. GTEST_FLAG(output) = "xml:path" GTEST_PATH_SEP_;
  147. const std::string expected_output_file =
  148. FilePath::ConcatPaths(
  149. original_working_dir_,
  150. FilePath(std::string("path") + GTEST_PATH_SEP_ +
  151. GetCurrentExecutableName().c_str() + ".xml")).c_str();
  152. const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
  153. #if GTEST_OS_WINDOWS
  154. EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
  155. #else
  156. EXPECT_EQ(expected_output_file, output_file.c_str());
  157. #endif
  158. }
  159. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsoluteFile) {
  160. #if GTEST_OS_WINDOWS
  161. GTEST_FLAG(output) = "xml:c:\\tmp\\filename.abc";
  162. EXPECT_STREQ(FilePath("c:\\tmp\\filename.abc").c_str(),
  163. UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
  164. #else
  165. GTEST_FLAG(output) ="xml:/tmp/filename.abc";
  166. EXPECT_STREQ(FilePath("/tmp/filename.abc").c_str(),
  167. UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
  168. #endif
  169. }
  170. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsolutePath) {
  171. #if GTEST_OS_WINDOWS
  172. const std::string path = "c:\\tmp\\";
  173. #else
  174. const std::string path = "/tmp/";
  175. #endif
  176. GTEST_FLAG(output) = "xml:" + path;
  177. const std::string expected_output_file =
  178. path + GetCurrentExecutableName().c_str() + ".xml";
  179. const String& output_file = 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. } // namespace
  187. } // namespace internal
  188. } // namespace testing