gmock-cardinalities.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // Google Mock - a framework for writing C++ mock classes.
  30. //
  31. // This file implements some commonly used cardinalities. More
  32. // cardinalities can be defined by the user implementing the
  33. // CardinalityInterface interface if necessary.
  34. // IWYU pragma: private, include "gmock/gmock.h"
  35. // IWYU pragma: friend gmock/.*
  36. #ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_
  37. #define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_
  38. #include <limits.h>
  39. #include <memory>
  40. #include <ostream> // NOLINT
  41. #include "gmock/internal/gmock-port.h"
  42. #include "gtest/gtest.h"
  43. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
  44. /* class A needs to have dll-interface to be used by clients of class B */)
  45. namespace testing {
  46. // To implement a cardinality Foo, define:
  47. // 1. a class FooCardinality that implements the
  48. // CardinalityInterface interface, and
  49. // 2. a factory function that creates a Cardinality object from a
  50. // const FooCardinality*.
  51. //
  52. // The two-level delegation design follows that of Matcher, providing
  53. // consistency for extension developers. It also eases ownership
  54. // management as Cardinality objects can now be copied like plain values.
  55. // The implementation of a cardinality.
  56. class CardinalityInterface {
  57. public:
  58. virtual ~CardinalityInterface() {}
  59. // Conservative estimate on the lower/upper bound of the number of
  60. // calls allowed.
  61. virtual int ConservativeLowerBound() const { return 0; }
  62. virtual int ConservativeUpperBound() const { return INT_MAX; }
  63. // Returns true if and only if call_count calls will satisfy this
  64. // cardinality.
  65. virtual bool IsSatisfiedByCallCount(int call_count) const = 0;
  66. // Returns true if and only if call_count calls will saturate this
  67. // cardinality.
  68. virtual bool IsSaturatedByCallCount(int call_count) const = 0;
  69. // Describes self to an ostream.
  70. virtual void DescribeTo(::std::ostream* os) const = 0;
  71. };
  72. // A Cardinality is a copyable and IMMUTABLE (except by assignment)
  73. // object that specifies how many times a mock function is expected to
  74. // be called. The implementation of Cardinality is just a std::shared_ptr
  75. // to const CardinalityInterface. Don't inherit from Cardinality!
  76. class GTEST_API_ Cardinality {
  77. public:
  78. // Constructs a null cardinality. Needed for storing Cardinality
  79. // objects in STL containers.
  80. Cardinality() {}
  81. // Constructs a Cardinality from its implementation.
  82. explicit Cardinality(const CardinalityInterface* impl) : impl_(impl) {}
  83. // Conservative estimate on the lower/upper bound of the number of
  84. // calls allowed.
  85. int ConservativeLowerBound() const { return impl_->ConservativeLowerBound(); }
  86. int ConservativeUpperBound() const { return impl_->ConservativeUpperBound(); }
  87. // Returns true if and only if call_count calls will satisfy this
  88. // cardinality.
  89. bool IsSatisfiedByCallCount(int call_count) const {
  90. return impl_->IsSatisfiedByCallCount(call_count);
  91. }
  92. // Returns true if and only if call_count calls will saturate this
  93. // cardinality.
  94. bool IsSaturatedByCallCount(int call_count) const {
  95. return impl_->IsSaturatedByCallCount(call_count);
  96. }
  97. // Returns true if and only if call_count calls will over-saturate this
  98. // cardinality, i.e. exceed the maximum number of allowed calls.
  99. bool IsOverSaturatedByCallCount(int call_count) const {
  100. return impl_->IsSaturatedByCallCount(call_count) &&
  101. !impl_->IsSatisfiedByCallCount(call_count);
  102. }
  103. // Describes self to an ostream
  104. void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); }
  105. // Describes the given actual call count to an ostream.
  106. static void DescribeActualCallCountTo(int actual_call_count,
  107. ::std::ostream* os);
  108. private:
  109. std::shared_ptr<const CardinalityInterface> impl_;
  110. };
  111. // Creates a cardinality that allows at least n calls.
  112. GTEST_API_ Cardinality AtLeast(int n);
  113. // Creates a cardinality that allows at most n calls.
  114. GTEST_API_ Cardinality AtMost(int n);
  115. // Creates a cardinality that allows any number of calls.
  116. GTEST_API_ Cardinality AnyNumber();
  117. // Creates a cardinality that allows between min and max calls.
  118. GTEST_API_ Cardinality Between(int min, int max);
  119. // Creates a cardinality that allows exactly n calls.
  120. GTEST_API_ Cardinality Exactly(int n);
  121. // Creates a cardinality from its implementation.
  122. inline Cardinality MakeCardinality(const CardinalityInterface* c) {
  123. return Cardinality(c);
  124. }
  125. } // namespace testing
  126. GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251
  127. #endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_