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.

346 lines
12 KiB

  1. $$ -*- mode: c++; -*-
  2. $$ This is a Pump source file. Please use Pump to convert
  3. $$ it to gmock-generated-matchers.h.
  4. $$
  5. $var n = 10 $$ The maximum arity we support.
  6. $$ }} This line fixes auto-indentation of the following code in Emacs.
  7. // Copyright 2008, Google Inc.
  8. // All rights reserved.
  9. //
  10. // Redistribution and use in source and binary forms, with or without
  11. // modification, are permitted provided that the following conditions are
  12. // met:
  13. //
  14. // * Redistributions of source code must retain the above copyright
  15. // notice, this list of conditions and the following disclaimer.
  16. // * Redistributions in binary form must reproduce the above
  17. // copyright notice, this list of conditions and the following disclaimer
  18. // in the documentation and/or other materials provided with the
  19. // distribution.
  20. // * Neither the name of Google Inc. nor the names of its
  21. // contributors may be used to endorse or promote products derived from
  22. // this software without specific prior written permission.
  23. //
  24. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. // Google Mock - a framework for writing C++ mock classes.
  36. //
  37. // This file implements some commonly used variadic matchers.
  38. // GOOGLETEST_CM0002 DO NOT DELETE
  39. #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_MATCHERS_H_
  40. #define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_MATCHERS_H_
  41. #include <iterator>
  42. #include <sstream>
  43. #include <string>
  44. #include <utility>
  45. #include <vector>
  46. #include "gmock/gmock-matchers.h"
  47. // The MATCHER* family of macros can be used in a namespace scope to
  48. // define custom matchers easily.
  49. //
  50. // Basic Usage
  51. // ===========
  52. //
  53. // The syntax
  54. //
  55. // MATCHER(name, description_string) { statements; }
  56. //
  57. // defines a matcher with the given name that executes the statements,
  58. // which must return a bool to indicate if the match succeeds. Inside
  59. // the statements, you can refer to the value being matched by 'arg',
  60. // and refer to its type by 'arg_type'.
  61. //
  62. // The description string documents what the matcher does, and is used
  63. // to generate the failure message when the match fails. Since a
  64. // MATCHER() is usually defined in a header file shared by multiple
  65. // C++ source files, we require the description to be a C-string
  66. // literal to avoid possible side effects. It can be empty, in which
  67. // case we'll use the sequence of words in the matcher name as the
  68. // description.
  69. //
  70. // For example:
  71. //
  72. // MATCHER(IsEven, "") { return (arg % 2) == 0; }
  73. //
  74. // allows you to write
  75. //
  76. // // Expects mock_foo.Bar(n) to be called where n is even.
  77. // EXPECT_CALL(mock_foo, Bar(IsEven()));
  78. //
  79. // or,
  80. //
  81. // // Verifies that the value of some_expression is even.
  82. // EXPECT_THAT(some_expression, IsEven());
  83. //
  84. // If the above assertion fails, it will print something like:
  85. //
  86. // Value of: some_expression
  87. // Expected: is even
  88. // Actual: 7
  89. //
  90. // where the description "is even" is automatically calculated from the
  91. // matcher name IsEven.
  92. //
  93. // Argument Type
  94. // =============
  95. //
  96. // Note that the type of the value being matched (arg_type) is
  97. // determined by the context in which you use the matcher and is
  98. // supplied to you by the compiler, so you don't need to worry about
  99. // declaring it (nor can you). This allows the matcher to be
  100. // polymorphic. For example, IsEven() can be used to match any type
  101. // where the value of "(arg % 2) == 0" can be implicitly converted to
  102. // a bool. In the "Bar(IsEven())" example above, if method Bar()
  103. // takes an int, 'arg_type' will be int; if it takes an unsigned long,
  104. // 'arg_type' will be unsigned long; and so on.
  105. //
  106. // Parameterizing Matchers
  107. // =======================
  108. //
  109. // Sometimes you'll want to parameterize the matcher. For that you
  110. // can use another macro:
  111. //
  112. // MATCHER_P(name, param_name, description_string) { statements; }
  113. //
  114. // For example:
  115. //
  116. // MATCHER_P(HasAbsoluteValue, value, "") { return abs(arg) == value; }
  117. //
  118. // will allow you to write:
  119. //
  120. // EXPECT_THAT(Blah("a"), HasAbsoluteValue(n));
  121. //
  122. // which may lead to this message (assuming n is 10):
  123. //
  124. // Value of: Blah("a")
  125. // Expected: has absolute value 10
  126. // Actual: -9
  127. //
  128. // Note that both the matcher description and its parameter are
  129. // printed, making the message human-friendly.
  130. //
  131. // In the matcher definition body, you can write 'foo_type' to
  132. // reference the type of a parameter named 'foo'. For example, in the
  133. // body of MATCHER_P(HasAbsoluteValue, value) above, you can write
  134. // 'value_type' to refer to the type of 'value'.
  135. //
  136. // We also provide MATCHER_P2, MATCHER_P3, ..., up to MATCHER_P$n to
  137. // support multi-parameter matchers.
  138. //
  139. // Describing Parameterized Matchers
  140. // =================================
  141. //
  142. // The last argument to MATCHER*() is a string-typed expression. The
  143. // expression can reference all of the matcher's parameters and a
  144. // special bool-typed variable named 'negation'. When 'negation' is
  145. // false, the expression should evaluate to the matcher's description;
  146. // otherwise it should evaluate to the description of the negation of
  147. // the matcher. For example,
  148. //
  149. // using testing::PrintToString;
  150. //
  151. // MATCHER_P2(InClosedRange, low, hi,
  152. // std::string(negation ? "is not" : "is") + " in range [" +
  153. // PrintToString(low) + ", " + PrintToString(hi) + "]") {
  154. // return low <= arg && arg <= hi;
  155. // }
  156. // ...
  157. // EXPECT_THAT(3, InClosedRange(4, 6));
  158. // EXPECT_THAT(3, Not(InClosedRange(2, 4)));
  159. //
  160. // would generate two failures that contain the text:
  161. //
  162. // Expected: is in range [4, 6]
  163. // ...
  164. // Expected: is not in range [2, 4]
  165. //
  166. // If you specify "" as the description, the failure message will
  167. // contain the sequence of words in the matcher name followed by the
  168. // parameter values printed as a tuple. For example,
  169. //
  170. // MATCHER_P2(InClosedRange, low, hi, "") { ... }
  171. // ...
  172. // EXPECT_THAT(3, InClosedRange(4, 6));
  173. // EXPECT_THAT(3, Not(InClosedRange(2, 4)));
  174. //
  175. // would generate two failures that contain the text:
  176. //
  177. // Expected: in closed range (4, 6)
  178. // ...
  179. // Expected: not (in closed range (2, 4))
  180. //
  181. // Types of Matcher Parameters
  182. // ===========================
  183. //
  184. // For the purpose of typing, you can view
  185. //
  186. // MATCHER_Pk(Foo, p1, ..., pk, description_string) { ... }
  187. //
  188. // as shorthand for
  189. //
  190. // template <typename p1_type, ..., typename pk_type>
  191. // FooMatcherPk<p1_type, ..., pk_type>
  192. // Foo(p1_type p1, ..., pk_type pk) { ... }
  193. //
  194. // When you write Foo(v1, ..., vk), the compiler infers the types of
  195. // the parameters v1, ..., and vk for you. If you are not happy with
  196. // the result of the type inference, you can specify the types by
  197. // explicitly instantiating the template, as in Foo<long, bool>(5,
  198. // false). As said earlier, you don't get to (or need to) specify
  199. // 'arg_type' as that's determined by the context in which the matcher
  200. // is used. You can assign the result of expression Foo(p1, ..., pk)
  201. // to a variable of type FooMatcherPk<p1_type, ..., pk_type>. This
  202. // can be useful when composing matchers.
  203. //
  204. // While you can instantiate a matcher template with reference types,
  205. // passing the parameters by pointer usually makes your code more
  206. // readable. If, however, you still want to pass a parameter by
  207. // reference, be aware that in the failure message generated by the
  208. // matcher you will see the value of the referenced object but not its
  209. // address.
  210. //
  211. // Explaining Match Results
  212. // ========================
  213. //
  214. // Sometimes the matcher description alone isn't enough to explain why
  215. // the match has failed or succeeded. For example, when expecting a
  216. // long string, it can be very helpful to also print the diff between
  217. // the expected string and the actual one. To achieve that, you can
  218. // optionally stream additional information to a special variable
  219. // named result_listener, whose type is a pointer to class
  220. // MatchResultListener:
  221. //
  222. // MATCHER_P(EqualsLongString, str, "") {
  223. // if (arg == str) return true;
  224. //
  225. // *result_listener << "the difference: "
  226. /// << DiffStrings(str, arg);
  227. // return false;
  228. // }
  229. //
  230. // Overloading Matchers
  231. // ====================
  232. //
  233. // You can overload matchers with different numbers of parameters:
  234. //
  235. // MATCHER_P(Blah, a, description_string1) { ... }
  236. // MATCHER_P2(Blah, a, b, description_string2) { ... }
  237. //
  238. // Caveats
  239. // =======
  240. //
  241. // When defining a new matcher, you should also consider implementing
  242. // MatcherInterface or using MakePolymorphicMatcher(). These
  243. // approaches require more work than the MATCHER* macros, but also
  244. // give you more control on the types of the value being matched and
  245. // the matcher parameters, which may leads to better compiler error
  246. // messages when the matcher is used wrong. They also allow
  247. // overloading matchers based on parameter types (as opposed to just
  248. // based on the number of parameters).
  249. //
  250. // MATCHER*() can only be used in a namespace scope as templates cannot be
  251. // declared inside of a local class.
  252. //
  253. // More Information
  254. // ================
  255. //
  256. // To learn more about using these macros, please search for 'MATCHER'
  257. // on
  258. // https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md
  259. $range i 0..n
  260. $for i
  261. [[
  262. $var macro_name = [[$if i==0 [[MATCHER]] $elif i==1 [[MATCHER_P]]
  263. $else [[MATCHER_P$i]]]]
  264. $var class_name = [[name##Matcher[[$if i==0 [[]] $elif i==1 [[P]]
  265. $else [[P$i]]]]]]
  266. $range j 0..i-1
  267. $var template = [[$if i==0 [[]] $else [[
  268. template <$for j, [[typename p$j##_type]]>\
  269. ]]]]
  270. $var ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]]
  271. $var impl_ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]]
  272. $var impl_inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(::std::move(gmock_p$j))]]]]]]
  273. $var inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(::std::move(gmock_p$j))]]]]]]
  274. $var params = [[$for j, [[p$j]]]]
  275. $var param_types = [[$if i==0 [[]] $else [[<$for j, [[p$j##_type]]>]]]]
  276. $var param_types_and_names = [[$for j, [[p$j##_type p$j]]]]
  277. $var param_field_decls = [[$for j
  278. [[
  279. p$j##_type const p$j;\
  280. ]]]]
  281. $var param_field_decls2 = [[$for j
  282. [[
  283. p$j##_type const p$j;\
  284. ]]]]
  285. #define $macro_name(name$for j [[, p$j]], description)\$template
  286. class $class_name {\
  287. public:\
  288. template <typename arg_type>\
  289. class gmock_Impl : public ::testing::MatcherInterface<\
  290. GTEST_REFERENCE_TO_CONST_(arg_type)> {\
  291. public:\
  292. [[$if i==1 [[explicit ]]]]gmock_Impl($impl_ctor_param_list)\
  293. $impl_inits {}\
  294. virtual bool MatchAndExplain(\
  295. GTEST_REFERENCE_TO_CONST_(arg_type) arg,\
  296. ::testing::MatchResultListener* result_listener) const;\
  297. virtual void DescribeTo(::std::ostream* gmock_os) const {\
  298. *gmock_os << FormatDescription(false);\
  299. }\
  300. virtual void DescribeNegationTo(::std::ostream* gmock_os) const {\
  301. *gmock_os << FormatDescription(true);\
  302. }\$param_field_decls
  303. private:\
  304. ::std::string FormatDescription(bool negation) const {\
  305. ::std::string gmock_description = (description);\
  306. if (!gmock_description.empty()) {\
  307. return gmock_description;\
  308. }\
  309. return ::testing::internal::FormatMatcherDescription(\
  310. negation, #name, \
  311. ::testing::internal::UniversalTersePrintTupleFieldsToStrings(\
  312. ::std::tuple<$for j, [[p$j##_type]]>($for j, [[p$j]])));\
  313. }\
  314. };\
  315. template <typename arg_type>\
  316. operator ::testing::Matcher<arg_type>() const {\
  317. return ::testing::Matcher<arg_type>(\
  318. new gmock_Impl<arg_type>($params));\
  319. }\
  320. [[$if i==1 [[explicit ]]]]$class_name($ctor_param_list)$inits {\
  321. }\$param_field_decls2
  322. private:\
  323. };\$template
  324. inline $class_name$param_types name($param_types_and_names) {\
  325. return $class_name$param_types($params);\
  326. }\$template
  327. template <typename arg_type>\
  328. bool $class_name$param_types::gmock_Impl<arg_type>::MatchAndExplain(\
  329. GTEST_REFERENCE_TO_CONST_(arg_type) arg,\
  330. ::testing::MatchResultListener* result_listener GTEST_ATTRIBUTE_UNUSED_)\
  331. const
  332. ]]
  333. #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_MATCHERS_H_