gmock-cardinalities.h 5.7 KB

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