![]() |
|
|||
File indexing completed on 2025-08-06 08:19:58
0001 // Copyright 2013, Google Inc. 0002 // All rights reserved. 0003 // 0004 // Redistribution and use in source and binary forms, with or without 0005 // modification, are permitted provided that the following conditions are 0006 // met: 0007 // 0008 // * Redistributions of source code must retain the above copyright 0009 // notice, this list of conditions and the following disclaimer. 0010 // * Redistributions in binary form must reproduce the above 0011 // copyright notice, this list of conditions and the following disclaimer 0012 // in the documentation and/or other materials provided with the 0013 // distribution. 0014 // * Neither the name of Google Inc. nor the names of its 0015 // contributors may be used to endorse or promote products derived from 0016 // this software without specific prior written permission. 0017 // 0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0029 // 0030 // Author: wan@google.com (Zhanyong Wan) 0031 0032 // Tests Google Mock's functionality that depends on exceptions. 0033 0034 #include "gmock/gmock.h" 0035 #include "gtest/gtest.h" 0036 0037 namespace { 0038 0039 using testing::HasSubstr; 0040 using testing::internal::GoogleTestFailureException; 0041 0042 // A type that cannot be default constructed. 0043 class NonDefaultConstructible { 0044 public: 0045 explicit NonDefaultConstructible(int /* dummy */) {} 0046 }; 0047 0048 class MockFoo { 0049 public: 0050 // A mock method that returns a user-defined type. Google Mock 0051 // doesn't know what the default value for this type is. 0052 MOCK_METHOD0(GetNonDefaultConstructible, NonDefaultConstructible()); 0053 }; 0054 0055 #if GTEST_HAS_EXCEPTIONS 0056 0057 TEST(DefaultValueTest, ThrowsRuntimeErrorWhenNoDefaultValue) { 0058 MockFoo mock; 0059 try { 0060 // No expectation is set on this method, so Google Mock must 0061 // return the default value. However, since Google Mock knows 0062 // nothing about the return type, it doesn't know what to return, 0063 // and has to throw (when exceptions are enabled) or abort 0064 // (otherwise). 0065 mock.GetNonDefaultConstructible(); 0066 FAIL() << "GetNonDefaultConstructible()'s return type has no default " 0067 << "value, so Google Mock should have thrown."; 0068 } catch (const GoogleTestFailureException& /* unused */) { 0069 FAIL() << "Google Test does not try to catch an exception of type " 0070 << "GoogleTestFailureException, which is used for reporting " 0071 << "a failure to other testing frameworks. Google Mock should " 0072 << "not throw a GoogleTestFailureException as it will kill the " 0073 << "entire test program instead of just the current TEST."; 0074 } catch (const std::exception& ex) { 0075 EXPECT_THAT(ex.what(), HasSubstr("has no default value")); 0076 } 0077 } 0078 0079 #endif 0080 0081 } // unnamed namespace
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |