shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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@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 | |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame] | 36 | #include "gmock/gmock.h" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 37 | |
| 38 | #include <string> |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame] | 39 | #include "gtest/gtest.h" |
kosak | 01db9d4 | 2015-07-28 00:24:28 +0000 | [diff] [blame] | 40 | |
| 41 | #if !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 42 | |
| 43 | using testing::GMOCK_FLAG(verbose); |
| 44 | using testing::InitGoogleMock; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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 | |
kosak | 01db9d4 | 2015-07-28 00:24:28 +0000 | [diff] [blame] | 213 | #endif // !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) |
| 214 | |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 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) == ""; |
zhanyong.wan | a684b5a | 2010-12-02 23:30:50 +0000 | [diff] [blame] | 219 | (void)dummy; // Avoids the "unused local variable" warning. |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 220 | } |