From b5825f9e15d8e7df84b41204abaacf23b3c23b0a Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 22 Jul 2022 21:08:41 +0800 Subject: [PATCH] test/encoding: refactor EncodingException::Macros so it is more compacted Signed-off-by: Kefu Chai (cherry picked from commit b3151a9c373b472c7e88080205fc256a1304fe9d) --- src/test/encoding.cc | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/test/encoding.cc b/src/test/encoding.cc index f18901cbd27d9..6bc89491f16bf 100644 --- a/src/test/encoding.cc +++ b/src/test/encoding.cc @@ -1,6 +1,7 @@ #include "include/buffer.h" #include "include/encoding.h" +#include #include "gtest/gtest.h" using namespace std; @@ -319,26 +320,27 @@ TEST(EncodingRoundTrip, Integers) { } } -const char* expected_what[] = { - "void lame_decoder(int) no longer understand old encoding version 100 < 200: Malformed input", - "void lame_decoder(int) decode past end of struct encoding: Malformed input" -}; - -void lame_decoder(int which) { - switch (which) { - case 0: - throw buffer::malformed_input(DECODE_ERR_OLDVERSION(__PRETTY_FUNCTION__, 100, 200)); - case 1: - throw buffer::malformed_input(DECODE_ERR_PAST(__PRETTY_FUNCTION__)); - } -} - TEST(EncodingException, Macros) { - for (unsigned i = 0; i < std::size(expected_what); i++) { + const struct { + buffer::malformed_input exc; + std::string expected_what; + } tests[] = { + { + DECODE_ERR_OLDVERSION(__PRETTY_FUNCTION__, 100, 200), + fmt::format("{} no longer understand old encoding version 100 < 200: Malformed input", + __PRETTY_FUNCTION__) + }, + { + DECODE_ERR_PAST(__PRETTY_FUNCTION__), + fmt::format("{} decode past end of struct encoding: Malformed input", + __PRETTY_FUNCTION__) + } + }; + for (auto& [exec, expected_what] : tests) { try { - lame_decoder(i); + throw exec; } catch (const exception& e) { - ASSERT_NE(string(e.what()).find(expected_what[i]), string::npos); + ASSERT_NE(string(e.what()).find(expected_what), string::npos); } } } -- 2.39.5