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