Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

111 lines
2.4KB

  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. struct CallMock
  25. {
  26. MAKE_MOCK0(deleter, void());
  27. };
  28. namespace
  29. {
  30. CallMock m;
  31. void deleter()
  32. {
  33. m.deleter();
  34. }
  35. }
  36. TEST_CASE("deleter called on destruction", "[ScopeGuard]")
  37. {
  38. {
  39. REQUIRE_CALL(m, deleter());
  40. auto guard = sr::scope_guard(deleter);
  41. static_cast<void>(guard);
  42. }
  43. }
  44. TEST_CASE("deleter is not called if released", "[ScopeGuard]")
  45. {
  46. std::size_t calls{0};
  47. {
  48. auto guard = sr::scope_guard([&calls] { ++calls; });
  49. guard.release();
  50. }
  51. REQUIRE(calls == 0);
  52. }
  53. TEST_CASE("move releases moved-from object", "[ScopeGuard]")
  54. {
  55. std::size_t calls{0};
  56. {
  57. auto movedFrom = sr::scope_guard([&calls] { ++calls; });
  58. auto guard = std::move(movedFrom);
  59. static_cast<void>(guard);
  60. }
  61. REQUIRE(calls == 1);
  62. }
  63. TEST_CASE("move transfers state", "[ScopeGuard]")
  64. {
  65. std::size_t calls{0};
  66. {
  67. auto movedFrom = sr::scope_guard([&calls] { ++calls; });
  68. auto guard = std::move(movedFrom);
  69. static_cast<void>(guard);
  70. }
  71. REQUIRE(calls == 1);
  72. }
  73. TEST_CASE("move transfers state if released", "[ScopeGuard]")
  74. {
  75. std::size_t calls{0};
  76. {
  77. auto movedFrom = sr::scope_guard([&calls] { ++calls; });
  78. movedFrom.release();
  79. auto guard = std::move(movedFrom);
  80. static_cast<void>(guard);
  81. }
  82. REQUIRE(calls == 0);
  83. }
  84. TEST_CASE("no exception propagation from deleter", "[ScopeGuard]")
  85. {
  86. REQUIRE_NOTHROW([] {
  87. auto guard = sr::scope_guard([] { throw "Don't propagate this!"; });
  88. static_cast<void>(guard);
  89. }());
  90. }