| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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> |
| 44 |
#include <string> |
| 45 |
#include <type_traits> |
| 46 |
#include "gmock/internal/gmock-port.h" |
| 47 |
#include "gtest/gtest.h" |
| 48 |
|
| 49 |
namespace testing { |
| 50 |
|
| 51 |
template <typename> |
| 52 |
class Matcher; |
| 53 |
|
| 54 |
namespace internal { |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
#ifdef _MSC_VER |
| 59 |
# pragma warning(push) |
| 60 |
# pragma warning(disable:4100) |
| 61 |
# pragma warning(disable:4805) |
| 62 |
#endif |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
GTEST_API_ std::string JoinAsTuple(const Strings& fields); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
GTEST_API_ std::string ConvertIdentifierNameToWords(const char* id_name); |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
template <typename Pointer> |
| 79 |
struct PointeeOf { |
| 80 |
|
| 81 |
|
| 82 |
typedef typename Pointer::element_type type; |
| 83 |
}; |
| 84 |
|
| 85 |
template <typename T> |
| 86 |
struct PointeeOf<T*> { typedef T type; }; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
template <typename Pointer> |
| 92 |
inline const typename Pointer::element_type* GetRawPointer(const Pointer& p) { |
| 93 |
return p.get(); |
| 94 |
} |
| 95 |
|
| 96 |
template <typename Element> |
| 97 |
inline Element* GetRawPointer(Element* p) { return p; } |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
#if defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED) |
| 104 |
|
| 105 |
#else |
| 106 |
# define GMOCK_WCHAR_T_IS_NATIVE_ 1 |
| 107 |
#endif |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
enum TypeKind { |
| 115 |
kBool, kInteger, kFloatingPoint, kOther |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
template <typename T> struct KindOf { |
| 120 |
enum { value = kOther }; |
| 121 |
}; |
| 122 |
|
| 123 |
|
| 124 |
#define GMOCK_DECLARE_KIND_(type, kind) \ |
| 125 |
template <> struct KindOf<type> { enum { value = kind }; } |
| 126 |
|
| 127 |
GMOCK_DECLARE_KIND_(bool, kBool); |
| 128 |
|
| 129 |
|
| 130 |
GMOCK_DECLARE_KIND_(char, kInteger); |
| 131 |
GMOCK_DECLARE_KIND_(signed char, kInteger); |
| 132 |
GMOCK_DECLARE_KIND_(unsigned char, kInteger); |
| 133 |
GMOCK_DECLARE_KIND_(short, kInteger); |
| 134 |
GMOCK_DECLARE_KIND_(unsigned short, kInteger); |
| 135 |
GMOCK_DECLARE_KIND_(int, kInteger); |
| 136 |
GMOCK_DECLARE_KIND_(unsigned int, kInteger); |
| 137 |
GMOCK_DECLARE_KIND_(long, kInteger); |
| 138 |
GMOCK_DECLARE_KIND_(unsigned long, kInteger); |
| 139 |
|
| 140 |
#if GMOCK_WCHAR_T_IS_NATIVE_ |
| 141 |
GMOCK_DECLARE_KIND_(wchar_t, kInteger); |
| 142 |
#endif |
| 143 |
|
| 144 |
|
| 145 |
GMOCK_DECLARE_KIND_(Int64, kInteger); |
| 146 |
GMOCK_DECLARE_KIND_(UInt64, kInteger); |
| 147 |
|
| 148 |
|
| 149 |
GMOCK_DECLARE_KIND_(float, kFloatingPoint); |
| 150 |
GMOCK_DECLARE_KIND_(double, kFloatingPoint); |
| 151 |
GMOCK_DECLARE_KIND_(long double, kFloatingPoint); |
| 152 |
|
| 153 |
#undef GMOCK_DECLARE_KIND_ |
| 154 |
|
| 155 |
|
| 156 |
#define GMOCK_KIND_OF_(type) \ |
| 157 |
static_cast< ::testing::internal::TypeKind>( \ |
| 158 |
::testing::internal::KindOf<type>::value) |
| 159 |
|
| 160 |
|
| 161 |
#define GMOCK_IS_SIGNED_(T) (static_cast<T>(-1) < 0) |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
template <TypeKind kFromKind, typename From, TypeKind kToKind, typename To> |
| 173 |
struct LosslessArithmeticConvertibleImpl : public std::false_type {}; |
| 174 |
|
| 175 |
|
| 176 |
template <> |
| 177 |
struct LosslessArithmeticConvertibleImpl<kBool, bool, kBool, bool> |
| 178 |
: public std::true_type {}; |
| 179 |
|
| 180 |
|
| 181 |
template <typename To> |
| 182 |
struct LosslessArithmeticConvertibleImpl<kBool, bool, kInteger, To> |
| 183 |
: public std::true_type {}; |
| 184 |
|
| 185 |
|
| 186 |
template <typename To> |
| 187 |
struct LosslessArithmeticConvertibleImpl<kBool, bool, kFloatingPoint, To> |
| 188 |
: public std::true_type {}; |
| 189 |
|
| 190 |
|
| 191 |
template <typename From> |
| 192 |
struct LosslessArithmeticConvertibleImpl<kInteger, From, kBool, bool> |
| 193 |
: public std::false_type {}; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
template <typename From, typename To> |
| 198 |
struct LosslessArithmeticConvertibleImpl<kInteger, From, kInteger, To> |
| 199 |
: public bool_constant< |
| 200 |
|
| 201 |
|
| 202 |
((sizeof(From) < sizeof(To)) && |
| 203 |
(!GMOCK_IS_SIGNED_(From) || GMOCK_IS_SIGNED_(To))) || |
| 204 |
|
| 205 |
((sizeof(From) == sizeof(To)) && |
| 206 |
(GMOCK_IS_SIGNED_(From) == GMOCK_IS_SIGNED_(To)))> {}; |
| 207 |
|
| 208 |
#undef GMOCK_IS_SIGNED_ |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
template <typename From, typename To> |
| 213 |
struct LosslessArithmeticConvertibleImpl<kInteger, From, kFloatingPoint, To> |
| 214 |
: public std::false_type {}; |
| 215 |
|
| 216 |
|
| 217 |
template <typename From> |
| 218 |
struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kBool, bool> |
| 219 |
: public std::false_type {}; |
| 220 |
|
| 221 |
|
| 222 |
template <typename From, typename To> |
| 223 |
struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kInteger, To> |
| 224 |
: public std::false_type {}; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
template <typename From, typename To> |
| 229 |
struct LosslessArithmeticConvertibleImpl< |
| 230 |
kFloatingPoint, From, kFloatingPoint, To> |
| 231 |
: public bool_constant<sizeof(From) <= sizeof(To)> {}; |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
template <typename From, typename To> |
| 241 |
struct LosslessArithmeticConvertible |
| 242 |
: public LosslessArithmeticConvertibleImpl< |
| 243 |
GMOCK_KIND_OF_(From), From, GMOCK_KIND_OF_(To), To> {}; |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
class FailureReporterInterface { |
| 248 |
public: |
| 249 |
|
| 250 |
enum FailureType { |
| 251 |
kNonfatal, kFatal |
| 252 |
}; |
| 253 |
|
| 254 |
virtual ~FailureReporterInterface() {} |
| 255 |
|
| 256 |
|
| 257 |
virtual void ReportFailure(FailureType type, const char* file, int line, |
| 258 |
const std::string& message) = 0; |
| 259 |
}; |
| 260 |
|
| 261 |
|
| 262 |
GTEST_API_ FailureReporterInterface* GetFailureReporter(); |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
inline 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 |
} |
| 276 |
inline void Assert(bool condition, const char* file, int line) { |
| 277 |
Assert(condition, file, line, "Assertion failed."); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
inline 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 |
} |
| 289 |
inline void Expect(bool condition, const char* file, int line) { |
| 290 |
Expect(condition, file, line, "Expectation failed."); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
enum LogSeverity { |
| 295 |
kInfo = 0, |
| 296 |
kWarning = 1 |
| 297 |
}; |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
const char kInfoVerbosity[] = "info"; |
| 303 |
|
| 304 |
const char kWarningVerbosity[] = "warning"; |
| 305 |
|
| 306 |
const char kErrorVerbosity[] = "error"; |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
GTEST_API_ bool LogIsVisible(LogSeverity severity); |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
GTEST_API_ void Log(LogSeverity severity, const std::string& message, |
| 320 |
int stack_frames_to_skip); |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
class WithoutMatchers { |
| 329 |
private: |
| 330 |
WithoutMatchers() {} |
| 331 |
friend GTEST_API_ WithoutMatchers GetWithoutMatchers(); |
| 332 |
}; |
| 333 |
|
| 334 |
|
| 335 |
GTEST_API_ WithoutMatchers GetWithoutMatchers(); |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
#ifdef _MSC_VER |
| 342 |
# pragma warning(push) |
| 343 |
# pragma warning(disable:4717) |
| 344 |
#endif |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
template <typename T> |
| 352 |
inline T Invalid() { |
| 353 |
Assert(false, "", -1, "Internal error: attempt to return invalid value"); |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
return Invalid<T>(); |
| 358 |
} |
| 359 |
|
| 360 |
#ifdef _MSC_VER |
| 361 |
# pragma warning(pop) |
| 362 |
#endif |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
template <class RawContainer> |
| 381 |
class 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 |
|
| 395 |
template <typename Element, size_t N> |
| 396 |
class StlContainerView<Element[N]> { |
| 397 |
public: |
| 398 |
typedef typename std::remove_const<Element>::type RawElement; |
| 399 |
typedef internal::NativeArray<RawElement> type; |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 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 |
|
| 418 |
|
| 419 |
template <typename ElementPointer, typename Size> |
| 420 |
class 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 |
|
| 438 |
|
| 439 |
template <typename T> class StlContainerView<T&>; |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
template <typename T> |
| 445 |
struct RemoveConstFromKey { |
| 446 |
typedef T type; |
| 447 |
}; |
| 448 |
|
| 449 |
|
| 450 |
template <typename K, typename V> |
| 451 |
struct RemoveConstFromKey<std::pair<const K, V> > { |
| 452 |
typedef std::pair<K, V> type; |
| 453 |
}; |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
GTEST_API_ void IllegalDoDefault(const char* file, int line); |
| 458 |
|
| 459 |
template <typename F, typename Tuple, size_t... Idx> |
| 460 |
auto 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 |
|
| 466 |
template <typename F, typename Tuple> |
| 467 |
auto 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 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
template <typename T> |
| 488 |
struct Function; |
| 489 |
|
| 490 |
template <typename R, typename... Args> |
| 491 |
struct 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 |
|
| 503 |
template <typename R, typename... Args> |
| 504 |
constexpr size_t Function<R(Args...)>::ArgumentCount; |
| 505 |
|
| 506 |
#ifdef _MSC_VER |
| 507 |
# pragma warning(pop) |
| 508 |
#endif |
| 509 |
|
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
#endif |