diff options
author | Håvard Pettersen <havardpe@oath.com> | 2021-03-22 11:00:35 +0000 |
---|---|---|
committer | Håvard Pettersen <havardpe@oath.com> | 2021-03-22 11:00:35 +0000 |
commit | 653a536e32d2a8d5100c6c0fec0a56d1627c2f30 (patch) | |
tree | 33a55e5b25a4d01a5d62693bb33877e109f522c3 /vespalib | |
parent | 65faa8da798a0aba103c8cbd1c5617f213651f9a (diff) |
added UNWIND_MSG macro
also extend unwind message test
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/tests/unwind_message/unwind_message_test.cpp | 41 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/macro.h | 7 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/unwind_message.h | 3 |
3 files changed, 44 insertions, 7 deletions
diff --git a/vespalib/src/tests/unwind_message/unwind_message_test.cpp b/vespalib/src/tests/unwind_message/unwind_message_test.cpp index 53a7bf26c90..079d0235eb2 100644 --- a/vespalib/src/tests/unwind_message/unwind_message_test.cpp +++ b/vespalib/src/tests/unwind_message/unwind_message_test.cpp @@ -9,24 +9,41 @@ using vespalib::UnwindMessage; //----------------------------------------------------------------------------- +struct MyCheck { + ~MyCheck() { + EXPECT_EQ(std::uncaught_exceptions(), 2); + } +}; + struct MyObj { - UnwindMessage msg1 = UnwindMessage("this SHOULD be printed (1/2)"); + UnwindMessage msg1 = UnwindMessage("this SHOULD be printed (1/4)"); UnwindMessage msg2 = UnwindMessage("this should NOT be printed (1)"); + UnwindMessage msg3 = UnwindMessage("this SHOULD be printed (2/4)"); ~MyObj() { EXPECT_EQ(std::uncaught_exceptions(), 1); + using E = std::invalid_argument; auto not_printed_1 = std::move(msg2); - auto not_printed_2 = unwind_msg("this should NOT be printed (2)"); + try { + MyCheck my_check; + auto printed_1 = std::move(msg1); + throw E("next level"); + } catch (const E &) {} } }; TEST(UnwindMessageTest, unwind_messages_are_printed_when_appropriate) { using E = std::invalid_argument; - auto not_printed_3 = unwind_msg("this should NOT be printed (3)"); + auto not_printed_5 = unwind_msg("this should NOT be printed (%d)", 5); + UNWIND_MSG("this should NOT be printed (%d)", 4); EXPECT_THROW( { EXPECT_EQ(std::uncaught_exceptions(), 0); - auto printed = unwind_msg("this SHOULD be printed (2/2)"); - { auto not_printed_4 = unwind_msg("this should NOT be printed (4)"); } + auto printed_4 = unwind_msg("this SHOULD be printed (%d/%d)", 4, 4); + UNWIND_MSG("this SHOULD be printed (%d/%d)", 3, 4); + { + auto not_printed_3 = unwind_msg("this should NOT be printed (%d)", 3); + UNWIND_MSG("this should NOT be printed (%d)", 2); + } MyObj my_obj; throw E("just testing"); }, E); @@ -34,4 +51,18 @@ TEST(UnwindMessageTest, unwind_messages_are_printed_when_appropriate) { //----------------------------------------------------------------------------- +// need make_string for VESPA_STRLOC macro +#include <vespa/vespalib/util/stringfmt.h> + +TEST(UnwindMessageTest, unwind_message_with_location) { + using E = std::invalid_argument; + EXPECT_THROW( + { + UNWIND_MSG("%s message with location information", VESPA_STRLOC.c_str()); + throw E("just testing"); + }, E); +} + +//----------------------------------------------------------------------------- + GTEST_MAIN_RUN_ALL_TESTS() diff --git a/vespalib/src/vespa/vespalib/util/macro.h b/vespalib/src/vespa/vespalib/util/macro.h index 16c66880e5d..506c1a59e54 100644 --- a/vespalib/src/vespa/vespalib/util/macro.h +++ b/vespalib/src/vespa/vespalib/util/macro.h @@ -2,8 +2,6 @@ #pragma once -// indirectly tested by exception test. - /** * @def VESPA_STRINGIZE(str) * @brief convert code to string @@ -23,3 +21,8 @@ **/ #define VESPA_STRLOC vespalib::make_string("%s in %s:%d",__func__,__FILE__,__LINE__) +/** + * Create a new token by concatenating two tokens (token pasting) + **/ +#define VESPA_CAT_IMPL(a, b) a ## b +#define VESPA_CAT(a, b) VESPA_CAT_IMPL(a, b) diff --git a/vespalib/src/vespa/vespalib/util/unwind_message.h b/vespalib/src/vespa/vespalib/util/unwind_message.h index 5133e92742d..56eb13bcf4f 100644 --- a/vespalib/src/vespa/vespalib/util/unwind_message.h +++ b/vespalib/src/vespa/vespalib/util/unwind_message.h @@ -2,6 +2,7 @@ #pragma once +#include "macro.h" #include <vespa/vespalib/stllike/string.h> namespace vespalib { @@ -30,4 +31,6 @@ extern UnwindMessage unwind_msg(const char *fmt, ...) #endif ; +#define UNWIND_MSG(...) auto VESPA_CAT(unwindMessageOnLine, __LINE__) = unwind_msg(__VA_ARGS__) + } // namespace |