1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-05 06:52:22 +03:00

test-format-table: add tests for TABLE_STRV

This commit is contained in:
Yu Watanabe 2020-01-14 19:07:39 +09:00
parent 29e15e98c7
commit bbaba5748d

View File

@ -5,6 +5,7 @@
#include "alloc-util.h"
#include "format-table.h"
#include "string-util.h"
#include "strv.h"
#include "time-util.h"
static void test_issue_9549(void) {
@ -143,6 +144,118 @@ static void test_multiline(void) {
formatted = mfree(formatted);
}
static void test_strv(void) {
_cleanup_(table_unrefp) Table *table = NULL;
_cleanup_free_ char *formatted = NULL;
assert_se(table = table_new("foo", "bar"));
assert_se(table_set_align_percent(table, TABLE_HEADER_CELL(1), 100) >= 0);
assert_se(table_add_many(table,
TABLE_STRV, STRV_MAKE("three", "different", "lines"),
TABLE_STRV, STRV_MAKE("two", "lines")) >= 0);
table_set_cell_height_max(table, 1);
assert_se(table_format(table, &formatted) >= 0);
fputs(formatted, stdout);
assert_se(streq(formatted,
"FOO BAR\n"
"three… two…\n"));
formatted = mfree(formatted);
table_set_cell_height_max(table, 2);
assert_se(table_format(table, &formatted) >= 0);
fputs(formatted, stdout);
assert_se(streq(formatted,
"FOO BAR\n"
"three two\n"
"different… lines\n"));
formatted = mfree(formatted);
table_set_cell_height_max(table, 3);
assert_se(table_format(table, &formatted) >= 0);
fputs(formatted, stdout);
assert_se(streq(formatted,
"FOO BAR\n"
"three two\n"
"different lines\n"
"lines \n"));
formatted = mfree(formatted);
table_set_cell_height_max(table, (size_t) -1);
assert_se(table_format(table, &formatted) >= 0);
fputs(formatted, stdout);
assert_se(streq(formatted,
"FOO BAR\n"
"three two\n"
"different lines\n"
"lines \n"));
formatted = mfree(formatted);
assert_se(table_add_many(table,
TABLE_STRING, "short",
TABLE_STRV, STRV_MAKE("a", "pair")) >= 0);
assert_se(table_add_many(table,
TABLE_STRV, STRV_MAKE("short2"),
TABLE_STRV, STRV_MAKE("a", "four", "line", "cell")) >= 0);
table_set_cell_height_max(table, 1);
assert_se(table_format(table, &formatted) >= 0);
fputs(formatted, stdout);
assert_se(streq(formatted,
"FOO BAR\n"
"three… two…\n"
"short a…\n"
"short2 a…\n"));
formatted = mfree(formatted);
table_set_cell_height_max(table, 2);
assert_se(table_format(table, &formatted) >= 0);
fputs(formatted, stdout);
assert_se(streq(formatted,
"FOO BAR\n"
"three two\n"
"different… lines\n"
"short a\n"
" pair\n"
"short2 a\n"
" four…\n"));
formatted = mfree(formatted);
table_set_cell_height_max(table, 3);
assert_se(table_format(table, &formatted) >= 0);
fputs(formatted, stdout);
assert_se(streq(formatted,
"FOO BAR\n"
"three two\n"
"different lines\n"
"lines \n"
"short a\n"
" pair\n"
"short2 a\n"
" four\n"
" line…\n"));
formatted = mfree(formatted);
table_set_cell_height_max(table, (size_t) -1);
assert_se(table_format(table, &formatted) >= 0);
fputs(formatted, stdout);
assert_se(streq(formatted,
"FOO BAR\n"
"three two\n"
"different lines\n"
"lines \n"
"short a\n"
" pair\n"
"short2 a\n"
" four\n"
" line\n"
" cell\n"));
formatted = mfree(formatted);
}
int main(int argc, char *argv[]) {
_cleanup_(table_unrefp) Table *t = NULL;
@ -285,6 +398,7 @@ int main(int argc, char *argv[]) {
test_issue_9549();
test_multiline();
test_strv();
return 0;
}