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

ScopeFailTest.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #include "scope_fail.h"
  21. #include <catch.hpp>
  22. #include <trompeloeil.hpp>
  23. using namespace trompeloeil;
  24. namespace
  25. {
  26. struct CallMock
  27. {
  28. MAKE_MOCK0(deleter, void());
  29. };
  30. struct ThrowOnCopyMock
  31. {
  32. ThrowOnCopyMock() { }
  33. ThrowOnCopyMock(const ThrowOnCopyMock&)
  34. {
  35. throw std::exception{};
  36. }
  37. MAKE_CONST_MOCK0(deleter, void());
  38. void operator()() const
  39. {
  40. this->deleter();
  41. }
  42. ThrowOnCopyMock& operator=(const ThrowOnCopyMock&)
  43. {
  44. throw std::exception{};
  45. }
  46. };
  47. struct NotNothrowMoveMock
  48. {
  49. NotNothrowMoveMock(CallMock* m) : m_mock(m) { }
  50. NotNothrowMoveMock(const NotNothrowMoveMock& other) : m_mock(other.m_mock) { }
  51. NotNothrowMoveMock(NotNothrowMoveMock&& other) noexcept(false) : m_mock(other.m_mock) { }
  52. void operator()() const
  53. {
  54. m_mock->deleter();
  55. }
  56. NotNothrowMoveMock& operator=(const NotNothrowMoveMock&)
  57. {
  58. throw "Not implemented";
  59. }
  60. NotNothrowMoveMock& operator=(NotNothrowMoveMock&&)
  61. {
  62. throw "Not implemented";
  63. }
  64. CallMock* m_mock;
  65. };
  66. CallMock m;
  67. void deleter()
  68. {
  69. m.deleter();
  70. }
  71. }
  72. TEST_CASE("exit function called on destruction", "[ScopeFail]")
  73. {
  74. REQUIRE_CALL(m, deleter()).TIMES(0);
  75. auto guard = sr::make_scope_fail(deleter);
  76. static_cast<void>(guard);
  77. }
  78. TEST_CASE("exit function lambda called on destruction", "[ScopeFail]")
  79. {
  80. CallMock cm;
  81. REQUIRE_CALL(cm, deleter()).TIMES(0);
  82. auto guard = sr::make_scope_fail([&cm] { cm.deleter(); });
  83. static_cast<void>(guard);
  84. }
  85. TEST_CASE("exit function called and rethrow on copy exception", "[ScopeFail]")
  86. {
  87. REQUIRE_THROWS([] {
  88. const ThrowOnCopyMock noMove;
  89. REQUIRE_CALL(noMove, deleter());
  90. sr::scope_fail<decltype(noMove)> guard{noMove};
  91. }());
  92. }
  93. TEST_CASE("exit function is not called if released", "[ScopeFail]")
  94. {
  95. REQUIRE_CALL(m, deleter()).TIMES(0);
  96. auto guard = sr::make_scope_fail(deleter);
  97. guard.release();
  98. }
  99. TEST_CASE("move releases moved-from object", "[ScopeFail]")
  100. {
  101. REQUIRE_CALL(m, deleter()).TIMES(0);
  102. auto movedFrom = sr::make_scope_fail(deleter);
  103. auto guard = std::move(movedFrom);
  104. static_cast<void>(guard);
  105. }
  106. TEST_CASE("move with copy init releases moved-from object", "[ScopeFail]")
  107. {
  108. CallMock mock;
  109. const NotNothrowMoveMock notNothrow{&mock};
  110. REQUIRE_CALL(mock, deleter()).TIMES(0);
  111. sr::scope_fail<decltype(notNothrow)> movedFrom{notNothrow};
  112. auto guard = std::move(movedFrom);
  113. }
  114. TEST_CASE("move transfers state", "[ScopeFail]")
  115. {
  116. REQUIRE_CALL(m, deleter()).TIMES(0);
  117. auto movedFrom = sr::make_scope_fail(deleter);
  118. auto guard = std::move(movedFrom);
  119. static_cast<void>(guard);
  120. }
  121. TEST_CASE("move transfers state if released", "[ScopeFail]")
  122. {
  123. REQUIRE_CALL(m, deleter()).TIMES(0);
  124. auto movedFrom = sr::make_scope_fail(deleter);
  125. movedFrom.release();
  126. auto guard = std::move(movedFrom);
  127. static_cast<void>(guard);
  128. }
  129. TEST_CASE("exit function called on exception", "[ScopeFail]")
  130. {
  131. try
  132. {
  133. REQUIRE_CALL(m, deleter());
  134. auto guard = sr::make_scope_fail(deleter);
  135. static_cast<void>(guard);
  136. throw 3;
  137. }
  138. catch( ... )
  139. {
  140. }
  141. }
  142. TEST_CASE("exit function not called on pending exception", "[ScopeFail]")
  143. {
  144. try
  145. {
  146. throw 3;
  147. }
  148. catch( ... )
  149. {
  150. auto guard = sr::make_scope_fail(deleter);
  151. static_cast<void>(guard);
  152. }
  153. }