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.

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