OLD | NEW |
| (Empty) |
1 // Copyright 2007, 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 the internal utilities. | |
35 | |
36 #include <gmock/internal/gmock-internal-utils.h> | |
37 #include <stdlib.h> | |
38 #include <map> | |
39 #include <string> | |
40 #include <sstream> | |
41 #include <vector> | |
42 #include <gmock/gmock.h> | |
43 #include <gmock/internal/gmock-port.h> | |
44 #include <gtest/gtest.h> | |
45 #include <gtest/gtest-spi.h> | |
46 | |
47 #if GTEST_OS_CYGWIN | |
48 #include <sys/types.h> // For ssize_t. NOLINT | |
49 #endif | |
50 | |
51 class ProtocolMessage; | |
52 | |
53 namespace proto2 { | |
54 class Message; | |
55 } // namespace proto2 | |
56 | |
57 namespace testing { | |
58 namespace internal { | |
59 | |
60 namespace { | |
61 | |
62 using ::std::tr1::make_tuple; | |
63 using ::std::tr1::tuple; | |
64 | |
65 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsNoWord) { | |
66 EXPECT_EQ("", ConvertIdentifierNameToWords("")); | |
67 EXPECT_EQ("", ConvertIdentifierNameToWords("_")); | |
68 EXPECT_EQ("", ConvertIdentifierNameToWords("__")); | |
69 } | |
70 | |
71 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsDigits) { | |
72 EXPECT_EQ("1", ConvertIdentifierNameToWords("_1")); | |
73 EXPECT_EQ("2", ConvertIdentifierNameToWords("2_")); | |
74 EXPECT_EQ("34", ConvertIdentifierNameToWords("_34_")); | |
75 EXPECT_EQ("34 56", ConvertIdentifierNameToWords("_34_56")); | |
76 } | |
77 | |
78 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsCamelCaseWords) { | |
79 EXPECT_EQ("a big word", ConvertIdentifierNameToWords("ABigWord")); | |
80 EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("FooBar")); | |
81 EXPECT_EQ("foo", ConvertIdentifierNameToWords("Foo_")); | |
82 EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_Foo_Bar_")); | |
83 EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_Foo__And_Bar")); | |
84 } | |
85 | |
86 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContains_SeparatedWords) { | |
87 EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("foo_bar")); | |
88 EXPECT_EQ("foo", ConvertIdentifierNameToWords("_foo_")); | |
89 EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_foo_bar_")); | |
90 EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_foo__and_bar")); | |
91 } | |
92 | |
93 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameIsMixture) { | |
94 EXPECT_EQ("foo bar 123", ConvertIdentifierNameToWords("Foo_bar123")); | |
95 EXPECT_EQ("chapter 11 section 1", | |
96 ConvertIdentifierNameToWords("_Chapter11Section_1_")); | |
97 } | |
98 | |
99 TEST(PointeeOfTest, WorksForSmartPointers) { | |
100 CompileAssertTypesEqual<const char, | |
101 PointeeOf<internal::linked_ptr<const char> >::type>(); | |
102 } | |
103 | |
104 TEST(PointeeOfTest, WorksForRawPointers) { | |
105 CompileAssertTypesEqual<int, PointeeOf<int*>::type>(); | |
106 CompileAssertTypesEqual<const char, PointeeOf<const char*>::type>(); | |
107 CompileAssertTypesEqual<void, PointeeOf<void*>::type>(); | |
108 } | |
109 | |
110 TEST(GetRawPointerTest, WorksForSmartPointers) { | |
111 const char* const raw_p4 = new const char('a'); // NOLINT | |
112 const internal::linked_ptr<const char> p4(raw_p4); | |
113 EXPECT_EQ(raw_p4, GetRawPointer(p4)); | |
114 } | |
115 | |
116 TEST(GetRawPointerTest, WorksForRawPointers) { | |
117 int* p = NULL; | |
118 // Don't use EXPECT_EQ as no NULL-testing magic on Symbian. | |
119 EXPECT_TRUE(NULL == GetRawPointer(p)); | |
120 int n = 1; | |
121 EXPECT_EQ(&n, GetRawPointer(&n)); | |
122 } | |
123 | |
124 // Tests KindOf<T>. | |
125 | |
126 class Base {}; | |
127 class Derived : public Base {}; | |
128 | |
129 TEST(KindOfTest, Bool) { | |
130 EXPECT_EQ(kBool, GMOCK_KIND_OF_(bool)); // NOLINT | |
131 } | |
132 | |
133 TEST(KindOfTest, Integer) { | |
134 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(char)); // NOLINT | |
135 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(signed char)); // NOLINT | |
136 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned char)); // NOLINT | |
137 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(short)); // NOLINT | |
138 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned short)); // NOLINT | |
139 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(int)); // NOLINT | |
140 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned int)); // NOLINT | |
141 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(long)); // NOLINT | |
142 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned long)); // NOLINT | |
143 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(wchar_t)); // NOLINT | |
144 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(Int64)); // NOLINT | |
145 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(UInt64)); // NOLINT | |
146 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(size_t)); // NOLINT | |
147 #if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN | |
148 // ssize_t is not defined on Windows and possibly some other OSes. | |
149 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(ssize_t)); // NOLINT | |
150 #endif | |
151 } | |
152 | |
153 TEST(KindOfTest, FloatingPoint) { | |
154 EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(float)); // NOLINT | |
155 EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(double)); // NOLINT | |
156 EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(long double)); // NOLINT | |
157 } | |
158 | |
159 TEST(KindOfTest, Other) { | |
160 EXPECT_EQ(kOther, GMOCK_KIND_OF_(void*)); // NOLINT | |
161 EXPECT_EQ(kOther, GMOCK_KIND_OF_(char**)); // NOLINT | |
162 EXPECT_EQ(kOther, GMOCK_KIND_OF_(Base)); // NOLINT | |
163 } | |
164 | |
165 // Tests LosslessArithmeticConvertible<T, U>. | |
166 | |
167 TEST(LosslessArithmeticConvertibleTest, BoolToBool) { | |
168 EXPECT_TRUE((LosslessArithmeticConvertible<bool, bool>::value)); | |
169 } | |
170 | |
171 TEST(LosslessArithmeticConvertibleTest, BoolToInteger) { | |
172 EXPECT_TRUE((LosslessArithmeticConvertible<bool, char>::value)); | |
173 EXPECT_TRUE((LosslessArithmeticConvertible<bool, int>::value)); | |
174 EXPECT_TRUE( | |
175 (LosslessArithmeticConvertible<bool, unsigned long>::value)); // NOLINT | |
176 } | |
177 | |
178 TEST(LosslessArithmeticConvertibleTest, BoolToFloatingPoint) { | |
179 EXPECT_TRUE((LosslessArithmeticConvertible<bool, float>::value)); | |
180 EXPECT_TRUE((LosslessArithmeticConvertible<bool, double>::value)); | |
181 } | |
182 | |
183 TEST(LosslessArithmeticConvertibleTest, IntegerToBool) { | |
184 EXPECT_FALSE((LosslessArithmeticConvertible<unsigned char, bool>::value)); | |
185 EXPECT_FALSE((LosslessArithmeticConvertible<int, bool>::value)); | |
186 } | |
187 | |
188 TEST(LosslessArithmeticConvertibleTest, IntegerToInteger) { | |
189 // Unsigned => larger signed is fine. | |
190 EXPECT_TRUE((LosslessArithmeticConvertible<unsigned char, int>::value)); | |
191 | |
192 // Unsigned => larger unsigned is fine. | |
193 EXPECT_TRUE( | |
194 (LosslessArithmeticConvertible<unsigned short, UInt64>::value)); // NOLINT | |
195 | |
196 // Signed => unsigned is not fine. | |
197 EXPECT_FALSE((LosslessArithmeticConvertible<short, UInt64>::value)); // NOLINT | |
198 EXPECT_FALSE((LosslessArithmeticConvertible< | |
199 signed char, unsigned int>::value)); // NOLINT | |
200 | |
201 // Same size and same signedness: fine too. | |
202 EXPECT_TRUE((LosslessArithmeticConvertible< | |
203 unsigned char, unsigned char>::value)); | |
204 EXPECT_TRUE((LosslessArithmeticConvertible<int, int>::value)); | |
205 EXPECT_TRUE((LosslessArithmeticConvertible<wchar_t, wchar_t>::value)); | |
206 EXPECT_TRUE((LosslessArithmeticConvertible< | |
207 unsigned long, unsigned long>::value)); // NOLINT | |
208 | |
209 // Same size, different signedness: not fine. | |
210 EXPECT_FALSE((LosslessArithmeticConvertible< | |
211 unsigned char, signed char>::value)); | |
212 EXPECT_FALSE((LosslessArithmeticConvertible<int, unsigned int>::value)); | |
213 EXPECT_FALSE((LosslessArithmeticConvertible<UInt64, Int64>::value)); | |
214 | |
215 // Larger size => smaller size is not fine. | |
216 EXPECT_FALSE((LosslessArithmeticConvertible<long, char>::value)); // NOLINT | |
217 EXPECT_FALSE((LosslessArithmeticConvertible<int, signed char>::value)); | |
218 EXPECT_FALSE((LosslessArithmeticConvertible<Int64, unsigned int>::value)); | |
219 } | |
220 | |
221 TEST(LosslessArithmeticConvertibleTest, IntegerToFloatingPoint) { | |
222 // Integers cannot be losslessly converted to floating-points, as | |
223 // the format of the latter is implementation-defined. | |
224 EXPECT_FALSE((LosslessArithmeticConvertible<char, float>::value)); | |
225 EXPECT_FALSE((LosslessArithmeticConvertible<int, double>::value)); | |
226 EXPECT_FALSE((LosslessArithmeticConvertible< | |
227 short, long double>::value)); // NOLINT | |
228 } | |
229 | |
230 TEST(LosslessArithmeticConvertibleTest, FloatingPointToBool) { | |
231 EXPECT_FALSE((LosslessArithmeticConvertible<float, bool>::value)); | |
232 EXPECT_FALSE((LosslessArithmeticConvertible<double, bool>::value)); | |
233 } | |
234 | |
235 TEST(LosslessArithmeticConvertibleTest, FloatingPointToInteger) { | |
236 EXPECT_FALSE((LosslessArithmeticConvertible<float, long>::value)); // NOLINT | |
237 EXPECT_FALSE((LosslessArithmeticConvertible<double, Int64>::value)); | |
238 EXPECT_FALSE((LosslessArithmeticConvertible<long double, int>::value)); | |
239 } | |
240 | |
241 TEST(LosslessArithmeticConvertibleTest, FloatingPointToFloatingPoint) { | |
242 // Smaller size => larger size is fine. | |
243 EXPECT_TRUE((LosslessArithmeticConvertible<float, double>::value)); | |
244 EXPECT_TRUE((LosslessArithmeticConvertible<float, long double>::value)); | |
245 EXPECT_TRUE((LosslessArithmeticConvertible<double, long double>::value)); | |
246 | |
247 // Same size: fine. | |
248 EXPECT_TRUE((LosslessArithmeticConvertible<float, float>::value)); | |
249 EXPECT_TRUE((LosslessArithmeticConvertible<double, double>::value)); | |
250 | |
251 // Larger size => smaller size is not fine. | |
252 EXPECT_FALSE((LosslessArithmeticConvertible<double, float>::value)); | |
253 if (sizeof(double) == sizeof(long double)) { // NOLINT | |
254 // In some implementations (e.g. MSVC), double and long double | |
255 // have the same size. | |
256 EXPECT_TRUE((LosslessArithmeticConvertible<long double, double>::value)); | |
257 } else { | |
258 EXPECT_FALSE((LosslessArithmeticConvertible<long double, double>::value)); | |
259 } | |
260 } | |
261 | |
262 // Tests the TupleMatches() template function. | |
263 | |
264 TEST(TupleMatchesTest, WorksForSize0) { | |
265 tuple<> matchers; | |
266 tuple<> values; | |
267 | |
268 EXPECT_TRUE(TupleMatches(matchers, values)); | |
269 } | |
270 | |
271 TEST(TupleMatchesTest, WorksForSize1) { | |
272 tuple<Matcher<int> > matchers(Eq(1)); | |
273 tuple<int> values1(1), | |
274 values2(2); | |
275 | |
276 EXPECT_TRUE(TupleMatches(matchers, values1)); | |
277 EXPECT_FALSE(TupleMatches(matchers, values2)); | |
278 } | |
279 | |
280 TEST(TupleMatchesTest, WorksForSize2) { | |
281 tuple<Matcher<int>, Matcher<char> > matchers(Eq(1), Eq('a')); | |
282 tuple<int, char> values1(1, 'a'), | |
283 values2(1, 'b'), | |
284 values3(2, 'a'), | |
285 values4(2, 'b'); | |
286 | |
287 EXPECT_TRUE(TupleMatches(matchers, values1)); | |
288 EXPECT_FALSE(TupleMatches(matchers, values2)); | |
289 EXPECT_FALSE(TupleMatches(matchers, values3)); | |
290 EXPECT_FALSE(TupleMatches(matchers, values4)); | |
291 } | |
292 | |
293 TEST(TupleMatchesTest, WorksForSize5) { | |
294 tuple<Matcher<int>, Matcher<char>, Matcher<bool>, Matcher<long>, // NOLINT | |
295 Matcher<string> > | |
296 matchers(Eq(1), Eq('a'), Eq(true), Eq(2L), Eq("hi")); | |
297 tuple<int, char, bool, long, string> // NOLINT | |
298 values1(1, 'a', true, 2L, "hi"), | |
299 values2(1, 'a', true, 2L, "hello"), | |
300 values3(2, 'a', true, 2L, "hi"); | |
301 | |
302 EXPECT_TRUE(TupleMatches(matchers, values1)); | |
303 EXPECT_FALSE(TupleMatches(matchers, values2)); | |
304 EXPECT_FALSE(TupleMatches(matchers, values3)); | |
305 } | |
306 | |
307 // Tests that Assert(true, ...) succeeds. | |
308 TEST(AssertTest, SucceedsOnTrue) { | |
309 Assert(true, __FILE__, __LINE__, "This should succeed."); | |
310 Assert(true, __FILE__, __LINE__); // This should succeed too. | |
311 } | |
312 | |
313 // Tests that Assert(false, ...) generates a fatal failure. | |
314 TEST(AssertTest, FailsFatallyOnFalse) { | |
315 EXPECT_DEATH_IF_SUPPORTED({ | |
316 Assert(false, __FILE__, __LINE__, "This should fail."); | |
317 }, ""); | |
318 | |
319 EXPECT_DEATH_IF_SUPPORTED({ | |
320 Assert(false, __FILE__, __LINE__); | |
321 }, ""); | |
322 } | |
323 | |
324 // Tests that Expect(true, ...) succeeds. | |
325 TEST(ExpectTest, SucceedsOnTrue) { | |
326 Expect(true, __FILE__, __LINE__, "This should succeed."); | |
327 Expect(true, __FILE__, __LINE__); // This should succeed too. | |
328 } | |
329 | |
330 // Tests that Expect(false, ...) generates a non-fatal failure. | |
331 TEST(ExpectTest, FailsNonfatallyOnFalse) { | |
332 EXPECT_NONFATAL_FAILURE({ // NOLINT | |
333 Expect(false, __FILE__, __LINE__, "This should fail."); | |
334 }, "This should fail"); | |
335 | |
336 EXPECT_NONFATAL_FAILURE({ // NOLINT | |
337 Expect(false, __FILE__, __LINE__); | |
338 }, "Expectation failed"); | |
339 } | |
340 | |
341 // Tests LogIsVisible(). | |
342 | |
343 class LogIsVisibleTest : public ::testing::Test { | |
344 protected: | |
345 virtual void SetUp() { | |
346 // The code needs to work when both ::string and ::std::string are | |
347 // defined and the flag is implemented as a | |
348 // testing::internal::String. In this case, without the call to | |
349 // c_str(), the compiler will complain that it cannot figure out | |
350 // whether the String flag should be converted to a ::string or an | |
351 // ::std::string before being assigned to original_verbose_. | |
352 original_verbose_ = GMOCK_FLAG(verbose).c_str(); | |
353 } | |
354 | |
355 virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; } | |
356 | |
357 string original_verbose_; | |
358 }; | |
359 | |
360 TEST_F(LogIsVisibleTest, AlwaysReturnsTrueIfVerbosityIsInfo) { | |
361 GMOCK_FLAG(verbose) = kInfoVerbosity; | |
362 EXPECT_TRUE(LogIsVisible(INFO)); | |
363 EXPECT_TRUE(LogIsVisible(WARNING)); | |
364 } | |
365 | |
366 TEST_F(LogIsVisibleTest, AlwaysReturnsFalseIfVerbosityIsError) { | |
367 GMOCK_FLAG(verbose) = kErrorVerbosity; | |
368 EXPECT_FALSE(LogIsVisible(INFO)); | |
369 EXPECT_FALSE(LogIsVisible(WARNING)); | |
370 } | |
371 | |
372 TEST_F(LogIsVisibleTest, WorksWhenVerbosityIsWarning) { | |
373 GMOCK_FLAG(verbose) = kWarningVerbosity; | |
374 EXPECT_FALSE(LogIsVisible(INFO)); | |
375 EXPECT_TRUE(LogIsVisible(WARNING)); | |
376 } | |
377 | |
378 #if GTEST_HAS_STREAM_REDIRECTION_ | |
379 | |
380 // Tests the Log() function. | |
381 | |
382 // Verifies that Log() behaves correctly for the given verbosity level | |
383 // and log severity. | |
384 void TestLogWithSeverity(const string& verbosity, LogSeverity severity, | |
385 bool should_print) { | |
386 const string old_flag = GMOCK_FLAG(verbose); | |
387 GMOCK_FLAG(verbose) = verbosity; | |
388 CaptureStdout(); | |
389 Log(severity, "Test log.\n", 0); | |
390 if (should_print) { | |
391 EXPECT_THAT(GetCapturedStdout().c_str(), | |
392 ContainsRegex( | |
393 severity == WARNING ? | |
394 "^\nGMOCK WARNING:\nTest log\\.\nStack trace:\n" : | |
395 "^\nTest log\\.\nStack trace:\n")); | |
396 } else { | |
397 EXPECT_STREQ("", GetCapturedStdout().c_str()); | |
398 } | |
399 GMOCK_FLAG(verbose) = old_flag; | |
400 } | |
401 | |
402 // Tests that when the stack_frames_to_skip parameter is negative, | |
403 // Log() doesn't include the stack trace in the output. | |
404 TEST(LogTest, NoStackTraceWhenStackFramesToSkipIsNegative) { | |
405 const string saved_flag = GMOCK_FLAG(verbose); | |
406 GMOCK_FLAG(verbose) = kInfoVerbosity; | |
407 CaptureStdout(); | |
408 Log(INFO, "Test log.\n", -1); | |
409 EXPECT_STREQ("\nTest log.\n", GetCapturedStdout().c_str()); | |
410 GMOCK_FLAG(verbose) = saved_flag; | |
411 } | |
412 | |
413 // Tests that in opt mode, a positive stack_frames_to_skip argument is | |
414 // treated as 0. | |
415 TEST(LogTest, NoSkippingStackFrameInOptMode) { | |
416 CaptureStdout(); | |
417 Log(WARNING, "Test log.\n", 100); | |
418 const String log = GetCapturedStdout(); | |
419 #if defined(NDEBUG) && GTEST_GOOGLE3_MODE_ | |
420 // In opt mode, no stack frame should be skipped. | |
421 EXPECT_THAT(log, ContainsRegex("\nGMOCK WARNING:\n" | |
422 "Test log\\.\n" | |
423 "Stack trace:\n" | |
424 ".+")); | |
425 #else | |
426 // In dbg mode, the stack frames should be skipped. | |
427 EXPECT_STREQ("\nGMOCK WARNING:\n" | |
428 "Test log.\n" | |
429 "Stack trace:\n", log.c_str()); | |
430 #endif | |
431 } | |
432 | |
433 // Tests that all logs are printed when the value of the | |
434 // --gmock_verbose flag is "info". | |
435 TEST(LogTest, AllLogsArePrintedWhenVerbosityIsInfo) { | |
436 TestLogWithSeverity(kInfoVerbosity, INFO, true); | |
437 TestLogWithSeverity(kInfoVerbosity, WARNING, true); | |
438 } | |
439 | |
440 // Tests that only warnings are printed when the value of the | |
441 // --gmock_verbose flag is "warning". | |
442 TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsWarning) { | |
443 TestLogWithSeverity(kWarningVerbosity, INFO, false); | |
444 TestLogWithSeverity(kWarningVerbosity, WARNING, true); | |
445 } | |
446 | |
447 // Tests that no logs are printed when the value of the | |
448 // --gmock_verbose flag is "error". | |
449 TEST(LogTest, NoLogsArePrintedWhenVerbosityIsError) { | |
450 TestLogWithSeverity(kErrorVerbosity, INFO, false); | |
451 TestLogWithSeverity(kErrorVerbosity, WARNING, false); | |
452 } | |
453 | |
454 // Tests that only warnings are printed when the value of the | |
455 // --gmock_verbose flag is invalid. | |
456 TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsInvalid) { | |
457 TestLogWithSeverity("invalid", INFO, false); | |
458 TestLogWithSeverity("invalid", WARNING, true); | |
459 } | |
460 | |
461 #endif // GTEST_HAS_STREAM_REDIRECTION_ | |
462 | |
463 TEST(TypeTraitsTest, true_type) { | |
464 EXPECT_TRUE(true_type::value); | |
465 } | |
466 | |
467 TEST(TypeTraitsTest, false_type) { | |
468 EXPECT_FALSE(false_type::value); | |
469 } | |
470 | |
471 TEST(TypeTraitsTest, is_reference) { | |
472 EXPECT_FALSE(is_reference<int>::value); | |
473 EXPECT_FALSE(is_reference<char*>::value); | |
474 EXPECT_TRUE(is_reference<const int&>::value); | |
475 } | |
476 | |
477 TEST(TypeTraitsTest, is_pointer) { | |
478 EXPECT_FALSE(is_pointer<int>::value); | |
479 EXPECT_FALSE(is_pointer<char&>::value); | |
480 EXPECT_TRUE(is_pointer<const int*>::value); | |
481 } | |
482 | |
483 TEST(TypeTraitsTest, type_equals) { | |
484 EXPECT_FALSE((type_equals<int, const int>::value)); | |
485 EXPECT_FALSE((type_equals<int, int&>::value)); | |
486 EXPECT_FALSE((type_equals<int, double>::value)); | |
487 EXPECT_TRUE((type_equals<char, char>::value)); | |
488 } | |
489 | |
490 TEST(TypeTraitsTest, remove_reference) { | |
491 EXPECT_TRUE((type_equals<char, remove_reference<char&>::type>::value)); | |
492 EXPECT_TRUE((type_equals<const int, | |
493 remove_reference<const int&>::type>::value)); | |
494 EXPECT_TRUE((type_equals<int, remove_reference<int>::type>::value)); | |
495 EXPECT_TRUE((type_equals<double*, remove_reference<double*>::type>::value)); | |
496 } | |
497 | |
498 #if GTEST_HAS_STREAM_REDIRECTION_ | |
499 | |
500 // Verifies that Log() behaves correctly for the given verbosity level | |
501 // and log severity. | |
502 String GrabOutput(void(*logger)(), const char* verbosity) { | |
503 const string saved_flag = GMOCK_FLAG(verbose); | |
504 GMOCK_FLAG(verbose) = verbosity; | |
505 CaptureStdout(); | |
506 logger(); | |
507 GMOCK_FLAG(verbose) = saved_flag; | |
508 return GetCapturedStdout(); | |
509 } | |
510 | |
511 class DummyMock { | |
512 public: | |
513 MOCK_METHOD0(TestMethod, void()); | |
514 MOCK_METHOD1(TestMethodArg, void(int dummy)); | |
515 }; | |
516 | |
517 void ExpectCallLogger() { | |
518 DummyMock mock; | |
519 EXPECT_CALL(mock, TestMethod()); | |
520 mock.TestMethod(); | |
521 }; | |
522 | |
523 // Verifies that EXPECT_CALL logs if the --gmock_verbose flag is set to "info". | |
524 TEST(ExpectCallTest, LogsWhenVerbosityIsInfo) { | |
525 EXPECT_THAT(GrabOutput(ExpectCallLogger, kInfoVerbosity), | |
526 HasSubstr("EXPECT_CALL(mock, TestMethod())")); | |
527 } | |
528 | |
529 // Verifies that EXPECT_CALL doesn't log | |
530 // if the --gmock_verbose flag is set to "warning". | |
531 TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsWarning) { | |
532 EXPECT_STREQ("", GrabOutput(ExpectCallLogger, kWarningVerbosity).c_str()); | |
533 } | |
534 | |
535 // Verifies that EXPECT_CALL doesn't log | |
536 // if the --gmock_verbose flag is set to "error". | |
537 TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsError) { | |
538 EXPECT_STREQ("", GrabOutput(ExpectCallLogger, kErrorVerbosity).c_str()); | |
539 } | |
540 | |
541 void OnCallLogger() { | |
542 DummyMock mock; | |
543 ON_CALL(mock, TestMethod()); | |
544 }; | |
545 | |
546 // Verifies that ON_CALL logs if the --gmock_verbose flag is set to "info". | |
547 TEST(OnCallTest, LogsWhenVerbosityIsInfo) { | |
548 EXPECT_THAT(GrabOutput(OnCallLogger, kInfoVerbosity), | |
549 HasSubstr("ON_CALL(mock, TestMethod())")); | |
550 } | |
551 | |
552 // Verifies that ON_CALL doesn't log | |
553 // if the --gmock_verbose flag is set to "warning". | |
554 TEST(OnCallTest, DoesNotLogWhenVerbosityIsWarning) { | |
555 EXPECT_STREQ("", GrabOutput(OnCallLogger, kWarningVerbosity).c_str()); | |
556 } | |
557 | |
558 // Verifies that ON_CALL doesn't log if | |
559 // the --gmock_verbose flag is set to "error". | |
560 TEST(OnCallTest, DoesNotLogWhenVerbosityIsError) { | |
561 EXPECT_STREQ("", GrabOutput(OnCallLogger, kErrorVerbosity).c_str()); | |
562 } | |
563 | |
564 void OnCallAnyArgumentLogger() { | |
565 DummyMock mock; | |
566 ON_CALL(mock, TestMethodArg(_)); | |
567 } | |
568 | |
569 // Verifies that ON_CALL prints provided _ argument. | |
570 TEST(OnCallTest, LogsAnythingArgument) { | |
571 EXPECT_THAT(GrabOutput(OnCallAnyArgumentLogger, kInfoVerbosity), | |
572 HasSubstr("ON_CALL(mock, TestMethodArg(_)")); | |
573 } | |
574 | |
575 #endif // GTEST_HAS_STREAM_REDIRECTION_ | |
576 | |
577 // Tests StlContainerView. | |
578 | |
579 TEST(StlContainerViewTest, WorksForStlContainer) { | |
580 StaticAssertTypeEq<std::vector<int>, | |
581 StlContainerView<std::vector<int> >::type>(); | |
582 StaticAssertTypeEq<const std::vector<double>&, | |
583 StlContainerView<std::vector<double> >::const_reference>(); | |
584 | |
585 typedef std::vector<char> Chars; | |
586 Chars v1; | |
587 const Chars& v2(StlContainerView<Chars>::ConstReference(v1)); | |
588 EXPECT_EQ(&v1, &v2); | |
589 | |
590 v1.push_back('a'); | |
591 Chars v3 = StlContainerView<Chars>::Copy(v1); | |
592 EXPECT_THAT(v3, Eq(v3)); | |
593 } | |
594 | |
595 TEST(StlContainerViewTest, WorksForStaticNativeArray) { | |
596 StaticAssertTypeEq<NativeArray<int>, | |
597 StlContainerView<int[3]>::type>(); | |
598 StaticAssertTypeEq<NativeArray<double>, | |
599 StlContainerView<const double[4]>::type>(); | |
600 StaticAssertTypeEq<NativeArray<char[3]>, | |
601 StlContainerView<const char[2][3]>::type>(); | |
602 | |
603 StaticAssertTypeEq<const NativeArray<int>, | |
604 StlContainerView<int[2]>::const_reference>(); | |
605 | |
606 int a1[3] = { 0, 1, 2 }; | |
607 NativeArray<int> a2 = StlContainerView<int[3]>::ConstReference(a1); | |
608 EXPECT_EQ(3U, a2.size()); | |
609 EXPECT_EQ(a1, a2.begin()); | |
610 | |
611 const NativeArray<int> a3 = StlContainerView<int[3]>::Copy(a1); | |
612 ASSERT_EQ(3U, a3.size()); | |
613 EXPECT_EQ(0, a3.begin()[0]); | |
614 EXPECT_EQ(1, a3.begin()[1]); | |
615 EXPECT_EQ(2, a3.begin()[2]); | |
616 | |
617 // Makes sure a1 and a3 aren't aliases. | |
618 a1[0] = 3; | |
619 EXPECT_EQ(0, a3.begin()[0]); | |
620 } | |
621 | |
622 TEST(StlContainerViewTest, WorksForDynamicNativeArray) { | |
623 StaticAssertTypeEq<NativeArray<int>, | |
624 StlContainerView<tuple<const int*, size_t> >::type>(); | |
625 StaticAssertTypeEq<NativeArray<double>, | |
626 StlContainerView<tuple<linked_ptr<double>, int> >::type>(); | |
627 | |
628 StaticAssertTypeEq<const NativeArray<int>, | |
629 StlContainerView<tuple<const int*, int> >::const_reference>(); | |
630 | |
631 int a1[3] = { 0, 1, 2 }; | |
632 const int* const p1 = a1; | |
633 NativeArray<int> a2 = StlContainerView<tuple<const int*, int> >:: | |
634 ConstReference(make_tuple(p1, 3)); | |
635 EXPECT_EQ(3U, a2.size()); | |
636 EXPECT_EQ(a1, a2.begin()); | |
637 | |
638 const NativeArray<int> a3 = StlContainerView<tuple<int*, size_t> >:: | |
639 Copy(make_tuple(static_cast<int*>(a1), 3)); | |
640 ASSERT_EQ(3U, a3.size()); | |
641 EXPECT_EQ(0, a3.begin()[0]); | |
642 EXPECT_EQ(1, a3.begin()[1]); | |
643 EXPECT_EQ(2, a3.begin()[2]); | |
644 | |
645 // Makes sure a1 and a3 aren't aliases. | |
646 a1[0] = 3; | |
647 EXPECT_EQ(0, a3.begin()[0]); | |
648 } | |
649 | |
650 } // namespace | |
651 } // namespace internal | |
652 } // namespace testing | |
OLD | NEW |