blob: 44cf7ca044b850dadc0df89628225c07dea27c91 [file] [log] [blame]
Austin Schuh0cbef622015-09-06 17:34:52 -07001// 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.
Austin Schuh0cbef622015-09-06 17:34:52 -070029
30#include "gtest/gtest-test-part.h"
31
32#include "gtest/gtest.h"
33
34using testing::Message;
35using testing::Test;
36using testing::TestPartResult;
37using testing::TestPartResultArray;
38
39namespace {
40
41// Tests the TestPartResult class.
42
43// The test fixture for testing TestPartResult.
44class TestPartResultTest : public Test {
45 protected:
46 TestPartResultTest()
47 : r1_(TestPartResult::kSuccess, "foo/bar.cc", 10, "Success!"),
48 r2_(TestPartResult::kNonFatalFailure, "foo/bar.cc", -1, "Failure!"),
James Kuszmaule2f15292021-05-10 22:37:32 -070049 r3_(TestPartResult::kFatalFailure, nullptr, -1, "Failure!"),
50 r4_(TestPartResult::kSkip, "foo/bar.cc", 2, "Skipped!") {}
Austin Schuh0cbef622015-09-06 17:34:52 -070051
James Kuszmaule2f15292021-05-10 22:37:32 -070052 TestPartResult r1_, r2_, r3_, r4_;
Austin Schuh0cbef622015-09-06 17:34:52 -070053};
54
55
56TEST_F(TestPartResultTest, ConstructorWorks) {
57 Message message;
58 message << "something is terribly wrong";
59 message << static_cast<const char*>(testing::internal::kStackTraceMarker);
60 message << "some unimportant stack trace";
61
62 const TestPartResult result(TestPartResult::kNonFatalFailure,
63 "some_file.cc",
64 42,
65 message.GetString().c_str());
66
67 EXPECT_EQ(TestPartResult::kNonFatalFailure, result.type());
68 EXPECT_STREQ("some_file.cc", result.file_name());
69 EXPECT_EQ(42, result.line_number());
70 EXPECT_STREQ(message.GetString().c_str(), result.message());
71 EXPECT_STREQ("something is terribly wrong", result.summary());
72}
73
74TEST_F(TestPartResultTest, ResultAccessorsWork) {
75 const TestPartResult success(TestPartResult::kSuccess,
76 "file.cc",
77 42,
78 "message");
79 EXPECT_TRUE(success.passed());
80 EXPECT_FALSE(success.failed());
81 EXPECT_FALSE(success.nonfatally_failed());
82 EXPECT_FALSE(success.fatally_failed());
James Kuszmaule2f15292021-05-10 22:37:32 -070083 EXPECT_FALSE(success.skipped());
Austin Schuh0cbef622015-09-06 17:34:52 -070084
85 const TestPartResult nonfatal_failure(TestPartResult::kNonFatalFailure,
86 "file.cc",
87 42,
88 "message");
89 EXPECT_FALSE(nonfatal_failure.passed());
90 EXPECT_TRUE(nonfatal_failure.failed());
91 EXPECT_TRUE(nonfatal_failure.nonfatally_failed());
92 EXPECT_FALSE(nonfatal_failure.fatally_failed());
James Kuszmaule2f15292021-05-10 22:37:32 -070093 EXPECT_FALSE(nonfatal_failure.skipped());
Austin Schuh0cbef622015-09-06 17:34:52 -070094
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());
James Kuszmaule2f15292021-05-10 22:37:32 -0700103 EXPECT_FALSE(fatal_failure.skipped());
104
105 const TestPartResult skip(TestPartResult::kSkip, "file.cc", 42, "message");
106 EXPECT_FALSE(skip.passed());
107 EXPECT_FALSE(skip.failed());
108 EXPECT_FALSE(skip.nonfatally_failed());
109 EXPECT_FALSE(skip.fatally_failed());
110 EXPECT_TRUE(skip.skipped());
Austin Schuh0cbef622015-09-06 17:34:52 -0700111}
112
113// Tests TestPartResult::type().
114TEST_F(TestPartResultTest, type) {
115 EXPECT_EQ(TestPartResult::kSuccess, r1_.type());
116 EXPECT_EQ(TestPartResult::kNonFatalFailure, r2_.type());
117 EXPECT_EQ(TestPartResult::kFatalFailure, r3_.type());
James Kuszmaule2f15292021-05-10 22:37:32 -0700118 EXPECT_EQ(TestPartResult::kSkip, r4_.type());
Austin Schuh0cbef622015-09-06 17:34:52 -0700119}
120
121// Tests TestPartResult::file_name().
122TEST_F(TestPartResultTest, file_name) {
123 EXPECT_STREQ("foo/bar.cc", r1_.file_name());
James Kuszmaule2f15292021-05-10 22:37:32 -0700124 EXPECT_STREQ(nullptr, r3_.file_name());
125 EXPECT_STREQ("foo/bar.cc", r4_.file_name());
Austin Schuh0cbef622015-09-06 17:34:52 -0700126}
127
128// Tests TestPartResult::line_number().
129TEST_F(TestPartResultTest, line_number) {
130 EXPECT_EQ(10, r1_.line_number());
131 EXPECT_EQ(-1, r2_.line_number());
James Kuszmaule2f15292021-05-10 22:37:32 -0700132 EXPECT_EQ(2, r4_.line_number());
Austin Schuh0cbef622015-09-06 17:34:52 -0700133}
134
135// Tests TestPartResult::message().
136TEST_F(TestPartResultTest, message) {
137 EXPECT_STREQ("Success!", r1_.message());
James Kuszmaule2f15292021-05-10 22:37:32 -0700138 EXPECT_STREQ("Skipped!", r4_.message());
Austin Schuh0cbef622015-09-06 17:34:52 -0700139}
140
141// Tests TestPartResult::passed().
142TEST_F(TestPartResultTest, Passed) {
143 EXPECT_TRUE(r1_.passed());
144 EXPECT_FALSE(r2_.passed());
145 EXPECT_FALSE(r3_.passed());
James Kuszmaule2f15292021-05-10 22:37:32 -0700146 EXPECT_FALSE(r4_.passed());
Austin Schuh0cbef622015-09-06 17:34:52 -0700147}
148
149// Tests TestPartResult::failed().
150TEST_F(TestPartResultTest, Failed) {
151 EXPECT_FALSE(r1_.failed());
152 EXPECT_TRUE(r2_.failed());
153 EXPECT_TRUE(r3_.failed());
James Kuszmaule2f15292021-05-10 22:37:32 -0700154 EXPECT_FALSE(r4_.failed());
155}
156
157// Tests TestPartResult::failed().
158TEST_F(TestPartResultTest, Skipped) {
159 EXPECT_FALSE(r1_.skipped());
160 EXPECT_FALSE(r2_.skipped());
161 EXPECT_FALSE(r3_.skipped());
162 EXPECT_TRUE(r4_.skipped());
Austin Schuh0cbef622015-09-06 17:34:52 -0700163}
164
165// Tests TestPartResult::fatally_failed().
166TEST_F(TestPartResultTest, FatallyFailed) {
167 EXPECT_FALSE(r1_.fatally_failed());
168 EXPECT_FALSE(r2_.fatally_failed());
169 EXPECT_TRUE(r3_.fatally_failed());
James Kuszmaule2f15292021-05-10 22:37:32 -0700170 EXPECT_FALSE(r4_.fatally_failed());
Austin Schuh0cbef622015-09-06 17:34:52 -0700171}
172
173// Tests TestPartResult::nonfatally_failed().
174TEST_F(TestPartResultTest, NonfatallyFailed) {
175 EXPECT_FALSE(r1_.nonfatally_failed());
176 EXPECT_TRUE(r2_.nonfatally_failed());
177 EXPECT_FALSE(r3_.nonfatally_failed());
James Kuszmaule2f15292021-05-10 22:37:32 -0700178 EXPECT_FALSE(r4_.nonfatally_failed());
Austin Schuh0cbef622015-09-06 17:34:52 -0700179}
180
181// Tests the TestPartResultArray class.
182
183class TestPartResultArrayTest : public Test {
184 protected:
185 TestPartResultArrayTest()
186 : r1_(TestPartResult::kNonFatalFailure, "foo/bar.cc", -1, "Failure 1"),
187 r2_(TestPartResult::kFatalFailure, "foo/bar.cc", -1, "Failure 2") {}
188
189 const TestPartResult r1_, r2_;
190};
191
192// Tests that TestPartResultArray initially has size 0.
193TEST_F(TestPartResultArrayTest, InitialSizeIsZero) {
194 TestPartResultArray results;
195 EXPECT_EQ(0, results.size());
196}
197
198// Tests that TestPartResultArray contains the given TestPartResult
199// after one Append() operation.
200TEST_F(TestPartResultArrayTest, ContainsGivenResultAfterAppend) {
201 TestPartResultArray results;
202 results.Append(r1_);
203 EXPECT_EQ(1, results.size());
204 EXPECT_STREQ("Failure 1", results.GetTestPartResult(0).message());
205}
206
207// Tests that TestPartResultArray contains the given TestPartResults
208// after two Append() operations.
209TEST_F(TestPartResultArrayTest, ContainsGivenResultsAfterTwoAppends) {
210 TestPartResultArray results;
211 results.Append(r1_);
212 results.Append(r2_);
213 EXPECT_EQ(2, results.size());
214 EXPECT_STREQ("Failure 1", results.GetTestPartResult(0).message());
215 EXPECT_STREQ("Failure 2", results.GetTestPartResult(1).message());
216}
217
218typedef TestPartResultArrayTest TestPartResultArrayDeathTest;
219
220// Tests that the program dies when GetTestPartResult() is called with
221// an invalid index.
222TEST_F(TestPartResultArrayDeathTest, DiesWhenIndexIsOutOfBound) {
223 TestPartResultArray results;
224 results.Append(r1_);
225
226 EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(-1), "");
227 EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(1), "");
228}
229
Austin Schuh0cbef622015-09-06 17:34:52 -0700230} // namespace