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

ScopeExitTest.cpp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_exit.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. MAKE_CONST_MOCK0(deleter, void());
  53. void operator()() const
  54. {
  55. m_mock->deleter();
  56. }
  57. NotNothrowMoveMock& operator=(const NotNothrowMoveMock&)
  58. {
  59. throw "Not implemented";
  60. }
  61. NotNothrowMoveMock& operator=(NotNothrowMoveMock&&)
  62. {
  63. throw "Not implemented";
  64. }
  65. CallMock* m_mock;
  66. };
  67. CallMock m;
  68. void deleter()
  69. {
  70. m.deleter();
  71. }
  72. }
  73. TEST_CASE("deleter called on destruction", "[ScopeExit]")
  74. {
  75. REQUIRE_CALL(m, deleter());
  76. auto guard = sr::make_scope_exit(deleter);
  77. static_cast<void>(guard);
  78. }
  79. TEST_CASE("deleter lambda called on destruction", "[ScopeExit]")
  80. {
  81. CallMock cm;
  82. REQUIRE_CALL(cm, deleter());
  83. auto guard = sr::make_scope_exit([&cm] { cm.deleter(); });
  84. static_cast<void>(guard);
  85. }
  86. TEST_CASE("deleter called and rethrow on copy exception", "[ScopeExit]")
  87. {
  88. REQUIRE_THROWS([] {
  89. const ThrowOnCopyMock noMove;
  90. REQUIRE_CALL(noMove, deleter());
  91. sr::scope_exit<decltype(noMove)> guard{noMove};
  92. }());
  93. }
  94. TEST_CASE("deleter is not called if released", "[ScopeExit]")
  95. {
  96. REQUIRE_CALL(m, deleter()).TIMES(0);
  97. auto guard = sr::make_scope_exit(deleter);
  98. guard.release();
  99. }
  100. TEST_CASE("move releases moved-from object", "[ScopeExit]")
  101. {
  102. REQUIRE_CALL(m, deleter());
  103. auto movedFrom = sr::make_scope_exit(deleter);
  104. auto guard = std::move(movedFrom);
  105. static_cast<void>(guard);
  106. }
  107. TEST_CASE("move with copy init releases moved-from object", "[ScopeExit]")
  108. {
  109. CallMock mock;
  110. const NotNothrowMoveMock notNothrow{&mock};
  111. REQUIRE_CALL(mock, deleter());
  112. sr::scope_exit<decltype(notNothrow)> movedFrom{notNothrow};
  113. auto guard = std::move(movedFrom);
  114. }
  115. TEST_CASE("move transfers state", "[ScopeExit]")
  116. {
  117. REQUIRE_CALL(m, deleter());
  118. auto movedFrom = sr::make_scope_exit(deleter);
  119. auto guard = std::move(movedFrom);
  120. static_cast<void>(guard);
  121. }
  122. TEST_CASE("move transfers state if released", "[ScopeExit]")
  123. {
  124. REQUIRE_CALL(m, deleter()).TIMES(0);
  125. auto movedFrom = sr::make_scope_exit(deleter);
  126. movedFrom.release();
  127. auto guard = std::move(movedFrom);
  128. static_cast<void>(guard);
  129. }
  130. TEST_CASE("no exception propagation from deleter", "[ScopeExit]")
  131. {
  132. REQUIRE_NOTHROW([] {
  133. auto guard = sr::make_scope_exit([] { throw std::exception{}; });
  134. static_cast<void>(guard);
  135. }());
  136. }