xref: /freebsd/contrib/googletest/googlemock/test/gmock_ex_test.cc (revision b89a7cc2ed6e4398d5be502f5bb5885d1ec6ff0f)
1*b89a7cc2SEnji Cooper // Copyright 2013, Google Inc.
2*b89a7cc2SEnji Cooper // All rights reserved.
3*b89a7cc2SEnji Cooper //
4*b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5*b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are
6*b89a7cc2SEnji Cooper // met:
7*b89a7cc2SEnji Cooper //
8*b89a7cc2SEnji Cooper //     * Redistributions of source code must retain the above copyright
9*b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10*b89a7cc2SEnji Cooper //     * Redistributions in binary form must reproduce the above
11*b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12*b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the
13*b89a7cc2SEnji Cooper // distribution.
14*b89a7cc2SEnji Cooper //     * Neither the name of Google Inc. nor the names of its
15*b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from
16*b89a7cc2SEnji Cooper // this software without specific prior written permission.
17*b89a7cc2SEnji Cooper //
18*b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21*b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23*b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24*b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28*b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*b89a7cc2SEnji Cooper 
30*b89a7cc2SEnji Cooper 
31*b89a7cc2SEnji Cooper // Tests Google Mock's functionality that depends on exceptions.
32*b89a7cc2SEnji Cooper 
33*b89a7cc2SEnji Cooper #include "gmock/gmock.h"
34*b89a7cc2SEnji Cooper #include "gtest/gtest.h"
35*b89a7cc2SEnji Cooper 
36*b89a7cc2SEnji Cooper #if GTEST_HAS_EXCEPTIONS
37*b89a7cc2SEnji Cooper namespace {
38*b89a7cc2SEnji Cooper 
39*b89a7cc2SEnji Cooper using testing::HasSubstr;
40*b89a7cc2SEnji Cooper 
41*b89a7cc2SEnji Cooper using testing::internal::GoogleTestFailureException;
42*b89a7cc2SEnji Cooper 
43*b89a7cc2SEnji Cooper // A type that cannot be default constructed.
44*b89a7cc2SEnji Cooper class NonDefaultConstructible {
45*b89a7cc2SEnji Cooper  public:
46*b89a7cc2SEnji Cooper   explicit NonDefaultConstructible(int /* dummy */) {}
47*b89a7cc2SEnji Cooper };
48*b89a7cc2SEnji Cooper 
49*b89a7cc2SEnji Cooper class MockFoo {
50*b89a7cc2SEnji Cooper  public:
51*b89a7cc2SEnji Cooper   // A mock method that returns a user-defined type.  Google Mock
52*b89a7cc2SEnji Cooper   // doesn't know what the default value for this type is.
53*b89a7cc2SEnji Cooper   MOCK_METHOD0(GetNonDefaultConstructible, NonDefaultConstructible());
54*b89a7cc2SEnji Cooper };
55*b89a7cc2SEnji Cooper 
56*b89a7cc2SEnji Cooper TEST(DefaultValueTest, ThrowsRuntimeErrorWhenNoDefaultValue) {
57*b89a7cc2SEnji Cooper   MockFoo mock;
58*b89a7cc2SEnji Cooper   try {
59*b89a7cc2SEnji Cooper     // No expectation is set on this method, so Google Mock must
60*b89a7cc2SEnji Cooper     // return the default value.  However, since Google Mock knows
61*b89a7cc2SEnji Cooper     // nothing about the return type, it doesn't know what to return,
62*b89a7cc2SEnji Cooper     // and has to throw (when exceptions are enabled) or abort
63*b89a7cc2SEnji Cooper     // (otherwise).
64*b89a7cc2SEnji Cooper     mock.GetNonDefaultConstructible();
65*b89a7cc2SEnji Cooper     FAIL() << "GetNonDefaultConstructible()'s return type has no default "
66*b89a7cc2SEnji Cooper            << "value, so Google Mock should have thrown.";
67*b89a7cc2SEnji Cooper   } catch (const GoogleTestFailureException& /* unused */) {
68*b89a7cc2SEnji Cooper     FAIL() << "Google Test does not try to catch an exception of type "
69*b89a7cc2SEnji Cooper            << "GoogleTestFailureException, which is used for reporting "
70*b89a7cc2SEnji Cooper            << "a failure to other testing frameworks.  Google Mock should "
71*b89a7cc2SEnji Cooper            << "not throw a GoogleTestFailureException as it will kill the "
72*b89a7cc2SEnji Cooper            << "entire test program instead of just the current TEST.";
73*b89a7cc2SEnji Cooper   } catch (const std::exception& ex) {
74*b89a7cc2SEnji Cooper     EXPECT_THAT(ex.what(), HasSubstr("has no default value"));
75*b89a7cc2SEnji Cooper   }
76*b89a7cc2SEnji Cooper }
77*b89a7cc2SEnji Cooper 
78*b89a7cc2SEnji Cooper 
79*b89a7cc2SEnji Cooper }  // unnamed namespace
80*b89a7cc2SEnji Cooper #endif
81