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.

ScopeGuardTest.cpp 2.3KB

7 年之前
7 年之前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "Mocks.h"
  23. using namespace trompeloeil;
  24. namespace
  25. {
  26. mock::CallMock m;
  27. void deleter()
  28. {
  29. m.deleter();
  30. }
  31. }
  32. TEST_CASE("deleter called on destruction", "[ScopeGuard]")
  33. {
  34. REQUIRE_CALL(m, deleter());
  35. auto guard = sr::scope_guard(deleter);
  36. static_cast<void>(guard);
  37. }
  38. TEST_CASE("deleter is not called if released", "[ScopeGuard]")
  39. {
  40. REQUIRE_CALL(m, deleter()).TIMES(0);
  41. auto guard = sr::scope_guard(deleter);
  42. guard.release();
  43. }
  44. TEST_CASE("deleter lambda called on destruction", "[ScopeGuard]")
  45. {
  46. mock::CallMock cm;
  47. REQUIRE_CALL(cm, deleter());
  48. auto guard = sr::scope_guard([&cm] { cm.deleter(); });
  49. static_cast<void>(guard);
  50. }
  51. TEST_CASE("move releases moved-from object", "[ScopeGuard]")
  52. {
  53. REQUIRE_CALL(m, deleter());
  54. auto movedFrom = sr::scope_guard(deleter);
  55. auto guard = std::move(movedFrom);
  56. static_cast<void>(guard);
  57. }
  58. TEST_CASE("move transfers state", "[ScopeGuard]")
  59. {
  60. REQUIRE_CALL(m, deleter());
  61. auto movedFrom = sr::scope_guard(deleter);
  62. auto guard = std::move(movedFrom);
  63. static_cast<void>(guard);
  64. }
  65. TEST_CASE("move transfers state if released", "[ScopeGuard]")
  66. {
  67. REQUIRE_CALL(m, deleter()).TIMES(0);
  68. auto movedFrom = sr::scope_guard(deleter);
  69. movedFrom.release();
  70. auto guard = std::move(movedFrom);
  71. static_cast<void>(guard);
  72. }
  73. TEST_CASE("no exception propagation from deleter", "[ScopeGuard]")
  74. {
  75. REQUIRE_NOTHROW([] {
  76. auto guard = sr::scope_guard([] { throw std::exception{}; });
  77. static_cast<void>(guard);
  78. }());
  79. }