gmock-cardinalities.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 cardinalities.
  32. #include "gmock/gmock-cardinalities.h"
  33. #include <limits.h>
  34. #include <ostream> // NOLINT
  35. #include <sstream>
  36. #include <string>
  37. #include "gmock/internal/gmock-internal-utils.h"
  38. #include "gtest/gtest.h"
  39. namespace testing {
  40. namespace {
  41. // Implements the Between(m, n) cardinality.
  42. class BetweenCardinalityImpl : public CardinalityInterface {
  43. public:
  44. BetweenCardinalityImpl(int min, int max)
  45. : min_(min >= 0 ? min : 0),
  46. max_(max >= min_ ? max : min_) {
  47. std::stringstream ss;
  48. if (min < 0) {
  49. ss << "The invocation lower bound must be >= 0, "
  50. << "but is actually " << min << ".";
  51. internal::Expect(false, __FILE__, __LINE__, ss.str());
  52. } else if (max < 0) {
  53. ss << "The invocation upper bound must be >= 0, "
  54. << "but is actually " << max << ".";
  55. internal::Expect(false, __FILE__, __LINE__, ss.str());
  56. } else if (min > max) {
  57. ss << "The invocation upper bound (" << max
  58. << ") must be >= the invocation lower bound (" << min
  59. << ").";
  60. internal::Expect(false, __FILE__, __LINE__, ss.str());
  61. }
  62. }
  63. // Conservative estimate on the lower/upper bound of the number of
  64. // calls allowed.
  65. virtual int ConservativeLowerBound() const { return min_; }
  66. virtual int ConservativeUpperBound() const { return max_; }
  67. virtual bool IsSatisfiedByCallCount(int call_count) const {
  68. return min_ <= call_count && call_count <= max_;
  69. }
  70. virtual bool IsSaturatedByCallCount(int call_count) const {
  71. return call_count >= max_;
  72. }
  73. virtual void DescribeTo(::std::ostream* os) const;
  74. private:
  75. const int min_;
  76. const int max_;
  77. GTEST_DISALLOW_COPY_AND_ASSIGN_(BetweenCardinalityImpl);
  78. };
  79. // Formats "n times" in a human-friendly way.
  80. inline std::string FormatTimes(int n) {
  81. if (n == 1) {
  82. return "once";
  83. } else if (n == 2) {
  84. return "twice";
  85. } else {
  86. std::stringstream ss;
  87. ss << n << " times";
  88. return ss.str();
  89. }
  90. }
  91. // Describes the Between(m, n) cardinality in human-friendly text.
  92. void BetweenCardinalityImpl::DescribeTo(::std::ostream* os) const {
  93. if (min_ == 0) {
  94. if (max_ == 0) {
  95. *os << "never called";
  96. } else if (max_ == INT_MAX) {
  97. *os << "called any number of times";
  98. } else {
  99. *os << "called at most " << FormatTimes(max_);
  100. }
  101. } else if (min_ == max_) {
  102. *os << "called " << FormatTimes(min_);
  103. } else if (max_ == INT_MAX) {
  104. *os << "called at least " << FormatTimes(min_);
  105. } else {
  106. // 0 < min_ < max_ < INT_MAX
  107. *os << "called between " << min_ << " and " << max_ << " times";
  108. }
  109. }
  110. } // Unnamed namespace
  111. // Describes the given call count to an ostream.
  112. void Cardinality::DescribeActualCallCountTo(int actual_call_count,
  113. ::std::ostream* os) {
  114. if (actual_call_count > 0) {
  115. *os << "called " << FormatTimes(actual_call_count);
  116. } else {
  117. *os << "never called";
  118. }
  119. }
  120. // Creates a cardinality that allows at least n calls.
  121. GTEST_API_ Cardinality AtLeast(int n) { return Between(n, INT_MAX); }
  122. // Creates a cardinality that allows at most n calls.
  123. GTEST_API_ Cardinality AtMost(int n) { return Between(0, n); }
  124. // Creates a cardinality that allows any number of calls.
  125. GTEST_API_ Cardinality AnyNumber() { return AtLeast(0); }
  126. // Creates a cardinality that allows between min and max calls.
  127. GTEST_API_ Cardinality Between(int min, int max) {
  128. return Cardinality(new BetweenCardinalityImpl(min, max));
  129. }
  130. // Creates a cardinality that allows exactly n calls.
  131. GTEST_API_ Cardinality Exactly(int n) { return Between(n, n); }
  132. } // namespace testing