• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2008 Google Inc. All Rights Reserved.
2 // Author: mheule@google.com (Markus Heule)
3 
4 #include <gtest/gtest-test-part.h>
5 
6 #include <gtest/gtest.h>
7 
8 using testing::Test;
9 using testing::TestPartResult;
10 using testing::TestPartResultArray;
11 
12 using testing::TPRT_FATAL_FAILURE;
13 using testing::TPRT_NONFATAL_FAILURE;
14 using testing::TPRT_SUCCESS;
15 
16 namespace {
17 
18 // Tests the TestPartResult class.
19 
20 // The test fixture for testing TestPartResult.
21 class TestPartResultTest : public Test {
22  protected:
TestPartResultTest()23   TestPartResultTest()
24       : r1_(TPRT_SUCCESS, "foo/bar.cc", 10, "Success!"),
25         r2_(TPRT_NONFATAL_FAILURE, "foo/bar.cc", -1, "Failure!"),
26         r3_(TPRT_FATAL_FAILURE, NULL, -1, "Failure!") {}
27 
28   TestPartResult r1_, r2_, r3_;
29 };
30 
31 // Tests TestPartResult::type().
TEST_F(TestPartResultTest,type)32 TEST_F(TestPartResultTest, type) {
33   EXPECT_EQ(TPRT_SUCCESS, r1_.type());
34   EXPECT_EQ(TPRT_NONFATAL_FAILURE, r2_.type());
35   EXPECT_EQ(TPRT_FATAL_FAILURE, r3_.type());
36 }
37 
38 // Tests TestPartResult::file_name().
TEST_F(TestPartResultTest,file_name)39 TEST_F(TestPartResultTest, file_name) {
40   EXPECT_STREQ("foo/bar.cc", r1_.file_name());
41   EXPECT_STREQ(NULL, r3_.file_name());
42 }
43 
44 // Tests TestPartResult::line_number().
TEST_F(TestPartResultTest,line_number)45 TEST_F(TestPartResultTest, line_number) {
46   EXPECT_EQ(10, r1_.line_number());
47   EXPECT_EQ(-1, r2_.line_number());
48 }
49 
50 // Tests TestPartResult::message().
TEST_F(TestPartResultTest,message)51 TEST_F(TestPartResultTest, message) {
52   EXPECT_STREQ("Success!", r1_.message());
53 }
54 
55 // Tests TestPartResult::passed().
TEST_F(TestPartResultTest,Passed)56 TEST_F(TestPartResultTest, Passed) {
57   EXPECT_TRUE(r1_.passed());
58   EXPECT_FALSE(r2_.passed());
59   EXPECT_FALSE(r3_.passed());
60 }
61 
62 // Tests TestPartResult::failed().
TEST_F(TestPartResultTest,Failed)63 TEST_F(TestPartResultTest, Failed) {
64   EXPECT_FALSE(r1_.failed());
65   EXPECT_TRUE(r2_.failed());
66   EXPECT_TRUE(r3_.failed());
67 }
68 
69 // Tests TestPartResult::fatally_failed().
TEST_F(TestPartResultTest,FatallyFailed)70 TEST_F(TestPartResultTest, FatallyFailed) {
71   EXPECT_FALSE(r1_.fatally_failed());
72   EXPECT_FALSE(r2_.fatally_failed());
73   EXPECT_TRUE(r3_.fatally_failed());
74 }
75 
76 // Tests TestPartResult::nonfatally_failed().
TEST_F(TestPartResultTest,NonfatallyFailed)77 TEST_F(TestPartResultTest, NonfatallyFailed) {
78   EXPECT_FALSE(r1_.nonfatally_failed());
79   EXPECT_TRUE(r2_.nonfatally_failed());
80   EXPECT_FALSE(r3_.nonfatally_failed());
81 }
82 
83 // Tests the TestPartResultArray class.
84 
85 class TestPartResultArrayTest : public Test {
86  protected:
TestPartResultArrayTest()87   TestPartResultArrayTest()
88       : r1_(TPRT_NONFATAL_FAILURE, "foo/bar.cc", -1, "Failure 1"),
89         r2_(TPRT_FATAL_FAILURE, "foo/bar.cc", -1, "Failure 2") {}
90 
91   const TestPartResult r1_, r2_;
92 };
93 
94 // Tests that TestPartResultArray initially has size 0.
TEST_F(TestPartResultArrayTest,InitialSizeIsZero)95 TEST_F(TestPartResultArrayTest, InitialSizeIsZero) {
96   TestPartResultArray results;
97   EXPECT_EQ(0, results.size());
98 }
99 
100 // Tests that TestPartResultArray contains the given TestPartResult
101 // after one Append() operation.
TEST_F(TestPartResultArrayTest,ContainsGivenResultAfterAppend)102 TEST_F(TestPartResultArrayTest, ContainsGivenResultAfterAppend) {
103   TestPartResultArray results;
104   results.Append(r1_);
105   EXPECT_EQ(1, results.size());
106   EXPECT_STREQ("Failure 1", results.GetTestPartResult(0).message());
107 }
108 
109 // Tests that TestPartResultArray contains the given TestPartResults
110 // after two Append() operations.
TEST_F(TestPartResultArrayTest,ContainsGivenResultsAfterTwoAppends)111 TEST_F(TestPartResultArrayTest, ContainsGivenResultsAfterTwoAppends) {
112   TestPartResultArray results;
113   results.Append(r1_);
114   results.Append(r2_);
115   EXPECT_EQ(2, results.size());
116   EXPECT_STREQ("Failure 1", results.GetTestPartResult(0).message());
117   EXPECT_STREQ("Failure 2", results.GetTestPartResult(1).message());
118 }
119 
120 #if GTEST_HAS_DEATH_TEST
121 
122 typedef TestPartResultArrayTest TestPartResultArrayDeathTest;
123 
124 // Tests that the program dies when GetTestPartResult() is called with
125 // an invalid index.
TEST_F(TestPartResultArrayDeathTest,DiesWhenIndexIsOutOfBound)126 TEST_F(TestPartResultArrayDeathTest, DiesWhenIndexIsOutOfBound) {
127   TestPartResultArray results;
128   results.Append(r1_);
129 
130   EXPECT_DEATH(results.GetTestPartResult(-1), "");
131   EXPECT_DEATH(results.GetTestPartResult(1), "");
132 }
133 
134 #endif  // GTEST_HAS_DEATH_TEST
135 
136 // TODO(mheule@google.com): Add a test for the class HasNewFatalFailureHelper.
137 
138 }  // namespace
139