You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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