2 // Created by jlr on 4/9/18.
11 #include <mon_internal.h>
14 * @return show command with no options
16 static MON_REQ *show_plain()
18 MON_REQ *req = mon_req_new(NULL, MON_CMD_SHOW);
24 * @param opts array of option types, terminated with OPT_TYPE_UNKNOWN
25 * @return show command with the requested options, excluding the terminator
27 static MON_REQ *show_options(const MON_OPT_TYPE *opts)
29 MON_REQ *req = mon_req_new(NULL, MON_CMD_SHOW);
32 while (*opts != OPT_TYPE_UNKNOWN) {
33 assert(MON_SUCCESS == mon_req_add_option(req, *opts));
40 * @return show command with every option
42 static MON_REQ *show_all_options()
44 MON_OPT_TYPE opts[] = {
45 OPT_TYPE_SHOW_CONFIG_FILES,
46 OPT_TYPE_SHOW_VERSION,
48 OPT_TYPE_SHOW_TID_REQS_PROCESSED,
49 OPT_TYPE_SHOW_TID_REQS_PENDING,
51 OPT_TYPE_SHOW_COMMUNITIES,
52 OPT_TYPE_UNKNOWN // terminator
55 return show_options(opts);
58 static char *read_file(const char *filename)
60 FILE *f = fopen(filename, "r");
63 ssize_t n = getline(&s, &nn, f);
67 if( (n > 0) && (s[n-1] == '\n'))
73 static int equal(MON_REQ *r1, MON_REQ *r2)
77 if (r1->command != r2->command)
80 if (mon_req_opt_count(r1) != mon_req_opt_count(r2))
83 for (ii=0; ii < mon_req_opt_count(r1); ii++) {
84 if (mon_req_opt_index(r1, ii)->type != mon_req_opt_index(r2, ii)->type)
91 static int run_test(const char *filename, MON_REQ *(generator)())
94 MON_REQ *expected = NULL;
95 char *req_json_str = NULL;
97 expected = generator();
100 req_json_str = read_file(filename);
101 assert(req_json_str);
103 req = mon_req_parse(NULL, req_json_str);
105 assert(equal(req, expected));
109 mon_req_free(expected);
117 // Test show command with no options
118 assert(run_test("req_show_no_options.test", show_plain));
120 // Test show command with all the options
121 assert(run_test("req_show_all_options.test", show_all_options));