-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_test.c
More file actions
255 lines (224 loc) · 6.13 KB
/
Copy pathsimple_test.c
File metadata and controls
255 lines (224 loc) · 6.13 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include "common.h"
static struct
{
char *name;
bool is_student;
int age;
double height;
char *address;
} g_arg = {0};
static cmdp_action_t g_process(cmdp_process_param_st *params);
static cmdp_command_st g_command = {
.doc = "Student struct.\n\n",
.options =
(cmdp_option_st[]){
{'n', "name", "Input Name String", CMDP_TYPE_STRING_PTR, &g_arg.name},
{'s', "student", "Indicate is student", CMDP_TYPE_BOOL, &g_arg.is_student},
{'a', "age", "Input Age", CMDP_TYPE_INT4, &g_arg.age},
{0, "height", "Input Height", CMDP_TYPE_DOUBLE, &g_arg.height, "<HEIGHT>"},
{'d', NULL, "Input Address", CMDP_TYPE_STRING_PTR, &g_arg.address, "<ADDRESS>"},
{0},
},
.fn_process = g_process,
};
static char *g_expect_help = "Student struct.\n\n"
" -n, --name <string> Input Name String\n"
" -s, --student Indicate is student\n"
" -a, --age <int> Input Age\n"
" --height <HEIGHT> Input Height\n"
" -d <ADDRESS> Input Address\n";
static cmdp_action_t g_process(cmdp_process_param_st *params)
{
LOG_INFO("name: %s\n", g_arg.name);
LOG_INFO("is_student: %d\n", g_arg.is_student);
LOG_INFO("age: %d\n", g_arg.age);
LOG_INFO("height: %f\n", g_arg.height);
LOG_INFO("address: %s\n", g_arg.address);
if (params->opts != 0)
{
return CMDP_ACT_OK;
}
if (params->argc == 0)
{
return CMDP_ACT_SHOW_HELP;
}
return CMDP_ACT_OK;
}
// normal input
UTEST(simple, init_output__expect_NULL)
{
g_arg.name = "test";
g_arg.is_student = true;
g_arg.age = 18;
g_arg.height = 1.8;
g_arg.address = "test address";
START_CMD();
RUN_CMD(&g_command, );
END_CMD();
EXPECT_EQ(NULL, g_arg.name);
EXPECT_EQ(false, g_arg.is_student);
EXPECT_EQ(0, g_arg.age);
EXPECT_EQ(0, g_arg.height);
EXPECT_EQ(NULL, g_arg.address);
}
UTEST(simple, input_space)
{
START_CMD();
RUN_CMD(&g_command, "--name", "hello", "-s", "-a", "18", "--height", "1.8", "-d", "beijing");
EXPECT_STREQ("hello", g_arg.name);
EXPECT_EQ(true, g_arg.is_student);
EXPECT_EQ(18, g_arg.age);
EXPECT_EQ(1.8, g_arg.height);
EXPECT_STREQ("beijing", g_arg.address);
EXPECT_CMD(0, "", "");
END_CMD();
}
UTEST(simple, input_equal_symbol)
{
START_CMD();
RUN_CMD(&g_command, "--name=hello", "-s", "-a", "18", "--height=1.8", "-d", "beijing");
EXPECT_STREQ("hello", g_arg.name);
EXPECT_EQ(true, g_arg.is_student);
EXPECT_EQ(18, g_arg.age);
EXPECT_EQ(1.8, g_arg.height);
EXPECT_STREQ("beijing", g_arg.address);
EXPECT_CMD(0, "", "");
END_CMD();
}
UTEST(simple, continuous_short_opt)
{
START_CMD();
RUN_CMD(&g_command, "--name", "hello", "-sa", "18");
EXPECT_STREQ("hello", g_arg.name);
EXPECT_EQ(true, g_arg.is_student);
EXPECT_EQ(18, g_arg.age);
EXPECT_EQ(0.0, g_arg.height);
EXPECT_EQ(NULL, g_arg.address);
EXPECT_CMD(0, "", "");
END_CMD();
}
UTEST(simple, continuous_short_opt_inverse)
{
START_CMD();
RUN_CMD(&g_command, "--name", "hello", "-as", "18");
EXPECT_STREQ("hello", g_arg.name);
EXPECT_EQ(true, g_arg.is_student);
EXPECT_EQ(18, g_arg.age);
EXPECT_EQ(0.0, g_arg.height);
EXPECT_EQ(NULL, g_arg.address);
EXPECT_CMD(0, "", "");
END_CMD();
}
UTEST(simple, input_x2)
{
START_CMD();
RUN_CMD(&g_command, "--name", "hello", "--student", "-a", "18", "--height", "1.8", "-d", "beijing");
EXPECT_STREQ("hello", g_arg.name);
EXPECT_EQ(true, g_arg.is_student);
EXPECT_EQ(18, g_arg.age);
EXPECT_EQ(1.8, g_arg.height);
EXPECT_STREQ("beijing", g_arg.address);
EXPECT_CMD(0, "", "");
RUN_CMD(&g_command, "--height", "1.7", "-d", "shanghai");
EXPECT_EQ(NULL, g_arg.name);
EXPECT_EQ(false, g_arg.is_student);
EXPECT_EQ(0, g_arg.age);
EXPECT_EQ(1.7, g_arg.height);
EXPECT_STREQ("shanghai", g_arg.address);
EXPECT_CMD(0, "", "");
END_CMD();
}
UTEST(simple, dash_name)
{
START_CMD();
RUN_CMD(&g_command, "--name", "--hello");
EXPECT_STREQ("--hello", g_arg.name);
EXPECT_CMD(0, "", "");
END_CMD();
}
// error
UTEST(simple, unknown_short_option)
{
START_CMD();
RUN_CMD(&g_command, "-u");
EXPECT_CMD(1, "", "Unknown option -u.\n");
END_CMD();
}
UTEST(simple, unknown_long_option)
{
START_CMD();
RUN_CMD(&g_command, "--unknown");
EXPECT_CMD(1, "", "Unknown option --unknown.\n");
END_CMD();
}
UTEST(simple, require_short_arg)
{
START_CMD();
RUN_CMD(&g_command, "-a");
EXPECT_CMD(1, "", "Option -a require args.\n");
END_CMD();
}
UTEST(simple, require_short_arg_cont)
{
START_CMD();
RUN_CMD(&g_command, "-sa");
EXPECT_CMD(1, "", "Option -a require args.\n");
END_CMD();
}
UTEST(simple, require_long_arg)
{
START_CMD();
RUN_CMD(&g_command, "--name");
EXPECT_CMD(1, "", "Option --name require args.\n");
END_CMD();
}
UTEST(simple, parse_not_int)
{
START_CMD();
RUN_CMD(&g_command, "--age", "123abc");
EXPECT_CMD(1, "", "Parse int failed: 123abc.\n");
END_CMD();
}
UTEST(simple, parse_not_float)
{
START_CMD();
RUN_CMD(&g_command, "--height", "123.abc");
EXPECT_CMD(1, "", "Parse float failed: 123.abc.\n");
END_CMD();
}
UTEST(simple, parse_repeat_option_short)
{
START_CMD();
RUN_CMD(&g_command, "-s", "-s");
EXPECT_CMD(1, "", "Option -s repeat.\n");
END_CMD();
}
UTEST(simple, parse_repeat_option_long)
{
START_CMD();
RUN_CMD(&g_command, "--student", "--student");
EXPECT_CMD(1, "", "Option --student repeat.\n");
END_CMD();
}
// help doc
UTEST(simple, help__void)
{
START_CMD();
RUN_CMD(&g_command, );
EXPECT_CMD(0, g_expect_help, "");
END_CMD();
}
UTEST(simple, help__h)
{
START_CMD();
RUN_CMD(&g_command, "-h");
EXPECT_CMD(0, g_expect_help, "");
END_CMD();
}
UTEST(simple, help__help)
{
START_CMD();
RUN_CMD(&g_command, "--help");
EXPECT_CMD(0, g_expect_help, "");
END_CMD();
}