Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CallMocks.h 4.7KB

5 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // MIT License
  2. //
  3. // Copyright (c) 2017-2019 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 ThrowOnCopyMock
  34. {
  35. ThrowOnCopyMock()
  36. {
  37. }
  38. ThrowOnCopyMock(const ThrowOnCopyMock&)
  39. {
  40. throw std::exception{};
  41. }
  42. MAKE_CONST_MOCK0(deleter, void());
  43. void operator()() const
  44. {
  45. this->deleter();
  46. }
  47. ThrowOnCopyMock& operator=(const ThrowOnCopyMock&)
  48. {
  49. throw std::exception{};
  50. }
  51. };
  52. struct NotNothrowMoveMock
  53. {
  54. explicit NotNothrowMoveMock(CallMock* m) : mock(m)
  55. {
  56. }
  57. NotNothrowMoveMock(const NotNothrowMoveMock& other) : mock(other.mock)
  58. {
  59. }
  60. NotNothrowMoveMock(NotNothrowMoveMock&& other) noexcept(false) : mock(other.mock)
  61. {
  62. }
  63. void operator()() const
  64. {
  65. mock->deleter();
  66. }
  67. NotNothrowMoveMock& operator=(const NotNothrowMoveMock&)
  68. {
  69. throw "Not implemented";
  70. }
  71. NotNothrowMoveMock& operator=(NotNothrowMoveMock&&)
  72. {
  73. throw "Not implemented";
  74. }
  75. CallMock* mock;
  76. };
  77. struct ConditionalThrowOnCopyMock
  78. {
  79. explicit ConditionalThrowOnCopyMock(Handle h, bool throwOnCopyMock) : handle(h),
  80. shouldThrow(throwOnCopyMock)
  81. {
  82. }
  83. ConditionalThrowOnCopyMock(const ConditionalThrowOnCopyMock& other) : handle(other.handle),
  84. shouldThrow(other.shouldThrow)
  85. {
  86. if( shouldThrow == true )
  87. {
  88. throw std::exception{};
  89. }
  90. }
  91. ConditionalThrowOnCopyMock(ConditionalThrowOnCopyMock&&) = default;
  92. ConditionalThrowOnCopyMock& operator=(const ConditionalThrowOnCopyMock& other)
  93. {
  94. if( &other != this )
  95. {
  96. handle = other.handle;
  97. shouldThrow = other.shouldThrow;
  98. if( shouldThrow == true )
  99. {
  100. throw std::exception{};
  101. }
  102. }
  103. return *this;
  104. }
  105. ConditionalThrowOnCopyMock& operator=(ConditionalThrowOnCopyMock&&) = default;
  106. Handle handle;
  107. bool shouldThrow;
  108. };
  109. struct NotNothrowAssignable
  110. {
  111. explicit NotNothrowAssignable(int v) : value(v) { }
  112. NotNothrowAssignable(const NotNothrowAssignable&) = default;
  113. NotNothrowAssignable& operator=(const NotNothrowAssignable& other)
  114. {
  115. if( this != &other )
  116. {
  117. assignNotNoexcept(other.value);
  118. }
  119. return *this;
  120. }
  121. void assignNotNoexcept(int v) noexcept(false)
  122. {
  123. value = v;
  124. }
  125. int value;
  126. };
  127. struct CopyMock
  128. {
  129. CopyMock() { }
  130. CopyMock(const CopyMock&) { }
  131. };
  132. struct ConditionalThrowOnCopyDeleter
  133. {
  134. ConditionalThrowOnCopyDeleter() { }
  135. ConditionalThrowOnCopyDeleter(const ConditionalThrowOnCopyDeleter&)
  136. {
  137. if( throwOnNextCopy == true )
  138. {
  139. throw std::exception{};
  140. }
  141. throwOnNextCopy = false;
  142. }
  143. MAKE_CONST_MOCK1(deleter, void(Handle));
  144. void operator()(Handle h) const
  145. {
  146. this->deleter(h);
  147. }
  148. static inline bool throwOnNextCopy{false};
  149. };
  150. }