gtest-test-part_test.cc
Go to the documentation of this file.
00001 // Copyright 2008 Google Inc.
00002 // All Rights Reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: mheule@google.com (Markus Heule)
00031 //
00032 
00033 #include "gtest/gtest-test-part.h"
00034 
00035 #include "gtest/gtest.h"
00036 
00037 using testing::Message;
00038 using testing::Test;
00039 using testing::TestPartResult;
00040 using testing::TestPartResultArray;
00041 
00042 namespace {
00043 
00044 // Tests the TestPartResult class.
00045 
00046 // The test fixture for testing TestPartResult.
00047 class TestPartResultTest : public Test {
00048  protected:
00049   TestPartResultTest()
00050       : r1_(TestPartResult::kSuccess, "foo/bar.cc", 10, "Success!"),
00051         r2_(TestPartResult::kNonFatalFailure, "foo/bar.cc", -1, "Failure!"),
00052         r3_(TestPartResult::kFatalFailure, NULL, -1, "Failure!") {}
00053 
00054   TestPartResult r1_, r2_, r3_;
00055 };
00056 
00057 
00058 TEST_F(TestPartResultTest, ConstructorWorks) {
00059   Message message;
00060   message << "something is terribly wrong";
00061   message << static_cast<const char*>(testing::internal::kStackTraceMarker);
00062   message << "some unimportant stack trace";
00063 
00064   const TestPartResult result(TestPartResult::kNonFatalFailure,
00065                               "some_file.cc",
00066                               42,
00067                               message.GetString().c_str());
00068 
00069   EXPECT_EQ(TestPartResult::kNonFatalFailure, result.type());
00070   EXPECT_STREQ("some_file.cc", result.file_name());
00071   EXPECT_EQ(42, result.line_number());
00072   EXPECT_STREQ(message.GetString().c_str(), result.message());
00073   EXPECT_STREQ("something is terribly wrong", result.summary());
00074 }
00075 
00076 TEST_F(TestPartResultTest, ResultAccessorsWork) {
00077   const TestPartResult success(TestPartResult::kSuccess,
00078                                "file.cc",
00079                                42,
00080                                "message");
00081   EXPECT_TRUE(success.passed());
00082   EXPECT_FALSE(success.failed());
00083   EXPECT_FALSE(success.nonfatally_failed());
00084   EXPECT_FALSE(success.fatally_failed());
00085 
00086   const TestPartResult nonfatal_failure(TestPartResult::kNonFatalFailure,
00087                                         "file.cc",
00088                                         42,
00089                                         "message");
00090   EXPECT_FALSE(nonfatal_failure.passed());
00091   EXPECT_TRUE(nonfatal_failure.failed());
00092   EXPECT_TRUE(nonfatal_failure.nonfatally_failed());
00093   EXPECT_FALSE(nonfatal_failure.fatally_failed());
00094 
00095   const TestPartResult fatal_failure(TestPartResult::kFatalFailure,
00096                                      "file.cc",
00097                                      42,
00098                                      "message");
00099   EXPECT_FALSE(fatal_failure.passed());
00100   EXPECT_TRUE(fatal_failure.failed());
00101   EXPECT_FALSE(fatal_failure.nonfatally_failed());
00102   EXPECT_TRUE(fatal_failure.fatally_failed());
00103 }
00104 
00105 // Tests TestPartResult::type().
00106 TEST_F(TestPartResultTest, type) {
00107   EXPECT_EQ(TestPartResult::kSuccess, r1_.type());
00108   EXPECT_EQ(TestPartResult::kNonFatalFailure, r2_.type());
00109   EXPECT_EQ(TestPartResult::kFatalFailure, r3_.type());
00110 }
00111 
00112 // Tests TestPartResult::file_name().
00113 TEST_F(TestPartResultTest, file_name) {
00114   EXPECT_STREQ("foo/bar.cc", r1_.file_name());
00115   EXPECT_STREQ(NULL, r3_.file_name());
00116 }
00117 
00118 // Tests TestPartResult::line_number().
00119 TEST_F(TestPartResultTest, line_number) {
00120   EXPECT_EQ(10, r1_.line_number());
00121   EXPECT_EQ(-1, r2_.line_number());
00122 }
00123 
00124 // Tests TestPartResult::message().
00125 TEST_F(TestPartResultTest, message) {
00126   EXPECT_STREQ("Success!", r1_.message());
00127 }
00128 
00129 // Tests TestPartResult::passed().
00130 TEST_F(TestPartResultTest, Passed) {
00131   EXPECT_TRUE(r1_.passed());
00132   EXPECT_FALSE(r2_.passed());
00133   EXPECT_FALSE(r3_.passed());
00134 }
00135 
00136 // Tests TestPartResult::failed().
00137 TEST_F(TestPartResultTest, Failed) {
00138   EXPECT_FALSE(r1_.failed());
00139   EXPECT_TRUE(r2_.failed());
00140   EXPECT_TRUE(r3_.failed());
00141 }
00142 
00143 // Tests TestPartResult::fatally_failed().
00144 TEST_F(TestPartResultTest, FatallyFailed) {
00145   EXPECT_FALSE(r1_.fatally_failed());
00146   EXPECT_FALSE(r2_.fatally_failed());
00147   EXPECT_TRUE(r3_.fatally_failed());
00148 }
00149 
00150 // Tests TestPartResult::nonfatally_failed().
00151 TEST_F(TestPartResultTest, NonfatallyFailed) {
00152   EXPECT_FALSE(r1_.nonfatally_failed());
00153   EXPECT_TRUE(r2_.nonfatally_failed());
00154   EXPECT_FALSE(r3_.nonfatally_failed());
00155 }
00156 
00157 // Tests the TestPartResultArray class.
00158 
00159 class TestPartResultArrayTest : public Test {
00160  protected:
00161   TestPartResultArrayTest()
00162       : r1_(TestPartResult::kNonFatalFailure, "foo/bar.cc", -1, "Failure 1"),
00163         r2_(TestPartResult::kFatalFailure, "foo/bar.cc", -1, "Failure 2") {}
00164 
00165   const TestPartResult r1_, r2_;
00166 };
00167 
00168 // Tests that TestPartResultArray initially has size 0.
00169 TEST_F(TestPartResultArrayTest, InitialSizeIsZero) {
00170   TestPartResultArray results;
00171   EXPECT_EQ(0, results.size());
00172 }
00173 
00174 // Tests that TestPartResultArray contains the given TestPartResult
00175 // after one Append() operation.
00176 TEST_F(TestPartResultArrayTest, ContainsGivenResultAfterAppend) {
00177   TestPartResultArray results;
00178   results.Append(r1_);
00179   EXPECT_EQ(1, results.size());
00180   EXPECT_STREQ("Failure 1", results.GetTestPartResult(0).message());
00181 }
00182 
00183 // Tests that TestPartResultArray contains the given TestPartResults
00184 // after two Append() operations.
00185 TEST_F(TestPartResultArrayTest, ContainsGivenResultsAfterTwoAppends) {
00186   TestPartResultArray results;
00187   results.Append(r1_);
00188   results.Append(r2_);
00189   EXPECT_EQ(2, results.size());
00190   EXPECT_STREQ("Failure 1", results.GetTestPartResult(0).message());
00191   EXPECT_STREQ("Failure 2", results.GetTestPartResult(1).message());
00192 }
00193 
00194 typedef TestPartResultArrayTest TestPartResultArrayDeathTest;
00195 
00196 // Tests that the program dies when GetTestPartResult() is called with
00197 // an invalid index.
00198 TEST_F(TestPartResultArrayDeathTest, DiesWhenIndexIsOutOfBound) {
00199   TestPartResultArray results;
00200   results.Append(r1_);
00201 
00202   EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(-1), "");
00203   EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(1), "");
00204 }
00205 
00206 // TODO(mheule@google.com): Add a test for the class HasNewFatalFailureHelper.
00207 
00208 }  // namespace


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:03