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.

130 line
3.2KB

  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 "CallMocks.h"
  22. #include <catch.hpp>
  23. using namespace trompeloeil;
  24. namespace
  25. {
  26. CallMock m;
  27. void deleter()
  28. {
  29. m.deleter();
  30. }
  31. }
  32. TEST_CASE("exit function called on destruction", "[ScopeSuccess]")
  33. {
  34. REQUIRE_CALL(m, deleter());
  35. auto guard = sr::make_scope_success(deleter);
  36. static_cast<void>(guard);
  37. }
  38. TEST_CASE("exit function lambda called on destruction", "[ScopeSuccess]")
  39. {
  40. CallMock cm;
  41. REQUIRE_CALL(cm, deleter());
  42. auto guard = sr::make_scope_success([&cm] { cm.deleter(); });
  43. static_cast<void>(guard);
  44. }
  45. TEST_CASE("exit function not called and rethrow on copy exception", "[ScopeSuccess]")
  46. {
  47. REQUIRE_THROWS([] {
  48. const ThrowOnCopyMock noMove;
  49. REQUIRE_CALL(noMove, deleter());
  50. sr::scope_success<decltype(noMove)> guard{noMove};
  51. }());
  52. }
  53. TEST_CASE("exit function is not called if released", "[ScopeSuccess]")
  54. {
  55. REQUIRE_CALL(m, deleter()).TIMES(0);
  56. auto guard = sr::make_scope_success(deleter);
  57. guard.release();
  58. }
  59. TEST_CASE("move releases moved-from object", "[ScopeSuccess]")
  60. {
  61. REQUIRE_CALL(m, deleter());
  62. auto movedFrom = sr::make_scope_success(deleter);
  63. auto guard = std::move(movedFrom);
  64. static_cast<void>(guard);
  65. }
  66. TEST_CASE("move with copy init releases moved-from object", "[ScopeSuccess]")
  67. {
  68. CallMock mock;
  69. const NotNothrowMoveMock notNothrow{&mock};
  70. REQUIRE_CALL(mock, deleter());
  71. sr::scope_success<decltype(notNothrow)> movedFrom{notNothrow};
  72. auto guard = std::move(movedFrom);
  73. }
  74. TEST_CASE("move transfers state", "[ScopeSuccess]")
  75. {
  76. REQUIRE_CALL(m, deleter());
  77. auto movedFrom = sr::make_scope_success(deleter);
  78. auto guard = std::move(movedFrom);
  79. static_cast<void>(guard);
  80. }
  81. TEST_CASE("move transfers state if released", "[ScopeSuccess]")
  82. {
  83. REQUIRE_CALL(m, deleter()).TIMES(0);
  84. auto movedFrom = sr::make_scope_success(deleter);
  85. movedFrom.release();
  86. auto guard = std::move(movedFrom);
  87. static_cast<void>(guard);
  88. }
  89. TEST_CASE("exit function not called on exception", "[ScopeFail]")
  90. {
  91. try
  92. {
  93. auto guard = sr::make_scope_success(deleter);
  94. static_cast<void>(guard);
  95. throw 3;
  96. }
  97. catch( ... )
  98. {
  99. }
  100. }
  101. TEST_CASE("exit function called on pending exception", "[ScopeFail]")
  102. {
  103. try
  104. {
  105. throw 3;
  106. }
  107. catch( ... )
  108. {
  109. REQUIRE_CALL(m, deleter());
  110. auto guard = sr::make_scope_success(deleter);
  111. static_cast<void>(guard);
  112. }
  113. }