Browse Source

Check the number of calls, not just whether the deleter has been called

or not (fixes #9).
main
offa 7 years ago
parent
commit
f9a89f8d34
1 changed files with 12 additions and 12 deletions
  1. +12
    -12
      test/ScopeGuardTest.cpp

+ 12
- 12
test/ScopeGuardTest.cpp View File



TEST_CASE("deleter called on destruction", "[ScopeGuard]") TEST_CASE("deleter called on destruction", "[ScopeGuard]")
{ {
bool executed = false;
std::size_t calls{0};


{ {
auto guard = sr::scope_guard([&executed] { executed = true; });
auto guard = sr::scope_guard([&calls] { ++calls; });
static_cast<void>(guard); static_cast<void>(guard);
} }


REQUIRE(executed == true);
REQUIRE(calls == 1);
} }


TEST_CASE("deleter is not called if released", "[ScopeGuard]") TEST_CASE("deleter is not called if released", "[ScopeGuard]")
{ {
bool executed = false;
std::size_t calls{0};


{ {
auto guard = sr::scope_guard([&executed] { executed = true; });
auto guard = sr::scope_guard([&calls] { ++calls; });
guard.release(); guard.release();
} }


REQUIRE(executed == false);
REQUIRE(calls == 0);
} }


TEST_CASE("move releases moved-from object", "[ScopeGuard]") TEST_CASE("move releases moved-from object", "[ScopeGuard]")


TEST_CASE("move transfers state", "[ScopeGuard]") TEST_CASE("move transfers state", "[ScopeGuard]")
{ {
bool executed = false;
std::size_t calls{0};


{ {
auto movedFrom = sr::scope_guard([&executed] { executed = true; });
auto movedFrom = sr::scope_guard([&calls] { ++calls; });
auto guard = std::move(movedFrom); auto guard = std::move(movedFrom);
static_cast<void>(guard); static_cast<void>(guard);
} }


REQUIRE(executed == true);
REQUIRE(calls == 1);
} }


TEST_CASE("move transfers state if released", "[ScopeGuard]") TEST_CASE("move transfers state if released", "[ScopeGuard]")
{ {
bool executed = false;
std::size_t calls{0};


{ {
auto movedFrom = sr::scope_guard([&executed] { executed = true; });
auto movedFrom = sr::scope_guard([&calls] { ++calls; });
movedFrom.release(); movedFrom.release();
auto guard = std::move(movedFrom); auto guard = std::move(movedFrom);
static_cast<void>(guard); static_cast<void>(guard);
} }


REQUIRE(executed == false);
REQUIRE(calls == 0);
} }


TEST_CASE("no exception propagation from deleter", "[ScopeGuard]") TEST_CASE("no exception propagation from deleter", "[ScopeGuard]")

Loading…
Cancel
Save