/Unittests/googletest/test/gtest-options_test.cc

http://unladen-swallow.googlecode.com/ · C++ · 267 lines · 181 code · 22 blank · 64 comment · 24 complexity · 502518e16694b3b8adc4d00046434e20 MD5 · raw file

  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. #if GTEST_OS_WINDOWS
  82. GTEST_FLAG(output) = "xml:path\\";
  83. const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
  84. EXPECT_TRUE(
  85. _strcmpi(output_file.c_str(),
  86. GetAbsolutePathOf(
  87. FilePath("path\\gtest-options_test.xml")).c_str()) == 0 ||
  88. _strcmpi(output_file.c_str(),
  89. GetAbsolutePathOf(
  90. FilePath("path\\gtest-options-ex_test.xml")).c_str()) == 0 ||
  91. _strcmpi(output_file.c_str(),
  92. GetAbsolutePathOf(
  93. FilePath("path\\gtest_all_test.xml")).c_str()) == 0)
  94. << " output_file = " << output_file;
  95. #else
  96. GTEST_FLAG(output) = "xml:path/";
  97. const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
  98. // TODO(wan@google.com): libtool causes the test binary file to be
  99. // named lt-gtest-options_test. Therefore the output file may be
  100. // named .../lt-gtest-options_test.xml. We should remove this
  101. // hard-coded logic when Chandler Carruth's libtool replacement is
  102. // ready.
  103. EXPECT_TRUE(output_file ==
  104. GetAbsolutePathOf(
  105. FilePath("path/gtest-options_test.xml")).c_str() ||
  106. output_file ==
  107. GetAbsolutePathOf(
  108. FilePath("path/lt-gtest-options_test.xml")).c_str() ||
  109. output_file ==
  110. GetAbsolutePathOf(
  111. FilePath("path/gtest_all_test.xml")).c_str() ||
  112. output_file ==
  113. GetAbsolutePathOf(
  114. FilePath("path/lt-gtest_all_test.xml")).c_str())
  115. << " output_file = " << output_file;
  116. #endif
  117. }
  118. TEST(OutputFileHelpersTest, GetCurrentExecutableName) {
  119. const FilePath executable = GetCurrentExecutableName();
  120. const char* const exe_str = executable.c_str();
  121. #if GTEST_OS_WINDOWS
  122. ASSERT_TRUE(_strcmpi("gtest-options_test", exe_str) == 0 ||
  123. _strcmpi("gtest-options-ex_test", exe_str) == 0 ||
  124. _strcmpi("gtest_all_test", exe_str) == 0)
  125. << "GetCurrentExecutableName() returns " << exe_str;
  126. #else
  127. // TODO(wan@google.com): remove the hard-coded "lt-" prefix when
  128. // Chandler Carruth's libtool replacement is ready.
  129. EXPECT_TRUE(String(exe_str) == "gtest-options_test" ||
  130. String(exe_str) == "lt-gtest-options_test" ||
  131. String(exe_str) == "gtest_all_test" ||
  132. String(exe_str) == "lt-gtest_all_test")
  133. << "GetCurrentExecutableName() returns " << exe_str;
  134. #endif // GTEST_OS_WINDOWS
  135. }
  136. class XmlOutputChangeDirTest : public Test {
  137. protected:
  138. virtual void SetUp() {
  139. original_working_dir_ = FilePath::GetCurrentDir();
  140. posix::ChDir("..");
  141. // This will make the test fail if run from the root directory.
  142. EXPECT_STRNE(original_working_dir_.c_str(),
  143. FilePath::GetCurrentDir().c_str());
  144. }
  145. virtual void TearDown() {
  146. posix::ChDir(original_working_dir_.c_str());
  147. }
  148. FilePath original_working_dir_;
  149. };
  150. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefault) {
  151. GTEST_FLAG(output) = "";
  152. EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_,
  153. FilePath("test_detail.xml")).c_str(),
  154. UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
  155. }
  156. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefaultXML) {
  157. GTEST_FLAG(output) = "xml";
  158. EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_,
  159. FilePath("test_detail.xml")).c_str(),
  160. UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
  161. }
  162. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativeFile) {
  163. GTEST_FLAG(output) = "xml:filename.abc";
  164. EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_,
  165. FilePath("filename.abc")).c_str(),
  166. UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
  167. }
  168. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) {
  169. #if GTEST_OS_WINDOWS
  170. GTEST_FLAG(output) = "xml:path\\";
  171. const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
  172. EXPECT_TRUE(
  173. _strcmpi(output_file.c_str(),
  174. FilePath::ConcatPaths(
  175. original_working_dir_,
  176. FilePath("path\\gtest-options_test.xml")).c_str()) == 0 ||
  177. _strcmpi(output_file.c_str(),
  178. FilePath::ConcatPaths(
  179. original_working_dir_,
  180. FilePath("path\\gtest-options-ex_test.xml")).c_str()) == 0 ||
  181. _strcmpi(output_file.c_str(),
  182. FilePath::ConcatPaths(
  183. original_working_dir_,
  184. FilePath("path\\gtest_all_test.xml")).c_str()) == 0)
  185. << " output_file = " << output_file;
  186. #else
  187. GTEST_FLAG(output) = "xml:path/";
  188. const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
  189. // TODO(wan@google.com): libtool causes the test binary file to be
  190. // named lt-gtest-options_test. Therefore the output file may be
  191. // named .../lt-gtest-options_test.xml. We should remove this
  192. // hard-coded logic when Chandler Carruth's libtool replacement is
  193. // ready.
  194. EXPECT_TRUE(output_file == FilePath::ConcatPaths(original_working_dir_,
  195. FilePath("path/gtest-options_test.xml")).c_str() ||
  196. output_file == FilePath::ConcatPaths(original_working_dir_,
  197. FilePath("path/lt-gtest-options_test.xml")).c_str() ||
  198. output_file == FilePath::ConcatPaths(original_working_dir_,
  199. FilePath("path/gtest_all_test.xml")).c_str() ||
  200. output_file == FilePath::ConcatPaths(original_working_dir_,
  201. FilePath("path/lt-gtest_all_test.xml")).c_str())
  202. << " output_file = " << output_file;
  203. #endif
  204. }
  205. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsoluteFile) {
  206. #if GTEST_OS_WINDOWS
  207. GTEST_FLAG(output) = "xml:c:\\tmp\\filename.abc";
  208. EXPECT_STREQ(FilePath("c:\\tmp\\filename.abc").c_str(),
  209. UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
  210. #else
  211. GTEST_FLAG(output) ="xml:/tmp/filename.abc";
  212. EXPECT_STREQ(FilePath("/tmp/filename.abc").c_str(),
  213. UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
  214. #endif
  215. }
  216. TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsolutePath) {
  217. #if GTEST_OS_WINDOWS
  218. GTEST_FLAG(output) = "xml:c:\\tmp\\";
  219. const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
  220. EXPECT_TRUE(
  221. _strcmpi(output_file.c_str(),
  222. FilePath("c:\\tmp\\gtest-options_test.xml").c_str()) == 0 ||
  223. _strcmpi(output_file.c_str(),
  224. FilePath("c:\\tmp\\gtest-options-ex_test.xml").c_str()) == 0 ||
  225. _strcmpi(output_file.c_str(),
  226. FilePath("c:\\tmp\\gtest_all_test.xml").c_str()) == 0)
  227. << " output_file = " << output_file;
  228. #else
  229. GTEST_FLAG(output) = "xml:/tmp/";
  230. const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
  231. // TODO(wan@google.com): libtool causes the test binary file to be
  232. // named lt-gtest-options_test. Therefore the output file may be
  233. // named .../lt-gtest-options_test.xml. We should remove this
  234. // hard-coded logic when Chandler Carruth's libtool replacement is
  235. // ready.
  236. EXPECT_TRUE(output_file == "/tmp/gtest-options_test.xml" ||
  237. output_file == "/tmp/lt-gtest-options_test.xml" ||
  238. output_file == "/tmp/gtest_all_test.xml" ||
  239. output_file == "/tmp/lt-gtest_all_test.xml")
  240. << " output_file = " << output_file;
  241. #endif
  242. }
  243. } // namespace
  244. } // namespace internal
  245. } // namespace testing