Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
googletest-test-part-test.cc
Go to the documentation of this file.
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 #include "gtest/gtest-test-part.h"
31 #include "gtest/gtest.h"
32 
33 using testing::Message;
34 using testing::Test;
35 using testing::TestPartResult;
36 using testing::TestPartResultArray;
37 
38 namespace {
39 
40 // Tests the TestPartResult class.
41 
42 // The test fixture for testing TestPartResult.
43 class TestPartResultTest : public Test {
44  protected:
45  TestPartResultTest()
46  : r1_(TestPartResult::kSuccess, "foo/bar.cc", 10, "Success!"),
47  r2_(TestPartResult::kNonFatalFailure, "foo/bar.cc", -1, "Failure!"),
48  r3_(TestPartResult::kFatalFailure, nullptr, -1, "Failure!"),
49  r4_(TestPartResult::kSkip, "foo/bar.cc", 2, "Skipped!") {}
50 
51  TestPartResult r1_, r2_, r3_, r4_;
52 };
53 
54 TEST_F(TestPartResultTest, ConstructorWorks) {
55  Message message;
56  message << "something is terribly wrong";
57  message << static_cast<const char*>(testing::internal::kStackTraceMarker);
58  message << "some unimportant stack trace";
59 
60  const TestPartResult result(TestPartResult::kNonFatalFailure, "some_file.cc",
61  42, message.GetString().c_str());
62 
63  EXPECT_EQ(TestPartResult::kNonFatalFailure, result.type());
64  EXPECT_STREQ("some_file.cc", result.file_name());
65  EXPECT_EQ(42, result.line_number());
66  EXPECT_STREQ(message.GetString().c_str(), result.message());
67  EXPECT_STREQ("something is terribly wrong", result.summary());
68 }
69 
70 TEST_F(TestPartResultTest, ResultAccessorsWork) {
71  const TestPartResult success(TestPartResult::kSuccess, "file.cc", 42,
72  "message");
73  EXPECT_TRUE(success.passed());
74  EXPECT_FALSE(success.failed());
75  EXPECT_FALSE(success.nonfatally_failed());
76  EXPECT_FALSE(success.fatally_failed());
77  EXPECT_FALSE(success.skipped());
78 
79  const TestPartResult nonfatal_failure(TestPartResult::kNonFatalFailure,
80  "file.cc", 42, "message");
81  EXPECT_FALSE(nonfatal_failure.passed());
82  EXPECT_TRUE(nonfatal_failure.failed());
83  EXPECT_TRUE(nonfatal_failure.nonfatally_failed());
84  EXPECT_FALSE(nonfatal_failure.fatally_failed());
85  EXPECT_FALSE(nonfatal_failure.skipped());
86 
87  const TestPartResult fatal_failure(TestPartResult::kFatalFailure, "file.cc",
88  42, "message");
89  EXPECT_FALSE(fatal_failure.passed());
90  EXPECT_TRUE(fatal_failure.failed());
91  EXPECT_FALSE(fatal_failure.nonfatally_failed());
92  EXPECT_TRUE(fatal_failure.fatally_failed());
93  EXPECT_FALSE(fatal_failure.skipped());
94 
95  const TestPartResult skip(TestPartResult::kSkip, "file.cc", 42, "message");
96  EXPECT_FALSE(skip.passed());
97  EXPECT_FALSE(skip.failed());
98  EXPECT_FALSE(skip.nonfatally_failed());
99  EXPECT_FALSE(skip.fatally_failed());
100  EXPECT_TRUE(skip.skipped());
101 }
102 
103 // Tests TestPartResult::type().
104 TEST_F(TestPartResultTest, type) {
105  EXPECT_EQ(TestPartResult::kSuccess, r1_.type());
106  EXPECT_EQ(TestPartResult::kNonFatalFailure, r2_.type());
107  EXPECT_EQ(TestPartResult::kFatalFailure, r3_.type());
108  EXPECT_EQ(TestPartResult::kSkip, r4_.type());
109 }
110 
111 // Tests TestPartResult::file_name().
112 TEST_F(TestPartResultTest, file_name) {
113  EXPECT_STREQ("foo/bar.cc", r1_.file_name());
114  EXPECT_STREQ(nullptr, r3_.file_name());
115  EXPECT_STREQ("foo/bar.cc", r4_.file_name());
116 }
117 
118 // Tests TestPartResult::line_number().
119 TEST_F(TestPartResultTest, line_number) {
120  EXPECT_EQ(10, r1_.line_number());
121  EXPECT_EQ(-1, r2_.line_number());
122  EXPECT_EQ(2, r4_.line_number());
123 }
124 
125 // Tests TestPartResult::message().
126 TEST_F(TestPartResultTest, message) {
127  EXPECT_STREQ("Success!", r1_.message());
128  EXPECT_STREQ("Skipped!", r4_.message());
129 }
130 
131 // Tests TestPartResult::passed().
132 TEST_F(TestPartResultTest, Passed) {
133  EXPECT_TRUE(r1_.passed());
134  EXPECT_FALSE(r2_.passed());
135  EXPECT_FALSE(r3_.passed());
136  EXPECT_FALSE(r4_.passed());
137 }
138 
139 // Tests TestPartResult::failed().
140 TEST_F(TestPartResultTest, Failed) {
141  EXPECT_FALSE(r1_.failed());
142  EXPECT_TRUE(r2_.failed());
143  EXPECT_TRUE(r3_.failed());
144  EXPECT_FALSE(r4_.failed());
145 }
146 
147 // Tests TestPartResult::failed().
148 TEST_F(TestPartResultTest, Skipped) {
149  EXPECT_FALSE(r1_.skipped());
150  EXPECT_FALSE(r2_.skipped());
151  EXPECT_FALSE(r3_.skipped());
152  EXPECT_TRUE(r4_.skipped());
153 }
154 
155 // Tests TestPartResult::fatally_failed().
156 TEST_F(TestPartResultTest, FatallyFailed) {
157  EXPECT_FALSE(r1_.fatally_failed());
158  EXPECT_FALSE(r2_.fatally_failed());
159  EXPECT_TRUE(r3_.fatally_failed());
160  EXPECT_FALSE(r4_.fatally_failed());
161 }
162 
163 // Tests TestPartResult::nonfatally_failed().
164 TEST_F(TestPartResultTest, NonfatallyFailed) {
165  EXPECT_FALSE(r1_.nonfatally_failed());
166  EXPECT_TRUE(r2_.nonfatally_failed());
167  EXPECT_FALSE(r3_.nonfatally_failed());
168  EXPECT_FALSE(r4_.nonfatally_failed());
169 }
170 
171 // Tests the TestPartResultArray class.
172 
173 class TestPartResultArrayTest : public Test {
174  protected:
175  TestPartResultArrayTest()
176  : r1_(TestPartResult::kNonFatalFailure, "foo/bar.cc", -1, "Failure 1"),
177  r2_(TestPartResult::kFatalFailure, "foo/bar.cc", -1, "Failure 2") {}
178 
179  const TestPartResult r1_, r2_;
180 };
181 
182 // Tests that TestPartResultArray initially has size 0.
183 TEST_F(TestPartResultArrayTest, InitialSizeIsZero) {
184  TestPartResultArray results;
185  EXPECT_EQ(0, results.size());
186 }
187 
188 // Tests that TestPartResultArray contains the given TestPartResult
189 // after one Append() operation.
190 TEST_F(TestPartResultArrayTest, ContainsGivenResultAfterAppend) {
191  TestPartResultArray results;
192  results.Append(r1_);
193  EXPECT_EQ(1, results.size());
194  EXPECT_STREQ("Failure 1", results.GetTestPartResult(0).message());
195 }
196 
197 // Tests that TestPartResultArray contains the given TestPartResults
198 // after two Append() operations.
199 TEST_F(TestPartResultArrayTest, ContainsGivenResultsAfterTwoAppends) {
200  TestPartResultArray results;
201  results.Append(r1_);
202  results.Append(r2_);
203  EXPECT_EQ(2, results.size());
204  EXPECT_STREQ("Failure 1", results.GetTestPartResult(0).message());
205  EXPECT_STREQ("Failure 2", results.GetTestPartResult(1).message());
206 }
207 
208 typedef TestPartResultArrayTest TestPartResultArrayDeathTest;
209 
210 // Tests that the program dies when GetTestPartResult() is called with
211 // an invalid index.
212 TEST_F(TestPartResultArrayDeathTest, DiesWhenIndexIsOutOfBound) {
213  TestPartResultArray results;
214  results.Append(r1_);
215 
216  EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(-1), "");
217  EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(1), "");
218 }
219 
220 } // namespace
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
#define TEST_F(test_fixture, test_name)
Definition: gtest.h:2224
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:1953
GTEST_API_ const char kStackTraceMarker[]
Definition: gtest.cc:206
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1884
#define EXPECT_TRUE(condition)
Definition: gtest.h:1823
#define EXPECT_FALSE(condition)
Definition: gtest.h:1827