googletest-test-part-test.cc 7.0 KB

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