Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
gmock-internal-utils_test.cc
Go to the documentation of this file.
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
37#include <stdlib.h>
38#include <map>
39#include <memory>
40#include <string>
41#include <sstream>
42#include <vector>
43#include "gmock/gmock.h"
45#include "gtest/gtest.h"
46#include "gtest/gtest-spi.h"
47
48// Indicates that this translation unit is part of Google Test's
49// implementation. It must come before gtest-internal-inl.h is
50// included, or there will be a compiler error. This trick is to
51// prevent a user from accidentally including gtest-internal-inl.h in
52// their code.
53#define GTEST_IMPLEMENTATION_ 1
55#undef GTEST_IMPLEMENTATION_
56
57#if GTEST_OS_CYGWIN
58# include <sys/types.h> // For ssize_t. NOLINT
59#endif
60
61class ProtocolMessage;
62
63namespace proto2 {
64class Message;
65} // namespace proto2
66
67namespace testing {
68namespace internal {
69
70namespace {
71
72TEST(JoinAsTupleTest, JoinsEmptyTuple) {
74}
75
76TEST(JoinAsTupleTest, JoinsOneTuple) {
77 const char* fields[] = {"1"};
78 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
79}
80
81TEST(JoinAsTupleTest, JoinsTwoTuple) {
82 const char* fields[] = {"1", "a"};
83 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
84}
85
86TEST(JoinAsTupleTest, JoinsTenTuple) {
87 const char* fields[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
88 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
89 JoinAsTuple(Strings(fields, fields + 10)));
90}
91
92TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsNoWord) {
96}
97
98TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsDigits) {
102 EXPECT_EQ("34 56", ConvertIdentifierNameToWords("_34_56"));
103}
104
105TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsCamelCaseWords) {
106 EXPECT_EQ("a big word", ConvertIdentifierNameToWords("ABigWord"));
107 EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("FooBar"));
109 EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_Foo_Bar_"));
110 EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_Foo__And_Bar"));
111}
112
113TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContains_SeparatedWords) {
114 EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("foo_bar"));
116 EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_foo_bar_"));
117 EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_foo__and_bar"));
118}
119
120TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameIsMixture) {
121 EXPECT_EQ("foo bar 123", ConvertIdentifierNameToWords("Foo_bar123"));
122 EXPECT_EQ("chapter 11 section 1",
123 ConvertIdentifierNameToWords("_Chapter11Section_1_"));
124}
125
126TEST(PointeeOfTest, WorksForSmartPointers) {
127 CompileAssertTypesEqual<const char,
128 PointeeOf<internal::linked_ptr<const char> >::type>();
129#if GTEST_HAS_STD_UNIQUE_PTR_
130 CompileAssertTypesEqual<int, PointeeOf<std::unique_ptr<int> >::type>();
131#endif // GTEST_HAS_STD_UNIQUE_PTR_
132#if GTEST_HAS_STD_SHARED_PTR_
133 CompileAssertTypesEqual<std::string,
134 PointeeOf<std::shared_ptr<std::string> >::type>();
135#endif // GTEST_HAS_STD_SHARED_PTR_
136}
137
138TEST(PointeeOfTest, WorksForRawPointers) {
139 CompileAssertTypesEqual<int, PointeeOf<int*>::type>();
140 CompileAssertTypesEqual<const char, PointeeOf<const char*>::type>();
141 CompileAssertTypesEqual<void, PointeeOf<void*>::type>();
142}
143
144TEST(GetRawPointerTest, WorksForSmartPointers) {
145#if GTEST_HAS_STD_UNIQUE_PTR_
146 const char* const raw_p1 = new const char('a'); // NOLINT
147 const std::unique_ptr<const char> p1(raw_p1);
148 EXPECT_EQ(raw_p1, GetRawPointer(p1));
149#endif // GTEST_HAS_STD_UNIQUE_PTR_
150#if GTEST_HAS_STD_SHARED_PTR_
151 double* const raw_p2 = new double(2.5); // NOLINT
152 const std::shared_ptr<double> p2(raw_p2);
153 EXPECT_EQ(raw_p2, GetRawPointer(p2));
154#endif // GTEST_HAS_STD_SHARED_PTR_
155
156 const char* const raw_p4 = new const char('a'); // NOLINT
157 const internal::linked_ptr<const char> p4(raw_p4);
158 EXPECT_EQ(raw_p4, GetRawPointer(p4));
159}
160
161TEST(GetRawPointerTest, WorksForRawPointers) {
162 int* p = NULL;
163 // Don't use EXPECT_EQ as no NULL-testing magic on Symbian.
164 EXPECT_TRUE(NULL == GetRawPointer(p));
165 int n = 1;
166 EXPECT_EQ(&n, GetRawPointer(&n));
167}
168
169// Tests KindOf<T>.
170
171class Base {};
172class Derived : public Base {};
173
174TEST(KindOfTest, Bool) {
175 EXPECT_EQ(kBool, GMOCK_KIND_OF_(bool)); // NOLINT
176}
177
178TEST(KindOfTest, Integer) {
179 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(char)); // NOLINT
180 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(signed char)); // NOLINT
181 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned char)); // NOLINT
182 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(short)); // NOLINT
183 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned short)); // NOLINT
184 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(int)); // NOLINT
185 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned int)); // NOLINT
186 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(long)); // NOLINT
187 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned long)); // NOLINT
188 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(wchar_t)); // NOLINT
191 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(size_t)); // NOLINT
192#if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN
193 // ssize_t is not defined on Windows and possibly some other OSes.
194 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(ssize_t)); // NOLINT
195#endif
196}
197
198TEST(KindOfTest, FloatingPoint) {
199 EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(float)); // NOLINT
200 EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(double)); // NOLINT
201 EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(long double)); // NOLINT
202}
203
204TEST(KindOfTest, Other) {
205 EXPECT_EQ(kOther, GMOCK_KIND_OF_(void*)); // NOLINT
206 EXPECT_EQ(kOther, GMOCK_KIND_OF_(char**)); // NOLINT
207 EXPECT_EQ(kOther, GMOCK_KIND_OF_(Base)); // NOLINT
208}
209
210// Tests LosslessArithmeticConvertible<T, U>.
211
212TEST(LosslessArithmeticConvertibleTest, BoolToBool) {
213 EXPECT_TRUE((LosslessArithmeticConvertible<bool, bool>::value));
214}
215
216TEST(LosslessArithmeticConvertibleTest, BoolToInteger) {
217 EXPECT_TRUE((LosslessArithmeticConvertible<bool, char>::value));
218 EXPECT_TRUE((LosslessArithmeticConvertible<bool, int>::value));
220 (LosslessArithmeticConvertible<bool, unsigned long>::value)); // NOLINT
221}
222
223TEST(LosslessArithmeticConvertibleTest, BoolToFloatingPoint) {
224 EXPECT_TRUE((LosslessArithmeticConvertible<bool, float>::value));
225 EXPECT_TRUE((LosslessArithmeticConvertible<bool, double>::value));
226}
227
228TEST(LosslessArithmeticConvertibleTest, IntegerToBool) {
229 EXPECT_FALSE((LosslessArithmeticConvertible<unsigned char, bool>::value));
230 EXPECT_FALSE((LosslessArithmeticConvertible<int, bool>::value));
231}
232
233TEST(LosslessArithmeticConvertibleTest, IntegerToInteger) {
234 // Unsigned => larger signed is fine.
235 EXPECT_TRUE((LosslessArithmeticConvertible<unsigned char, int>::value));
236
237 // Unsigned => larger unsigned is fine.
239 (LosslessArithmeticConvertible<unsigned short, UInt64>::value)); // NOLINT
240
241 // Signed => unsigned is not fine.
242 EXPECT_FALSE((LosslessArithmeticConvertible<short, UInt64>::value)); // NOLINT
243 EXPECT_FALSE((LosslessArithmeticConvertible<
244 signed char, unsigned int>::value)); // NOLINT
245
246 // Same size and same signedness: fine too.
247 EXPECT_TRUE((LosslessArithmeticConvertible<
248 unsigned char, unsigned char>::value));
249 EXPECT_TRUE((LosslessArithmeticConvertible<int, int>::value));
250 EXPECT_TRUE((LosslessArithmeticConvertible<wchar_t, wchar_t>::value));
251 EXPECT_TRUE((LosslessArithmeticConvertible<
252 unsigned long, unsigned long>::value)); // NOLINT
253
254 // Same size, different signedness: not fine.
255 EXPECT_FALSE((LosslessArithmeticConvertible<
256 unsigned char, signed char>::value));
257 EXPECT_FALSE((LosslessArithmeticConvertible<int, unsigned int>::value));
258 EXPECT_FALSE((LosslessArithmeticConvertible<UInt64, Int64>::value));
259
260 // Larger size => smaller size is not fine.
261 EXPECT_FALSE((LosslessArithmeticConvertible<long, char>::value)); // NOLINT
262 EXPECT_FALSE((LosslessArithmeticConvertible<int, signed char>::value));
263 EXPECT_FALSE((LosslessArithmeticConvertible<Int64, unsigned int>::value));
264}
265
266TEST(LosslessArithmeticConvertibleTest, IntegerToFloatingPoint) {
267 // Integers cannot be losslessly converted to floating-points, as
268 // the format of the latter is implementation-defined.
269 EXPECT_FALSE((LosslessArithmeticConvertible<char, float>::value));
270 EXPECT_FALSE((LosslessArithmeticConvertible<int, double>::value));
271 EXPECT_FALSE((LosslessArithmeticConvertible<
272 short, long double>::value)); // NOLINT
273}
274
275TEST(LosslessArithmeticConvertibleTest, FloatingPointToBool) {
276 EXPECT_FALSE((LosslessArithmeticConvertible<float, bool>::value));
277 EXPECT_FALSE((LosslessArithmeticConvertible<double, bool>::value));
278}
279
280TEST(LosslessArithmeticConvertibleTest, FloatingPointToInteger) {
281 EXPECT_FALSE((LosslessArithmeticConvertible<float, long>::value)); // NOLINT
282 EXPECT_FALSE((LosslessArithmeticConvertible<double, Int64>::value));
283 EXPECT_FALSE((LosslessArithmeticConvertible<long double, int>::value));
284}
285
286TEST(LosslessArithmeticConvertibleTest, FloatingPointToFloatingPoint) {
287 // Smaller size => larger size is fine.
288 EXPECT_TRUE((LosslessArithmeticConvertible<float, double>::value));
289 EXPECT_TRUE((LosslessArithmeticConvertible<float, long double>::value));
290 EXPECT_TRUE((LosslessArithmeticConvertible<double, long double>::value));
291
292 // Same size: fine.
293 EXPECT_TRUE((LosslessArithmeticConvertible<float, float>::value));
294 EXPECT_TRUE((LosslessArithmeticConvertible<double, double>::value));
295
296 // Larger size => smaller size is not fine.
297 EXPECT_FALSE((LosslessArithmeticConvertible<double, float>::value));
299 if (sizeof(double) == sizeof(long double)) { // NOLINT
301 // In some implementations (e.g. MSVC), double and long double
302 // have the same size.
303 EXPECT_TRUE((LosslessArithmeticConvertible<long double, double>::value));
304 } else {
305 EXPECT_FALSE((LosslessArithmeticConvertible<long double, double>::value));
306 }
307}
308
309// Tests the TupleMatches() template function.
310
311TEST(TupleMatchesTest, WorksForSize0) {
312 tuple<> matchers;
313 tuple<> values;
314
315 EXPECT_TRUE(TupleMatches(matchers, values));
316}
317
318TEST(TupleMatchesTest, WorksForSize1) {
319 tuple<Matcher<int> > matchers(Eq(1));
320 tuple<int> values1(1),
321 values2(2);
322
323 EXPECT_TRUE(TupleMatches(matchers, values1));
324 EXPECT_FALSE(TupleMatches(matchers, values2));
325}
326
327TEST(TupleMatchesTest, WorksForSize2) {
328 tuple<Matcher<int>, Matcher<char> > matchers(Eq(1), Eq('a'));
329 tuple<int, char> values1(1, 'a'),
330 values2(1, 'b'),
331 values3(2, 'a'),
332 values4(2, 'b');
333
334 EXPECT_TRUE(TupleMatches(matchers, values1));
335 EXPECT_FALSE(TupleMatches(matchers, values2));
336 EXPECT_FALSE(TupleMatches(matchers, values3));
337 EXPECT_FALSE(TupleMatches(matchers, values4));
338}
339
340TEST(TupleMatchesTest, WorksForSize5) {
341 tuple<Matcher<int>, Matcher<char>, Matcher<bool>, Matcher<long>, // NOLINT
342 Matcher<std::string> >
343 matchers(Eq(1), Eq('a'), Eq(true), Eq(2L), Eq("hi"));
344 tuple<int, char, bool, long, std::string> // NOLINT
345 values1(1, 'a', true, 2L, "hi"), values2(1, 'a', true, 2L, "hello"),
346 values3(2, 'a', true, 2L, "hi");
347
348 EXPECT_TRUE(TupleMatches(matchers, values1));
349 EXPECT_FALSE(TupleMatches(matchers, values2));
350 EXPECT_FALSE(TupleMatches(matchers, values3));
351}
352
353// Tests that Assert(true, ...) succeeds.
354TEST(AssertTest, SucceedsOnTrue) {
355 Assert(true, __FILE__, __LINE__, "This should succeed.");
356 Assert(true, __FILE__, __LINE__); // This should succeed too.
357}
358
359// Tests that Assert(false, ...) generates a fatal failure.
360TEST(AssertTest, FailsFatallyOnFalse) {
362 Assert(false, __FILE__, __LINE__, "This should fail.");
363 }, "");
364
366 Assert(false, __FILE__, __LINE__);
367 }, "");
368}
369
370// Tests that Expect(true, ...) succeeds.
371TEST(ExpectTest, SucceedsOnTrue) {
372 Expect(true, __FILE__, __LINE__, "This should succeed.");
373 Expect(true, __FILE__, __LINE__); // This should succeed too.
374}
375
376// Tests that Expect(false, ...) generates a non-fatal failure.
377TEST(ExpectTest, FailsNonfatallyOnFalse) {
378 EXPECT_NONFATAL_FAILURE({ // NOLINT
379 Expect(false, __FILE__, __LINE__, "This should fail.");
380 }, "This should fail");
381
382 EXPECT_NONFATAL_FAILURE({ // NOLINT
383 Expect(false, __FILE__, __LINE__);
384 }, "Expectation failed");
385}
386
387// Tests LogIsVisible().
388
389class LogIsVisibleTest : public ::testing::Test {
390 protected:
391 virtual void SetUp() {
392 original_verbose_ = GMOCK_FLAG(verbose);
393 }
394
395 virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; }
396
397 std::string original_verbose_;
398};
399
400TEST_F(LogIsVisibleTest, AlwaysReturnsTrueIfVerbosityIsInfo) {
401 GMOCK_FLAG(verbose) = kInfoVerbosity;
404}
405
406TEST_F(LogIsVisibleTest, AlwaysReturnsFalseIfVerbosityIsError) {
407 GMOCK_FLAG(verbose) = kErrorVerbosity;
410}
411
412TEST_F(LogIsVisibleTest, WorksWhenVerbosityIsWarning) {
413 GMOCK_FLAG(verbose) = kWarningVerbosity;
416}
417
418#if GTEST_HAS_STREAM_REDIRECTION
419
420// Tests the Log() function.
421
422// Verifies that Log() behaves correctly for the given verbosity level
423// and log severity.
424void TestLogWithSeverity(const std::string& verbosity, LogSeverity severity,
425 bool should_print) {
426 const std::string old_flag = GMOCK_FLAG(verbose);
427 GMOCK_FLAG(verbose) = verbosity;
429 Log(severity, "Test log.\n", 0);
430 if (should_print) {
433 severity == kWarning ?
434 "^\nGMOCK WARNING:\nTest log\\.\nStack trace:\n" :
435 "^\nTest log\\.\nStack trace:\n"));
436 } else {
437 EXPECT_STREQ("", GetCapturedStdout().c_str());
438 }
439 GMOCK_FLAG(verbose) = old_flag;
440}
441
442// Tests that when the stack_frames_to_skip parameter is negative,
443// Log() doesn't include the stack trace in the output.
444TEST(LogTest, NoStackTraceWhenStackFramesToSkipIsNegative) {
445 const std::string saved_flag = GMOCK_FLAG(verbose);
446 GMOCK_FLAG(verbose) = kInfoVerbosity;
448 Log(kInfo, "Test log.\n", -1);
449 EXPECT_STREQ("\nTest log.\n", GetCapturedStdout().c_str());
450 GMOCK_FLAG(verbose) = saved_flag;
451}
452
453struct MockStackTraceGetter : testing::internal::OsStackTraceGetterInterface {
454 virtual std::string CurrentStackTrace(int max_depth, int skip_count) {
455 return (testing::Message() << max_depth << "::" << skip_count << "\n")
456 .GetString();
457 }
458 virtual void UponLeavingGTest() {}
459};
460
461// Tests that in opt mode, a positive stack_frames_to_skip argument is
462// treated as 0.
463TEST(LogTest, NoSkippingStackFrameInOptMode) {
464 MockStackTraceGetter* mock_os_stack_trace_getter = new MockStackTraceGetter;
465 GetUnitTestImpl()->set_os_stack_trace_getter(mock_os_stack_trace_getter);
466
468 Log(kWarning, "Test log.\n", 100);
469 const std::string log = GetCapturedStdout();
470
471 std::string expected_trace =
472 (testing::Message() << GTEST_FLAG(stack_trace_depth) << "::").GetString();
473 std::string expected_message =
474 "\nGMOCK WARNING:\n"
475 "Test log.\n"
476 "Stack trace:\n" +
477 expected_trace;
478 EXPECT_THAT(log, HasSubstr(expected_message));
479 int skip_count = atoi(log.substr(expected_message.size()).c_str());
480
481# if defined(NDEBUG)
482 // In opt mode, no stack frame should be skipped.
483 const int expected_skip_count = 0;
484# else
485 // In dbg mode, the stack frames should be skipped.
486 const int expected_skip_count = 100;
487# endif
488
489 // Note that each inner implementation layer will +1 the number to remove
490 // itself from the trace. This means that the value is a little higher than
491 // expected, but close enough.
492 EXPECT_THAT(skip_count,
493 AllOf(Ge(expected_skip_count), Le(expected_skip_count + 10)));
494
495 // Restores the default OS stack trace getter.
497}
498
499// Tests that all logs are printed when the value of the
500// --gmock_verbose flag is "info".
501TEST(LogTest, AllLogsArePrintedWhenVerbosityIsInfo) {
502 TestLogWithSeverity(kInfoVerbosity, kInfo, true);
503 TestLogWithSeverity(kInfoVerbosity, kWarning, true);
504}
505
506// Tests that only warnings are printed when the value of the
507// --gmock_verbose flag is "warning".
508TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsWarning) {
509 TestLogWithSeverity(kWarningVerbosity, kInfo, false);
510 TestLogWithSeverity(kWarningVerbosity, kWarning, true);
511}
512
513// Tests that no logs are printed when the value of the
514// --gmock_verbose flag is "error".
515TEST(LogTest, NoLogsArePrintedWhenVerbosityIsError) {
516 TestLogWithSeverity(kErrorVerbosity, kInfo, false);
517 TestLogWithSeverity(kErrorVerbosity, kWarning, false);
518}
519
520// Tests that only warnings are printed when the value of the
521// --gmock_verbose flag is invalid.
522TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsInvalid) {
523 TestLogWithSeverity("invalid", kInfo, false);
524 TestLogWithSeverity("invalid", kWarning, true);
525}
526
527#endif // GTEST_HAS_STREAM_REDIRECTION
528
529TEST(TypeTraitsTest, true_type) {
531}
532
533TEST(TypeTraitsTest, false_type) {
535}
536
537TEST(TypeTraitsTest, is_reference) {
541}
542
543TEST(TypeTraitsTest, is_pointer) {
547}
548
549TEST(TypeTraitsTest, type_equals) {
554}
555
556TEST(TypeTraitsTest, remove_reference) {
558 EXPECT_TRUE((type_equals<const int,
562}
563
564#if GTEST_HAS_STREAM_REDIRECTION
565
566// Verifies that Log() behaves correctly for the given verbosity level
567// and log severity.
568std::string GrabOutput(void(*logger)(), const char* verbosity) {
569 const std::string saved_flag = GMOCK_FLAG(verbose);
570 GMOCK_FLAG(verbose) = verbosity;
572 logger();
573 GMOCK_FLAG(verbose) = saved_flag;
574 return GetCapturedStdout();
575}
576
577class DummyMock {
578 public:
579 MOCK_METHOD0(TestMethod, void());
580 MOCK_METHOD1(TestMethodArg, void(int dummy));
581};
582
583void ExpectCallLogger() {
584 DummyMock mock;
585 EXPECT_CALL(mock, TestMethod());
586 mock.TestMethod();
587};
588
589// Verifies that EXPECT_CALL logs if the --gmock_verbose flag is set to "info".
590TEST(ExpectCallTest, LogsWhenVerbosityIsInfo) {
591 EXPECT_THAT(std::string(GrabOutput(ExpectCallLogger, kInfoVerbosity)),
592 HasSubstr("EXPECT_CALL(mock, TestMethod())"));
593}
594
595// Verifies that EXPECT_CALL doesn't log
596// if the --gmock_verbose flag is set to "warning".
597TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsWarning) {
598 EXPECT_STREQ("", GrabOutput(ExpectCallLogger, kWarningVerbosity).c_str());
599}
600
601// Verifies that EXPECT_CALL doesn't log
602// if the --gmock_verbose flag is set to "error".
603TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsError) {
604 EXPECT_STREQ("", GrabOutput(ExpectCallLogger, kErrorVerbosity).c_str());
605}
606
607void OnCallLogger() {
608 DummyMock mock;
609 ON_CALL(mock, TestMethod());
610};
611
612// Verifies that ON_CALL logs if the --gmock_verbose flag is set to "info".
613TEST(OnCallTest, LogsWhenVerbosityIsInfo) {
614 EXPECT_THAT(std::string(GrabOutput(OnCallLogger, kInfoVerbosity)),
615 HasSubstr("ON_CALL(mock, TestMethod())"));
616}
617
618// Verifies that ON_CALL doesn't log
619// if the --gmock_verbose flag is set to "warning".
620TEST(OnCallTest, DoesNotLogWhenVerbosityIsWarning) {
621 EXPECT_STREQ("", GrabOutput(OnCallLogger, kWarningVerbosity).c_str());
622}
623
624// Verifies that ON_CALL doesn't log if
625// the --gmock_verbose flag is set to "error".
626TEST(OnCallTest, DoesNotLogWhenVerbosityIsError) {
627 EXPECT_STREQ("", GrabOutput(OnCallLogger, kErrorVerbosity).c_str());
628}
629
630void OnCallAnyArgumentLogger() {
631 DummyMock mock;
632 ON_CALL(mock, TestMethodArg(_));
633}
634
635// Verifies that ON_CALL prints provided _ argument.
636TEST(OnCallTest, LogsAnythingArgument) {
637 EXPECT_THAT(std::string(GrabOutput(OnCallAnyArgumentLogger, kInfoVerbosity)),
638 HasSubstr("ON_CALL(mock, TestMethodArg(_)"));
639}
640
641#endif // GTEST_HAS_STREAM_REDIRECTION
642
643// Tests StlContainerView.
644
645TEST(StlContainerViewTest, WorksForStlContainer) {
646 StaticAssertTypeEq<std::vector<int>,
647 StlContainerView<std::vector<int> >::type>();
648 StaticAssertTypeEq<const std::vector<double>&,
649 StlContainerView<std::vector<double> >::const_reference>();
650
651 typedef std::vector<char> Chars;
652 Chars v1;
653 const Chars& v2(StlContainerView<Chars>::ConstReference(v1));
654 EXPECT_EQ(&v1, &v2);
655
656 v1.push_back('a');
657 Chars v3 = StlContainerView<Chars>::Copy(v1);
658 EXPECT_THAT(v3, Eq(v3));
659}
660
661TEST(StlContainerViewTest, WorksForStaticNativeArray) {
662 StaticAssertTypeEq<NativeArray<int>,
664 StaticAssertTypeEq<NativeArray<double>,
666 StaticAssertTypeEq<NativeArray<char[3]>,
668
669 StaticAssertTypeEq<const NativeArray<int>,
671
672 int a1[3] = { 0, 1, 2 };
673 NativeArray<int> a2 = StlContainerView<int[3]>::ConstReference(a1);
674 EXPECT_EQ(3U, a2.size());
675 EXPECT_EQ(a1, a2.begin());
676
677 const NativeArray<int> a3 = StlContainerView<int[3]>::Copy(a1);
678 ASSERT_EQ(3U, a3.size());
679 EXPECT_EQ(0, a3.begin()[0]);
680 EXPECT_EQ(1, a3.begin()[1]);
681 EXPECT_EQ(2, a3.begin()[2]);
682
683 // Makes sure a1 and a3 aren't aliases.
684 a1[0] = 3;
685 EXPECT_EQ(0, a3.begin()[0]);
686}
687
688TEST(StlContainerViewTest, WorksForDynamicNativeArray) {
689 StaticAssertTypeEq<NativeArray<int>,
690 StlContainerView<tuple<const int*, size_t> >::type>();
691 StaticAssertTypeEq<NativeArray<double>,
692 StlContainerView<tuple<linked_ptr<double>, int> >::type>();
693
694 StaticAssertTypeEq<const NativeArray<int>,
695 StlContainerView<tuple<const int*, int> >::const_reference>();
696
697 int a1[3] = { 0, 1, 2 };
698 const int* const p1 = a1;
699 NativeArray<int> a2 = StlContainerView<tuple<const int*, int> >::
700 ConstReference(make_tuple(p1, 3));
701 EXPECT_EQ(3U, a2.size());
702 EXPECT_EQ(a1, a2.begin());
703
704 const NativeArray<int> a3 = StlContainerView<tuple<int*, size_t> >::
705 Copy(make_tuple(static_cast<int*>(a1), 3));
706 ASSERT_EQ(3U, a3.size());
707 EXPECT_EQ(0, a3.begin()[0]);
708 EXPECT_EQ(1, a3.begin()[1]);
709 EXPECT_EQ(2, a3.begin()[2]);
710
711 // Makes sure a1 and a3 aren't aliases.
712 a1[0] = 3;
713 EXPECT_EQ(0, a3.begin()[0]);
714}
715
716} // namespace
717} // namespace internal
718} // namespace testing
const mie::Vuint & p
Definition bn.cpp:27
static const_reference ConstReference(const RawContainer &container)
static type Copy(const RawContainer &container)
void set_os_stack_trace_getter(OsStackTraceGetterInterface *getter)
Definition gtest.cc:5321
#define MOCK_METHOD0(m,...)
#define MOCK_METHOD1(m,...)
#define GMOCK_KIND_OF_(type)
std::string original_verbose_
#define EXPECT_THAT(value, matcher)
#define GMOCK_FLAG(name)
Definition gmock-port.h:66
#define EXPECT_CALL(obj, call)
#define ON_CALL(obj, call)
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
#define GTEST_INTENTIONAL_CONST_COND_PUSH_()
Definition gtest-port.h:940
#define GTEST_FLAG(name)
#define GTEST_INTENTIONAL_CONST_COND_POP_()
Definition gtest-port.h:942
#define EXPECT_NONFATAL_FAILURE(statement, substr)
Definition gtest-spi.h:203
#define TEST_F(test_fixture, test_name)
Definition gtest.h:2304
#define ASSERT_EQ(val1, val2)
Definition gtest.h:1988
#define EXPECT_EQ(val1, val2)
Definition gtest.h:1954
#define EXPECT_TRUE(condition)
Definition gtest.h:1895
#define EXPECT_STREQ(s1, s2)
Definition gtest.h:2027
#define TEST(test_case_name, test_name)
Definition gtest.h:2275
#define EXPECT_FALSE(condition)
Definition gtest.h:1898
fc::logger logger
GeneratorWrapper< T > values(std::initializer_list< T > values)
Definition Logging.h:12
GTEST_API_ std::string ConvertIdentifierNameToWords(const char *id_name)
bool TupleMatches(const MatcherTuple &matcher_tuple, const ValueTuple &value_tuple)
TypeWithSize< 8 >::Int Int64
GTEST_API_ std::string JoinAsTuple(const Strings &fields)
::std::vector< ::std::string > Strings
bool_constant< true > true_type
GTEST_API_ bool LogIsVisible(LogSeverity severity)
void Assert(bool condition, const char *file, int line)
class UnitTestImpl * GetUnitTestImpl()
TypeWithSize< 8 >::UInt UInt64
bool_constant< false > false_type
GTEST_API_ void CaptureStdout()
const Pointer::element_type * GetRawPointer(const Pointer &p)
void Expect(bool condition, const char *file, int line, const std::string &msg)
GTEST_API_ std::string GetCapturedStdout()
internal::Le2Matcher Le()
PolymorphicMatcher< internal::HasSubstrMatcher< std::string > > HasSubstr(const std::string &substring)
internal::AllOfResult2< M1, M2 >::type AllOf(M1 m1, M2 m2)
internal::Ge2Matcher Ge()
PolymorphicMatcher< internal::MatchesRegexMatcher > ContainsRegex(const internal::RE *regex)
internal::Eq2Matcher Eq()
#define value
Definition pkcs11.h:157
if(ppFunctionList==NULL)
c_gkp_out sizeof(template))