Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:19:59

0001 // Copyright 2008, 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 // Google Mock - a framework for writing C++ mock classes.
0033 //
0034 // This file tests code in gmock.cc.
0035 
0036 #include "gmock/gmock.h"
0037 
0038 #include <string>
0039 #include "gtest/gtest.h"
0040 
0041 #if !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
0042 
0043 using testing::GMOCK_FLAG(verbose);
0044 using testing::InitGoogleMock;
0045 
0046 // Verifies that calling InitGoogleMock() on argv results in new_argv,
0047 // and the gmock_verbose flag's value is set to expected_gmock_verbose.
0048 template <typename Char, int M, int N>
0049 void TestInitGoogleMock(const Char* (&argv)[M], const Char* (&new_argv)[N],
0050                         const ::std::string& expected_gmock_verbose) {
0051   const ::std::string old_verbose = GMOCK_FLAG(verbose);
0052 
0053   int argc = M;
0054   InitGoogleMock(&argc, const_cast<Char**>(argv));
0055   ASSERT_EQ(N, argc) << "The new argv has wrong number of elements.";
0056 
0057   for (int i = 0; i < N; i++) {
0058     EXPECT_STREQ(new_argv[i], argv[i]);
0059   }
0060 
0061   EXPECT_EQ(expected_gmock_verbose, GMOCK_FLAG(verbose).c_str());
0062   GMOCK_FLAG(verbose) = old_verbose;  // Restores the gmock_verbose flag.
0063 }
0064 
0065 TEST(InitGoogleMockTest, ParsesInvalidCommandLine) {
0066   const char* argv[] = {
0067     NULL
0068   };
0069 
0070   const char* new_argv[] = {
0071     NULL
0072   };
0073 
0074   TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
0075 }
0076 
0077 TEST(InitGoogleMockTest, ParsesEmptyCommandLine) {
0078   const char* argv[] = {
0079     "foo.exe",
0080     NULL
0081   };
0082 
0083   const char* new_argv[] = {
0084     "foo.exe",
0085     NULL
0086   };
0087 
0088   TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
0089 }
0090 
0091 TEST(InitGoogleMockTest, ParsesSingleFlag) {
0092   const char* argv[] = {
0093     "foo.exe",
0094     "--gmock_verbose=info",
0095     NULL
0096   };
0097 
0098   const char* new_argv[] = {
0099     "foo.exe",
0100     NULL
0101   };
0102 
0103   TestInitGoogleMock(argv, new_argv, "info");
0104 }
0105 
0106 TEST(InitGoogleMockTest, ParsesUnrecognizedFlag) {
0107   const char* argv[] = {
0108     "foo.exe",
0109     "--non_gmock_flag=blah",
0110     NULL
0111   };
0112 
0113   const char* new_argv[] = {
0114     "foo.exe",
0115     "--non_gmock_flag=blah",
0116     NULL
0117   };
0118 
0119   TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
0120 }
0121 
0122 TEST(InitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
0123   const char* argv[] = {
0124     "foo.exe",
0125     "--non_gmock_flag=blah",
0126     "--gmock_verbose=error",
0127     NULL
0128   };
0129 
0130   const char* new_argv[] = {
0131     "foo.exe",
0132     "--non_gmock_flag=blah",
0133     NULL
0134   };
0135 
0136   TestInitGoogleMock(argv, new_argv, "error");
0137 }
0138 
0139 TEST(WideInitGoogleMockTest, ParsesInvalidCommandLine) {
0140   const wchar_t* argv[] = {
0141     NULL
0142   };
0143 
0144   const wchar_t* new_argv[] = {
0145     NULL
0146   };
0147 
0148   TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
0149 }
0150 
0151 TEST(WideInitGoogleMockTest, ParsesEmptyCommandLine) {
0152   const wchar_t* argv[] = {
0153     L"foo.exe",
0154     NULL
0155   };
0156 
0157   const wchar_t* new_argv[] = {
0158     L"foo.exe",
0159     NULL
0160   };
0161 
0162   TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
0163 }
0164 
0165 TEST(WideInitGoogleMockTest, ParsesSingleFlag) {
0166   const wchar_t* argv[] = {
0167     L"foo.exe",
0168     L"--gmock_verbose=info",
0169     NULL
0170   };
0171 
0172   const wchar_t* new_argv[] = {
0173     L"foo.exe",
0174     NULL
0175   };
0176 
0177   TestInitGoogleMock(argv, new_argv, "info");
0178 }
0179 
0180 TEST(WideInitGoogleMockTest, ParsesUnrecognizedFlag) {
0181   const wchar_t* argv[] = {
0182     L"foo.exe",
0183     L"--non_gmock_flag=blah",
0184     NULL
0185   };
0186 
0187   const wchar_t* new_argv[] = {
0188     L"foo.exe",
0189     L"--non_gmock_flag=blah",
0190     NULL
0191   };
0192 
0193   TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
0194 }
0195 
0196 TEST(WideInitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
0197   const wchar_t* argv[] = {
0198     L"foo.exe",
0199     L"--non_gmock_flag=blah",
0200     L"--gmock_verbose=error",
0201     NULL
0202   };
0203 
0204   const wchar_t* new_argv[] = {
0205     L"foo.exe",
0206     L"--non_gmock_flag=blah",
0207     NULL
0208   };
0209 
0210   TestInitGoogleMock(argv, new_argv, "error");
0211 }
0212 
0213 #endif  // !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
0214 
0215 // Makes sure Google Mock flags can be accessed in code.
0216 TEST(FlagTest, IsAccessibleInCode) {
0217   bool dummy = testing::GMOCK_FLAG(catch_leaked_mocks) &&
0218       testing::GMOCK_FLAG(verbose) == "";
0219   (void)dummy;  // Avoids the "unused local variable" warning.
0220 }