]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-format-table.c
format-table: automatically show empty cells in grey
[thirdparty/systemd.git] / src / test / test-format-table.c
CommitLineData
1960e736
LP
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3#include "alloc-util.h"
4#include "format-table.h"
5#include "string-util.h"
6#include "time-util.h"
7
a6e96471
YW
8static void test_issue_9549(void) {
9 _cleanup_(table_unrefp) Table *table = NULL;
10 _cleanup_free_ char *formatted = NULL;
11
9969b542 12 assert_se(table = table_new("name", "type", "ro", "usage", "created", "modified"));
a6e96471
YW
13 assert_se(table_set_align_percent(table, TABLE_HEADER_CELL(3), 100) >= 0);
14 assert_se(table_add_many(table,
15 TABLE_STRING, "foooo",
16 TABLE_STRING, "raw",
17 TABLE_BOOLEAN, false,
18 TABLE_SIZE, (uint64_t) (673.7*1024*1024),
19 TABLE_STRING, "Wed 2018-07-11 00:10:33 JST",
20 TABLE_STRING, "Wed 2018-07-11 00:16:00 JST") >= 0);
21
22 table_set_width(table, 75);
23 assert_se(table_format(table, &formatted) >= 0);
24
25 printf("%s\n", formatted);
26 assert_se(streq(formatted,
27 "NAME TYPE RO USAGE CREATED MODIFIED \n"
28 "foooo raw no 673.6M Wed 2018-07-11 00:10:33 J… Wed 2018-07-11 00:16:00 JST\n"
29 ));
30}
31
1960e736
LP
32int main(int argc, char *argv[]) {
33
34 _cleanup_(table_unrefp) Table *t = NULL;
35 _cleanup_free_ char *formatted = NULL;
36
dea55040 37 assert_se(setenv("SYSTEMD_COLORS", "0", 1) >= 0);
1960e736
LP
38 assert_se(setenv("COLUMNS", "40", 1) >= 0);
39
9969b542 40 assert_se(t = table_new("one", "two", "three"));
1960e736
LP
41
42 assert_se(table_set_align_percent(t, TABLE_HEADER_CELL(2), 100) >= 0);
43
44 assert_se(table_add_many(t,
45 TABLE_STRING, "xxx",
46 TABLE_STRING, "yyy",
47 TABLE_BOOLEAN, true) >= 0);
48
49 assert_se(table_add_many(t,
50 TABLE_STRING, "a long field",
51 TABLE_STRING, "yyy",
8792cecc 52 TABLE_SET_UPPERCASE, 1,
1960e736
LP
53 TABLE_BOOLEAN, false) >= 0);
54
55 assert_se(table_format(t, &formatted) >= 0);
56 printf("%s\n", formatted);
57
58 assert_se(streq(formatted,
59 "ONE TWO THREE\n"
60 "xxx yyy yes\n"
8792cecc 61 "a long field YYY no\n"));
1960e736
LP
62
63 formatted = mfree(formatted);
64
65 table_set_width(t, 40);
66
67 assert_se(table_format(t, &formatted) >= 0);
68 printf("%s\n", formatted);
69
70 assert_se(streq(formatted,
71 "ONE TWO THREE\n"
72 "xxx yyy yes\n"
8792cecc 73 "a long field YYY no\n"));
1960e736
LP
74
75 formatted = mfree(formatted);
76
77 table_set_width(t, 12);
78 assert_se(table_format(t, &formatted) >= 0);
79 printf("%s\n", formatted);
80
81 assert_se(streq(formatted,
82 "ONE TWO THR…\n"
83 "xxx yyy yes\n"
8792cecc 84 "a … YYY no\n"));
1960e736
LP
85
86 formatted = mfree(formatted);
87
88 table_set_width(t, 5);
89 assert_se(table_format(t, &formatted) >= 0);
90 printf("%s\n", formatted);
91
92 assert_se(streq(formatted,
93 "… … …\n"
94 "… … …\n"
95 "… … …\n"));
96
97 formatted = mfree(formatted);
98
99 table_set_width(t, 3);
100 assert_se(table_format(t, &formatted) >= 0);
101 printf("%s\n", formatted);
102
103 assert_se(streq(formatted,
104 "… … …\n"
105 "… … …\n"
106 "… … …\n"));
107
108 formatted = mfree(formatted);
109
110 table_set_width(t, (size_t) -1);
111 assert_se(table_set_sort(t, (size_t) 0, (size_t) 2, (size_t) -1) >= 0);
112
113 assert_se(table_format(t, &formatted) >= 0);
114 printf("%s\n", formatted);
115
116 assert_se(streq(formatted,
117 "ONE TWO THREE\n"
8792cecc 118 "a long field YYY no\n"
1960e736
LP
119 "xxx yyy yes\n"));
120
121 formatted = mfree(formatted);
122
123 table_set_header(t, false);
124
125 assert_se(table_add_many(t,
126 TABLE_STRING, "fäää",
127 TABLE_STRING, "uuu",
128 TABLE_BOOLEAN, true) >= 0);
129
130 assert_se(table_add_many(t,
131 TABLE_STRING, "fäää",
132 TABLE_STRING, "zzz",
133 TABLE_BOOLEAN, false) >= 0);
134
135 assert_se(table_add_many(t,
136 TABLE_EMPTY,
137 TABLE_SIZE, (uint64_t) 4711,
138 TABLE_TIMESPAN, (usec_t) 5*USEC_PER_MINUTE) >= 0);
139
140 assert_se(table_format(t, &formatted) >= 0);
141 printf("%s\n", formatted);
142
143 assert_se(streq(formatted,
8792cecc 144 "a long field YYY no\n"
1960e736
LP
145 "fäää zzz no\n"
146 "fäää uuu yes\n"
147 "xxx yyy yes\n"
148 " 4.6K 5min\n"));
149
150 formatted = mfree(formatted);
151
152 assert_se(table_set_display(t, (size_t) 2, (size_t) 0, (size_t) 2, (size_t) 0, (size_t) 0, (size_t) -1) >= 0);
153
154 assert_se(table_format(t, &formatted) >= 0);
155 printf("%s\n", formatted);
156
157 assert_se(streq(formatted,
158 " no a long f… no a long f… a long fi…\n"
159 " no fäää no fäää fäää \n"
160 " yes fäää yes fäää fäää \n"
161 " yes xxx yes xxx xxx \n"
162 "5min 5min \n"));
163
a6e96471
YW
164 test_issue_9549();
165
1960e736
LP
166 return 0;
167}