-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnested_procedure_test.c
More file actions
149 lines (130 loc) · 4.25 KB
/
Copy pathnested_procedure_test.c
File metadata and controls
149 lines (130 loc) · 4.25 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "common.h"
static char g_procedure[1024];
#define __PROCEDURE_CAT(...) sprintf(g_procedure + strlen(g_procedure), __VA_ARGS__);
static cmdp_action_t g_action[10]; // default: CMDP_ACT_CONTINUE
#define __ACTION_SET(level, act) g_action[level] = act;
#define __START() \
START_CMD(); \
memset(g_procedure, 0, sizeof(g_procedure)); \
memset(g_action, 0, sizeof(g_action));
#define __END() \
LOG_INFO("%s %s\n", DIM("procedure:"), g_procedure); \
END_CMD();
#define __EXPECT(code, out, err, procedure) \
EXPECT_CMD(code, out, err); \
EXPECT_STREQ(procedure, g_procedure);
static cmdp_action_t cb_process(cmdp_process_param_st *params)
{
__PROCEDURE_CAT("L%d ", params->sub_level);
if (params->next == NULL && params->argc == 0 && params->opts == 0)
{
return CMDP_ACT_SHOW_HELP;
}
return g_action[params->sub_level];
}
static cmdp_command_st g_command = {
.name = "L0",
.desc = "L0 desc",
.doc = "L0 doc\n",
.fn_process = cb_process,
.sub_commands =
(cmdp_command_st *[]){
&(cmdp_command_st){
.name = "L1",
.desc = "L1 desc",
.doc = "L1 doc\n",
.fn_process = cb_process,
.sub_commands =
(cmdp_command_st *[]){
&(cmdp_command_st){
.name = "L2",
.desc = "L2 desc",
.doc = "L2 doc\n",
.fn_process = cb_process,
},
NULL,
},
},
NULL,
},
};
static const char *g_L0_help = "L0 doc\n"
" L1 L1 desc\n";
static const char *g_L1_help = "L1 doc\n"
" L2 L2 desc\n";
static const char *g_L2_help = "L2 doc\n";
static const char *err_other = "Unknown command other.\n";
UTEST(nested_procedure, L0)
{
__START();
RUN_CMD(&g_command, );
__EXPECT(0, g_L0_help, "", "L0 ");
__END();
}
UTEST(nested_procedure, L1)
{
__START();
RUN_CMD(&g_command, "L1");
__EXPECT(0, g_L1_help, "", "L0 L1 ");
__END();
}
UTEST(nested_procedure, L1_L2)
{
__START();
RUN_CMD(&g_command, "L1", "L2");
__EXPECT(0, g_L2_help, "", "L0 L1 L2 ");
__END();
}
UTEST(nested_procedure, L0_other)
{
__START();
RUN_CMD(&g_command, "other");
__EXPECT(1, "", err_other, "");
__END();
}
UTEST(nested_procedure, L1_other)
{
__START();
RUN_CMD(&g_command, "L1", "other");
__EXPECT(1, "", err_other, "");
__END();
}
UTEST(nested_procedure, L1_L2_other)
{
__START();
RUN_CMD(&g_command, "L1", "L2", "other");
__EXPECT(0, "", "", "L0 L1 L2 ");
__END();
}
UTEST(nested_procedure, L1_L2_params__L0_over)
{
__START();
__ACTION_SET(0, CMDP_ACT_OK);
RUN_CMD(&g_command, "L1", "L2", "other");
__EXPECT(0, "", "", "L0 ");
__END();
}
UTEST(nested_procedure, L1_L2_params__L0_fail)
{
__START();
__ACTION_SET(0, CMDP_ACT_ERROR);
RUN_CMD(&g_command, "L1", "L2", "other");
__EXPECT(1, "", "", "L0 ");
__END();
}
UTEST(nested_procedure, L1_L2_params__L0_over_help)
{
__START();
__ACTION_SET(0, CMDP_ACT_OK | CMDP_ACT_SHOW_HELP);
RUN_CMD(&g_command, "L1", "L2", "other");
__EXPECT(0, g_L0_help, "", "L0 ");
__END();
}
UTEST(nested_procedure, L1_L2_params__L0_fail_help)
{
__START();
__ACTION_SET(0, CMDP_ACT_ERROR | CMDP_ACT_SHOW_HELP);
RUN_CMD(&g_command, "L1", "L2", "other");
__EXPECT(1, "", g_L0_help, "L0 ");
__END();
}