Add first version
[ric-plt/sdl.git] / 3rdparty / googletest / 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[] = {
68     NULL
69   };
70
71   const char* new_argv[] = {
72     NULL
73   };
74
75   TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
76 }
77
78 TEST(InitGoogleMockTest, ParsesEmptyCommandLine) {
79   const char* argv[] = {
80     "foo.exe",
81     NULL
82   };
83
84   const char* new_argv[] = {
85     "foo.exe",
86     NULL
87   };
88
89   TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
90 }
91
92 TEST(InitGoogleMockTest, ParsesSingleFlag) {
93   const char* argv[] = {
94     "foo.exe",
95     "--gmock_verbose=info",
96     NULL
97   };
98
99   const char* new_argv[] = {
100     "foo.exe",
101     NULL
102   };
103
104   TestInitGoogleMock(argv, new_argv, "info");
105 }
106
107 TEST(InitGoogleMockTest, ParsesMultipleFlags) {
108   int old_default_behavior = GMOCK_FLAG(default_mock_behavior);
109   const wchar_t* argv[] = {
110     L"foo.exe",
111     L"--gmock_verbose=info",
112     L"--gmock_default_mock_behavior=2",
113     NULL
114   };
115
116   const wchar_t* new_argv[] = {
117     L"foo.exe",
118     NULL
119   };
120
121   TestInitGoogleMock(argv, new_argv, "info");
122   EXPECT_EQ(2, GMOCK_FLAG(default_mock_behavior));
123   EXPECT_NE(2, old_default_behavior);
124   GMOCK_FLAG(default_mock_behavior) = old_default_behavior;
125 }
126
127 TEST(InitGoogleMockTest, ParsesUnrecognizedFlag) {
128   const char* argv[] = {
129     "foo.exe",
130     "--non_gmock_flag=blah",
131     NULL
132   };
133
134   const char* new_argv[] = {
135     "foo.exe",
136     "--non_gmock_flag=blah",
137     NULL
138   };
139
140   TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
141 }
142
143 TEST(InitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
144   const char* argv[] = {
145     "foo.exe",
146     "--non_gmock_flag=blah",
147     "--gmock_verbose=error",
148     NULL
149   };
150
151   const char* new_argv[] = {
152     "foo.exe",
153     "--non_gmock_flag=blah",
154     NULL
155   };
156
157   TestInitGoogleMock(argv, new_argv, "error");
158 }
159
160 TEST(WideInitGoogleMockTest, ParsesInvalidCommandLine) {
161   const wchar_t* argv[] = {
162     NULL
163   };
164
165   const wchar_t* new_argv[] = {
166     NULL
167   };
168
169   TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
170 }
171
172 TEST(WideInitGoogleMockTest, ParsesEmptyCommandLine) {
173   const wchar_t* argv[] = {
174     L"foo.exe",
175     NULL
176   };
177
178   const wchar_t* new_argv[] = {
179     L"foo.exe",
180     NULL
181   };
182
183   TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
184 }
185
186 TEST(WideInitGoogleMockTest, ParsesSingleFlag) {
187   const wchar_t* argv[] = {
188     L"foo.exe",
189     L"--gmock_verbose=info",
190     NULL
191   };
192
193   const wchar_t* new_argv[] = {
194     L"foo.exe",
195     NULL
196   };
197
198   TestInitGoogleMock(argv, new_argv, "info");
199 }
200
201 TEST(WideInitGoogleMockTest, ParsesMultipleFlags) {
202   int old_default_behavior = GMOCK_FLAG(default_mock_behavior);
203   const wchar_t* argv[] = {
204     L"foo.exe",
205     L"--gmock_verbose=info",
206     L"--gmock_default_mock_behavior=2",
207     NULL
208   };
209
210   const wchar_t* new_argv[] = {
211     L"foo.exe",
212     NULL
213   };
214
215   TestInitGoogleMock(argv, new_argv, "info");
216   EXPECT_EQ(2, GMOCK_FLAG(default_mock_behavior));
217   EXPECT_NE(2, old_default_behavior);
218   GMOCK_FLAG(default_mock_behavior) = old_default_behavior;
219 }
220
221 TEST(WideInitGoogleMockTest, ParsesUnrecognizedFlag) {
222   const wchar_t* argv[] = {
223     L"foo.exe",
224     L"--non_gmock_flag=blah",
225     NULL
226   };
227
228   const wchar_t* new_argv[] = {
229     L"foo.exe",
230     L"--non_gmock_flag=blah",
231     NULL
232   };
233
234   TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
235 }
236
237 TEST(WideInitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
238   const wchar_t* argv[] = {
239     L"foo.exe",
240     L"--non_gmock_flag=blah",
241     L"--gmock_verbose=error",
242     NULL
243   };
244
245   const wchar_t* new_argv[] = {
246     L"foo.exe",
247     L"--non_gmock_flag=blah",
248     NULL
249   };
250
251   TestInitGoogleMock(argv, new_argv, "error");
252 }
253
254 #endif  // !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
255
256 // Makes sure Google Mock flags can be accessed in code.
257 TEST(FlagTest, IsAccessibleInCode) {
258   bool dummy = testing::GMOCK_FLAG(catch_leaked_mocks) &&
259       testing::GMOCK_FLAG(verbose) == "";
260   (void)dummy;  // Avoids the "unused local variable" warning.
261 }