You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.9 KiB

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