Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ScopeGuardTest.cpp 2.4KB

7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
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_guard.h"
  21. #include <catch.hpp>
  22. #include <trompeloeil.hpp>
  23. using namespace trompeloeil;
  24. namespace mock
  25. {
  26. struct CallMock
  27. {
  28. MAKE_MOCK0(deleter, void());
  29. };
  30. }
  31. namespace
  32. {
  33. mock::CallMock m;
  34. void deleter()
  35. {
  36. m.deleter();
  37. }
  38. void deleterThrow()
  39. {
  40. throw std::exception{};
  41. }
  42. }
  43. TEST_CASE("deleter called on destruction", "[ScopeGuard]")
  44. {
  45. REQUIRE_CALL(m, deleter());
  46. auto guard = sr::scope_guard(deleter);
  47. static_cast<void>(guard);
  48. }
  49. TEST_CASE("deleter is not called if released", "[ScopeGuard]")
  50. {
  51. REQUIRE_CALL(m, deleter()).TIMES(0);
  52. auto guard = sr::scope_guard(deleter);
  53. guard.release();
  54. }
  55. TEST_CASE("deleter lambda called on destruction", "[ScopeGuard]")
  56. {
  57. mock::CallMock cm;
  58. REQUIRE_CALL(cm, deleter());
  59. auto guard = sr::scope_guard([&cm] { cm.deleter(); });
  60. static_cast<void>(guard);
  61. }
  62. TEST_CASE("move releases moved-from object", "[ScopeGuard]")
  63. {
  64. REQUIRE_CALL(m, deleter());
  65. auto movedFrom = sr::scope_guard(deleter);
  66. auto guard = std::move(movedFrom);
  67. static_cast<void>(guard);
  68. }
  69. TEST_CASE("move transfers state", "[ScopeGuard]")
  70. {
  71. REQUIRE_CALL(m, deleter());
  72. auto movedFrom = sr::scope_guard(deleter);
  73. auto guard = std::move(movedFrom);
  74. static_cast<void>(guard);
  75. }
  76. TEST_CASE("move transfers state if released", "[ScopeGuard]")
  77. {
  78. REQUIRE_CALL(m, deleter()).TIMES(0);
  79. auto movedFrom = sr::scope_guard(deleter);
  80. movedFrom.release();
  81. auto guard = std::move(movedFrom);
  82. static_cast<void>(guard);
  83. }
  84. TEST_CASE("no exception propagation from deleter", "[ScopeGuard]")
  85. {
  86. REQUIRE_NOTHROW([] {
  87. auto guard = sr::scope_guard(deleterThrow);
  88. static_cast<void>(guard);
  89. }());
  90. }