2
0

googletest-test-part-test.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #include "gtest/gtest-test-part.h"
  30. #include "gtest/gtest.h"
  31. using testing::Message;
  32. using testing::Test;
  33. using testing::TestPartResult;
  34. using testing::TestPartResultArray;
  35. namespace {
  36. // Tests the TestPartResult class.
  37. // The test fixture for testing TestPartResult.
  38. class TestPartResultTest : public Test {
  39. protected:
  40. TestPartResultTest()
  41. : r1_(TestPartResult::kSuccess, "foo/bar.cc", 10, "Success!"),
  42. r2_(TestPartResult::kNonFatalFailure, "foo/bar.cc", -1, "Failure!"),
  43. r3_(TestPartResult::kFatalFailure, nullptr, -1, "Failure!"),
  44. r4_(TestPartResult::kSkip, "foo/bar.cc", 2, "Skipped!") {}
  45. TestPartResult r1_, r2_, r3_, r4_;
  46. };
  47. TEST_F(TestPartResultTest, ConstructorWorks) {
  48. Message message;
  49. message << "something is terribly wrong";
  50. message << static_cast<const char*>(testing::internal::kStackTraceMarker);
  51. message << "some unimportant stack trace";
  52. const TestPartResult result(TestPartResult::kNonFatalFailure, "some_file.cc",
  53. 42, message.GetString().c_str());
  54. EXPECT_EQ(TestPartResult::kNonFatalFailure, result.type());
  55. EXPECT_STREQ("some_file.cc", result.file_name());
  56. EXPECT_EQ(42, result.line_number());
  57. EXPECT_STREQ(message.GetString().c_str(), result.message());
  58. EXPECT_STREQ("something is terribly wrong", result.summary());
  59. }
  60. TEST_F(TestPartResultTest, ResultAccessorsWork) {
  61. const TestPartResult success(TestPartResult::kSuccess, "file.cc", 42,
  62. "message");
  63. EXPECT_TRUE(success.passed());
  64. EXPECT_FALSE(success.failed());
  65. EXPECT_FALSE(success.nonfatally_failed());
  66. EXPECT_FALSE(success.fatally_failed());
  67. EXPECT_FALSE(success.skipped());
  68. const TestPartResult nonfatal_failure(TestPartResult::kNonFatalFailure,
  69. "file.cc", 42, "message");
  70. EXPECT_FALSE(nonfatal_failure.passed());
  71. EXPECT_TRUE(nonfatal_failure.failed());
  72. EXPECT_TRUE(nonfatal_failure.nonfatally_failed());
  73. EXPECT_FALSE(nonfatal_failure.fatally_failed());
  74. EXPECT_FALSE(nonfatal_failure.skipped());
  75. const TestPartResult fatal_failure(TestPartResult::kFatalFailure, "file.cc",
  76. 42, "message");
  77. EXPECT_FALSE(fatal_failure.passed());
  78. EXPECT_TRUE(fatal_failure.failed());
  79. EXPECT_FALSE(fatal_failure.nonfatally_failed());
  80. EXPECT_TRUE(fatal_failure.fatally_failed());
  81. EXPECT_FALSE(fatal_failure.skipped());
  82. const TestPartResult skip(TestPartResult::kSkip, "file.cc", 42, "message");
  83. EXPECT_FALSE(skip.passed());
  84. EXPECT_FALSE(skip.failed());
  85. EXPECT_FALSE(skip.nonfatally_failed());
  86. EXPECT_FALSE(skip.fatally_failed());
  87. EXPECT_TRUE(skip.skipped());
  88. }
  89. // Tests TestPartResult::type().
  90. TEST_F(TestPartResultTest, type) {
  91. EXPECT_EQ(TestPartResult::kSuccess, r1_.type());
  92. EXPECT_EQ(TestPartResult::kNonFatalFailure, r2_.type());
  93. EXPECT_EQ(TestPartResult::kFatalFailure, r3_.type());
  94. EXPECT_EQ(TestPartResult::kSkip, r4_.type());
  95. }
  96. // Tests TestPartResult::file_name().
  97. TEST_F(TestPartResultTest, file_name) {
  98. EXPECT_STREQ("foo/bar.cc", r1_.file_name());
  99. EXPECT_STREQ(nullptr, r3_.file_name());
  100. EXPECT_STREQ("foo/bar.cc", r4_.file_name());
  101. }
  102. // Tests TestPartResult::line_number().
  103. TEST_F(TestPartResultTest, line_number) {
  104. EXPECT_EQ(10, r1_.line_number());
  105. EXPECT_EQ(-1, r2_.line_number());
  106. EXPECT_EQ(2, r4_.line_number());
  107. }
  108. // Tests TestPartResult::message().
  109. TEST_F(TestPartResultTest, message) {
  110. EXPECT_STREQ("Success!", r1_.message());
  111. EXPECT_STREQ("Skipped!", r4_.message());
  112. }
  113. // Tests TestPartResult::passed().
  114. TEST_F(TestPartResultTest, Passed) {
  115. EXPECT_TRUE(r1_.passed());
  116. EXPECT_FALSE(r2_.passed());
  117. EXPECT_FALSE(r3_.passed());
  118. EXPECT_FALSE(r4_.passed());
  119. }
  120. // Tests TestPartResult::failed().
  121. TEST_F(TestPartResultTest, Failed) {
  122. EXPECT_FALSE(r1_.failed());
  123. EXPECT_TRUE(r2_.failed());
  124. EXPECT_TRUE(r3_.failed());
  125. EXPECT_FALSE(r4_.failed());
  126. }
  127. // Tests TestPartResult::failed().
  128. TEST_F(TestPartResultTest, Skipped) {
  129. EXPECT_FALSE(r1_.skipped());
  130. EXPECT_FALSE(r2_.skipped());
  131. EXPECT_FALSE(r3_.skipped());
  132. EXPECT_TRUE(r4_.skipped());
  133. }
  134. // Tests TestPartResult::fatally_failed().
  135. TEST_F(TestPartResultTest, FatallyFailed) {
  136. EXPECT_FALSE(r1_.fatally_failed());
  137. EXPECT_FALSE(r2_.fatally_failed());
  138. EXPECT_TRUE(r3_.fatally_failed());
  139. EXPECT_FALSE(r4_.fatally_failed());
  140. }
  141. // Tests TestPartResult::nonfatally_failed().
  142. TEST_F(TestPartResultTest, NonfatallyFailed) {
  143. EXPECT_FALSE(r1_.nonfatally_failed());
  144. EXPECT_TRUE(r2_.nonfatally_failed());
  145. EXPECT_FALSE(r3_.nonfatally_failed());
  146. EXPECT_FALSE(r4_.nonfatally_failed());
  147. }
  148. // Tests the TestPartResultArray class.
  149. class TestPartResultArrayTest : public Test {
  150. protected:
  151. TestPartResultArrayTest()
  152. : r1_(TestPartResult::kNonFatalFailure, "foo/bar.cc", -1, "Failure 1"),
  153. r2_(TestPartResult::kFatalFailure, "foo/bar.cc", -1, "Failure 2") {}
  154. const TestPartResult r1_, r2_;
  155. };
  156. // Tests that TestPartResultArray initially has size 0.
  157. TEST_F(TestPartResultArrayTest, InitialSizeIsZero) {
  158. TestPartResultArray results;
  159. EXPECT_EQ(0, results.size());
  160. }
  161. // Tests that TestPartResultArray contains the given TestPartResult
  162. // after one Append() operation.
  163. TEST_F(TestPartResultArrayTest, ContainsGivenResultAfterAppend) {
  164. TestPartResultArray results;
  165. results.Append(r1_);
  166. EXPECT_EQ(1, results.size());
  167. EXPECT_STREQ("Failure 1", results.GetTestPartResult(0).message());
  168. }
  169. // Tests that TestPartResultArray contains the given TestPartResults
  170. // after two Append() operations.
  171. TEST_F(TestPartResultArrayTest, ContainsGivenResultsAfterTwoAppends) {
  172. TestPartResultArray results;
  173. results.Append(r1_);
  174. results.Append(r2_);
  175. EXPECT_EQ(2, results.size());
  176. EXPECT_STREQ("Failure 1", results.GetTestPartResult(0).message());
  177. EXPECT_STREQ("Failure 2", results.GetTestPartResult(1).message());
  178. }
  179. typedef TestPartResultArrayTest TestPartResultArrayDeathTest;
  180. // Tests that the program dies when GetTestPartResult() is called with
  181. // an invalid index.
  182. TEST_F(TestPartResultArrayDeathTest, DiesWhenIndexIsOutOfBound) {
  183. TestPartResultArray results;
  184. results.Append(r1_);
  185. EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(-1), "");
  186. EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(1), "");
  187. }
  188. } // namespace