Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

104 lines
2.6KB

  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 "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", "[ScopeExit]")
  33. {
  34. REQUIRE_CALL(m, deleter());
  35. auto guard = sr::make_scope_exit(deleter);
  36. static_cast<void>(guard);
  37. }
  38. TEST_CASE("exit function lambda called on destruction", "[ScopeExit]")
  39. {
  40. CallMock cm;
  41. REQUIRE_CALL(cm, deleter());
  42. auto guard = sr::make_scope_exit([&cm] { cm.deleter(); });
  43. static_cast<void>(guard);
  44. }
  45. TEST_CASE("exit function called and rethrow on copy exception", "[ScopeExit]")
  46. {
  47. REQUIRE_THROWS([] {
  48. const ThrowOnCopyMock noMove;
  49. REQUIRE_CALL(noMove, deleter());
  50. sr::scope_exit<decltype(noMove)> guard{noMove};
  51. }());
  52. }
  53. TEST_CASE("exit function is not called if released", "[ScopeExit]")
  54. {
  55. REQUIRE_CALL(m, deleter()).TIMES(0);
  56. auto guard = sr::make_scope_exit(deleter);
  57. guard.release();
  58. }
  59. TEST_CASE("move releases moved-from object", "[ScopeExit]")
  60. {
  61. REQUIRE_CALL(m, deleter());
  62. auto movedFrom = sr::make_scope_exit(deleter);
  63. auto guard = std::move(movedFrom);
  64. static_cast<void>(guard);
  65. }
  66. TEST_CASE("move with copy init releases moved-from object", "[ScopeExit]")
  67. {
  68. CallMock mock;
  69. const NotNothrowMoveMock notNothrow{&mock};
  70. REQUIRE_CALL(mock, deleter());
  71. sr::scope_exit<decltype(notNothrow)> movedFrom{notNothrow};
  72. auto guard = std::move(movedFrom);
  73. }
  74. TEST_CASE("move transfers state", "[ScopeExit]")
  75. {
  76. REQUIRE_CALL(m, deleter());
  77. auto movedFrom = sr::make_scope_exit(deleter);
  78. auto guard = std::move(movedFrom);
  79. static_cast<void>(guard);
  80. }
  81. TEST_CASE("move transfers state if released", "[ScopeExit]")
  82. {
  83. REQUIRE_CALL(m, deleter()).TIMES(0);
  84. auto movedFrom = sr::make_scope_exit(deleter);
  85. movedFrom.release();
  86. auto guard = std::move(movedFrom);
  87. static_cast<void>(guard);
  88. }