Skip to content

Commit e32b8ec

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 564d025 commit e32b8ec

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

reftable/test_framework.h

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,38 @@ license that can be found in the LICENSE file or at
1212
#include "system.h"
1313
#include "reftable-error.h"
1414

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)
4147

4248
#define RUN_TEST(f) \
4349
fprintf(stderr, "running %s\n", #f); \

0 commit comments

Comments
 (0)