]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
test/encoding: refactor EncodingException::Macros
authorKefu Chai <tchaikov@gmail.com>
Fri, 22 Jul 2022 13:08:41 +0000 (21:08 +0800)
committerKefu Chai <tchaikov@gmail.com>
Fri, 22 Jul 2022 16:14:35 +0000 (00:14 +0800)
so it is more compacted

Signed-off-by: Kefu Chai <tchaikov@gmail.com>
src/test/encoding.cc

index f18901cbd27d94795679d30cd40ffdee8b3fb116..6bc89491f16bfb2ed36e06aabfda48742dcb2557 100644 (file)
@@ -1,6 +1,7 @@
 #include "include/buffer.h"
 #include "include/encoding.h"
 
+#include <fmt/format.h>
 #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);
     }
   }
 }