選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

173 行
4.2KB

  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) : 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 ConditialThrowOnCopyMock
  78. {
  79. explicit ConditialThrowOnCopyMock(Handle h, bool throwOnCopyMock) : handle(h),
  80. shouldThrow(throwOnCopyMock)
  81. {
  82. }
  83. ConditialThrowOnCopyMock(const ConditialThrowOnCopyMock& other) : handle(other.handle),
  84. shouldThrow(other.shouldThrow)
  85. {
  86. if( 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. handle = other.handle;
  97. shouldThrow = other.shouldThrow;
  98. if( shouldThrow == true )
  99. {
  100. throw std::exception{};
  101. }
  102. }
  103. return *this;
  104. }
  105. ConditialThrowOnCopyMock& operator=(ConditialThrowOnCopyMock&&) = 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. }