michael@0: // Copyright 2008 Google Inc. michael@0: // All Rights Reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // Author: mheule@google.com (Markus Heule) michael@0: // michael@0: michael@0: #include "gtest/gtest-test-part.h" michael@0: michael@0: #include "gtest/gtest.h" michael@0: michael@0: using testing::Message; michael@0: using testing::Test; michael@0: using testing::TestPartResult; michael@0: using testing::TestPartResultArray; michael@0: michael@0: namespace { michael@0: michael@0: // Tests the TestPartResult class. michael@0: michael@0: // The test fixture for testing TestPartResult. michael@0: class TestPartResultTest : public Test { michael@0: protected: michael@0: TestPartResultTest() michael@0: : r1_(TestPartResult::kSuccess, "foo/bar.cc", 10, "Success!"), michael@0: r2_(TestPartResult::kNonFatalFailure, "foo/bar.cc", -1, "Failure!"), michael@0: r3_(TestPartResult::kFatalFailure, NULL, -1, "Failure!") {} michael@0: michael@0: TestPartResult r1_, r2_, r3_; michael@0: }; michael@0: michael@0: michael@0: TEST_F(TestPartResultTest, ConstructorWorks) { michael@0: Message message; michael@0: message << "something is terribly wrong"; michael@0: message << static_cast(testing::internal::kStackTraceMarker); michael@0: message << "some unimportant stack trace"; michael@0: michael@0: const TestPartResult result(TestPartResult::kNonFatalFailure, michael@0: "some_file.cc", michael@0: 42, michael@0: message.GetString().c_str()); michael@0: michael@0: EXPECT_EQ(TestPartResult::kNonFatalFailure, result.type()); michael@0: EXPECT_STREQ("some_file.cc", result.file_name()); michael@0: EXPECT_EQ(42, result.line_number()); michael@0: EXPECT_STREQ(message.GetString().c_str(), result.message()); michael@0: EXPECT_STREQ("something is terribly wrong", result.summary()); michael@0: } michael@0: michael@0: TEST_F(TestPartResultTest, ResultAccessorsWork) { michael@0: const TestPartResult success(TestPartResult::kSuccess, michael@0: "file.cc", michael@0: 42, michael@0: "message"); michael@0: EXPECT_TRUE(success.passed()); michael@0: EXPECT_FALSE(success.failed()); michael@0: EXPECT_FALSE(success.nonfatally_failed()); michael@0: EXPECT_FALSE(success.fatally_failed()); michael@0: michael@0: const TestPartResult nonfatal_failure(TestPartResult::kNonFatalFailure, michael@0: "file.cc", michael@0: 42, michael@0: "message"); michael@0: EXPECT_FALSE(nonfatal_failure.passed()); michael@0: EXPECT_TRUE(nonfatal_failure.failed()); michael@0: EXPECT_TRUE(nonfatal_failure.nonfatally_failed()); michael@0: EXPECT_FALSE(nonfatal_failure.fatally_failed()); michael@0: michael@0: const TestPartResult fatal_failure(TestPartResult::kFatalFailure, michael@0: "file.cc", michael@0: 42, michael@0: "message"); michael@0: EXPECT_FALSE(fatal_failure.passed()); michael@0: EXPECT_TRUE(fatal_failure.failed()); michael@0: EXPECT_FALSE(fatal_failure.nonfatally_failed()); michael@0: EXPECT_TRUE(fatal_failure.fatally_failed()); michael@0: } michael@0: michael@0: // Tests TestPartResult::type(). michael@0: TEST_F(TestPartResultTest, type) { michael@0: EXPECT_EQ(TestPartResult::kSuccess, r1_.type()); michael@0: EXPECT_EQ(TestPartResult::kNonFatalFailure, r2_.type()); michael@0: EXPECT_EQ(TestPartResult::kFatalFailure, r3_.type()); michael@0: } michael@0: michael@0: // Tests TestPartResult::file_name(). michael@0: TEST_F(TestPartResultTest, file_name) { michael@0: EXPECT_STREQ("foo/bar.cc", r1_.file_name()); michael@0: EXPECT_STREQ(NULL, r3_.file_name()); michael@0: } michael@0: michael@0: // Tests TestPartResult::line_number(). michael@0: TEST_F(TestPartResultTest, line_number) { michael@0: EXPECT_EQ(10, r1_.line_number()); michael@0: EXPECT_EQ(-1, r2_.line_number()); michael@0: } michael@0: michael@0: // Tests TestPartResult::message(). michael@0: TEST_F(TestPartResultTest, message) { michael@0: EXPECT_STREQ("Success!", r1_.message()); michael@0: } michael@0: michael@0: // Tests TestPartResult::passed(). michael@0: TEST_F(TestPartResultTest, Passed) { michael@0: EXPECT_TRUE(r1_.passed()); michael@0: EXPECT_FALSE(r2_.passed()); michael@0: EXPECT_FALSE(r3_.passed()); michael@0: } michael@0: michael@0: // Tests TestPartResult::failed(). michael@0: TEST_F(TestPartResultTest, Failed) { michael@0: EXPECT_FALSE(r1_.failed()); michael@0: EXPECT_TRUE(r2_.failed()); michael@0: EXPECT_TRUE(r3_.failed()); michael@0: } michael@0: michael@0: // Tests TestPartResult::fatally_failed(). michael@0: TEST_F(TestPartResultTest, FatallyFailed) { michael@0: EXPECT_FALSE(r1_.fatally_failed()); michael@0: EXPECT_FALSE(r2_.fatally_failed()); michael@0: EXPECT_TRUE(r3_.fatally_failed()); michael@0: } michael@0: michael@0: // Tests TestPartResult::nonfatally_failed(). michael@0: TEST_F(TestPartResultTest, NonfatallyFailed) { michael@0: EXPECT_FALSE(r1_.nonfatally_failed()); michael@0: EXPECT_TRUE(r2_.nonfatally_failed()); michael@0: EXPECT_FALSE(r3_.nonfatally_failed()); michael@0: } michael@0: michael@0: // Tests the TestPartResultArray class. michael@0: michael@0: class TestPartResultArrayTest : public Test { michael@0: protected: michael@0: TestPartResultArrayTest() michael@0: : r1_(TestPartResult::kNonFatalFailure, "foo/bar.cc", -1, "Failure 1"), michael@0: r2_(TestPartResult::kFatalFailure, "foo/bar.cc", -1, "Failure 2") {} michael@0: michael@0: const TestPartResult r1_, r2_; michael@0: }; michael@0: michael@0: // Tests that TestPartResultArray initially has size 0. michael@0: TEST_F(TestPartResultArrayTest, InitialSizeIsZero) { michael@0: TestPartResultArray results; michael@0: EXPECT_EQ(0, results.size()); michael@0: } michael@0: michael@0: // Tests that TestPartResultArray contains the given TestPartResult michael@0: // after one Append() operation. michael@0: TEST_F(TestPartResultArrayTest, ContainsGivenResultAfterAppend) { michael@0: TestPartResultArray results; michael@0: results.Append(r1_); michael@0: EXPECT_EQ(1, results.size()); michael@0: EXPECT_STREQ("Failure 1", results.GetTestPartResult(0).message()); michael@0: } michael@0: michael@0: // Tests that TestPartResultArray contains the given TestPartResults michael@0: // after two Append() operations. michael@0: TEST_F(TestPartResultArrayTest, ContainsGivenResultsAfterTwoAppends) { michael@0: TestPartResultArray results; michael@0: results.Append(r1_); michael@0: results.Append(r2_); michael@0: EXPECT_EQ(2, results.size()); michael@0: EXPECT_STREQ("Failure 1", results.GetTestPartResult(0).message()); michael@0: EXPECT_STREQ("Failure 2", results.GetTestPartResult(1).message()); michael@0: } michael@0: michael@0: typedef TestPartResultArrayTest TestPartResultArrayDeathTest; michael@0: michael@0: // Tests that the program dies when GetTestPartResult() is called with michael@0: // an invalid index. michael@0: TEST_F(TestPartResultArrayDeathTest, DiesWhenIndexIsOutOfBound) { michael@0: TestPartResultArray results; michael@0: results.Append(r1_); michael@0: michael@0: EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(-1), ""); michael@0: EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(1), ""); michael@0: } michael@0: michael@0: // TODO(mheule@google.com): Add a test for the class HasNewFatalFailureHelper. michael@0: michael@0: } // namespace