2 * Unit tests of table printer
4 * (c) 2014 Robert Kessl <robert.kessl@economia.cz>
12 enum test_table_cols {
13 test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
16 static struct table_col_info test_column_order[] = { TBL_COL(test_col3_bool), TBL_COL(test_col4_double), TBL_COL(test_col2_uint), TBL_COL(test_col1_int), TBL_COL(test_col0_str) };
18 static struct table test_tbl = {
20 [test_col0_str] = TBL_COL_STR("col0_str", 20),
21 [test_col1_int] = TBL_COL_INT("col1_int", 8),
22 [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
23 [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9),
24 [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
27 TBL_COL_ORDER(test_column_order),
28 TBL_OUTPUT_HUMAN_READABLE,
29 TBL_COL_DELIMITER("\t"),
32 enum test_default_order_cols {
33 test_default_order_col0_int, test_default_order_col1_int, test_default_order_col2_int
36 static struct table test_default_order_tbl = {
38 [test_default_order_col0_int] = TBL_COL_INT("col0_int", 8),
39 [test_default_order_col1_int] = TBL_COL_INT("col1_int", 9),
40 [test_default_order_col2_int] = TBL_COL_INT("col2_int", 9),
43 TBL_OUTPUT_HUMAN_READABLE,
44 TBL_COL_DELIMITER("\t"),
47 static void do_default_order_test(struct fastbuf *out)
49 table_init(&test_default_order_tbl);
51 table_start(&test_default_order_tbl, out);
53 table_col_int(&test_default_order_tbl, test_default_order_col0_int, 0);
54 table_col_int(&test_default_order_tbl, test_default_order_col1_int, 1);
55 table_col_int(&test_default_order_tbl, test_default_order_col2_int, 2);
56 table_end_row(&test_default_order_tbl);
58 table_col_int(&test_default_order_tbl, test_default_order_col0_int, 10);
59 table_col_int(&test_default_order_tbl, test_default_order_col1_int, 11);
60 table_col_int(&test_default_order_tbl, test_default_order_col2_int, 12);
61 table_end_row(&test_default_order_tbl);
63 table_end(&test_default_order_tbl);
64 table_cleanup(&test_default_order_tbl);
68 * tests: table_col_int, table_col_uint, table_col_bool, table_col_double, table_col_printf
70 static void do_print1(struct table *test_tbl)
72 table_col_str(test_tbl, test_col0_str, "sdsdf");
73 table_col_int(test_tbl, test_col1_int, -10);
74 table_col_int(test_tbl, test_col1_int, 10000);
75 table_col_uint(test_tbl, test_col2_uint, 10);
76 table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
77 table_col_bool(test_tbl, test_col3_bool, 1);
78 table_col_double(test_tbl, test_col4_double, 1.5);
79 table_end_row(test_tbl);
81 table_col_str(test_tbl, test_col0_str, "test");
82 table_col_int(test_tbl, test_col1_int, -100);
83 table_col_uint(test_tbl, test_col2_uint, 100);
84 table_col_bool(test_tbl, test_col3_bool, 0);
85 table_col_double(test_tbl, test_col4_double, 1.5);
86 table_end_row(test_tbl);
89 static char **cli_table_opts;
92 TEST_DEFAULT_COLUMN_ORDER = 1,
93 TEST_INVALID_OPTION = 2,
94 TEST_INVALID_ORDER = 3
97 static int test_to_perform = -1;
99 static struct opt_section table_printer_opts = {
101 OPT_HELP("Options:"),
102 OPT_STRING_MULTIPLE('T', "table", cli_table_opts, OPT_REQUIRED_VALUE, "\tSets options for the table."),
103 OPT_SWITCH('d', 0, test_to_perform, TEST_DEFAULT_COLUMN_ORDER, OPT_SINGLE, "\tRun the test that uses the default column order."),
104 OPT_SWITCH('i', 0, test_to_perform, TEST_INVALID_OPTION, OPT_SINGLE, "\tTest the output for invalid option."),
105 OPT_SWITCH('n', 0, test_to_perform, TEST_INVALID_ORDER, OPT_SINGLE, "\tTest the output for invalid names of columns for the 'cols' option."),
110 static void process_command_line_opts(char *argv[], struct table *tbl)
112 GARY_INIT(cli_table_opts, 0);
114 opt_parse(&table_printer_opts, argv+1);
115 table_set_gary_options(tbl, cli_table_opts);
117 GARY_FREE(cli_table_opts);
120 static bool user_defined_option(struct table *tbl UNUSED, const char *key, const char *value, const char **err UNUSED)
122 if(value == NULL && strcmp(key, "novaluekey") == 0) {
123 printf("setting key: %s; value: (null)\n", key);
126 if(value != NULL && strcmp(value, "value") == 0 &&
127 key != NULL && strcmp(key, "valuekey") == 0) {
128 printf("setting key: %s; value: %s\n", key, value);
134 static void test_option_parser(struct table *tbl)
136 tbl->formatter->process_option = user_defined_option;
137 const char *rv = table_set_option(tbl, "invalid:option");
138 if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
140 rv = table_set_option(tbl, "invalid");
141 if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
143 rv = table_set_option(tbl, "novaluekey");
144 if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
146 rv = table_set_option(tbl, "valuekey:value");
147 if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
150 int main(int argc UNUSED, char **argv)
153 out = bfdopen_shared(1, 4096);
155 table_init(&test_tbl);
157 process_command_line_opts(argv, &test_tbl);
159 const char *rv = NULL;
160 switch(test_to_perform) {
161 case TEST_INVALID_ORDER:
162 rv = table_set_option(&test_tbl, "cols:test_col0_str,test_col1_int,xxx");
163 if(rv) printf("Tableprinter option parser returned: '%s'.\n", rv);
165 case TEST_DEFAULT_COLUMN_ORDER:
166 do_default_order_test(out);
169 case TEST_INVALID_OPTION:
170 test_option_parser(&test_tbl);
175 table_start(&test_tbl, out);
176 do_print1(&test_tbl);
177 table_end(&test_tbl);
178 table_cleanup(&test_tbl);