reftable: wrap EXPECT macros in do/while

The `EXPECT` macros used by the reftable test framework are all using a single `if` statement with the actual condition. This results in weird syntax when using them in if/else statements like the following: ``` if (foo) EXPECT(foo == 2) else EXPECT(bar == 2) ``` Note that there need not be a trailing semicolon. Furthermore, it is not immediately obvious whether the else now belongs to the `if (foo)` or whether it belongs to the expanded `if (foo == 2)` from the macro. Fix this by wrapping the macros in a do/while loop. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Dec 11, 2023 at 10:07 UTC e32b8ece640fc700c5a74dd53e6cae8b1a893a6d
1 file changed +32 -26
reftable/test_framework.h
+32 -26
@@ -12,32 +12,38 @@ https://developers.google.com/open-source/licenses/bsd
12 #include "system.h"
13 #include "reftable-error.h"
14
15 -#define EXPECT_ERR(c) \
16 - if (c != 0) { \
17 - fflush(stderr); \
18 - fflush(stdout); \
19 - fprintf(stderr, "%s: %d: error == %d (%s), want 0\n", \
20 - __FILE__, __LINE__, c, reftable_error_str(c)); \
21 - abort(); \
22 - }
23 -
24 -#define EXPECT_STREQ(a, b) \
25 - if (strcmp(a, b)) { \
26 - fflush(stderr); \
27 - fflush(stdout); \
28 - fprintf(stderr, "%s:%d: %s (%s) != %s (%s)\n", __FILE__, \
29 - __LINE__, #a, a, #b, b); \
30 - abort(); \
31 - }
32 -
33 -#define EXPECT(c) \
34 - if (!(c)) { \
35 - fflush(stderr); \
36 - fflush(stdout); \
37 - fprintf(stderr, "%s: %d: failed assertion %s\n", __FILE__, \
38 - __LINE__, #c); \
39 - abort(); \
40 - }
15 +#define EXPECT_ERR(c) \
16 + do { \
17 + if (c != 0) { \
18 + fflush(stderr); \
19 + fflush(stdout); \
20 + fprintf(stderr, "%s: %d: error == %d (%s), want 0\n", \
21 + __FILE__, __LINE__, c, reftable_error_str(c)); \
22 + abort(); \
23 + } \
24 + } while (0)
25 +
26 +#define EXPECT_STREQ(a, b) \
27 + do { \
28 + if (strcmp(a, b)) { \
29 + fflush(stderr); \
30 + fflush(stdout); \
31 + fprintf(stderr, "%s:%d: %s (%s) != %s (%s)\n", __FILE__, \
32 + __LINE__, #a, a, #b, b); \
33 + abort(); \
34 + } \
35 + } while (0)
36 +
37 +#define EXPECT(c) \
38 + do { \
39 + if (!(c)) { \
40 + fflush(stderr); \
41 + fflush(stdout); \
42 + fprintf(stderr, "%s: %d: failed assertion %s\n", __FILE__, \
43 + __LINE__, #c); \
44 + abort(); \
45 + } \
46 + } while (0)
47
48 #define RUN_TEST(f) \
49 fprintf(stderr, "running %s\n", #f); \