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.

171 line
3.9KB

  1. /*
  2. * Scope Guard
  3. * Copyright (C) 2017-2018 offa
  4. *
  5. * This file is part of Scope Guard.
  6. *
  7. * Scope Guard is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Scope Guard is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Scope Guard. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #pragma once
  21. #include <trompeloeil.hpp>
  22. namespace mock
  23. {
  24. using Handle = int;
  25. using PtrHandle = std::add_pointer_t<Handle>;
  26. struct CallMock
  27. {
  28. MAKE_MOCK0(deleter, void());
  29. MAKE_MOCK1(deleter, void(Handle));
  30. };
  31. struct ThrowOnCopyMock
  32. {
  33. ThrowOnCopyMock()
  34. {
  35. }
  36. ThrowOnCopyMock(const ThrowOnCopyMock&)
  37. {
  38. throw std::exception{};
  39. }
  40. MAKE_CONST_MOCK0(deleter, void());
  41. void operator()() const
  42. {
  43. this->deleter();
  44. }
  45. ThrowOnCopyMock& operator=(const ThrowOnCopyMock&)
  46. {
  47. throw std::exception{};
  48. }
  49. };
  50. struct NotNothrowMoveMock
  51. {
  52. explicit NotNothrowMoveMock(CallMock* m) : m_mock(m)
  53. {
  54. }
  55. NotNothrowMoveMock(const NotNothrowMoveMock& other) : m_mock(other.m_mock)
  56. {
  57. }
  58. NotNothrowMoveMock(NotNothrowMoveMock&& other) noexcept(false) : m_mock(other.m_mock)
  59. {
  60. }
  61. void operator()() const
  62. {
  63. m_mock->deleter();
  64. }
  65. NotNothrowMoveMock& operator=(const NotNothrowMoveMock&)
  66. {
  67. throw "Not implemented";
  68. }
  69. NotNothrowMoveMock& operator=(NotNothrowMoveMock&&)
  70. {
  71. throw "Not implemented";
  72. }
  73. CallMock* m_mock;
  74. };
  75. struct ConditialThrowOnCopyMock
  76. {
  77. explicit ConditialThrowOnCopyMock(Handle h, bool shouldThrow) : m_handle(h),
  78. m_shouldThrow(shouldThrow)
  79. {
  80. }
  81. ConditialThrowOnCopyMock(const ConditialThrowOnCopyMock& other) : m_handle(other.m_handle),
  82. m_shouldThrow(other.m_shouldThrow)
  83. {
  84. if( m_shouldThrow == true )
  85. {
  86. throw std::exception{};
  87. }
  88. }
  89. ConditialThrowOnCopyMock(ConditialThrowOnCopyMock&&) = default;
  90. ConditialThrowOnCopyMock& operator=(const ConditialThrowOnCopyMock& other)
  91. {
  92. if( &other != this )
  93. {
  94. m_handle = other.m_handle;
  95. m_shouldThrow = other.m_shouldThrow;
  96. if( m_shouldThrow == true )
  97. {
  98. throw std::exception{};
  99. }
  100. }
  101. return *this;
  102. }
  103. ConditialThrowOnCopyMock& operator=(ConditialThrowOnCopyMock&&) = default;
  104. Handle m_handle;
  105. bool m_shouldThrow;
  106. };
  107. struct NotNothrowAssignable
  108. {
  109. explicit NotNothrowAssignable(int value) : m_value(value) { }
  110. NotNothrowAssignable(const NotNothrowAssignable&) = default;
  111. NotNothrowAssignable& operator=(const NotNothrowAssignable& other)
  112. {
  113. if( this != &other )
  114. {
  115. assignNotNoexcept(other.m_value);
  116. }
  117. return *this;
  118. }
  119. void assignNotNoexcept(int value) noexcept(false)
  120. {
  121. m_value = value;
  122. }
  123. int m_value;
  124. };
  125. struct CopyMock
  126. {
  127. CopyMock() { }
  128. CopyMock(const CopyMock&) { }
  129. };
  130. }