gmock-cardinalities.h 5.8 KB

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