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.

173 lines
4.3KB

  1. // MIT License
  2. //
  3. // Copyright (c) 2017-2018 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) : m_mock(m)
  55. {
  56. }
  57. NotNothrowMoveMock(const NotNothrowMoveMock& other) : m_mock(other.m_mock)
  58. {
  59. }
  60. NotNothrowMoveMock(NotNothrowMoveMock&& other) noexcept(false) : m_mock(other.m_mock)
  61. {
  62. }
  63. void operator()() const
  64. {
  65. m_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* m_mock;
  76. };
  77. struct ConditialThrowOnCopyMock
  78. {
  79. explicit ConditialThrowOnCopyMock(Handle h, bool shouldThrow) : m_handle(h),
  80. m_shouldThrow(shouldThrow)
  81. {
  82. }
  83. ConditialThrowOnCopyMock(const ConditialThrowOnCopyMock& other) : m_handle(other.m_handle),
  84. m_shouldThrow(other.m_shouldThrow)
  85. {
  86. if( m_shouldThrow == true )
  87. {
  88. throw std::exception{};
  89. }
  90. }
  91. ConditialThrowOnCopyMock(ConditialThrowOnCopyMock&&) = default;
  92. ConditialThrowOnCopyMock& operator=(const ConditialThrowOnCopyMock& other)
  93. {
  94. if( &other != this )
  95. {
  96. m_handle = other.m_handle;
  97. m_shouldThrow = other.m_shouldThrow;
  98. if( m_shouldThrow == true )
  99. {
  100. throw std::exception{};
  101. }
  102. }
  103. return *this;
  104. }
  105. ConditialThrowOnCopyMock& operator=(ConditialThrowOnCopyMock&&) = default;
  106. Handle m_handle;
  107. bool m_shouldThrow;
  108. };
  109. struct NotNothrowAssignable
  110. {
  111. explicit NotNothrowAssignable(int value) : m_value(value) { }
  112. NotNothrowAssignable(const NotNothrowAssignable&) = default;
  113. NotNothrowAssignable& operator=(const NotNothrowAssignable& other)
  114. {
  115. if( this != &other )
  116. {
  117. assignNotNoexcept(other.m_value);
  118. }
  119. return *this;
  120. }
  121. void assignNotNoexcept(int value) noexcept(false)
  122. {
  123. m_value = value;
  124. }
  125. int m_value;
  126. };
  127. struct CopyMock
  128. {
  129. CopyMock() { }
  130. CopyMock(const CopyMock&) { }
  131. };
  132. }