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
31// Google Mock - a framework for writing C++ mock classes.
32//
33// This file defines some utilities useful for implementing Google
34// Mock. They are subject to change without notice, so please DO NOT
35// USE THEM IN USER CODE.
36
37// GOOGLETEST_CM0002 DO NOT DELETE
38
39#ifndef GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_
40#define GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_
41
42#include <stdio.h>
43#include <ostream> // NOLINT
44#include <string>
45#include <type_traits>
46#include "gmock/internal/gmock-port.h"
47#include "gtest/gtest.h"
48
49namespace testing {
50
51template <typename>
52class Matcher;
53
54namespace internal {
55
56// Silence MSVC C4100 (unreferenced formal parameter) and
57// C4805('==': unsafe mix of type 'const int' and type 'const bool')
58#ifdef _MSC_VER
59# pragma warning(push)
60# pragma warning(disable:4100)
61# pragma warning(disable:4805)
62#endif
63
64// Joins a vector of strings as if they are fields of a tuple; returns
65// the joined string.
66GTEST_API_ std::string JoinAsTuple(const Strings& fields);
67
68// Converts an identifier name to a space-separated list of lower-case
69// words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is
70// treated as one word. For example, both "FooBar123" and
71// "foo_bar_123" are converted to "foo bar 123".
72GTEST_API_ std::string ConvertIdentifierNameToWords(const char* id_name);
73
74// PointeeOf<Pointer>::type is the type of a value pointed to by a
75// Pointer, which can be either a smart pointer or a raw pointer. The
76// following default implementation is for the case where Pointer is a
77// smart pointer.
78template <typename Pointer>
79struct PointeeOf {
80 // Smart pointer classes define type element_type as the type of
81 // their pointees.
82 typedef typename Pointer::element_type type;
83};
84// This specialization is for the raw pointer case.
85template <typename T>
86struct PointeeOf<T*> { typedef T type; }; // NOLINT
87
88// GetRawPointer(p) returns the raw pointer underlying p when p is a
89// smart pointer, or returns p itself when p is already a raw pointer.
90// The following default implementation is for the smart pointer case.
91template <typename Pointer>
92inline const typename Pointer::element_type* GetRawPointer(const Pointer& p) {
93 return p.get();
94}
95// This overloaded version is for the raw pointer case.
96template <typename Element>
97inline Element* GetRawPointer(Element* p) { return p; }
98
99// MSVC treats wchar_t as a native type usually, but treats it as the
100// same as unsigned short when the compiler option /Zc:wchar_t- is
101// specified. It defines _NATIVE_WCHAR_T_DEFINED symbol when wchar_t
102// is a native type.
103#if defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED)
104// wchar_t is a typedef.
105#else
106# define GMOCK_WCHAR_T_IS_NATIVE_ 1
107#endif
108
109// In what follows, we use the term "kind" to indicate whether a type
110// is bool, an integer type (excluding bool), a floating-point type,
111// or none of them. This categorization is useful for determining
112// when a matcher argument type can be safely converted to another
113// type in the implementation of SafeMatcherCast.
114enum TypeKind {
115 kBool, kInteger, kFloatingPoint, kOther
116};
117
118// KindOf<T>::value is the kind of type T.
119template <typename T> struct KindOf {
120 enum { value = kOther }; // The default kind.
121};
122
123// This macro declares that the kind of 'type' is 'kind'.
124#define GMOCK_DECLARE_KIND_(type, kind) \
125 template <> struct KindOf<type> { enum { value = kind }; }
126
127GMOCK_DECLARE_KIND_(bool, kBool);
128
129// All standard integer types.
130GMOCK_DECLARE_KIND_(char, kInteger);
131GMOCK_DECLARE_KIND_(signed char, kInteger);
132GMOCK_DECLARE_KIND_(unsigned char, kInteger);
133GMOCK_DECLARE_KIND_(short, kInteger); // NOLINT
134GMOCK_DECLARE_KIND_(unsigned short, kInteger); // NOLINT
135GMOCK_DECLARE_KIND_(int, kInteger);
136GMOCK_DECLARE_KIND_(unsigned int, kInteger);
137GMOCK_DECLARE_KIND_(long, kInteger); // NOLINT
138GMOCK_DECLARE_KIND_(unsigned long, kInteger); // NOLINT
139
140#if GMOCK_WCHAR_T_IS_NATIVE_
141GMOCK_DECLARE_KIND_(wchar_t, kInteger);
142#endif
143
144// Non-standard integer types.
145GMOCK_DECLARE_KIND_(Int64, kInteger);
146GMOCK_DECLARE_KIND_(UInt64, kInteger);
147
148// All standard floating-point types.
149GMOCK_DECLARE_KIND_(float, kFloatingPoint);
150GMOCK_DECLARE_KIND_(double, kFloatingPoint);
151GMOCK_DECLARE_KIND_(long double, kFloatingPoint);
152
153#undef GMOCK_DECLARE_KIND_
154
155// Evaluates to the kind of 'type'.
156#define GMOCK_KIND_OF_(type) \
157 static_cast< ::testing::internal::TypeKind>( \
158 ::testing::internal::KindOf<type>::value)
159
160// Evaluates to true if and only if integer type T is signed.
161#define GMOCK_IS_SIGNED_(T) (static_cast<T>(-1) < 0)
162
163// LosslessArithmeticConvertibleImpl<kFromKind, From, kToKind, To>::value
164// is true if and only if arithmetic type From can be losslessly converted to
165// arithmetic type To.
166//
167// It's the user's responsibility to ensure that both From and To are
168// raw (i.e. has no CV modifier, is not a pointer, and is not a
169// reference) built-in arithmetic types, kFromKind is the kind of
170// From, and kToKind is the kind of To; the value is
171// implementation-defined when the above pre-condition is violated.
172template <TypeKind kFromKind, typename From, TypeKind kToKind, typename To>
173struct LosslessArithmeticConvertibleImpl : public std::false_type {};
174
175// Converting bool to bool is lossless.
176template <>
177struct LosslessArithmeticConvertibleImpl<kBool, bool, kBool, bool>
178 : public std::true_type {};
179
180// Converting bool to any integer type is lossless.
181template <typename To>
182struct LosslessArithmeticConvertibleImpl<kBool, bool, kInteger, To>
183 : public std::true_type {};
184
185// Converting bool to any floating-point type is lossless.
186template <typename To>
187struct LosslessArithmeticConvertibleImpl<kBool, bool, kFloatingPoint, To>
188 : public std::true_type {};
189
190// Converting an integer to bool is lossy.
191template <typename From>
192struct LosslessArithmeticConvertibleImpl<kInteger, From, kBool, bool>
193 : public std::false_type {};
194
195// Converting an integer to another non-bool integer is lossless
196// if and only if the target type's range encloses the source type's range.
197template <typename From, typename To>
198struct LosslessArithmeticConvertibleImpl<kInteger, From, kInteger, To>
199 : public bool_constant<
200 // When converting from a smaller size to a larger size, we are
201 // fine as long as we are not converting from signed to unsigned.
202 ((sizeof(From) < sizeof(To)) &&
203 (!GMOCK_IS_SIGNED_(From) || GMOCK_IS_SIGNED_(To))) ||
204 // When converting between the same size, the signedness must match.
205 ((sizeof(From) == sizeof(To)) &&
206 (GMOCK_IS_SIGNED_(From) == GMOCK_IS_SIGNED_(To)))> {}; // NOLINT
207
208#undef GMOCK_IS_SIGNED_
209
210// Converting an integer to a floating-point type may be lossy, since
211// the format of a floating-point number is implementation-defined.
212template <typename From, typename To>
213struct LosslessArithmeticConvertibleImpl<kInteger, From, kFloatingPoint, To>
214 : public std::false_type {};
215
216// Converting a floating-point to bool is lossy.
217template <typename From>
218struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kBool, bool>
219 : public std::false_type {};
220
221// Converting a floating-point to an integer is lossy.
222template <typename From, typename To>
223struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kInteger, To>
224 : public std::false_type {};
225
226// Converting a floating-point to another floating-point is lossless
227// if and only if the target type is at least as big as the source type.
228template <typename From, typename To>
229struct LosslessArithmeticConvertibleImpl<
230 kFloatingPoint, From, kFloatingPoint, To>
231 : public bool_constant<sizeof(From) <= sizeof(To)> {}; // NOLINT
232
233// LosslessArithmeticConvertible<From, To>::value is true if and only if
234// arithmetic type From can be losslessly converted to arithmetic type To.
235//
236// It's the user's responsibility to ensure that both From and To are
237// raw (i.e. has no CV modifier, is not a pointer, and is not a
238// reference) built-in arithmetic types; the value is
239// implementation-defined when the above pre-condition is violated.
240template <typename From, typename To>
241struct LosslessArithmeticConvertible
242 : public LosslessArithmeticConvertibleImpl<
243 GMOCK_KIND_OF_(From), From, GMOCK_KIND_OF_(To), To> {}; // NOLINT
244
245// This interface knows how to report a Google Mock failure (either
246// non-fatal or fatal).
247class FailureReporterInterface {
248 public:
249 // The type of a failure (either non-fatal or fatal).
250 enum FailureType {
251 kNonfatal, kFatal
252 };
253
254 virtual ~FailureReporterInterface() {}
255
256 // Reports a failure that occurred at the given source file location.
257 virtual void ReportFailure(FailureType type, const char* file, int line,
258 const std::string& message) = 0;
259};
260
261// Returns the failure reporter used by Google Mock.
262GTEST_API_ FailureReporterInterface* GetFailureReporter();
263
264// Asserts that condition is true; aborts the process with the given
265// message if condition is false. We cannot use LOG(FATAL) or CHECK()
266// as Google Mock might be used to mock the log sink itself. We
267// inline this function to prevent it from showing up in the stack
268// trace.
269inline void Assert(bool condition, const char* file, int line,
270 const std::string& msg) {
271 if (!condition) {
272 GetFailureReporter()->ReportFailure(FailureReporterInterface::kFatal,
273 file, line, msg);
274 }
275}
276inline void Assert(bool condition, const char* file, int line) {
277 Assert(condition, file, line, "Assertion failed.");
278}
279
280// Verifies that condition is true; generates a non-fatal failure if
281// condition is false.
282inline void Expect(bool condition, const char* file, int line,
283 const std::string& msg) {
284 if (!condition) {
285 GetFailureReporter()->ReportFailure(FailureReporterInterface::kNonfatal,
286 file, line, msg);
287 }
288}
289inline void Expect(bool condition, const char* file, int line) {
290 Expect(condition, file, line, "Expectation failed.");
291}
292
293// Severity level of a log.
294enum LogSeverity {
295 kInfo = 0,
296 kWarning = 1
297};
298
299// Valid values for the --gmock_verbose flag.
300
301// All logs (informational and warnings) are printed.
302const char kInfoVerbosity[] = "info";
303// Only warnings are printed.
304const char kWarningVerbosity[] = "warning";
305// No logs are printed.
306const char kErrorVerbosity[] = "error";
307
308// Returns true if and only if a log with the given severity is visible
309// according to the --gmock_verbose flag.
310GTEST_API_ bool LogIsVisible(LogSeverity severity);
311
312// Prints the given message to stdout if and only if 'severity' >= the level
313// specified by the --gmock_verbose flag. If stack_frames_to_skip >=
314// 0, also prints the stack trace excluding the top
315// stack_frames_to_skip frames. In opt mode, any positive
316// stack_frames_to_skip is treated as 0, since we don't know which
317// function calls will be inlined by the compiler and need to be
318// conservative.
319GTEST_API_ void Log(LogSeverity severity, const std::string& message,
320 int stack_frames_to_skip);
321
322// A marker class that is used to resolve parameterless expectations to the
323// correct overload. This must not be instantiable, to prevent client code from
324// accidentally resolving to the overload; for example:
325//
326// ON_CALL(mock, Method({}, nullptr))...
327//
328class WithoutMatchers {
329 private:
330 WithoutMatchers() {}
331 friend GTEST_API_ WithoutMatchers GetWithoutMatchers();
332};
333
334// Internal use only: access the singleton instance of WithoutMatchers.
335GTEST_API_ WithoutMatchers GetWithoutMatchers();
336
337// Type traits.
338
339// Disable MSVC warnings for infinite recursion, since in this case the
340// the recursion is unreachable.
341#ifdef _MSC_VER
342# pragma warning(push)
343# pragma warning(disable:4717)
344#endif
345
346// Invalid<T>() is usable as an expression of type T, but will terminate
347// the program with an assertion failure if actually run. This is useful
348// when a value of type T is needed for compilation, but the statement
349// will not really be executed (or we don't care if the statement
350// crashes).
351template <typename T>
352inline T Invalid() {
353 Assert(false, "", -1, "Internal error: attempt to return invalid value");
354 // This statement is unreachable, and would never terminate even if it
355 // could be reached. It is provided only to placate compiler warnings
356 // about missing return statements.
357 return Invalid<T>();
358}
359
360#ifdef _MSC_VER
361# pragma warning(pop)
362#endif
363
364// Given a raw type (i.e. having no top-level reference or const
365// modifier) RawContainer that's either an STL-style container or a
366// native array, class StlContainerView<RawContainer> has the
367// following members:
368//
369// - type is a type that provides an STL-style container view to
370// (i.e. implements the STL container concept for) RawContainer;
371// - const_reference is a type that provides a reference to a const
372// RawContainer;
373// - ConstReference(raw_container) returns a const reference to an STL-style
374// container view to raw_container, which is a RawContainer.
375// - Copy(raw_container) returns an STL-style container view of a
376// copy of raw_container, which is a RawContainer.
377//
378// This generic version is used when RawContainer itself is already an
379// STL-style container.
380template <class RawContainer>
381class StlContainerView {
382 public:
383 typedef RawContainer type;
384 typedef const type& const_reference;
385
386 static const_reference ConstReference(const RawContainer& container) {
387 static_assert(!std::is_const<RawContainer>::value,
388 "RawContainer type must not be const");
389 return container;
390 }
391 static type Copy(const RawContainer& container) { return container; }
392};
393
394// This specialization is used when RawContainer is a native array type.
395template <typename Element, size_t N>
396class StlContainerView<Element[N]> {
397 public:
398 typedef typename std::remove_const<Element>::type RawElement;
399 typedef internal::NativeArray<RawElement> type;
400 // NativeArray<T> can represent a native array either by value or by
401 // reference (selected by a constructor argument), so 'const type'
402 // can be used to reference a const native array. We cannot
403 // 'typedef const type& const_reference' here, as that would mean
404 // ConstReference() has to return a reference to a local variable.
405 typedef const type const_reference;
406
407 static const_reference ConstReference(const Element (&array)[N]) {
408 static_assert(std::is_same<Element, RawElement>::value,
409 "Element type must not be const");
410 return type(array, N, RelationToSourceReference());
411 }
412 static type Copy(const Element (&array)[N]) {
413 return type(array, N, RelationToSourceCopy());
414 }
415};
416
417// This specialization is used when RawContainer is a native array
418// represented as a (pointer, size) tuple.
419template <typename ElementPointer, typename Size>
420class StlContainerView< ::std::tuple<ElementPointer, Size> > {
421 public:
422 typedef typename std::remove_const<
423 typename internal::PointeeOf<ElementPointer>::type>::type RawElement;
424 typedef internal::NativeArray<RawElement> type;
425 typedef const type const_reference;
426
427 static const_reference ConstReference(
428 const ::std::tuple<ElementPointer, Size>& array) {
429 return type(std::get<0>(array), std::get<1>(array),
430 RelationToSourceReference());
431 }
432 static type Copy(const ::std::tuple<ElementPointer, Size>& array) {
433 return type(std::get<0>(array), std::get<1>(array), RelationToSourceCopy());
434 }
435};
436
437// The following specialization prevents the user from instantiating
438// StlContainer with a reference type.
439template <typename T> class StlContainerView<T&>;
440
441// A type transform to remove constness from the first part of a pair.
442// Pairs like that are used as the value_type of associative containers,
443// and this transform produces a similar but assignable pair.
444template <typename T>
445struct RemoveConstFromKey {
446 typedef T type;
447};
448
449// Partially specialized to remove constness from std::pair<const K, V>.
450template <typename K, typename V>
451struct RemoveConstFromKey<std::pair<const K, V> > {
452 typedef std::pair<K, V> type;
453};
454
455// Emit an assertion failure due to incorrect DoDefault() usage. Out-of-lined to
456// reduce code size.
457GTEST_API_ void IllegalDoDefault(const char* file, int line);
458
459template <typename F, typename Tuple, size_t... Idx>
460auto ApplyImpl(F&& f, Tuple&& args, IndexSequence<Idx...>) -> decltype(
461 std::forward<F>(f)(std::get<Idx>(std::forward<Tuple>(args))...)) {
462 return std::forward<F>(f)(std::get<Idx>(std::forward<Tuple>(args))...);
463}
464
465// Apply the function to a tuple of arguments.
466template <typename F, typename Tuple>
467auto Apply(F&& f, Tuple&& args)
468 -> decltype(ApplyImpl(std::forward<F>(f), std::forward<Tuple>(args),
469 MakeIndexSequence<std::tuple_size<Tuple>::value>())) {
470 return ApplyImpl(std::forward<F>(f), std::forward<Tuple>(args),
471 MakeIndexSequence<std::tuple_size<Tuple>::value>());
472}
473
474// Template struct Function<F>, where F must be a function type, contains
475// the following typedefs:
476//
477// Result: the function's return type.
478// Arg<N>: the type of the N-th argument, where N starts with 0.
479// ArgumentTuple: the tuple type consisting of all parameters of F.
480// ArgumentMatcherTuple: the tuple type consisting of Matchers for all
481// parameters of F.
482// MakeResultVoid: the function type obtained by substituting void
483// for the return type of F.
484// MakeResultIgnoredValue:
485// the function type obtained by substituting Something
486// for the return type of F.
487template <typename T>
488struct Function;
489
490template <typename R, typename... Args>
491struct Function<R(Args...)> {
492 using Result = R;
493 static constexpr size_t ArgumentCount = sizeof...(Args);
494 template <size_t I>
495 using Arg = ElemFromList<I, typename MakeIndexSequence<sizeof...(Args)>::type,
496 Args...>;
497 using ArgumentTuple = std::tuple<Args...>;
498 using ArgumentMatcherTuple = std::tuple<Matcher<Args>...>;
499 using MakeResultVoid = void(Args...);
500 using MakeResultIgnoredValue = IgnoredValue(Args...);
501};
502
503template <typename R, typename... Args>
504constexpr size_t Function<R(Args...)>::ArgumentCount;
505
506#ifdef _MSC_VER
507# pragma warning(pop)
508#endif
509
510} // namespace internal
511} // namespace testing
512
513#endif // GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_INTERNAL_UTILS_H_
514