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.

CallMocks.h 6.5KB

3 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // MIT License
  2. //
  3. // Copyright (c) 2017-2021 offa
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. // SOFTWARE.
  22. #pragma once
  23. #include <trompeloeil.hpp>
  24. namespace mock
  25. {
  26. using Handle = int;
  27. using PtrHandle = std::add_pointer_t<Handle>;
  28. struct CallMock
  29. {
  30. MAKE_MOCK0(deleter, void());
  31. MAKE_MOCK1(deleter, void(Handle));
  32. };
  33. struct MoveableMock
  34. {
  35. static constexpr bool trompeloeil_movable_mock = true;
  36. MAKE_CONST_MOCK1(deleter, void(Handle));
  37. void operator()(Handle h) const
  38. {
  39. this->deleter(h);
  40. }
  41. };
  42. struct ThrowOnCopyMock
  43. {
  44. ThrowOnCopyMock()
  45. {
  46. }
  47. ThrowOnCopyMock(const ThrowOnCopyMock&)
  48. {
  49. throw std::exception{};
  50. }
  51. MAKE_CONST_MOCK0(deleter, void());
  52. void operator()() const
  53. {
  54. this->deleter();
  55. }
  56. ThrowOnCopyMock& operator=(const ThrowOnCopyMock&)
  57. {
  58. throw std::exception{};
  59. }
  60. };
  61. struct NotNothrowMoveMock
  62. {
  63. explicit NotNothrowMoveMock(CallMock* m) : mock(m)
  64. {
  65. }
  66. NotNothrowMoveMock(const NotNothrowMoveMock& other) : mock(other.mock)
  67. {
  68. }
  69. NotNothrowMoveMock(NotNothrowMoveMock&& other) noexcept(false) : mock(other.mock)
  70. {
  71. }
  72. void operator()() const
  73. {
  74. mock->deleter();
  75. }
  76. NotNothrowMoveMock& operator=(const NotNothrowMoveMock&)
  77. {
  78. throw "Not implemented";
  79. }
  80. NotNothrowMoveMock& operator=(NotNothrowMoveMock&&)
  81. {
  82. throw "Not implemented";
  83. }
  84. CallMock* mock;
  85. };
  86. struct ConditionalThrowOnCopyMock
  87. {
  88. explicit ConditionalThrowOnCopyMock(Handle h, bool throwOnCopyMock) : handle(h),
  89. shouldThrow(throwOnCopyMock)
  90. {
  91. }
  92. ConditionalThrowOnCopyMock(const ConditionalThrowOnCopyMock& other) : handle(other.handle),
  93. shouldThrow(other.shouldThrow)
  94. {
  95. if( shouldThrow == true )
  96. {
  97. throw std::exception{};
  98. }
  99. }
  100. ConditionalThrowOnCopyMock(ConditionalThrowOnCopyMock&&) = default;
  101. ConditionalThrowOnCopyMock& operator=(const ConditionalThrowOnCopyMock& other)
  102. {
  103. if( &other != this )
  104. {
  105. handle = other.handle;
  106. shouldThrow = other.shouldThrow;
  107. if( shouldThrow == true )
  108. {
  109. throw std::exception{};
  110. }
  111. }
  112. return *this;
  113. }
  114. ConditionalThrowOnCopyMock& operator=(ConditionalThrowOnCopyMock&&) = default;
  115. Handle handle;
  116. bool shouldThrow;
  117. };
  118. struct NotNothrowAssignable
  119. {
  120. explicit NotNothrowAssignable(int v) : value(v) { }
  121. NotNothrowAssignable(const NotNothrowAssignable&) = default;
  122. NotNothrowAssignable& operator=(const NotNothrowAssignable& other)
  123. {
  124. if( this != &other )
  125. {
  126. assignNotNoexcept(other.value);
  127. }
  128. return *this;
  129. }
  130. void assignNotNoexcept(int v) noexcept(false)
  131. {
  132. value = v;
  133. }
  134. int value;
  135. };
  136. struct CopyMock
  137. {
  138. CopyMock() { }
  139. CopyMock(const CopyMock&) { }
  140. };
  141. struct ConditionalThrowOnCopyDeleter
  142. {
  143. ConditionalThrowOnCopyDeleter() { }
  144. ConditionalThrowOnCopyDeleter(const ConditionalThrowOnCopyDeleter&)
  145. {
  146. if( throwOnNextCopy == true )
  147. {
  148. throw std::exception{};
  149. }
  150. throwOnNextCopy = false;
  151. }
  152. MAKE_CONST_MOCK1(deleter, void(Handle));
  153. void operator()(Handle h) const
  154. {
  155. this->deleter(h);
  156. }
  157. static inline bool throwOnNextCopy{false};
  158. };
  159. template<bool noexceptMove>
  160. struct NoexceptResource
  161. {
  162. NoexceptResource() = default;
  163. ~NoexceptResource() = default;
  164. NoexceptResource(const NoexceptResource&) = default;
  165. NoexceptResource(NoexceptResource&&) noexcept(noexceptMove)
  166. {
  167. }
  168. NoexceptResource& operator=(const NoexceptResource&) = default;
  169. NoexceptResource& operator=(NoexceptResource&&) noexcept(noexceptMove)
  170. {
  171. return *this;
  172. }
  173. };
  174. template<bool noexceptMove>
  175. struct NoexceptDeleter
  176. {
  177. NoexceptDeleter() = default;
  178. ~NoexceptDeleter() = default;
  179. NoexceptDeleter(const NoexceptDeleter&) = default;
  180. NoexceptDeleter(NoexceptDeleter&&) noexcept(noexceptMove)
  181. {
  182. }
  183. NoexceptDeleter& operator=(const NoexceptDeleter&) = default;
  184. NoexceptDeleter& operator=(NoexceptDeleter&&) noexcept(noexceptMove)
  185. {
  186. return *this;
  187. }
  188. template<class Resource>
  189. void operator()(const Resource&) const
  190. {
  191. }
  192. };
  193. struct FunctionDeleter
  194. {
  195. FunctionDeleter() noexcept = default;
  196. FunctionDeleter(const FunctionDeleter&) = default;
  197. FunctionDeleter(FunctionDeleter&&) = default;
  198. FunctionDeleter& operator=(const FunctionDeleter&)
  199. {
  200. return *this;
  201. }
  202. FunctionDeleter& operator=(FunctionDeleter&&)
  203. {
  204. return *this;
  205. }
  206. void operator()(Handle) const
  207. {
  208. }
  209. };
  210. }