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