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.

ScopeExitTest.cpp 2.6KB

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