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.

пре 4 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // MIT License
  2. //
  3. // Copyright (c) 2017-2020 offa
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. // SOFTWARE.
  22. #include "scope_fail.h"
  23. #include "CallMocks.h"
  24. #include <catch2/catch.hpp>
  25. using namespace mock;
  26. using namespace trompeloeil;
  27. namespace
  28. {
  29. CallMock m;
  30. void deleter()
  31. {
  32. m.deleter();
  33. }
  34. }
  35. TEST_CASE("exit function called on destruction", "[ScopeFail]")
  36. {
  37. REQUIRE_CALL(m, deleter()).TIMES(0);
  38. [[maybe_unused]] auto guard = sr::scope_fail{deleter};
  39. }
  40. TEST_CASE("exit function lambda called on destruction", "[ScopeFail]")
  41. {
  42. CallMock cm;
  43. REQUIRE_CALL(cm, deleter()).TIMES(0);
  44. [[maybe_unused]] auto guard = sr::scope_fail{[&cm] { cm.deleter(); }};
  45. }
  46. TEST_CASE("exit function called and rethrow on copy exception", "[ScopeFail]")
  47. {
  48. REQUIRE_THROWS([] {
  49. const ThrowOnCopyMock noMove;
  50. REQUIRE_CALL(noMove, deleter());
  51. sr::scope_fail guard{noMove};
  52. }());
  53. }
  54. TEST_CASE("exit function is not called if released", "[ScopeFail]")
  55. {
  56. REQUIRE_CALL(m, deleter()).TIMES(0);
  57. auto guard = sr::scope_fail{deleter};
  58. guard.release();
  59. }
  60. TEST_CASE("move releases moved-from object", "[ScopeFail]")
  61. {
  62. REQUIRE_CALL(m, deleter()).TIMES(0);
  63. auto movedFrom = sr::scope_fail{deleter};
  64. [[maybe_unused]] auto guard = std::move(movedFrom);
  65. }
  66. TEST_CASE("move with copy init releases moved-from object", "[ScopeFail]")
  67. {
  68. CallMock mock;
  69. const NotNothrowMoveMock notNothrow{&mock};
  70. REQUIRE_CALL(mock, deleter()).TIMES(0);
  71. sr::scope_fail movedFrom{notNothrow};
  72. auto guard = std::move(movedFrom);
  73. }
  74. TEST_CASE("move transfers state", "[ScopeFail]")
  75. {
  76. REQUIRE_CALL(m, deleter()).TIMES(0);
  77. auto movedFrom = sr::scope_fail{deleter};
  78. [[maybe_unused]] auto guard = std::move(movedFrom);
  79. }
  80. TEST_CASE("move transfers state if released", "[ScopeFail]")
  81. {
  82. REQUIRE_CALL(m, deleter()).TIMES(0);
  83. auto movedFrom = sr::scope_fail{deleter};
  84. movedFrom.release();
  85. [[maybe_unused]] auto guard = std::move(movedFrom);
  86. }
  87. TEST_CASE("exit function called on exception", "[ScopeFail]")
  88. {
  89. try
  90. {
  91. REQUIRE_CALL(m, deleter());
  92. [[maybe_unused]] auto guard = sr::scope_fail{deleter};
  93. throw std::exception{};
  94. }
  95. catch( ... )
  96. {
  97. }
  98. }
  99. TEST_CASE("exit function not called on pending exception", "[ScopeFail]")
  100. {
  101. try
  102. {
  103. throw std::exception{};
  104. }
  105. catch( ... )
  106. {
  107. [[maybe_unused]] auto guard = sr::scope_fail{deleter};
  108. }
  109. }