probe.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #pragma once
  2. #include <util/system/yassert.h>
  3. #include <library/cpp/testing/common/probe.h>
  4. #include <gtest/gtest.h>
  5. #include <gmock/gmock.h>
  6. namespace testing {
  7. using NTesting::TProbe;
  8. using NTesting::TProbeState;
  9. using NTesting::TCoercibleToProbe;
  10. // A helper functor which extracts from probe-like objectss their state.
  11. struct TProbableTraits {
  12. static const TProbeState& ExtractState(const TProbeState& probe) {
  13. return probe;
  14. }
  15. static const TProbeState& ExtractState(const TProbeState* probe) {
  16. return *probe;
  17. }
  18. static const TProbeState& ExtractState(const TProbe& probe) {
  19. return *probe.State;
  20. }
  21. static const TProbeState& ExtractState(const TCoercibleToProbe& probe) {
  22. return *probe.State;
  23. }
  24. };
  25. void PrintTo(const TProbeState& state, ::std::ostream* os);
  26. inline void PrintTo(const TProbe& probe, ::std::ostream* os) {
  27. PrintTo(TProbableTraits::ExtractState(probe), os);
  28. }
  29. inline void PrintTo(const TCoercibleToProbe& probe, ::std::ostream* os) {
  30. PrintTo(TProbableTraits::ExtractState(probe), os);
  31. }
  32. MATCHER(IsAlive, "is alive") {
  33. Y_UNUSED(result_listener);
  34. const auto& state = TProbableTraits::ExtractState(arg);
  35. return state.Destructors < state.Constructors + state.CopyConstructors + state.CopyAssignments;
  36. }
  37. MATCHER(IsDead, "is dead") {
  38. Y_UNUSED(result_listener);
  39. const auto& state = TProbableTraits::ExtractState(arg);
  40. return state.Destructors == state.Constructors + state.CopyConstructors + state.CopyAssignments;
  41. }
  42. MATCHER_P2(HasCopyMoveCounts, copyCount, moveCount, "" + \
  43. PrintToString(copyCount) + " copy constructors and " + \
  44. PrintToString(moveCount) + " move constructors were called") {
  45. Y_UNUSED(result_listener);
  46. const auto& state = TProbableTraits::ExtractState(arg);
  47. return state.CopyConstructors == copyCount && state.MoveConstructors == moveCount;
  48. }
  49. MATCHER(NoCopies, "no copies were made") {
  50. Y_UNUSED(result_listener);
  51. const auto& state = TProbableTraits::ExtractState(arg);
  52. return 0 == state.CopyConstructors && 0 == state.CopyAssignments;
  53. }
  54. MATCHER(NoMoves, "no moves were made") {
  55. Y_UNUSED(result_listener);
  56. const auto& state = TProbableTraits::ExtractState(arg);
  57. return 0 == state.MoveConstructors && 0 == state.MoveAssignments;
  58. }
  59. MATCHER(NoAssignments, "no assignments were made") {
  60. Y_UNUSED(result_listener);
  61. const auto& state = TProbableTraits::ExtractState(arg);
  62. return 0 == state.CopyAssignments && 0 == state.MoveAssignments;
  63. }
  64. }