1 // Copyright 2008 Google Inc.
2 // All Rights Reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 #include "gtest/gtest-test-part.h"
32 #include "gtest/gtest.h"
34 using testing::Message;
36 using testing::TestPartResult;
37 using testing::TestPartResultArray;
41 // Tests the TestPartResult class.
43 // The test fixture for testing TestPartResult.
44 class TestPartResultTest : public Test {
47 : r1_(TestPartResult::kSuccess, "foo/bar.cc", 10, "Success!"),
48 r2_(TestPartResult::kNonFatalFailure, "foo/bar.cc", -1, "Failure!"),
49 r3_(TestPartResult::kFatalFailure, NULL, -1, "Failure!") {}
51 TestPartResult r1_, r2_, r3_;
55 TEST_F(TestPartResultTest, ConstructorWorks) {
57 message << "something is terribly wrong";
58 message << static_cast<const char*>(testing::internal::kStackTraceMarker);
59 message << "some unimportant stack trace";
61 const TestPartResult result(TestPartResult::kNonFatalFailure,
64 message.GetString().c_str());
66 EXPECT_EQ(TestPartResult::kNonFatalFailure, result.type());
67 EXPECT_STREQ("some_file.cc", result.file_name());
68 EXPECT_EQ(42, result.line_number());
69 EXPECT_STREQ(message.GetString().c_str(), result.message());
70 EXPECT_STREQ("something is terribly wrong", result.summary());
73 TEST_F(TestPartResultTest, ResultAccessorsWork) {
74 const TestPartResult success(TestPartResult::kSuccess,
78 EXPECT_TRUE(success.passed());
79 EXPECT_FALSE(success.failed());
80 EXPECT_FALSE(success.nonfatally_failed());
81 EXPECT_FALSE(success.fatally_failed());
83 const TestPartResult nonfatal_failure(TestPartResult::kNonFatalFailure,
87 EXPECT_FALSE(nonfatal_failure.passed());
88 EXPECT_TRUE(nonfatal_failure.failed());
89 EXPECT_TRUE(nonfatal_failure.nonfatally_failed());
90 EXPECT_FALSE(nonfatal_failure.fatally_failed());
92 const TestPartResult fatal_failure(TestPartResult::kFatalFailure,
96 EXPECT_FALSE(fatal_failure.passed());
97 EXPECT_TRUE(fatal_failure.failed());
98 EXPECT_FALSE(fatal_failure.nonfatally_failed());
99 EXPECT_TRUE(fatal_failure.fatally_failed());
102 // Tests TestPartResult::type().
103 TEST_F(TestPartResultTest, type) {
104 EXPECT_EQ(TestPartResult::kSuccess, r1_.type());
105 EXPECT_EQ(TestPartResult::kNonFatalFailure, r2_.type());
106 EXPECT_EQ(TestPartResult::kFatalFailure, r3_.type());
109 // Tests TestPartResult::file_name().
110 TEST_F(TestPartResultTest, file_name) {
111 EXPECT_STREQ("foo/bar.cc", r1_.file_name());
112 EXPECT_STREQ(NULL, r3_.file_name());
115 // Tests TestPartResult::line_number().
116 TEST_F(TestPartResultTest, line_number) {
117 EXPECT_EQ(10, r1_.line_number());
118 EXPECT_EQ(-1, r2_.line_number());
121 // Tests TestPartResult::message().
122 TEST_F(TestPartResultTest, message) {
123 EXPECT_STREQ("Success!", r1_.message());
126 // Tests TestPartResult::passed().
127 TEST_F(TestPartResultTest, Passed) {
128 EXPECT_TRUE(r1_.passed());
129 EXPECT_FALSE(r2_.passed());
130 EXPECT_FALSE(r3_.passed());
133 // Tests TestPartResult::failed().
134 TEST_F(TestPartResultTest, Failed) {
135 EXPECT_FALSE(r1_.failed());
136 EXPECT_TRUE(r2_.failed());
137 EXPECT_TRUE(r3_.failed());
140 // Tests TestPartResult::fatally_failed().
141 TEST_F(TestPartResultTest, FatallyFailed) {
142 EXPECT_FALSE(r1_.fatally_failed());
143 EXPECT_FALSE(r2_.fatally_failed());
144 EXPECT_TRUE(r3_.fatally_failed());
147 // Tests TestPartResult::nonfatally_failed().
148 TEST_F(TestPartResultTest, NonfatallyFailed) {
149 EXPECT_FALSE(r1_.nonfatally_failed());
150 EXPECT_TRUE(r2_.nonfatally_failed());
151 EXPECT_FALSE(r3_.nonfatally_failed());
154 // Tests the TestPartResultArray class.
156 class TestPartResultArrayTest : public Test {
158 TestPartResultArrayTest()
159 : r1_(TestPartResult::kNonFatalFailure, "foo/bar.cc", -1, "Failure 1"),
160 r2_(TestPartResult::kFatalFailure, "foo/bar.cc", -1, "Failure 2") {}
162 const TestPartResult r1_, r2_;
165 // Tests that TestPartResultArray initially has size 0.
166 TEST_F(TestPartResultArrayTest, InitialSizeIsZero) {
167 TestPartResultArray results;
168 EXPECT_EQ(0, results.size());
171 // Tests that TestPartResultArray contains the given TestPartResult
172 // after one Append() operation.
173 TEST_F(TestPartResultArrayTest, ContainsGivenResultAfterAppend) {
174 TestPartResultArray results;
176 EXPECT_EQ(1, results.size());
177 EXPECT_STREQ("Failure 1", results.GetTestPartResult(0).message());
180 // Tests that TestPartResultArray contains the given TestPartResults
181 // after two Append() operations.
182 TEST_F(TestPartResultArrayTest, ContainsGivenResultsAfterTwoAppends) {
183 TestPartResultArray results;
186 EXPECT_EQ(2, results.size());
187 EXPECT_STREQ("Failure 1", results.GetTestPartResult(0).message());
188 EXPECT_STREQ("Failure 2", results.GetTestPartResult(1).message());
191 typedef TestPartResultArrayTest TestPartResultArrayDeathTest;
193 // Tests that the program dies when GetTestPartResult() is called with
195 TEST_F(TestPartResultArrayDeathTest, DiesWhenIndexIsOutOfBound) {
196 TestPartResultArray results;
199 EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(-1), "");
200 EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(1), "");
203 // FIXME: Add a test for the class HasNewFatalFailureHelper.