-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
56 lines (46 loc) · 4.22 KB
/
common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#pragma once
#include "cmdparser.h"
#include "utest.h"
#pragma GCC diagnostic ignored "-Wformat-security"
extern bool g_log_switch;
#define LOG_INFO(...) \
if (g_log_switch) \
{ \
fprintf(stdout, __VA_ARGS__); \
}
#define DIM(STRING) "\x1b[2m" STRING "\x1b[0m"
#define countof(array) (sizeof(array) / sizeof(array[0]))
#define __COMBINE__(X, Y) X##Y
#define COMBINE(X, Y) __COMBINE__(X, Y)
#define START_CMD() \
char *r_out = NULL; \
size_t _out_len = 0; \
FILE *out_stream = open_memstream(&r_out, &_out_len); \
char *r_err = NULL; \
size_t _err_len = 0; \
FILE *err_stream = open_memstream(&r_err, &_err_len); \
cmdp_ctx _ctx = {0}; \
cmdp_set_default_context(&_ctx); \
_ctx.out_stream = out_stream; \
_ctx.err_stream = err_stream; \
int r_code = 0;
#define RUN_CMD(ref_cmdp, ...) \
char *COMBINE(_arg_, __LINE__)[] = {__VA_ARGS__}; \
r_code = cmdp_run(countof(COMBINE(_arg_, __LINE__)), COMBINE(_arg_, __LINE__), ref_cmdp, &_ctx); \
fflush(out_stream); \
fflush(err_stream); \
LOG_INFO("%s %d\n", DIM("r_code:"), r_code); \
LOG_INFO("%s\n%s\n", DIM("r_out:"), r_out); \
LOG_INFO("%s\n%s\n", DIM("r_err:"), r_err);
#define EXPECT_CMD(code, out, err) \
EXPECT_EQ(code, r_code); \
EXPECT_STREQ(out, r_out); \
EXPECT_STREQ(err, r_err);
#define END_CMD() \
fclose(out_stream); \
fclose(err_stream); \
(void)r_code; \
free(r_out); \
r_out = NULL; \
free(r_err); \
r_err = NULL;