From 9749af0be52b0976a55738c3c9512d493dc59d50 Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Mon, 13 Jul 2020 15:31:57 +0300 Subject: [PATCH] [base] ExceptionCatcher. Test on prevent returning reference on a local temporary object. --- base/base_tests/exception_tests.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/base/base_tests/exception_tests.cpp b/base/base_tests/exception_tests.cpp index e9eac58fab..1b0043db31 100644 --- a/base/base_tests/exception_tests.cpp +++ b/base/base_tests/exception_tests.cpp @@ -3,6 +3,7 @@ #include "base/exception.hpp" #include +#include namespace { @@ -22,6 +23,14 @@ void FuncThrowsRootExceptionVoid(int) { throw RootException("RootException", "Ro void FuncThrowsExceptionVoid() { throw std::exception(); } void FuncThrowsNumberVoid() { throw 1; }; +std::string const & ReturnsByRef(std::string const & str) +{ + bool exception = true; + return ExceptionCatcher( + "ReturnsByRef().", exception, + [](std::string const & str) -> std::string const & { return str; }, str); +} + UNIT_TEST(ExceptionCatcher_FunctionsWithoutArgs) { bool exception = false; @@ -147,4 +156,11 @@ UNIT_TEST(ExceptionCatcher_FunctionsReturnVoid) ExceptionCatcher("Function returns void.", exception, FuncThrowsExceptionVoid); ExceptionCatcher("Function returns void.", exception, FuncThrowsNumberVoid); } + +UNIT_TEST(ExceptionCatcher_PreventReturningRefOnLocaleTemporaryObj) +{ + std::string const str = "A string"; + auto const returnedStr = ReturnsByRef(str); + TEST_EQUAL(str, returnedStr, ()); +} } // namespace