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

http://unladen-swallow.googlecode.com/ · C++ · 159 lines · 83 code · 26 blank · 50 comment · 0 complexity · 395fadb21d70d18bc9cc6fc0e73cca51 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. // Author: mheule@google.com (Markus Heule)
  31. //
  32. #include <gtest/gtest-test-part.h>
  33. #include <gtest/gtest.h>
  34. using testing::Test;
  35. using testing::TestPartResult;
  36. using testing::TestPartResultArray;
  37. namespace {
  38. // Tests the TestPartResult class.
  39. // The test fixture for testing TestPartResult.
  40. class TestPartResultTest : public Test {
  41. protected:
  42. TestPartResultTest()
  43. : r1_(TestPartResult::kSuccess, "foo/bar.cc", 10, "Success!"),
  44. r2_(TestPartResult::kNonFatalFailure, "foo/bar.cc", -1, "Failure!"),
  45. r3_(TestPartResult::kFatalFailure, NULL, -1, "Failure!") {}
  46. TestPartResult r1_, r2_, r3_;
  47. };
  48. // Tests TestPartResult::type().
  49. TEST_F(TestPartResultTest, type) {
  50. EXPECT_EQ(TestPartResult::kSuccess, r1_.type());
  51. EXPECT_EQ(TestPartResult::kNonFatalFailure, r2_.type());
  52. EXPECT_EQ(TestPartResult::kFatalFailure, r3_.type());
  53. }
  54. // Tests TestPartResult::file_name().
  55. TEST_F(TestPartResultTest, file_name) {
  56. EXPECT_STREQ("foo/bar.cc", r1_.file_name());
  57. EXPECT_STREQ(NULL, r3_.file_name());
  58. }
  59. // Tests TestPartResult::line_number().
  60. TEST_F(TestPartResultTest, line_number) {
  61. EXPECT_EQ(10, r1_.line_number());
  62. EXPECT_EQ(-1, r2_.line_number());
  63. }
  64. // Tests TestPartResult::message().
  65. TEST_F(TestPartResultTest, message) {
  66. EXPECT_STREQ("Success!", r1_.message());
  67. }
  68. // Tests TestPartResult::passed().
  69. TEST_F(TestPartResultTest, Passed) {
  70. EXPECT_TRUE(r1_.passed());
  71. EXPECT_FALSE(r2_.passed());
  72. EXPECT_FALSE(r3_.passed());
  73. }
  74. // Tests TestPartResult::failed().
  75. TEST_F(TestPartResultTest, Failed) {
  76. EXPECT_FALSE(r1_.failed());
  77. EXPECT_TRUE(r2_.failed());
  78. EXPECT_TRUE(r3_.failed());
  79. }
  80. // Tests TestPartResult::fatally_failed().
  81. TEST_F(TestPartResultTest, FatallyFailed) {
  82. EXPECT_FALSE(r1_.fatally_failed());
  83. EXPECT_FALSE(r2_.fatally_failed());
  84. EXPECT_TRUE(r3_.fatally_failed());
  85. }
  86. // Tests TestPartResult::nonfatally_failed().
  87. TEST_F(TestPartResultTest, NonfatallyFailed) {
  88. EXPECT_FALSE(r1_.nonfatally_failed());
  89. EXPECT_TRUE(r2_.nonfatally_failed());
  90. EXPECT_FALSE(r3_.nonfatally_failed());
  91. }
  92. // Tests the TestPartResultArray class.
  93. class TestPartResultArrayTest : public Test {
  94. protected:
  95. TestPartResultArrayTest()
  96. : r1_(TestPartResult::kNonFatalFailure, "foo/bar.cc", -1, "Failure 1"),
  97. r2_(TestPartResult::kFatalFailure, "foo/bar.cc", -1, "Failure 2") {}
  98. const TestPartResult r1_, r2_;
  99. };
  100. // Tests that TestPartResultArray initially has size 0.
  101. TEST_F(TestPartResultArrayTest, InitialSizeIsZero) {
  102. TestPartResultArray results;
  103. EXPECT_EQ(0, results.size());
  104. }
  105. // Tests that TestPartResultArray contains the given TestPartResult
  106. // after one Append() operation.
  107. TEST_F(TestPartResultArrayTest, ContainsGivenResultAfterAppend) {
  108. TestPartResultArray results;
  109. results.Append(r1_);
  110. EXPECT_EQ(1, results.size());
  111. EXPECT_STREQ("Failure 1", results.GetTestPartResult(0).message());
  112. }
  113. // Tests that TestPartResultArray contains the given TestPartResults
  114. // after two Append() operations.
  115. TEST_F(TestPartResultArrayTest, ContainsGivenResultsAfterTwoAppends) {
  116. TestPartResultArray results;
  117. results.Append(r1_);
  118. results.Append(r2_);
  119. EXPECT_EQ(2, results.size());
  120. EXPECT_STREQ("Failure 1", results.GetTestPartResult(0).message());
  121. EXPECT_STREQ("Failure 2", results.GetTestPartResult(1).message());
  122. }
  123. typedef TestPartResultArrayTest TestPartResultArrayDeathTest;
  124. // Tests that the program dies when GetTestPartResult() is called with
  125. // an invalid index.
  126. TEST_F(TestPartResultArrayDeathTest, DiesWhenIndexIsOutOfBound) {
  127. TestPartResultArray results;
  128. results.Append(r1_);
  129. EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(-1), "");
  130. EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(1), "");
  131. }
  132. // TODO(mheule@google.com): Add a test for the class HasNewFatalFailureHelper.
  133. } // namespace