Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

160 lines
3.8KB

  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("deleter 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("deleter 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("deleter 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("deleter 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. }