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.

216 lines
7.7 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. // Google Test UnitTestOptions tests
  31. //
  32. // This file tests classes and functions used internally by
  33. // Google Test. They are subject to change without notice.
  34. //
  35. // This file is #included from gtest.cc, to avoid changing build or
  36. // make-files on Windows and other platforms. Do not #include this file
  37. // anywhere else!
  38. #include "gtest/gtest.h"
  39. #if GTEST_OS_WINDOWS_MOBILE
  40. # include <windows.h>
  41. #elif GTEST_OS_WINDOWS
  42. # include <direct.h>
  43. #endif // GTEST_OS_WINDOWS_MOBILE
  44. #include "src/gtest-internal-inl.h"
  45. namespace testing {
  46. namespace internal {
  47. namespace {
  48. // Turns the given relative path into an absolute path.
  49. FilePath GetAbsolutePathOf(const FilePath& relative_path) {
  50. return FilePath::ConcatPaths(FilePath::GetCurrentDir(), relative_path);
  51. }
  52. // Testing UnitTestOptions::GetOutputFormat/GetOutputFile.
  53. TEST(XmlOutputTest, GetOutputFormatDefault) {
  54. GTEST_FLAG(output) = "";
  55. EXPECT_STREQ("", UnitTestOptions::GetOutputFormat().c_str());
  56. }
  57. TEST(XmlOutputTest, GetOutputFormat) {
  58. GTEST_FLAG(output) = "xml:filename";
  59. EXPECT_STREQ("xml", UnitTestOptions::GetOutputFormat().c_str());
  60. }
  61. TEST(XmlOutputTest, GetOutputFileDefault) {
  62. GTEST_FLAG(output) = "";
  63. EXPECT_EQ(GetAbsolutePathOf(FilePath("test_detail.xml")).string(),
  64. UnitTestOptions::GetAbsolutePathToOutputFile());
  65. }
  66. TEST(XmlOutputTest, GetOutputFileSingleFile) {
  67. GTEST_FLAG(output) = "xml:filename.abc";
  68. EXPECT_EQ(GetAbsolutePathOf(FilePath("filename.abc")).string(),
  69. UnitTestOptions::GetAbsolutePathToOutputFile());
  70. }
  71. TEST(XmlOutputTest, GetOutputFileFromDirectoryPath) {
  72. GTEST_FLAG(output) = "xml:path" GTEST_PATH_SEP_;
  73. const std::string expected_output_file =
  74. GetAbsolutePathOf(
  75. FilePath(std::string("path") + GTEST_PATH_SEP_ +
  76. GetCurrentExecutableName().string() + ".xml")).string();
  77. const std::string& output_file =
  78. UnitTestOptions::GetAbsolutePathToOutputFile();
  79. #if GTEST_OS_WINDOWS
  80. EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
  81. #else
  82. EXPECT_EQ(expected_output_file, output_file.c_str());
  83. #endif
  84. }
  85. TEST(OutputFileHelpersTest, GetCurrentExecutableName) {
  86. const std::string exe_str = GetCurrentExecutableName().string();
  87. #if GTEST_OS_WINDOWS
  88. const bool success =
  89. _strcmpi("googletest-options-test", exe_str.c_str()) == 0 ||
  90. _strcmpi("gtest-options-ex_test", exe_str.c_str()) == 0 ||
  91. _strcmpi("gtest_all_test", exe_str.c_str()) == 0 ||
  92. _strcmpi("gtest_dll_test", exe_str.c_str()) == 0;
  93. #elif GTEST_OS_OS2
  94. const bool success =
  95. strcasecmp("googletest-options-test", exe_str.c_str()) == 0 ||
  96. strcasecmp("gtest-options-ex_test", exe_str.c_str()) == 0 ||
  97. strcasecmp("gtest_all_test", exe_str.c_str()) == 0 ||
  98. strcasecmp("gtest_dll_test", exe_str.c_str()) == 0;
  99. #elif GTEST_OS_FUCHSIA
  100. const bool success = exe_str == "app";
  101. #else
  102. const bool success =
  103. exe_str == "googletest-options-test" ||
  104. exe_str == "gtest_all_test" ||
  105. exe_str == "lt-gtest_all_test" ||
  106. exe_str == "gtest_dll_test";
  107. #endif // GTEST_OS_WINDOWS
  108. if (!success)
  109. FAIL() << "GetCurrentExecutableName() returns " << exe_str;
  110. }
  111. #if !GTEST_OS_FUCHSIA
  112. class XmlOutputChangeDirTest : public Test {
  113. protected:
  114. void SetUp() override {
  115. original_working_dir_ = FilePath::GetCurrentDir();
  116. posix::ChDir("..");
  117. // This will make the test fail if run from the root directory.
  118. EXPECT_NE(original_working_dir_.string(),
  119. FilePath::GetCurrentDir().string());
  120. }
  121. void TearDown() override {
  122. posix::ChDir(original_working_dir_.string().c_str());
  123. }
  124. FilePath original_working_dir_;
  125. };
  126. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefault) {
  127. GTEST_FLAG(output) = "";
  128. EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_,
  129. FilePath("test_detail.xml")).string(),
  130. UnitTestOptions::GetAbsolutePathToOutputFile());
  131. }
  132. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefaultXML) {
  133. GTEST_FLAG(output) = "xml";
  134. EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_,
  135. FilePath("test_detail.xml")).string(),
  136. UnitTestOptions::GetAbsolutePathToOutputFile());
  137. }
  138. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativeFile) {
  139. GTEST_FLAG(output) = "xml:filename.abc";
  140. EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_,
  141. FilePath("filename.abc")).string(),
  142. UnitTestOptions::GetAbsolutePathToOutputFile());
  143. }
  144. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) {
  145. GTEST_FLAG(output) = "xml:path" GTEST_PATH_SEP_;
  146. const std::string expected_output_file =
  147. FilePath::ConcatPaths(
  148. original_working_dir_,
  149. FilePath(std::string("path") + GTEST_PATH_SEP_ +
  150. GetCurrentExecutableName().string() + ".xml")).string();
  151. const std::string& output_file =
  152. 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_EQ(FilePath("c:\\tmp\\filename.abc").string(),
  163. UnitTestOptions::GetAbsolutePathToOutputFile());
  164. #else
  165. GTEST_FLAG(output) ="xml:/tmp/filename.abc";
  166. EXPECT_EQ(FilePath("/tmp/filename.abc").string(),
  167. UnitTestOptions::GetAbsolutePathToOutputFile());
  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().string() + ".xml";
  179. const std::string& output_file =
  180. UnitTestOptions::GetAbsolutePathToOutputFile();
  181. #if GTEST_OS_WINDOWS
  182. EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
  183. #else
  184. EXPECT_EQ(expected_output_file, output_file.c_str());
  185. #endif
  186. }
  187. #endif // !GTEST_OS_FUCHSIA
  188. } // namespace
  189. } // namespace internal
  190. } // namespace testing