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
TEST(ConvertIdentifierNameToWordsTest,WorksWhenNameContainsNoWord)65 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsNoWord) {
66 EXPECT_EQ("", ConvertIdentifierNameToWords(""));
67 EXPECT_EQ("", ConvertIdentifierNameToWords("_"));
68 EXPECT_EQ("", ConvertIdentifierNameToWords("__"));
69 }
70
TEST(ConvertIdentifierNameToWordsTest,WorksWhenNameContainsDigits)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
TEST(ConvertIdentifierNameToWordsTest,WorksWhenNameContainsCamelCaseWords)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
TEST(ConvertIdentifierNameToWordsTest,WorksWhenNameContains_SeparatedWords)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
TEST(ConvertIdentifierNameToWordsTest,WorksWhenNameIsMixture)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
TEST(PointeeOfTest,WorksForSmartPointers)99 TEST(PointeeOfTest, WorksForSmartPointers) {
100 CompileAssertTypesEqual<const char,
101 PointeeOf<internal::linked_ptr<const char> >::type>();
102 }
103
TEST(PointeeOfTest,WorksForRawPointers)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
TEST(GetRawPointerTest,WorksForSmartPointers)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
TEST(GetRawPointerTest,WorksForRawPointers)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
TEST(KindOfTest,Bool)129 TEST(KindOfTest, Bool) {
130 EXPECT_EQ(kBool, GMOCK_KIND_OF_(bool)); // NOLINT
131 }
132
TEST(KindOfTest,Integer)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
TEST(KindOfTest,FloatingPoint)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
TEST(KindOfTest,Other)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
TEST(LosslessArithmeticConvertibleTest,BoolToBool)167 TEST(LosslessArithmeticConvertibleTest, BoolToBool) {
168 EXPECT_TRUE((LosslessArithmeticConvertible<bool, bool>::value));
169 }
170
TEST(LosslessArithmeticConvertibleTest,BoolToInteger)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
TEST(LosslessArithmeticConvertibleTest,BoolToFloatingPoint)178 TEST(LosslessArithmeticConvertibleTest, BoolToFloatingPoint) {
179 EXPECT_TRUE((LosslessArithmeticConvertible<bool, float>::value));
180 EXPECT_TRUE((LosslessArithmeticConvertible<bool, double>::value));
181 }
182
TEST(LosslessArithmeticConvertibleTest,IntegerToBool)183 TEST(LosslessArithmeticConvertibleTest, IntegerToBool) {
184 EXPECT_FALSE((LosslessArithmeticConvertible<unsigned char, bool>::value));
185 EXPECT_FALSE((LosslessArithmeticConvertible<int, bool>::value));
186 }
187
TEST(LosslessArithmeticConvertibleTest,IntegerToInteger)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
TEST(LosslessArithmeticConvertibleTest,IntegerToFloatingPoint)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
TEST(LosslessArithmeticConvertibleTest,FloatingPointToBool)230 TEST(LosslessArithmeticConvertibleTest, FloatingPointToBool) {
231 EXPECT_FALSE((LosslessArithmeticConvertible<float, bool>::value));
232 EXPECT_FALSE((LosslessArithmeticConvertible<double, bool>::value));
233 }
234
TEST(LosslessArithmeticConvertibleTest,FloatingPointToInteger)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
TEST(LosslessArithmeticConvertibleTest,FloatingPointToFloatingPoint)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
TEST(TupleMatchesTest,WorksForSize0)264 TEST(TupleMatchesTest, WorksForSize0) {
265 tuple<> matchers;
266 tuple<> values;
267
268 EXPECT_TRUE(TupleMatches(matchers, values));
269 }
270
TEST(TupleMatchesTest,WorksForSize1)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
TEST(TupleMatchesTest,WorksForSize2)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
TEST(TupleMatchesTest,WorksForSize5)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.
TEST(AssertTest,SucceedsOnTrue)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.
TEST(AssertTest,FailsFatallyOnFalse)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.
TEST(ExpectTest,SucceedsOnTrue)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.
TEST(ExpectTest,FailsNonfatallyOnFalse)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:
SetUp()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
TearDown()355 virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; }
356
357 string original_verbose_;
358 };
359
TEST_F(LogIsVisibleTest,AlwaysReturnsTrueIfVerbosityIsInfo)360 TEST_F(LogIsVisibleTest, AlwaysReturnsTrueIfVerbosityIsInfo) {
361 GMOCK_FLAG(verbose) = kInfoVerbosity;
362 EXPECT_TRUE(LogIsVisible(kInfo));
363 EXPECT_TRUE(LogIsVisible(kWarning));
364 }
365
TEST_F(LogIsVisibleTest,AlwaysReturnsFalseIfVerbosityIsError)366 TEST_F(LogIsVisibleTest, AlwaysReturnsFalseIfVerbosityIsError) {
367 GMOCK_FLAG(verbose) = kErrorVerbosity;
368 EXPECT_FALSE(LogIsVisible(kInfo));
369 EXPECT_FALSE(LogIsVisible(kWarning));
370 }
371
TEST_F(LogIsVisibleTest,WorksWhenVerbosityIsWarning)372 TEST_F(LogIsVisibleTest, WorksWhenVerbosityIsWarning) {
373 GMOCK_FLAG(verbose) = kWarningVerbosity;
374 EXPECT_FALSE(LogIsVisible(kInfo));
375 EXPECT_TRUE(LogIsVisible(kWarning));
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.
TestLogWithSeverity(const string & verbosity,LogSeverity severity,bool should_print)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 == kWarning ?
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.
TEST(LogTest,NoStackTraceWhenStackFramesToSkipIsNegative)404 TEST(LogTest, NoStackTraceWhenStackFramesToSkipIsNegative) {
405 const string saved_flag = GMOCK_FLAG(verbose);
406 GMOCK_FLAG(verbose) = kInfoVerbosity;
407 CaptureStdout();
408 Log(kInfo, "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.
TEST(LogTest,NoSkippingStackFrameInOptMode)415 TEST(LogTest, NoSkippingStackFrameInOptMode) {
416 CaptureStdout();
417 Log(kWarning, "Test log.\n", 100);
418 const String log = GetCapturedStdout();
419
420 # if defined(NDEBUG) && GTEST_GOOGLE3_MODE_
421
422 // In opt mode, no stack frame should be skipped.
423 EXPECT_THAT(log, ContainsRegex("\nGMOCK WARNING:\n"
424 "Test log\\.\n"
425 "Stack trace:\n"
426 ".+"));
427 # else
428
429 // In dbg mode, the stack frames should be skipped.
430 EXPECT_STREQ("\nGMOCK WARNING:\n"
431 "Test log.\n"
432 "Stack trace:\n", log.c_str());
433 # endif
434 }
435
436 // Tests that all logs are printed when the value of the
437 // --gmock_verbose flag is "info".
TEST(LogTest,AllLogsArePrintedWhenVerbosityIsInfo)438 TEST(LogTest, AllLogsArePrintedWhenVerbosityIsInfo) {
439 TestLogWithSeverity(kInfoVerbosity, kInfo, true);
440 TestLogWithSeverity(kInfoVerbosity, kWarning, true);
441 }
442
443 // Tests that only warnings are printed when the value of the
444 // --gmock_verbose flag is "warning".
TEST(LogTest,OnlyWarningsArePrintedWhenVerbosityIsWarning)445 TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsWarning) {
446 TestLogWithSeverity(kWarningVerbosity, kInfo, false);
447 TestLogWithSeverity(kWarningVerbosity, kWarning, true);
448 }
449
450 // Tests that no logs are printed when the value of the
451 // --gmock_verbose flag is "error".
TEST(LogTest,NoLogsArePrintedWhenVerbosityIsError)452 TEST(LogTest, NoLogsArePrintedWhenVerbosityIsError) {
453 TestLogWithSeverity(kErrorVerbosity, kInfo, false);
454 TestLogWithSeverity(kErrorVerbosity, kWarning, false);
455 }
456
457 // Tests that only warnings are printed when the value of the
458 // --gmock_verbose flag is invalid.
TEST(LogTest,OnlyWarningsArePrintedWhenVerbosityIsInvalid)459 TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsInvalid) {
460 TestLogWithSeverity("invalid", kInfo, false);
461 TestLogWithSeverity("invalid", kWarning, true);
462 }
463
464 #endif // GTEST_HAS_STREAM_REDIRECTION
465
TEST(TypeTraitsTest,true_type)466 TEST(TypeTraitsTest, true_type) {
467 EXPECT_TRUE(true_type::value);
468 }
469
TEST(TypeTraitsTest,false_type)470 TEST(TypeTraitsTest, false_type) {
471 EXPECT_FALSE(false_type::value);
472 }
473
TEST(TypeTraitsTest,is_reference)474 TEST(TypeTraitsTest, is_reference) {
475 EXPECT_FALSE(is_reference<int>::value);
476 EXPECT_FALSE(is_reference<char*>::value);
477 EXPECT_TRUE(is_reference<const int&>::value);
478 }
479
TEST(TypeTraitsTest,is_pointer)480 TEST(TypeTraitsTest, is_pointer) {
481 EXPECT_FALSE(is_pointer<int>::value);
482 EXPECT_FALSE(is_pointer<char&>::value);
483 EXPECT_TRUE(is_pointer<const int*>::value);
484 }
485
TEST(TypeTraitsTest,type_equals)486 TEST(TypeTraitsTest, type_equals) {
487 EXPECT_FALSE((type_equals<int, const int>::value));
488 EXPECT_FALSE((type_equals<int, int&>::value));
489 EXPECT_FALSE((type_equals<int, double>::value));
490 EXPECT_TRUE((type_equals<char, char>::value));
491 }
492
TEST(TypeTraitsTest,remove_reference)493 TEST(TypeTraitsTest, remove_reference) {
494 EXPECT_TRUE((type_equals<char, remove_reference<char&>::type>::value));
495 EXPECT_TRUE((type_equals<const int,
496 remove_reference<const int&>::type>::value));
497 EXPECT_TRUE((type_equals<int, remove_reference<int>::type>::value));
498 EXPECT_TRUE((type_equals<double*, remove_reference<double*>::type>::value));
499 }
500
501 #if GTEST_HAS_STREAM_REDIRECTION
502
503 // Verifies that Log() behaves correctly for the given verbosity level
504 // and log severity.
GrabOutput(void (* logger)(),const char * verbosity)505 String GrabOutput(void(*logger)(), const char* verbosity) {
506 const string saved_flag = GMOCK_FLAG(verbose);
507 GMOCK_FLAG(verbose) = verbosity;
508 CaptureStdout();
509 logger();
510 GMOCK_FLAG(verbose) = saved_flag;
511 return GetCapturedStdout();
512 }
513
514 class DummyMock {
515 public:
516 MOCK_METHOD0(TestMethod, void());
517 MOCK_METHOD1(TestMethodArg, void(int dummy));
518 };
519
ExpectCallLogger()520 void ExpectCallLogger() {
521 DummyMock mock;
522 EXPECT_CALL(mock, TestMethod());
523 mock.TestMethod();
524 };
525
526 // Verifies that EXPECT_CALL logs if the --gmock_verbose flag is set to "info".
TEST(ExpectCallTest,LogsWhenVerbosityIsInfo)527 TEST(ExpectCallTest, LogsWhenVerbosityIsInfo) {
528 EXPECT_THAT(GrabOutput(ExpectCallLogger, kInfoVerbosity),
529 HasSubstr("EXPECT_CALL(mock, TestMethod())"));
530 }
531
532 // Verifies that EXPECT_CALL doesn't log
533 // if the --gmock_verbose flag is set to "warning".
TEST(ExpectCallTest,DoesNotLogWhenVerbosityIsWarning)534 TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsWarning) {
535 EXPECT_STREQ("", GrabOutput(ExpectCallLogger, kWarningVerbosity).c_str());
536 }
537
538 // Verifies that EXPECT_CALL doesn't log
539 // if the --gmock_verbose flag is set to "error".
TEST(ExpectCallTest,DoesNotLogWhenVerbosityIsError)540 TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsError) {
541 EXPECT_STREQ("", GrabOutput(ExpectCallLogger, kErrorVerbosity).c_str());
542 }
543
OnCallLogger()544 void OnCallLogger() {
545 DummyMock mock;
546 ON_CALL(mock, TestMethod());
547 };
548
549 // Verifies that ON_CALL logs if the --gmock_verbose flag is set to "info".
TEST(OnCallTest,LogsWhenVerbosityIsInfo)550 TEST(OnCallTest, LogsWhenVerbosityIsInfo) {
551 EXPECT_THAT(GrabOutput(OnCallLogger, kInfoVerbosity),
552 HasSubstr("ON_CALL(mock, TestMethod())"));
553 }
554
555 // Verifies that ON_CALL doesn't log
556 // if the --gmock_verbose flag is set to "warning".
TEST(OnCallTest,DoesNotLogWhenVerbosityIsWarning)557 TEST(OnCallTest, DoesNotLogWhenVerbosityIsWarning) {
558 EXPECT_STREQ("", GrabOutput(OnCallLogger, kWarningVerbosity).c_str());
559 }
560
561 // Verifies that ON_CALL doesn't log if
562 // the --gmock_verbose flag is set to "error".
TEST(OnCallTest,DoesNotLogWhenVerbosityIsError)563 TEST(OnCallTest, DoesNotLogWhenVerbosityIsError) {
564 EXPECT_STREQ("", GrabOutput(OnCallLogger, kErrorVerbosity).c_str());
565 }
566
OnCallAnyArgumentLogger()567 void OnCallAnyArgumentLogger() {
568 DummyMock mock;
569 ON_CALL(mock, TestMethodArg(_));
570 }
571
572 // Verifies that ON_CALL prints provided _ argument.
TEST(OnCallTest,LogsAnythingArgument)573 TEST(OnCallTest, LogsAnythingArgument) {
574 EXPECT_THAT(GrabOutput(OnCallAnyArgumentLogger, kInfoVerbosity),
575 HasSubstr("ON_CALL(mock, TestMethodArg(_)"));
576 }
577
578 #endif // GTEST_HAS_STREAM_REDIRECTION
579
580 // Tests StlContainerView.
581
TEST(StlContainerViewTest,WorksForStlContainer)582 TEST(StlContainerViewTest, WorksForStlContainer) {
583 StaticAssertTypeEq<std::vector<int>,
584 StlContainerView<std::vector<int> >::type>();
585 StaticAssertTypeEq<const std::vector<double>&,
586 StlContainerView<std::vector<double> >::const_reference>();
587
588 typedef std::vector<char> Chars;
589 Chars v1;
590 const Chars& v2(StlContainerView<Chars>::ConstReference(v1));
591 EXPECT_EQ(&v1, &v2);
592
593 v1.push_back('a');
594 Chars v3 = StlContainerView<Chars>::Copy(v1);
595 EXPECT_THAT(v3, Eq(v3));
596 }
597
TEST(StlContainerViewTest,WorksForStaticNativeArray)598 TEST(StlContainerViewTest, WorksForStaticNativeArray) {
599 StaticAssertTypeEq<NativeArray<int>,
600 StlContainerView<int[3]>::type>();
601 StaticAssertTypeEq<NativeArray<double>,
602 StlContainerView<const double[4]>::type>();
603 StaticAssertTypeEq<NativeArray<char[3]>,
604 StlContainerView<const char[2][3]>::type>();
605
606 StaticAssertTypeEq<const NativeArray<int>,
607 StlContainerView<int[2]>::const_reference>();
608
609 int a1[3] = { 0, 1, 2 };
610 NativeArray<int> a2 = StlContainerView<int[3]>::ConstReference(a1);
611 EXPECT_EQ(3U, a2.size());
612 EXPECT_EQ(a1, a2.begin());
613
614 const NativeArray<int> a3 = StlContainerView<int[3]>::Copy(a1);
615 ASSERT_EQ(3U, a3.size());
616 EXPECT_EQ(0, a3.begin()[0]);
617 EXPECT_EQ(1, a3.begin()[1]);
618 EXPECT_EQ(2, a3.begin()[2]);
619
620 // Makes sure a1 and a3 aren't aliases.
621 a1[0] = 3;
622 EXPECT_EQ(0, a3.begin()[0]);
623 }
624
TEST(StlContainerViewTest,WorksForDynamicNativeArray)625 TEST(StlContainerViewTest, WorksForDynamicNativeArray) {
626 StaticAssertTypeEq<NativeArray<int>,
627 StlContainerView<tuple<const int*, size_t> >::type>();
628 StaticAssertTypeEq<NativeArray<double>,
629 StlContainerView<tuple<linked_ptr<double>, int> >::type>();
630
631 StaticAssertTypeEq<const NativeArray<int>,
632 StlContainerView<tuple<const int*, int> >::const_reference>();
633
634 int a1[3] = { 0, 1, 2 };
635 const int* const p1 = a1;
636 NativeArray<int> a2 = StlContainerView<tuple<const int*, int> >::
637 ConstReference(make_tuple(p1, 3));
638 EXPECT_EQ(3U, a2.size());
639 EXPECT_EQ(a1, a2.begin());
640
641 const NativeArray<int> a3 = StlContainerView<tuple<int*, size_t> >::
642 Copy(make_tuple(static_cast<int*>(a1), 3));
643 ASSERT_EQ(3U, a3.size());
644 EXPECT_EQ(0, a3.begin()[0]);
645 EXPECT_EQ(1, a3.begin()[1]);
646 EXPECT_EQ(2, a3.begin()[2]);
647
648 // Makes sure a1 and a3 aren't aliases.
649 a1[0] = 3;
650 EXPECT_EQ(0, a3.begin()[0]);
651 }
652
653 } // namespace
654 } // namespace internal
655 } // namespace testing
656