Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

276 lines
6.6KB

  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. CopyMock& operator=(const CopyMock&) = default;
  141. };
  142. struct ConditionalThrowOnCopyDeleter
  143. {
  144. ConditionalThrowOnCopyDeleter() { }
  145. ConditionalThrowOnCopyDeleter(const ConditionalThrowOnCopyDeleter&)
  146. {
  147. if( throwOnNextCopy == true )
  148. {
  149. throw std::exception{};
  150. }
  151. throwOnNextCopy = false;
  152. }
  153. MAKE_CONST_MOCK1(deleter, void(Handle));
  154. void operator()(Handle h) const
  155. {
  156. this->deleter(h);
  157. }
  158. static inline bool throwOnNextCopy{false};
  159. };
  160. template<bool noexceptMove>
  161. struct NoexceptResource
  162. {
  163. NoexceptResource() = default;
  164. ~NoexceptResource() = default;
  165. NoexceptResource(const NoexceptResource&) = default;
  166. NoexceptResource(NoexceptResource&&) noexcept(noexceptMove)
  167. {
  168. }
  169. NoexceptResource& operator=(const NoexceptResource&) = default;
  170. NoexceptResource& operator=(NoexceptResource&&) noexcept(noexceptMove)
  171. {
  172. return *this;
  173. }
  174. };
  175. template<bool noexceptMove>
  176. struct NoexceptDeleter
  177. {
  178. NoexceptDeleter() = default;
  179. ~NoexceptDeleter() = default;
  180. NoexceptDeleter(const NoexceptDeleter&) = default;
  181. NoexceptDeleter(NoexceptDeleter&&) noexcept(noexceptMove)
  182. {
  183. }
  184. NoexceptDeleter& operator=(const NoexceptDeleter&) = default;
  185. NoexceptDeleter& operator=(NoexceptDeleter&&) noexcept(noexceptMove)
  186. {
  187. return *this;
  188. }
  189. template<class Resource>
  190. void operator()(const Resource&) const
  191. {
  192. }
  193. };
  194. struct FunctionDeleter
  195. {
  196. FunctionDeleter() noexcept = default;
  197. FunctionDeleter(const FunctionDeleter&) = default;
  198. FunctionDeleter(FunctionDeleter&&) = default;
  199. FunctionDeleter& operator=(const FunctionDeleter&)
  200. {
  201. return *this;
  202. }
  203. FunctionDeleter& operator=(FunctionDeleter&&)
  204. {
  205. return *this;
  206. }
  207. void operator()(Handle) const
  208. {
  209. }
  210. };
  211. }