No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

ScopeSuccessTest.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_success.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", "[ScopeSuccess]")
  73. {
  74. REQUIRE_CALL(m, deleter());
  75. auto guard = sr::make_scope_success(deleter);
  76. static_cast<void>(guard);
  77. }
  78. TEST_CASE("exit function lambda called on destruction", "[ScopeSuccess]")
  79. {
  80. CallMock cm;
  81. REQUIRE_CALL(cm, deleter());
  82. auto guard = sr::make_scope_success([&cm] { cm.deleter(); });
  83. static_cast<void>(guard);
  84. }
  85. TEST_CASE("exit function not called and rethrow on copy exception", "[ScopeSuccess]")
  86. {
  87. REQUIRE_THROWS([] {
  88. const ThrowOnCopyMock noMove;
  89. sr::scope_success<decltype(noMove)> guard{noMove};
  90. }());
  91. }
  92. TEST_CASE("exit function is not called if released", "[ScopeSuccess]")
  93. {
  94. REQUIRE_CALL(m, deleter()).TIMES(0);
  95. auto guard = sr::make_scope_success(deleter);
  96. guard.release();
  97. }
  98. TEST_CASE("move releases moved-from object", "[ScopeSuccess]")
  99. {
  100. REQUIRE_CALL(m, deleter());
  101. auto movedFrom = sr::make_scope_success(deleter);
  102. auto guard = std::move(movedFrom);
  103. static_cast<void>(guard);
  104. }
  105. TEST_CASE("move with copy init releases moved-from object", "[ScopeSuccess]")
  106. {
  107. CallMock mock;
  108. const NotNothrowMoveMock notNothrow{&mock};
  109. REQUIRE_CALL(mock, deleter());
  110. sr::scope_success<decltype(notNothrow)> movedFrom{notNothrow};
  111. auto guard = std::move(movedFrom);
  112. }
  113. TEST_CASE("move transfers state", "[ScopeSuccess]")
  114. {
  115. REQUIRE_CALL(m, deleter());
  116. auto movedFrom = sr::make_scope_success(deleter);
  117. auto guard = std::move(movedFrom);
  118. static_cast<void>(guard);
  119. }
  120. TEST_CASE("move transfers state if released", "[ScopeSuccess]")
  121. {
  122. REQUIRE_CALL(m, deleter()).TIMES(0);
  123. auto movedFrom = sr::make_scope_success(deleter);
  124. movedFrom.release();
  125. auto guard = std::move(movedFrom);
  126. static_cast<void>(guard);
  127. }
  128. TEST_CASE("exit function not called on exception", "[ScopeFail]")
  129. {
  130. try
  131. {
  132. auto guard = sr::make_scope_success(deleter);
  133. static_cast<void>(guard);
  134. throw 3;
  135. }
  136. catch( ... )
  137. {
  138. }
  139. }
  140. TEST_CASE("exit function called on pending exception", "[ScopeFail]")
  141. {
  142. try
  143. {
  144. throw 3;
  145. }
  146. catch( ... )
  147. {
  148. REQUIRE_CALL(m, deleter());
  149. auto guard = sr::make_scope_success(deleter);
  150. static_cast<void>(guard);
  151. }
  152. }