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.

131 lines
3.0KB

  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_guard.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() = default;
  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. CallMock m;
  48. void deleter()
  49. {
  50. m.deleter();
  51. }
  52. }
  53. TEST_CASE("deleter called on destruction", "[ScopeGuard]")
  54. {
  55. REQUIRE_CALL(m, deleter());
  56. auto guard = sr::scope_guard(deleter);
  57. static_cast<void>(guard);
  58. }
  59. TEST_CASE("deleter lambda called on destruction", "[ScopeGuard]")
  60. {
  61. CallMock cm;
  62. REQUIRE_CALL(cm, deleter());
  63. auto guard = sr::scope_guard([&cm] { cm.deleter(); });
  64. static_cast<void>(guard);
  65. }
  66. TEST_CASE("deleter called and rethrow on copy exception", "[ScopeGuard]")
  67. {
  68. REQUIRE_THROWS([] {
  69. const ThrowOnCopyMock noMove;
  70. REQUIRE_CALL(noMove, deleter());
  71. sr::scope_guard_t<decltype(noMove)> guard{noMove};
  72. }());
  73. }
  74. TEST_CASE("deleter is not called if released", "[ScopeGuard]")
  75. {
  76. REQUIRE_CALL(m, deleter()).TIMES(0);
  77. auto guard = sr::scope_guard(deleter);
  78. guard.release();
  79. }
  80. TEST_CASE("move releases moved-from object", "[ScopeGuard]")
  81. {
  82. REQUIRE_CALL(m, deleter());
  83. auto movedFrom = sr::scope_guard(deleter);
  84. auto guard = std::move(movedFrom);
  85. static_cast<void>(guard);
  86. }
  87. TEST_CASE("move transfers state", "[ScopeGuard]")
  88. {
  89. REQUIRE_CALL(m, deleter());
  90. auto movedFrom = sr::scope_guard(deleter);
  91. auto guard = std::move(movedFrom);
  92. static_cast<void>(guard);
  93. }
  94. TEST_CASE("move transfers state if released", "[ScopeGuard]")
  95. {
  96. REQUIRE_CALL(m, deleter()).TIMES(0);
  97. auto movedFrom = sr::scope_guard(deleter);
  98. movedFrom.release();
  99. auto guard = std::move(movedFrom);
  100. static_cast<void>(guard);
  101. }
  102. TEST_CASE("no exception propagation from deleter", "[ScopeGuard]")
  103. {
  104. REQUIRE_NOTHROW([] {
  105. auto guard = sr::scope_guard([] { throw std::exception{}; });
  106. static_cast<void>(guard);
  107. }());
  108. }