Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

100 linhas
2.5KB

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