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: wan (at) google.com (Zhanyong Wan) 31 32 // Google Mock - a framework for writing C++ mock classes. 33 // 34 // This file tests code in gmock.cc. 35 36 #include "gmock/gmock.h" 37 38 #include <string> 39 #include "gtest/gtest.h" 40 41 #if !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) 42 43 using testing::GMOCK_FLAG(verbose); 44 using testing::InitGoogleMock; 45 46 // Verifies that calling InitGoogleMock() on argv results in new_argv, 47 // and the gmock_verbose flag's value is set to expected_gmock_verbose. 48 template <typename Char, int M, int N> 49 void TestInitGoogleMock(const Char* (&argv)[M], const Char* (&new_argv)[N], 50 const ::std::string& expected_gmock_verbose) { 51 const ::std::string old_verbose = GMOCK_FLAG(verbose); 52 53 int argc = M; 54 InitGoogleMock(&argc, const_cast<Char**>(argv)); 55 ASSERT_EQ(N, argc) << "The new argv has wrong number of elements."; 56 57 for (int i = 0; i < N; i++) { 58 EXPECT_STREQ(new_argv[i], argv[i]); 59 } 60 61 EXPECT_EQ(expected_gmock_verbose, GMOCK_FLAG(verbose).c_str()); 62 GMOCK_FLAG(verbose) = old_verbose; // Restores the gmock_verbose flag. 63 } 64 65 TEST(InitGoogleMockTest, ParsesInvalidCommandLine) { 66 const char* argv[] = { 67 NULL 68 }; 69 70 const char* new_argv[] = { 71 NULL 72 }; 73 74 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose)); 75 } 76 77 TEST(InitGoogleMockTest, ParsesEmptyCommandLine) { 78 const char* argv[] = { 79 "foo.exe", 80 NULL 81 }; 82 83 const char* new_argv[] = { 84 "foo.exe", 85 NULL 86 }; 87 88 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose)); 89 } 90 91 TEST(InitGoogleMockTest, ParsesSingleFlag) { 92 const char* argv[] = { 93 "foo.exe", 94 "--gmock_verbose=info", 95 NULL 96 }; 97 98 const char* new_argv[] = { 99 "foo.exe", 100 NULL 101 }; 102 103 TestInitGoogleMock(argv, new_argv, "info"); 104 } 105 106 TEST(InitGoogleMockTest, ParsesUnrecognizedFlag) { 107 const char* argv[] = { 108 "foo.exe", 109 "--non_gmock_flag=blah", 110 NULL 111 }; 112 113 const char* new_argv[] = { 114 "foo.exe", 115 "--non_gmock_flag=blah", 116 NULL 117 }; 118 119 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose)); 120 } 121 122 TEST(InitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) { 123 const char* argv[] = { 124 "foo.exe", 125 "--non_gmock_flag=blah", 126 "--gmock_verbose=error", 127 NULL 128 }; 129 130 const char* new_argv[] = { 131 "foo.exe", 132 "--non_gmock_flag=blah", 133 NULL 134 }; 135 136 TestInitGoogleMock(argv, new_argv, "error"); 137 } 138 139 TEST(WideInitGoogleMockTest, ParsesInvalidCommandLine) { 140 const wchar_t* argv[] = { 141 NULL 142 }; 143 144 const wchar_t* new_argv[] = { 145 NULL 146 }; 147 148 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose)); 149 } 150 151 TEST(WideInitGoogleMockTest, ParsesEmptyCommandLine) { 152 const wchar_t* argv[] = { 153 L"foo.exe", 154 NULL 155 }; 156 157 const wchar_t* new_argv[] = { 158 L"foo.exe", 159 NULL 160 }; 161 162 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose)); 163 } 164 165 TEST(WideInitGoogleMockTest, ParsesSingleFlag) { 166 const wchar_t* argv[] = { 167 L"foo.exe", 168 L"--gmock_verbose=info", 169 NULL 170 }; 171 172 const wchar_t* new_argv[] = { 173 L"foo.exe", 174 NULL 175 }; 176 177 TestInitGoogleMock(argv, new_argv, "info"); 178 } 179 180 TEST(WideInitGoogleMockTest, ParsesUnrecognizedFlag) { 181 const wchar_t* argv[] = { 182 L"foo.exe", 183 L"--non_gmock_flag=blah", 184 NULL 185 }; 186 187 const wchar_t* new_argv[] = { 188 L"foo.exe", 189 L"--non_gmock_flag=blah", 190 NULL 191 }; 192 193 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose)); 194 } 195 196 TEST(WideInitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) { 197 const wchar_t* argv[] = { 198 L"foo.exe", 199 L"--non_gmock_flag=blah", 200 L"--gmock_verbose=error", 201 NULL 202 }; 203 204 const wchar_t* new_argv[] = { 205 L"foo.exe", 206 L"--non_gmock_flag=blah", 207 NULL 208 }; 209 210 TestInitGoogleMock(argv, new_argv, "error"); 211 } 212 213 #endif // !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) 214 215 // Makes sure Google Mock flags can be accessed in code. 216 TEST(FlagTest, IsAccessibleInCode) { 217 bool dummy = testing::GMOCK_FLAG(catch_leaked_mocks) && 218 testing::GMOCK_FLAG(verbose) == ""; 219 (void)dummy; // Avoids the "unused local variable" warning. 220 } 221