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.

162 lines
6.0 KiB

  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 actions that depend on gmock-generated-actions.h.
  32. // GOOGLETEST_CM0002 DO NOT DELETE
  33. #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
  34. #define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
  35. #include <algorithm>
  36. #include <type_traits>
  37. #include "gmock/gmock-generated-actions.h"
  38. namespace testing {
  39. namespace internal {
  40. // An internal replacement for std::copy which mimics its behavior. This is
  41. // necessary because Visual Studio deprecates ::std::copy, issuing warning 4996.
  42. // However Visual Studio 2010 and later do not honor #pragmas which disable that
  43. // warning.
  44. template<typename InputIterator, typename OutputIterator>
  45. inline OutputIterator CopyElements(InputIterator first,
  46. InputIterator last,
  47. OutputIterator output) {
  48. for (; first != last; ++first, ++output) {
  49. *output = *first;
  50. }
  51. return output;
  52. }
  53. } // namespace internal
  54. // Various overloads for Invoke().
  55. // The ACTION*() macros trigger warning C4100 (unreferenced formal
  56. // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
  57. // the macro definition, as the warnings are generated when the macro
  58. // is expanded and macro expansion cannot contain #pragma. Therefore
  59. // we suppress them here.
  60. #ifdef _MSC_VER
  61. # pragma warning(push)
  62. # pragma warning(disable:4100)
  63. #endif
  64. // Action ReturnArg<k>() returns the k-th argument of the mock function.
  65. ACTION_TEMPLATE(ReturnArg,
  66. HAS_1_TEMPLATE_PARAMS(int, k),
  67. AND_0_VALUE_PARAMS()) {
  68. return ::std::get<k>(args);
  69. }
  70. // Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
  71. // mock function to *pointer.
  72. ACTION_TEMPLATE(SaveArg,
  73. HAS_1_TEMPLATE_PARAMS(int, k),
  74. AND_1_VALUE_PARAMS(pointer)) {
  75. *pointer = ::std::get<k>(args);
  76. }
  77. // Action SaveArgPointee<k>(pointer) saves the value pointed to
  78. // by the k-th (0-based) argument of the mock function to *pointer.
  79. ACTION_TEMPLATE(SaveArgPointee,
  80. HAS_1_TEMPLATE_PARAMS(int, k),
  81. AND_1_VALUE_PARAMS(pointer)) {
  82. *pointer = *::std::get<k>(args);
  83. }
  84. // Action SetArgReferee<k>(value) assigns 'value' to the variable
  85. // referenced by the k-th (0-based) argument of the mock function.
  86. ACTION_TEMPLATE(SetArgReferee,
  87. HAS_1_TEMPLATE_PARAMS(int, k),
  88. AND_1_VALUE_PARAMS(value)) {
  89. typedef typename ::std::tuple_element<k, args_type>::type argk_type;
  90. // Ensures that argument #k is a reference. If you get a compiler
  91. // error on the next line, you are using SetArgReferee<k>(value) in
  92. // a mock function whose k-th (0-based) argument is not a reference.
  93. GTEST_COMPILE_ASSERT_(std::is_reference<argk_type>::value,
  94. SetArgReferee_must_be_used_with_a_reference_argument);
  95. ::std::get<k>(args) = value;
  96. }
  97. // Action SetArrayArgument<k>(first, last) copies the elements in
  98. // source range [first, last) to the array pointed to by the k-th
  99. // (0-based) argument, which can be either a pointer or an
  100. // iterator. The action does not take ownership of the elements in the
  101. // source range.
  102. ACTION_TEMPLATE(SetArrayArgument,
  103. HAS_1_TEMPLATE_PARAMS(int, k),
  104. AND_2_VALUE_PARAMS(first, last)) {
  105. // Visual Studio deprecates ::std::copy, so we use our own copy in that case.
  106. #ifdef _MSC_VER
  107. internal::CopyElements(first, last, ::std::get<k>(args));
  108. #else
  109. ::std::copy(first, last, ::std::get<k>(args));
  110. #endif
  111. }
  112. // Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
  113. // function.
  114. ACTION_TEMPLATE(DeleteArg,
  115. HAS_1_TEMPLATE_PARAMS(int, k),
  116. AND_0_VALUE_PARAMS()) {
  117. delete ::std::get<k>(args);
  118. }
  119. // This action returns the value pointed to by 'pointer'.
  120. ACTION_P(ReturnPointee, pointer) { return *pointer; }
  121. // Action Throw(exception) can be used in a mock function of any type
  122. // to throw the given exception. Any copyable value can be thrown.
  123. #if GTEST_HAS_EXCEPTIONS
  124. // Suppresses the 'unreachable code' warning that VC generates in opt modes.
  125. # ifdef _MSC_VER
  126. # pragma warning(push) // Saves the current warning state.
  127. # pragma warning(disable:4702) // Temporarily disables warning 4702.
  128. # endif
  129. ACTION_P(Throw, exception) { throw exception; }
  130. # ifdef _MSC_VER
  131. # pragma warning(pop) // Restores the warning state.
  132. # endif
  133. #endif // GTEST_HAS_EXCEPTIONS
  134. #ifdef _MSC_VER
  135. # pragma warning(pop)
  136. #endif
  137. } // namespace testing
  138. #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_