Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: testing/gmock/include/gmock/gmock-printers.h

Issue 115846: Retry to checkin a version of gmock, modified to use our boost_tuple in VS2005. (Closed)
Patch Set: Created 11 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 implements a universal value printer that can print a
35 // value of any type T:
36 //
37 // void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
38 //
39 // A user can teach this function how to print a class type T by
40 // defining either operator<<() or PrintTo() in the namespace that
41 // defines T. More specifically, the FIRST defined function in the
42 // following list will be used (assuming T is defined in namespace
43 // foo):
44 //
45 // 1. foo::PrintTo(const T&, ostream*)
46 // 2. operator<<(ostream&, const T&) defined in either foo or the
47 // global namespace.
48 //
49 // If none of the above is defined, it will print the debug string of
50 // the value if it is a protocol buffer, or print the raw bytes in the
51 // value otherwise.
52 //
53 // To aid debugging: when T is a reference type, the address of the
54 // value is also printed; when T is a (const) char pointer, both the
55 // pointer value and the NUL-terminated string it points to are
56 // printed.
57 //
58 // We also provide some convenient wrappers:
59 //
60 // // Prints a value as the given type to a string.
61 // string ::testing::internal::UniversalPrinter<T>::PrintToString(value);
62 //
63 // // Prints a value tersely: for a reference type, the referenced
64 // // value (but not the address) is printed; for a (const) char
65 // // pointer, the NUL-terminated string (but not the pointer) is
66 // // printed.
67 // void ::testing::internal::UniversalTersePrint(const T& value, ostream*);
68 //
69 // // Prints the fields of a tuple tersely to a string vector, one
70 // // element for each field.
71 // std::vector<string> UniversalTersePrintTupleFieldsToStrings(
72 // const Tuple& value);
73
74 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_PRINTERS_H_
75 #define GMOCK_INCLUDE_GMOCK_GMOCK_PRINTERS_H_
76
77 #include <ostream> // NOLINT
78 #include <sstream>
79 #include <string>
80 #include <utility>
81 #include <vector>
82
83 #include <gmock/internal/gmock-internal-utils.h>
84 #include <gmock/internal/gmock-port.h>
85 #include <gtest/gtest.h>
86
87 namespace testing {
88
89 // Definitions in the 'internal' and 'internal2' name spaces are
90 // subject to change without notice. DO NOT USE THEM IN USER CODE!
91 namespace internal2 {
92
93 // Prints the given number of bytes in the given object to the given
94 // ostream.
95 void PrintBytesInObjectTo(const unsigned char* obj_bytes,
96 size_t count,
97 ::std::ostream* os);
98
99 // TypeWithoutFormatter<T, kIsProto>::PrintValue(value, os) is called
100 // by the universal printer to print a value of type T when neither
101 // operator<< nor PrintTo() is defined for type T. When T is
102 // ProtocolMessage, proto2::Message, or a subclass of those, kIsProto
103 // will be true and the short debug string of the protocol message
104 // value will be printed; otherwise kIsProto will be false and the
105 // bytes in the value will be printed.
106 template <typename T, bool kIsProto>
107 class TypeWithoutFormatter {
108 public:
109 static void PrintValue(const T& value, ::std::ostream* os) {
110 PrintBytesInObjectTo(reinterpret_cast<const unsigned char*>(&value),
111 sizeof(value), os);
112 }
113 };
114 template <typename T>
115 class TypeWithoutFormatter<T, true> {
116 public:
117 static void PrintValue(const T& value, ::std::ostream* os) {
118 // Both ProtocolMessage and proto2::Message have the
119 // ShortDebugString() method, so the same implementation works for
120 // both.
121 ::std::operator<<(*os, "<" + value.ShortDebugString() + ">");
122 }
123 };
124
125 // Prints the given value to the given ostream. If the value is a
126 // protocol message, its short debug string is printed; otherwise the
127 // bytes in the value are printed. This is what
128 // UniversalPrinter<T>::Print() does when it knows nothing about type
129 // T and T has no << operator.
130 //
131 // A user can override this behavior for a class type Foo by defining
132 // a << operator in the namespace where Foo is defined.
133 //
134 // We put this operator in namespace 'internal2' instead of 'internal'
135 // to simplify the implementation, as much code in 'internal' needs to
136 // use << in STL, which would conflict with our own << were it defined
137 // in 'internal'.
138 //
139 // Note that this operator<< takes a generic std::basic_ostream<Char,
140 // CharTraits> type instead of the more restricted std::ostream. If
141 // we define it to take an std::ostream instead, we'll get an
142 // "ambiguous overloads" compiler error when trying to print a type
143 // Foo that supports streaming to std::basic_ostream<Char,
144 // CharTraits>, as the compiler cannot tell whether
145 // operator<<(std::ostream&, const T&) or
146 // operator<<(std::basic_stream<Char, CharTraits>, const Foo&) is more
147 // specific.
148 template <typename Char, typename CharTraits, typename T>
149 ::std::basic_ostream<Char, CharTraits>& operator<<(
150 ::std::basic_ostream<Char, CharTraits>& os, const T& x) {
151 TypeWithoutFormatter<T, ::testing::internal::IsAProtocolMessage<T>::value>::
152 PrintValue(x, &os);
153 return os;
154 }
155
156 } // namespace internal2
157 } // namespace testing
158
159 // This namespace MUST NOT BE NESTED IN ::testing, or the name look-up
160 // magic needed for implementing UniversalPrinter won't work.
161 namespace testing_internal {
162
163 // Used to print a value that is not an STL-style container when the
164 // user doesn't define PrintTo() for it.
165 template <typename T>
166 void DefaultPrintNonContainerTo(const T& value, ::std::ostream* os) {
167 // With the following statement, during unqualified name lookup,
168 // testing::internal2::operator<< appears as if it was declared in
169 // the nearest enclosing namespace that contains both
170 // ::testing_internal and ::testing::internal2, i.e. the global
171 // namespace. For more details, refer to the C++ Standard section
172 // 7.3.4-1 [namespace.udir]. This allows us to fall back onto
173 // testing::internal2::operator<< in case T doesn't come with a <<
174 // operator.
175 //
176 // We cannot write 'using ::testing::internal2::operator<<;', which
177 // gcc 3.3 fails to compile due to a compiler bug.
178 using namespace ::testing::internal2; // NOLINT
179
180 // Assuming T is defined in namespace foo, in the next statement,
181 // the compiler will consider all of:
182 //
183 // 1. foo::operator<< (thanks to Koenig look-up),
184 // 2. ::operator<< (as the current namespace is enclosed in ::),
185 // 3. testing::internal2::operator<< (thanks to the using statement above).
186 //
187 // The operator<< whose type matches T best will be picked.
188 //
189 // We deliberately allow #2 to be a candidate, as sometimes it's
190 // impossible to define #1 (e.g. when foo is ::std, defining
191 // anything in it is undefined behavior unless you are a compiler
192 // vendor.).
193 *os << value;
194 }
195
196 } // namespace testing_internal
197
198 namespace testing {
199 namespace internal {
200
201 // UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
202 // value to the given ostream. The caller must ensure that
203 // 'ostream_ptr' is not NULL, or the behavior is undefined.
204 //
205 // We define UniversalPrinter as a class template (as opposed to a
206 // function template), as we need to partially specialize it for
207 // reference types, which cannot be done with function templates.
208 template <typename T>
209 class UniversalPrinter;
210
211 // Used to print an STL-style container when the user doesn't define
212 // a PrintTo() for it.
213 template <typename C>
214 void DefaultPrintTo(IsContainer /* dummy */,
215 false_type /* is not a pointer */,
216 const C& container, ::std::ostream* os) {
217 const size_t kMaxCount = 32; // The maximum number of elements to print.
218 *os << '{';
219 size_t count = 0;
220 for (typename C::const_iterator it = container.begin();
221 it != container.end(); ++it, ++count) {
222 if (count > 0) {
223 *os << ',';
224 if (count == kMaxCount) { // Enough has been printed.
225 *os << " ...";
226 break;
227 }
228 }
229 *os << ' ';
230 PrintTo(*it, os);
231 }
232
233 if (count > 0) {
234 *os << ' ';
235 }
236 *os << '}';
237 }
238
239 // Used to print a pointer that is neither a char pointer nor a member
240 // pointer, when the user doesn't define PrintTo() for it. (A member
241 // variable pointer or member function pointer doesn't really point to
242 // a location in the address space. Their representation is
243 // implementation-defined. Therefore they will be printed as raw
244 // bytes.)
245 template <typename T>
246 void DefaultPrintTo(IsNotContainer /* dummy */,
247 true_type /* is a pointer */,
248 T* p, ::std::ostream* os) {
249 if (p == NULL) {
250 *os << "NULL";
251 } else {
252 // We cannot use implicit_cast or static_cast here, as they don't
253 // work when p is a function pointer.
254 *os << reinterpret_cast<const void*>(p);
255 }
256 }
257
258 // Used to print a non-container, non-pointer value when the user
259 // doesn't define PrintTo() for it.
260 template <typename T>
261 void DefaultPrintTo(IsNotContainer /* dummy */,
262 false_type /* is not a pointer */,
263 const T& value, ::std::ostream* os) {
264 ::testing_internal::DefaultPrintNonContainerTo(value, os);
265 }
266
267 // Prints the given value using the << operator if it has one;
268 // otherwise prints the bytes in it. This is what
269 // UniversalPrinter<T>::Print() does when PrintTo() is not specialized
270 // or overloaded for type T.
271 //
272 // A user can override this behavior for a class type Foo by defining
273 // an overload of PrintTo() in the namespace where Foo is defined. We
274 // give the user this option as sometimes defining a << operator for
275 // Foo is not desirable (e.g. the coding style may prevent doing it,
276 // or there is already a << operator but it doesn't do what the user
277 // wants).
278 template <typename T>
279 void PrintTo(const T& value, ::std::ostream* os) {
280 // DefaultPrintTo() is overloaded. The type of its first two
281 // arguments determine which version will be picked. If T is an
282 // STL-style container, the version for container will be called; if
283 // T is a pointer, the pointer version will be called; otherwise the
284 // generic version will be called.
285 //
286 // Note that we check for container types here, prior to we check
287 // for protocol message types in our operator<<. The rationale is:
288 //
289 // For protocol messages, we want to give people a chance to
290 // override Google Mock's format by defining a PrintTo() or
291 // operator<<. For STL containers, other formats can be
292 // incompatible with Google Mock's format for the container
293 // elements; therefore we check for container types here to ensure
294 // that our format is used.
295 //
296 // The second argument of DefaultPrintTo() is needed to bypass a bug
297 // in Symbian's C++ compiler that prevents it from picking the right
298 // overload between:
299 //
300 // PrintTo(const T& x, ...);
301 // PrintTo(T* x, ...);
302 DefaultPrintTo(IsContainerTest<T>(0), is_pointer<T>(), value, os);
303 }
304
305 // The following list of PrintTo() overloads tells
306 // UniversalPrinter<T>::Print() how to print standard types (built-in
307 // types, strings, plain arrays, and pointers).
308
309 // Overloads for various char types.
310 void PrintCharTo(char c, int char_code, ::std::ostream* os);
311 inline void PrintTo(unsigned char c, ::std::ostream* os) {
312 PrintCharTo(c, c, os);
313 }
314 inline void PrintTo(signed char c, ::std::ostream* os) {
315 PrintCharTo(c, c, os);
316 }
317 inline void PrintTo(char c, ::std::ostream* os) {
318 // When printing a plain char, we always treat it as unsigned. This
319 // way, the output won't be affected by whether the compiler thinks
320 // char is signed or not.
321 PrintTo(static_cast<unsigned char>(c), os);
322 }
323
324 // Overloads for other simple built-in types.
325 inline void PrintTo(bool x, ::std::ostream* os) {
326 *os << (x ? "true" : "false");
327 }
328
329 // Overload for wchar_t type.
330 // Prints a wchar_t as a symbol if it is printable or as its internal
331 // code otherwise and also as its decimal code (except for L'\0').
332 // The L'\0' char is printed as "L'\\0'". The decimal code is printed
333 // as signed integer when wchar_t is implemented by the compiler
334 // as a signed type and is printed as an unsigned integer when wchar_t
335 // is implemented as an unsigned type.
336 void PrintTo(wchar_t wc, ::std::ostream* os);
337
338 // Overloads for C strings.
339 void PrintTo(const char* s, ::std::ostream* os);
340 inline void PrintTo(char* s, ::std::ostream* os) {
341 PrintTo(implicit_cast<const char*>(s), os);
342 }
343
344 // MSVC can be configured to define wchar_t as a typedef of unsigned
345 // short. It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native
346 // type. When wchar_t is a typedef, defining an overload for const
347 // wchar_t* would cause unsigned short* be printed as a wide string,
348 // possibly causing invalid memory accesses.
349 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
350 // Overloads for wide C strings
351 void PrintTo(const wchar_t* s, ::std::ostream* os);
352 inline void PrintTo(wchar_t* s, ::std::ostream* os) {
353 PrintTo(implicit_cast<const wchar_t*>(s), os);
354 }
355 #endif
356
357 // Overload for C arrays. Multi-dimensional arrays are printed
358 // properly.
359
360 // Prints the given number of elements in an array, without printing
361 // the curly braces.
362 template <typename T>
363 void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
364 UniversalPrinter<T>::Print(a[0], os);
365 for (size_t i = 1; i != count; i++) {
366 *os << ", ";
367 UniversalPrinter<T>::Print(a[i], os);
368 }
369 }
370
371 // Overloads for ::string and ::std::string.
372 #if GTEST_HAS_GLOBAL_STRING
373 void PrintStringTo(const ::string&s, ::std::ostream* os);
374 inline void PrintTo(const ::string& s, ::std::ostream* os) {
375 PrintStringTo(s, os);
376 }
377 #endif // GTEST_HAS_GLOBAL_STRING
378
379 #if GTEST_HAS_STD_STRING
380 void PrintStringTo(const ::std::string&s, ::std::ostream* os);
381 inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
382 PrintStringTo(s, os);
383 }
384 #endif // GTEST_HAS_STD_STRING
385
386 // Overloads for ::wstring and ::std::wstring.
387 #if GTEST_HAS_GLOBAL_WSTRING
388 void PrintWideStringTo(const ::wstring&s, ::std::ostream* os);
389 inline void PrintTo(const ::wstring& s, ::std::ostream* os) {
390 PrintWideStringTo(s, os);
391 }
392 #endif // GTEST_HAS_GLOBAL_WSTRING
393
394 #if GTEST_HAS_STD_WSTRING
395 void PrintWideStringTo(const ::std::wstring&s, ::std::ostream* os);
396 inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
397 PrintWideStringTo(s, os);
398 }
399 #endif // GTEST_HAS_STD_WSTRING
400
401 // Overload for ::std::tr1::tuple. Needed for printing function
402 // arguments, which are packed as tuples.
403
404 typedef ::std::vector<string> Strings;
405
406 // This helper template allows PrintTo() for tuples and
407 // UniversalTersePrintTupleFieldsToStrings() to be defined by
408 // induction on the number of tuple fields. The idea is that
409 // TuplePrefixPrinter<N>::PrintPrefixTo(t, os) prints the first N
410 // fields in tuple t, and can be defined in terms of
411 // TuplePrefixPrinter<N - 1>.
412
413 // The inductive case.
414 template <size_t N>
415 struct TuplePrefixPrinter {
416 // Prints the first N fields of a tuple.
417 template <typename Tuple>
418 static void PrintPrefixTo(const Tuple& t, ::std::ostream* os) {
419 TuplePrefixPrinter<N - 1>::PrintPrefixTo(t, os);
420 *os << ", ";
421 UniversalPrinter<typename ::std::tr1::tuple_element<N - 1, Tuple>::type>
422 ::Print(::std::tr1::get<N - 1>(t), os);
423 }
424
425 // Tersely prints the first N fields of a tuple to a string vector,
426 // one element for each field.
427 template <typename Tuple>
428 static void TersePrintPrefixToStrings(const Tuple& t, Strings* strings) {
429 TuplePrefixPrinter<N - 1>::TersePrintPrefixToStrings(t, strings);
430 ::std::stringstream ss;
431 UniversalTersePrint(::std::tr1::get<N - 1>(t), &ss);
432 strings->push_back(ss.str());
433 }
434 };
435
436 // Base cases.
437 template <>
438 struct TuplePrefixPrinter<0> {
439 template <typename Tuple>
440 static void PrintPrefixTo(const Tuple&, ::std::ostream*) {}
441
442 template <typename Tuple>
443 static void TersePrintPrefixToStrings(const Tuple&, Strings*) {}
444 };
445 template <>
446 template <typename Tuple>
447 void TuplePrefixPrinter<1>::PrintPrefixTo(const Tuple& t, ::std::ostream* os) {
448 UniversalPrinter<typename ::std::tr1::tuple_element<0, Tuple>::type>::
449 Print(::std::tr1::get<0>(t), os);
450 }
451
452 // Helper function for printing a tuple. T must be instantiated with
453 // a tuple type.
454 template <typename T>
455 void PrintTupleTo(const T& t, ::std::ostream* os) {
456 *os << "(";
457 TuplePrefixPrinter< ::std::tr1::tuple_size<T>::value>::
458 PrintPrefixTo(t, os);
459 *os << ")";
460 }
461
462 // Overloaded PrintTo() for tuples of various arities. We support
463 // tuples of up-to 10 fields. The following implementation works
464 // regardless of whether tr1::tuple is implemented using the
465 // non-standard variadic template feature or not.
466
467 inline void PrintTo(const ::std::tr1::tuple<>& t, ::std::ostream* os) {
468 PrintTupleTo(t, os);
469 }
470
471 template <typename T1>
472 void PrintTo(const ::std::tr1::tuple<T1>& t, ::std::ostream* os) {
473 PrintTupleTo(t, os);
474 }
475
476 template <typename T1, typename T2>
477 void PrintTo(const ::std::tr1::tuple<T1, T2>& t, ::std::ostream* os) {
478 PrintTupleTo(t, os);
479 }
480
481 template <typename T1, typename T2, typename T3>
482 void PrintTo(const ::std::tr1::tuple<T1, T2, T3>& t, ::std::ostream* os) {
483 PrintTupleTo(t, os);
484 }
485
486 template <typename T1, typename T2, typename T3, typename T4>
487 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4>& t, ::std::ostream* os) {
488 PrintTupleTo(t, os);
489 }
490
491 template <typename T1, typename T2, typename T3, typename T4, typename T5>
492 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5>& t,
493 ::std::ostream* os) {
494 PrintTupleTo(t, os);
495 }
496
497 template <typename T1, typename T2, typename T3, typename T4, typename T5,
498 typename T6>
499 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6>& t,
500 ::std::ostream* os) {
501 PrintTupleTo(t, os);
502 }
503
504 template <typename T1, typename T2, typename T3, typename T4, typename T5,
505 typename T6, typename T7>
506 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7>& t,
507 ::std::ostream* os) {
508 PrintTupleTo(t, os);
509 }
510
511 template <typename T1, typename T2, typename T3, typename T4, typename T5,
512 typename T6, typename T7, typename T8>
513 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8>& t,
514 ::std::ostream* os) {
515 PrintTupleTo(t, os);
516 }
517
518 template <typename T1, typename T2, typename T3, typename T4, typename T5,
519 typename T6, typename T7, typename T8, typename T9>
520 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9>& t,
521 ::std::ostream* os) {
522 PrintTupleTo(t, os);
523 }
524
525 template <typename T1, typename T2, typename T3, typename T4, typename T5,
526 typename T6, typename T7, typename T8, typename T9, typename T10>
527 void PrintTo(
528 const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& t,
529 ::std::ostream* os) {
530 PrintTupleTo(t, os);
531 }
532
533 // Overload for std::pair.
534 template <typename T1, typename T2>
535 void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
536 *os << '(';
537 UniversalPrinter<T1>::Print(value.first, os);
538 *os << ", ";
539 UniversalPrinter<T2>::Print(value.second, os);
540 *os << ')';
541 }
542
543 // Implements printing a non-reference type T by letting the compiler
544 // pick the right overload of PrintTo() for T.
545 template <typename T>
546 class UniversalPrinter {
547 public:
548 // MSVC warns about adding const to a function type, so we want to
549 // disable the warning.
550 #ifdef _MSC_VER
551 #pragma warning(push) // Saves the current warning state.
552 #pragma warning(disable:4180) // Temporarily disables warning 4180.
553 #endif // _MSC_VER
554
555 // Note: we deliberately don't call this PrintTo(), as that name
556 // conflicts with ::testing::internal::PrintTo in the body of the
557 // function.
558 static void Print(const T& value, ::std::ostream* os) {
559 // By default, ::testing::internal::PrintTo() is used for printing
560 // the value.
561 //
562 // Thanks to Koenig look-up, if T is a class and has its own
563 // PrintTo() function defined in its namespace, that function will
564 // be visible here. Since it is more specific than the generic ones
565 // in ::testing::internal, it will be picked by the compiler in the
566 // following statement - exactly what we want.
567 PrintTo(value, os);
568 }
569
570 // A convenient wrapper for Print() that returns the print-out as a
571 // string.
572 static string PrintToString(const T& value) {
573 ::std::stringstream ss;
574 Print(value, &ss);
575 return ss.str();
576 }
577
578 #ifdef _MSC_VER
579 #pragma warning(pop) // Restores the warning state.
580 #endif // _MSC_VER
581 };
582
583 // Implements printing an array type T[N].
584 template <typename T, size_t N>
585 class UniversalPrinter<T[N]> {
586 public:
587 // Prints the given array, omitting some elements when there are too
588 // many.
589 static void Print(const T (&a)[N], ::std::ostream* os) {
590 // Prints a char array as a C string. Note that we compare 'const
591 // T' with 'const char' instead of comparing T with char, in case
592 // that T is already a const type.
593 if (internal::type_equals<const T, const char>::value) {
594 UniversalPrinter<const T*>::Print(a, os);
595 return;
596 }
597
598 if (N == 0) {
599 *os << "{}";
600 } else {
601 *os << "{ ";
602 const size_t kThreshold = 18;
603 const size_t kChunkSize = 8;
604 // If the array has more than kThreshold elements, we'll have to
605 // omit some details by printing only the first and the last
606 // kChunkSize elements.
607 // TODO(wan): let the user control the threshold using a flag.
608 if (N <= kThreshold) {
609 PrintRawArrayTo(a, N, os);
610 } else {
611 PrintRawArrayTo(a, kChunkSize, os);
612 *os << ", ..., ";
613 PrintRawArrayTo(a + N - kChunkSize, kChunkSize, os);
614 }
615 *os << " }";
616 }
617 }
618
619 // A convenient wrapper for Print() that returns the print-out as a
620 // string.
621 static string PrintToString(const T (&a)[N]) {
622 ::std::stringstream ss;
623 Print(a, &ss);
624 return ss.str();
625 }
626 };
627
628 // Implements printing a reference type T&.
629 template <typename T>
630 class UniversalPrinter<T&> {
631 public:
632 // MSVC warns about adding const to a function type, so we want to
633 // disable the warning.
634 #ifdef _MSC_VER
635 #pragma warning(push) // Saves the current warning state.
636 #pragma warning(disable:4180) // Temporarily disables warning 4180.
637 #endif // _MSC_VER
638
639 static void Print(const T& value, ::std::ostream* os) {
640 // Prints the address of the value. We use reinterpret_cast here
641 // as static_cast doesn't compile when T is a function type.
642 *os << "@" << reinterpret_cast<const void*>(&value) << " ";
643
644 // Then prints the value itself.
645 UniversalPrinter<T>::Print(value, os);
646 }
647
648 // A convenient wrapper for Print() that returns the print-out as a
649 // string.
650 static string PrintToString(const T& value) {
651 ::std::stringstream ss;
652 Print(value, &ss);
653 return ss.str();
654 }
655
656 #ifdef _MSC_VER
657 #pragma warning(pop) // Restores the warning state.
658 #endif // _MSC_VER
659 };
660
661 // Prints a value tersely: for a reference type, the referenced value
662 // (but not the address) is printed; for a (const) char pointer, the
663 // NUL-terminated string (but not the pointer) is printed.
664 template <typename T>
665 void UniversalTersePrint(const T& value, ::std::ostream* os) {
666 UniversalPrinter<T>::Print(value, os);
667 }
668 inline void UniversalTersePrint(const char* str, ::std::ostream* os) {
669 if (str == NULL) {
670 *os << "NULL";
671 } else {
672 UniversalPrinter<string>::Print(string(str), os);
673 }
674 }
675 inline void UniversalTersePrint(char* str, ::std::ostream* os) {
676 UniversalTersePrint(static_cast<const char*>(str), os);
677 }
678
679 // Prints the fields of a tuple tersely to a string vector, one
680 // element for each field. See the comment before
681 // UniversalTersePrint() for how we define "tersely".
682 template <typename Tuple>
683 Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
684 Strings result;
685 TuplePrefixPrinter< ::std::tr1::tuple_size<Tuple>::value>::
686 TersePrintPrefixToStrings(value, &result);
687 return result;
688 }
689
690 } // namespace internal
691 } // namespace testing
692
693 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_PRINTERS_H_
OLDNEW
« no previous file with comments | « testing/gmock/include/gmock/gmock-matchers.h ('k') | testing/gmock/include/gmock/gmock-spec-builders.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698