-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs_test.c
64 lines (53 loc) · 1.74 KB
/
args_test.c
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
#include <assert.h>
#include <stddef.h>
#include <string.h>
#include "args.h"
#include "testlib.h"
static void args_any_order()
{
int ret;
json_args_t res;
char* argv[] = {"./json", "-i", "test-input.txt", "-o", "/tmp/test-output.txt"};
ret = get_args(&res, 5, argv);
assert(ret == 0);
assert(strcmp(res.input_file, "test-input.txt") == 0);
assert(strcmp(res.output_file, "/tmp/test-output.txt") == 0);
char *argv2[] = {"./any-binary", "-o", "output.json", "-i", "./path/input.json"};
ret = get_args(&res, 5, argv2);
assert(ret == 0);
assert(strcmp(res.input_file, "./path/input.json") == 0);
assert(strcmp(res.output_file, "output.json") == 0);
}
static void less_args()
{
int ret;
json_args_t res = {NULL, NULL};
char* argv[] = {"./json", "-i", "test-input.txt"};
ret = get_args(&res, 3, argv);
assert(ret == 1);
assert(res.input_file == NULL);
assert(res.output_file == NULL);
}
static void more_args()
{
int ret;
json_args_t res;
char* argv[] = {"./json", "-i", "test-input.txt", "-o", "/tmp/test-output.txt", "-test", "something"};
ret = get_args(&res, 7, argv);
assert(ret == 0);
assert(strcmp(res.input_file, "test-input.txt") == 0);
assert(strcmp(res.output_file, "/tmp/test-output.txt") == 0);
char *argv2[] = {"./any-binary", "-o", "output.json", "-i", "./path/input.json", "something-file.json"};
ret = get_args(&res, 6, argv2);
assert(ret == 0);
assert(strcmp(res.input_file, "./path/input.json") == 0);
assert(strcmp(res.output_file, "output.json") == 0);
}
static void* b() { return NULL; }
static void a() {}
int main(void)
{
test("required arguments in any order work", args_any_order, b, a);
test("less arguments work", less_args, b, a);
test("more arguments work", more_args, b, a);
}