gtest_pred_impl.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright 2006, 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. // Implements a family of generic predicate assertion macros.
  31. // IWYU pragma: private, include "gtest/gtest.h"
  32. // IWYU pragma: friend gtest/.*
  33. // IWYU pragma: friend gmock/.*
  34. #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_
  35. #define GOOGLETEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_
  36. #include "gtest/gtest-assertion-result.h"
  37. #include "gtest/internal/gtest-internal.h"
  38. #include "gtest/internal/gtest-port.h"
  39. namespace testing {
  40. // This header implements a family of generic predicate assertion
  41. // macros:
  42. //
  43. // ASSERT_PRED_FORMAT1(pred_format, v1)
  44. // ASSERT_PRED_FORMAT2(pred_format, v1, v2)
  45. // ...
  46. //
  47. // where pred_format is a function or functor that takes n (in the
  48. // case of ASSERT_PRED_FORMATn) values and their source expression
  49. // text, and returns a testing::AssertionResult. See the definition
  50. // of ASSERT_EQ in gtest.h for an example.
  51. //
  52. // If you don't care about formatting, you can use the more
  53. // restrictive version:
  54. //
  55. // ASSERT_PRED1(pred, v1)
  56. // ASSERT_PRED2(pred, v1, v2)
  57. // ...
  58. //
  59. // where pred is an n-ary function or functor that returns bool,
  60. // and the values v1, v2, ..., must support the << operator for
  61. // streaming to std::ostream.
  62. //
  63. // We also define the EXPECT_* variations.
  64. //
  65. // For now we only support predicates whose arity is at most 5.
  66. // Please email googletestframework@googlegroups.com if you need
  67. // support for higher arities.
  68. // GTEST_ASSERT_ is the basic statement to which all of the assertions
  69. // in this file reduce. Don't use this in your code.
  70. #define GTEST_ASSERT_(expression, on_failure) \
  71. GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
  72. if (const ::testing::AssertionResult gtest_ar = (expression)) \
  73. ; \
  74. else \
  75. on_failure(gtest_ar.failure_message())
  76. // Helper function for implementing {EXPECT|ASSERT}_PRED1. Don't use
  77. // this in your code.
  78. template <typename Pred, typename T1>
  79. AssertionResult AssertPred1Helper(const char* pred_text, const char* e1,
  80. Pred pred, const T1& v1) {
  81. if (pred(v1)) return AssertionSuccess();
  82. return AssertionFailure()
  83. << pred_text << "(" << e1 << ") evaluates to false, where"
  84. << "\n"
  85. << e1 << " evaluates to " << ::testing::PrintToString(v1);
  86. }
  87. // Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT1.
  88. // Don't use this in your code.
  89. #define GTEST_PRED_FORMAT1_(pred_format, v1, on_failure) \
  90. GTEST_ASSERT_(pred_format(#v1, v1), on_failure)
  91. // Internal macro for implementing {EXPECT|ASSERT}_PRED1. Don't use
  92. // this in your code.
  93. #define GTEST_PRED1_(pred, v1, on_failure) \
  94. GTEST_ASSERT_(::testing::AssertPred1Helper(#pred, #v1, pred, v1), on_failure)
  95. // Unary predicate assertion macros.
  96. #define EXPECT_PRED_FORMAT1(pred_format, v1) \
  97. GTEST_PRED_FORMAT1_(pred_format, v1, GTEST_NONFATAL_FAILURE_)
  98. #define EXPECT_PRED1(pred, v1) GTEST_PRED1_(pred, v1, GTEST_NONFATAL_FAILURE_)
  99. #define ASSERT_PRED_FORMAT1(pred_format, v1) \
  100. GTEST_PRED_FORMAT1_(pred_format, v1, GTEST_FATAL_FAILURE_)
  101. #define ASSERT_PRED1(pred, v1) GTEST_PRED1_(pred, v1, GTEST_FATAL_FAILURE_)
  102. // Helper function for implementing {EXPECT|ASSERT}_PRED2. Don't use
  103. // this in your code.
  104. template <typename Pred, typename T1, typename T2>
  105. AssertionResult AssertPred2Helper(const char* pred_text, const char* e1,
  106. const char* e2, Pred pred, const T1& v1,
  107. const T2& v2) {
  108. if (pred(v1, v2)) return AssertionSuccess();
  109. return AssertionFailure()
  110. << pred_text << "(" << e1 << ", " << e2
  111. << ") evaluates to false, where"
  112. << "\n"
  113. << e1 << " evaluates to " << ::testing::PrintToString(v1) << "\n"
  114. << e2 << " evaluates to " << ::testing::PrintToString(v2);
  115. }
  116. // Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT2.
  117. // Don't use this in your code.
  118. #define GTEST_PRED_FORMAT2_(pred_format, v1, v2, on_failure) \
  119. GTEST_ASSERT_(pred_format(#v1, #v2, v1, v2), on_failure)
  120. // Internal macro for implementing {EXPECT|ASSERT}_PRED2. Don't use
  121. // this in your code.
  122. #define GTEST_PRED2_(pred, v1, v2, on_failure) \
  123. GTEST_ASSERT_(::testing::AssertPred2Helper(#pred, #v1, #v2, pred, v1, v2), \
  124. on_failure)
  125. // Binary predicate assertion macros.
  126. #define EXPECT_PRED_FORMAT2(pred_format, v1, v2) \
  127. GTEST_PRED_FORMAT2_(pred_format, v1, v2, GTEST_NONFATAL_FAILURE_)
  128. #define EXPECT_PRED2(pred, v1, v2) \
  129. GTEST_PRED2_(pred, v1, v2, GTEST_NONFATAL_FAILURE_)
  130. #define ASSERT_PRED_FORMAT2(pred_format, v1, v2) \
  131. GTEST_PRED_FORMAT2_(pred_format, v1, v2, GTEST_FATAL_FAILURE_)
  132. #define ASSERT_PRED2(pred, v1, v2) \
  133. GTEST_PRED2_(pred, v1, v2, GTEST_FATAL_FAILURE_)
  134. // Helper function for implementing {EXPECT|ASSERT}_PRED3. Don't use
  135. // this in your code.
  136. template <typename Pred, typename T1, typename T2, typename T3>
  137. AssertionResult AssertPred3Helper(const char* pred_text, const char* e1,
  138. const char* e2, const char* e3, Pred pred,
  139. const T1& v1, const T2& v2, const T3& v3) {
  140. if (pred(v1, v2, v3)) return AssertionSuccess();
  141. return AssertionFailure()
  142. << pred_text << "(" << e1 << ", " << e2 << ", " << e3
  143. << ") evaluates to false, where"
  144. << "\n"
  145. << e1 << " evaluates to " << ::testing::PrintToString(v1) << "\n"
  146. << e2 << " evaluates to " << ::testing::PrintToString(v2) << "\n"
  147. << e3 << " evaluates to " << ::testing::PrintToString(v3);
  148. }
  149. // Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT3.
  150. // Don't use this in your code.
  151. #define GTEST_PRED_FORMAT3_(pred_format, v1, v2, v3, on_failure) \
  152. GTEST_ASSERT_(pred_format(#v1, #v2, #v3, v1, v2, v3), on_failure)
  153. // Internal macro for implementing {EXPECT|ASSERT}_PRED3. Don't use
  154. // this in your code.
  155. #define GTEST_PRED3_(pred, v1, v2, v3, on_failure) \
  156. GTEST_ASSERT_( \
  157. ::testing::AssertPred3Helper(#pred, #v1, #v2, #v3, pred, v1, v2, v3), \
  158. on_failure)
  159. // Ternary predicate assertion macros.
  160. #define EXPECT_PRED_FORMAT3(pred_format, v1, v2, v3) \
  161. GTEST_PRED_FORMAT3_(pred_format, v1, v2, v3, GTEST_NONFATAL_FAILURE_)
  162. #define EXPECT_PRED3(pred, v1, v2, v3) \
  163. GTEST_PRED3_(pred, v1, v2, v3, GTEST_NONFATAL_FAILURE_)
  164. #define ASSERT_PRED_FORMAT3(pred_format, v1, v2, v3) \
  165. GTEST_PRED_FORMAT3_(pred_format, v1, v2, v3, GTEST_FATAL_FAILURE_)
  166. #define ASSERT_PRED3(pred, v1, v2, v3) \
  167. GTEST_PRED3_(pred, v1, v2, v3, GTEST_FATAL_FAILURE_)
  168. // Helper function for implementing {EXPECT|ASSERT}_PRED4. Don't use
  169. // this in your code.
  170. template <typename Pred, typename T1, typename T2, typename T3, typename T4>
  171. AssertionResult AssertPred4Helper(const char* pred_text, const char* e1,
  172. const char* e2, const char* e3,
  173. const char* e4, Pred pred, const T1& v1,
  174. const T2& v2, const T3& v3, const T4& v4) {
  175. if (pred(v1, v2, v3, v4)) return AssertionSuccess();
  176. return AssertionFailure()
  177. << pred_text << "(" << e1 << ", " << e2 << ", " << e3 << ", " << e4
  178. << ") evaluates to false, where"
  179. << "\n"
  180. << e1 << " evaluates to " << ::testing::PrintToString(v1) << "\n"
  181. << e2 << " evaluates to " << ::testing::PrintToString(v2) << "\n"
  182. << e3 << " evaluates to " << ::testing::PrintToString(v3) << "\n"
  183. << e4 << " evaluates to " << ::testing::PrintToString(v4);
  184. }
  185. // Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT4.
  186. // Don't use this in your code.
  187. #define GTEST_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, on_failure) \
  188. GTEST_ASSERT_(pred_format(#v1, #v2, #v3, #v4, v1, v2, v3, v4), on_failure)
  189. // Internal macro for implementing {EXPECT|ASSERT}_PRED4. Don't use
  190. // this in your code.
  191. #define GTEST_PRED4_(pred, v1, v2, v3, v4, on_failure) \
  192. GTEST_ASSERT_(::testing::AssertPred4Helper(#pred, #v1, #v2, #v3, #v4, pred, \
  193. v1, v2, v3, v4), \
  194. on_failure)
  195. // 4-ary predicate assertion macros.
  196. #define EXPECT_PRED_FORMAT4(pred_format, v1, v2, v3, v4) \
  197. GTEST_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, GTEST_NONFATAL_FAILURE_)
  198. #define EXPECT_PRED4(pred, v1, v2, v3, v4) \
  199. GTEST_PRED4_(pred, v1, v2, v3, v4, GTEST_NONFATAL_FAILURE_)
  200. #define ASSERT_PRED_FORMAT4(pred_format, v1, v2, v3, v4) \
  201. GTEST_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, GTEST_FATAL_FAILURE_)
  202. #define ASSERT_PRED4(pred, v1, v2, v3, v4) \
  203. GTEST_PRED4_(pred, v1, v2, v3, v4, GTEST_FATAL_FAILURE_)
  204. // Helper function for implementing {EXPECT|ASSERT}_PRED5. Don't use
  205. // this in your code.
  206. template <typename Pred, typename T1, typename T2, typename T3, typename T4,
  207. typename T5>
  208. AssertionResult AssertPred5Helper(const char* pred_text, const char* e1,
  209. const char* e2, const char* e3,
  210. const char* e4, const char* e5, Pred pred,
  211. const T1& v1, const T2& v2, const T3& v3,
  212. const T4& v4, const T5& v5) {
  213. if (pred(v1, v2, v3, v4, v5)) return AssertionSuccess();
  214. return AssertionFailure()
  215. << pred_text << "(" << e1 << ", " << e2 << ", " << e3 << ", " << e4
  216. << ", " << e5 << ") evaluates to false, where"
  217. << "\n"
  218. << e1 << " evaluates to " << ::testing::PrintToString(v1) << "\n"
  219. << e2 << " evaluates to " << ::testing::PrintToString(v2) << "\n"
  220. << e3 << " evaluates to " << ::testing::PrintToString(v3) << "\n"
  221. << e4 << " evaluates to " << ::testing::PrintToString(v4) << "\n"
  222. << e5 << " evaluates to " << ::testing::PrintToString(v5);
  223. }
  224. // Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT5.
  225. // Don't use this in your code.
  226. #define GTEST_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, on_failure) \
  227. GTEST_ASSERT_(pred_format(#v1, #v2, #v3, #v4, #v5, v1, v2, v3, v4, v5), \
  228. on_failure)
  229. // Internal macro for implementing {EXPECT|ASSERT}_PRED5. Don't use
  230. // this in your code.
  231. #define GTEST_PRED5_(pred, v1, v2, v3, v4, v5, on_failure) \
  232. GTEST_ASSERT_(::testing::AssertPred5Helper(#pred, #v1, #v2, #v3, #v4, #v5, \
  233. pred, v1, v2, v3, v4, v5), \
  234. on_failure)
  235. // 5-ary predicate assertion macros.
  236. #define EXPECT_PRED_FORMAT5(pred_format, v1, v2, v3, v4, v5) \
  237. GTEST_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, GTEST_NONFATAL_FAILURE_)
  238. #define EXPECT_PRED5(pred, v1, v2, v3, v4, v5) \
  239. GTEST_PRED5_(pred, v1, v2, v3, v4, v5, GTEST_NONFATAL_FAILURE_)
  240. #define ASSERT_PRED_FORMAT5(pred_format, v1, v2, v3, v4, v5) \
  241. GTEST_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, GTEST_FATAL_FAILURE_)
  242. #define ASSERT_PRED5(pred, v1, v2, v3, v4, v5) \
  243. GTEST_PRED5_(pred, v1, v2, v3, v4, v5, GTEST_FATAL_FAILURE_)
  244. } // namespace testing
  245. #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_