No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

209 líneas
5.0KB

  1. // MIT License
  2. //
  3. // Copyright (c) 2017-2020 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. }