Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

CallMocks.h 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Scope Guard
  3. * Copyright (C) 2017 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. 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 CopyMock
  108. {
  109. CopyMock() {}
  110. CopyMock(const CopyMock&) { }
  111. };
  112. }