From 440b56b016357d7ae6682a4b7c7488b0f3903d6c Mon Sep 17 00:00:00 2001 From: Martin Moene Date: Wed, 17 Aug 2022 15:44:16 +0200 Subject: [PATCH] Move use of scope guard into constexpr function (#5) --- test/scope.t.cpp | 60 ++++++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/test/scope.t.cpp b/test/scope.t.cpp index 4dc9ba7..414979f 100644 --- a/test/scope.t.cpp +++ b/test/scope.t.cpp @@ -47,21 +47,37 @@ void success() namespace cexpr { scope_constexpr_ext -void exit() +bool is_called_exit() { - is_called = true; + bool result = false; + { + auto change = nonstd::make_scope_exit( [&]{ result = true; }); + } + return result; } scope_constexpr_ext -void fail() +bool is_called_fail() { - is_called = true; + bool result = false; + try + { + auto change = nonstd::make_scope_fail( [&]{ result = true; }); + // throw std::exception(); + } + catch(...) {} + return result; } scope_constexpr_ext -void success() +bool is_called_success() { - is_called = true; + bool result = false; + { + auto guard = make_scope_success( [&](){ result = true; } ); + // throw std::exception(); + } + return result; } } // namespace cexpr @@ -102,15 +118,7 @@ CASE( "scope_exit: exit function is called at end of scope (lambda)" ) CASE( "scope_exit: exit function is called at end of scope (constexpr)" " [extension]" ) { #if scope_CPP11_OR_GREATER - is_called = false; - - // scope: - { - auto guard = make_scope_exit( cexpr::exit ); - // auto guard = make_scope_exit( [](){ is_called = true; } ); - } - - EXPECT( is_called ); + EXPECT( cexpr::is_called_exit() ); #else EXPECT( !!"Test for constexpr scope_exit not suitable for C++98." ); #endif @@ -208,16 +216,7 @@ CASE( "scope_fail: exit function is not called when no exception occurs" ) CASE( "scope_fail: exit function is not called when no exception occurs (constexpr)" " [extension]" ) { #if scope_CPP11_OR_GREATER - is_called = false; - - try - { - auto guard = make_scope_fail( cexpr::fail ); - // throw std::exception(); - } - catch(...) {} - - EXPECT_NOT( is_called ); + EXPECT_NOT( cexpr::is_called_fail() ); #else EXPECT( !!"Test for constexpr scope_fail not suitable for C++98." ); #endif @@ -282,16 +281,7 @@ CASE( "scope_success: exit function is called when no exception occurs (lambda)" CASE( "scope_success: exit function is called when no exception occurs (constexpr)" " [extension]" ) { #if scope_CPP11_OR_GREATER - is_called = false; - - try - { - auto guard = make_scope_success( cexpr::success ); - // throw std::exception(); - } - catch(...) {} - - EXPECT( is_called ); + EXPECT( cexpr::is_called_success() ); #else EXPECT( !!"Test for constexpr scope_success not suitable for C++98." ); #endif