Home | History | Annotate | Download | only in test
      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 (at) google.com (Markus Heule)
     31 //
     32 
     33 #include <gtest/gtest-test-part.h>
     34 
     35 #include <gtest/gtest.h>
     36 
     37 using testing::Test;
     38 using testing::TestPartResult;
     39 using testing::TestPartResultArray;
     40 
     41 namespace {
     42 
     43 // Tests the TestPartResult class.
     44 
     45 // The test fixture for testing TestPartResult.
     46 class TestPartResultTest : public Test {
     47  protected:
     48   TestPartResultTest()
     49       : r1_(TestPartResult::kSuccess, "foo/bar.cc", 10, "Success!"),
     50         r2_(TestPartResult::kNonFatalFailure, "foo/bar.cc", -1, "Failure!"),
     51         r3_(TestPartResult::kFatalFailure, NULL, -1, "Failure!") {}
     52 
     53   TestPartResult r1_, r2_, r3_;
     54 };
     55 
     56 // Tests TestPartResult::type().
     57 TEST_F(TestPartResultTest, type) {
     58   EXPECT_EQ(TestPartResult::kSuccess, r1_.type());
     59   EXPECT_EQ(TestPartResult::kNonFatalFailure, r2_.type());
     60   EXPECT_EQ(TestPartResult::kFatalFailure, r3_.type());
     61 }
     62 
     63 // Tests TestPartResult::file_name().
     64 TEST_F(TestPartResultTest, file_name) {
     65   EXPECT_STREQ("foo/bar.cc", r1_.file_name());
     66   EXPECT_STREQ(NULL, r3_.file_name());
     67 }
     68 
     69 // Tests TestPartResult::line_number().
     70 TEST_F(TestPartResultTest, line_number) {
     71   EXPECT_EQ(10, r1_.line_number());
     72   EXPECT_EQ(-1, r2_.line_number());
     73 }
     74 
     75 // Tests TestPartResult::message().
     76 TEST_F(TestPartResultTest, message) {
     77   EXPECT_STREQ("Success!", r1_.message());
     78 }
     79 
     80 // Tests TestPartResult::passed().
     81 TEST_F(TestPartResultTest, Passed) {
     82   EXPECT_TRUE(r1_.passed());
     83   EXPECT_FALSE(r2_.passed());
     84   EXPECT_FALSE(r3_.passed());
     85 }
     86 
     87 // Tests TestPartResult::failed().
     88 TEST_F(TestPartResultTest, Failed) {
     89   EXPECT_FALSE(r1_.failed());
     90   EXPECT_TRUE(r2_.failed());
     91   EXPECT_TRUE(r3_.failed());
     92 }
     93 
     94 // Tests TestPartResult::fatally_failed().
     95 TEST_F(TestPartResultTest, FatallyFailed) {
     96   EXPECT_FALSE(r1_.fatally_failed());
     97   EXPECT_FALSE(r2_.fatally_failed());
     98   EXPECT_TRUE(r3_.fatally_failed());
     99 }
    100 
    101 // Tests TestPartResult::nonfatally_failed().
    102 TEST_F(TestPartResultTest, NonfatallyFailed) {
    103   EXPECT_FALSE(r1_.nonfatally_failed());
    104   EXPECT_TRUE(r2_.nonfatally_failed());
    105   EXPECT_FALSE(r3_.nonfatally_failed());
    106 }
    107 
    108 // Tests the TestPartResultArray class.
    109 
    110 class TestPartResultArrayTest : public Test {
    111  protected:
    112   TestPartResultArrayTest()
    113       : r1_(TestPartResult::kNonFatalFailure, "foo/bar.cc", -1, "Failure 1"),
    114         r2_(TestPartResult::kFatalFailure, "foo/bar.cc", -1, "Failure 2") {}
    115 
    116   const TestPartResult r1_, r2_;
    117 };
    118 
    119 // Tests that TestPartResultArray initially has size 0.
    120 TEST_F(TestPartResultArrayTest, InitialSizeIsZero) {
    121   TestPartResultArray results;
    122   EXPECT_EQ(0, results.size());
    123 }
    124 
    125 // Tests that TestPartResultArray contains the given TestPartResult
    126 // after one Append() operation.
    127 TEST_F(TestPartResultArrayTest, ContainsGivenResultAfterAppend) {
    128   TestPartResultArray results;
    129   results.Append(r1_);
    130   EXPECT_EQ(1, results.size());
    131   EXPECT_STREQ("Failure 1", results.GetTestPartResult(0).message());
    132 }
    133 
    134 // Tests that TestPartResultArray contains the given TestPartResults
    135 // after two Append() operations.
    136 TEST_F(TestPartResultArrayTest, ContainsGivenResultsAfterTwoAppends) {
    137   TestPartResultArray results;
    138   results.Append(r1_);
    139   results.Append(r2_);
    140   EXPECT_EQ(2, results.size());
    141   EXPECT_STREQ("Failure 1", results.GetTestPartResult(0).message());
    142   EXPECT_STREQ("Failure 2", results.GetTestPartResult(1).message());
    143 }
    144 
    145 typedef TestPartResultArrayTest TestPartResultArrayDeathTest;
    146 
    147 // Tests that the program dies when GetTestPartResult() is called with
    148 // an invalid index.
    149 TEST_F(TestPartResultArrayDeathTest, DiesWhenIndexIsOutOfBound) {
    150   TestPartResultArray results;
    151   results.Append(r1_);
    152 
    153   EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(-1), "");
    154   EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(1), "");
    155 }
    156 
    157 // TODO(mheule (at) google.com): Add a test for the class HasNewFatalFailureHelper.
    158 
    159 }  // namespace
    160