| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "ansi-color.h" |
| 8 |
#include "env-util.h" |
| 9 |
#include "format-table.h" |
| 10 |
#include "json-util.h" |
| 11 |
#include "terminal-util.h" |
| 12 |
#include "tests.h" |
| 13 |
#include "time-util.h" |
| 14 |
|
| 15 |
TEST(issue_9549) { |
| 16 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 17 |
_cleanup_free_ char *formatted = NULL; |
| 18 |
|
| 19 |
ASSERT_NOT_NULL((table = table_new("name", "type", "ro", "usage", "created", "modified"))); |
| 20 |
ASSERT_OK(table_set_align_percent(table, TABLE_HEADER_CELL(3), 100)); |
| 21 |
ASSERT_OK(table_add_many(table, |
| 22 |
TABLE_STRING, "foooo", |
| 23 |
TABLE_STRING, "raw", |
| 24 |
TABLE_BOOLEAN, false, |
| 25 |
TABLE_SIZE, (uint64_t) (673.7*1024*1024), |
| 26 |
TABLE_STRING, "Wed 2018-07-11 00:10:33 JST", |
| 27 |
TABLE_STRING, "Wed 2018-07-11 00:16:00 JST")); |
| 28 |
|
| 29 |
table_set_width(table, 75); |
| 30 |
ASSERT_OK(table_format(table, &formatted)); |
| 31 |
|
| 32 |
printf("%s\n", formatted); |
| 33 |
ASSERT_STREQ(formatted, |
| 34 |
"NAME TYPE RO USAGE CREATED MODIFIED\n" |
| 35 |
"foooo raw no 673.6M Wed 2018-07-11 00:10:33 J… Wed 2018-07-11 00:16:00 JST\n"); |
| 36 |
} |
| 37 |
|
| 38 |
TEST(multiline) { |
| 39 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 40 |
_cleanup_free_ char *formatted = NULL; |
| 41 |
|
| 42 |
ASSERT_NOT_NULL((table = table_new("foo", "bar"))); |
| 43 |
|
| 44 |
ASSERT_OK(table_set_align_percent(table, TABLE_HEADER_CELL(1), 100)); |
| 45 |
|
| 46 |
ASSERT_OK(table_add_many(table, |
| 47 |
TABLE_STRING, "three\ndifferent\nlines", |
| 48 |
TABLE_STRING, "two\nlines\n")); |
| 49 |
|
| 50 |
table_set_cell_height_max(table, 1); |
| 51 |
ASSERT_OK(table_format(table, &formatted)); |
| 52 |
fputs(formatted, stdout); |
| 53 |
ASSERT_STREQ(formatted, |
| 54 |
"FOO BAR\n" |
| 55 |
"three… two…\n"); |
| 56 |
formatted = mfree(formatted); |
| 57 |
|
| 58 |
table_set_cell_height_max(table, 2); |
| 59 |
ASSERT_OK(table_format(table, &formatted)); |
| 60 |
fputs(formatted, stdout); |
| 61 |
ASSERT_STREQ(formatted, |
| 62 |
"FOO BAR\n" |
| 63 |
"three two\n" |
| 64 |
"different… lines\n"); |
| 65 |
formatted = mfree(formatted); |
| 66 |
|
| 67 |
table_set_cell_height_max(table, 3); |
| 68 |
ASSERT_OK(table_format(table, &formatted)); |
| 69 |
fputs(formatted, stdout); |
| 70 |
ASSERT_STREQ(formatted, |
| 71 |
"FOO BAR\n" |
| 72 |
"three two\n" |
| 73 |
"different lines\n" |
| 74 |
"lines \n"); |
| 75 |
formatted = mfree(formatted); |
| 76 |
|
| 77 |
table_set_cell_height_max(table, SIZE_MAX); |
| 78 |
ASSERT_OK(table_format(table, &formatted)); |
| 79 |
fputs(formatted, stdout); |
| 80 |
ASSERT_STREQ(formatted, |
| 81 |
"FOO BAR\n" |
| 82 |
"three two\n" |
| 83 |
"different lines\n" |
| 84 |
"lines \n"); |
| 85 |
formatted = mfree(formatted); |
| 86 |
|
| 87 |
ASSERT_OK(table_add_many(table, |
| 88 |
TABLE_STRING, "short", |
| 89 |
TABLE_STRING, "a\npair")); |
| 90 |
|
| 91 |
ASSERT_OK(table_add_many(table, |
| 92 |
TABLE_STRING, "short2\n", |
| 93 |
TABLE_STRING, "a\nfour\nline\ncell")); |
| 94 |
|
| 95 |
table_set_cell_height_max(table, 1); |
| 96 |
ASSERT_OK(table_format(table, &formatted)); |
| 97 |
fputs(formatted, stdout); |
| 98 |
ASSERT_STREQ(formatted, |
| 99 |
"FOO BAR\n" |
| 100 |
"three… two…\n" |
| 101 |
"short a…\n" |
| 102 |
"short2 a…\n"); |
| 103 |
formatted = mfree(formatted); |
| 104 |
|
| 105 |
table_set_cell_height_max(table, 2); |
| 106 |
ASSERT_OK(table_format(table, &formatted)); |
| 107 |
fputs(formatted, stdout); |
| 108 |
ASSERT_STREQ(formatted, |
| 109 |
"FOO BAR\n" |
| 110 |
"three two\n" |
| 111 |
"different… lines\n" |
| 112 |
"short a\n" |
| 113 |
" pair\n" |
| 114 |
"short2 a\n" |
| 115 |
" four…\n"); |
| 116 |
formatted = mfree(formatted); |
| 117 |
|
| 118 |
table_set_cell_height_max(table, 3); |
| 119 |
ASSERT_OK(table_format(table, &formatted)); |
| 120 |
fputs(formatted, stdout); |
| 121 |
ASSERT_STREQ(formatted, |
| 122 |
"FOO BAR\n" |
| 123 |
"three two\n" |
| 124 |
"different lines\n" |
| 125 |
"lines \n" |
| 126 |
"short a\n" |
| 127 |
" pair\n" |
| 128 |
"short2 a\n" |
| 129 |
" four\n" |
| 130 |
" line…\n"); |
| 131 |
formatted = mfree(formatted); |
| 132 |
|
| 133 |
table_set_cell_height_max(table, SIZE_MAX); |
| 134 |
ASSERT_OK(table_format(table, &formatted)); |
| 135 |
fputs(formatted, stdout); |
| 136 |
ASSERT_STREQ(formatted, |
| 137 |
"FOO BAR\n" |
| 138 |
"three two\n" |
| 139 |
"different lines\n" |
| 140 |
"lines \n" |
| 141 |
"short a\n" |
| 142 |
" pair\n" |
| 143 |
"short2 a\n" |
| 144 |
" four\n" |
| 145 |
" line\n" |
| 146 |
" cell\n"); |
| 147 |
formatted = mfree(formatted); |
| 148 |
} |
| 149 |
|
| 150 |
TEST(strv) { |
| 151 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 152 |
_cleanup_free_ char *formatted = NULL; |
| 153 |
|
| 154 |
ASSERT_NOT_NULL((table = table_new("foo", "bar"))); |
| 155 |
|
| 156 |
ASSERT_OK(table_set_align_percent(table, TABLE_HEADER_CELL(1), 100)); |
| 157 |
|
| 158 |
ASSERT_OK(table_add_many(table, |
| 159 |
TABLE_STRV, STRV_MAKE("three", "different", "lines"), |
| 160 |
TABLE_STRV, STRV_MAKE("two", "lines"))); |
| 161 |
|
| 162 |
table_set_cell_height_max(table, 1); |
| 163 |
ASSERT_OK(table_format(table, &formatted)); |
| 164 |
fputs(formatted, stdout); |
| 165 |
ASSERT_STREQ(formatted, |
| 166 |
"FOO BAR\n" |
| 167 |
"three… two…\n"); |
| 168 |
formatted = mfree(formatted); |
| 169 |
|
| 170 |
table_set_cell_height_max(table, 2); |
| 171 |
ASSERT_OK(table_format(table, &formatted)); |
| 172 |
fputs(formatted, stdout); |
| 173 |
ASSERT_STREQ(formatted, |
| 174 |
"FOO BAR\n" |
| 175 |
"three two\n" |
| 176 |
"different… lines\n"); |
| 177 |
formatted = mfree(formatted); |
| 178 |
|
| 179 |
table_set_cell_height_max(table, 3); |
| 180 |
ASSERT_OK(table_format(table, &formatted)); |
| 181 |
fputs(formatted, stdout); |
| 182 |
ASSERT_STREQ(formatted, |
| 183 |
"FOO BAR\n" |
| 184 |
"three two\n" |
| 185 |
"different lines\n" |
| 186 |
"lines \n"); |
| 187 |
formatted = mfree(formatted); |
| 188 |
|
| 189 |
table_set_cell_height_max(table, SIZE_MAX); |
| 190 |
ASSERT_OK(table_format(table, &formatted)); |
| 191 |
fputs(formatted, stdout); |
| 192 |
ASSERT_STREQ(formatted, |
| 193 |
"FOO BAR\n" |
| 194 |
"three two\n" |
| 195 |
"different lines\n" |
| 196 |
"lines \n"); |
| 197 |
formatted = mfree(formatted); |
| 198 |
|
| 199 |
ASSERT_OK(table_add_many(table, |
| 200 |
TABLE_STRING, "short", |
| 201 |
TABLE_STRV, STRV_MAKE("a", "pair"))); |
| 202 |
|
| 203 |
ASSERT_OK(table_add_many(table, |
| 204 |
TABLE_STRV, STRV_MAKE("short2"), |
| 205 |
TABLE_STRV, STRV_MAKE("a", "four", "line", "cell"))); |
| 206 |
|
| 207 |
table_set_cell_height_max(table, 1); |
| 208 |
ASSERT_OK(table_format(table, &formatted)); |
| 209 |
fputs(formatted, stdout); |
| 210 |
ASSERT_STREQ(formatted, |
| 211 |
"FOO BAR\n" |
| 212 |
"three… two…\n" |
| 213 |
"short a…\n" |
| 214 |
"short2 a…\n"); |
| 215 |
formatted = mfree(formatted); |
| 216 |
|
| 217 |
table_set_cell_height_max(table, 2); |
| 218 |
ASSERT_OK(table_format(table, &formatted)); |
| 219 |
fputs(formatted, stdout); |
| 220 |
ASSERT_STREQ(formatted, |
| 221 |
"FOO BAR\n" |
| 222 |
"three two\n" |
| 223 |
"different… lines\n" |
| 224 |
"short a\n" |
| 225 |
" pair\n" |
| 226 |
"short2 a\n" |
| 227 |
" four…\n"); |
| 228 |
formatted = mfree(formatted); |
| 229 |
|
| 230 |
table_set_cell_height_max(table, 3); |
| 231 |
ASSERT_OK(table_format(table, &formatted)); |
| 232 |
fputs(formatted, stdout); |
| 233 |
ASSERT_STREQ(formatted, |
| 234 |
"FOO BAR\n" |
| 235 |
"three two\n" |
| 236 |
"different lines\n" |
| 237 |
"lines \n" |
| 238 |
"short a\n" |
| 239 |
" pair\n" |
| 240 |
"short2 a\n" |
| 241 |
" four\n" |
| 242 |
" line…\n"); |
| 243 |
formatted = mfree(formatted); |
| 244 |
|
| 245 |
table_set_cell_height_max(table, SIZE_MAX); |
| 246 |
ASSERT_OK(table_format(table, &formatted)); |
| 247 |
fputs(formatted, stdout); |
| 248 |
ASSERT_STREQ(formatted, |
| 249 |
"FOO BAR\n" |
| 250 |
"three two\n" |
| 251 |
"different lines\n" |
| 252 |
"lines \n" |
| 253 |
"short a\n" |
| 254 |
" pair\n" |
| 255 |
"short2 a\n" |
| 256 |
" four\n" |
| 257 |
" line\n" |
| 258 |
" cell\n"); |
| 259 |
formatted = mfree(formatted); |
| 260 |
} |
| 261 |
|
| 262 |
TEST(strv_wrapped) { |
| 263 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 264 |
_cleanup_free_ char *formatted = NULL; |
| 265 |
|
| 266 |
ASSERT_NOT_NULL((table = table_new("foo", "bar"))); |
| 267 |
|
| 268 |
ASSERT_OK(table_set_align_percent(table, TABLE_HEADER_CELL(1), 100)); |
| 269 |
|
| 270 |
ASSERT_OK(table_add_many(table, |
| 271 |
TABLE_STRV_WRAPPED, STRV_MAKE("three", "different", "lines"), |
| 272 |
TABLE_STRV_WRAPPED, STRV_MAKE("two", "lines"))); |
| 273 |
|
| 274 |
table_set_cell_height_max(table, 1); |
| 275 |
ASSERT_OK(table_format(table, &formatted)); |
| 276 |
fputs(formatted, stdout); |
| 277 |
ASSERT_STREQ(formatted, |
| 278 |
"FOO BAR\n" |
| 279 |
"three different lines two lines\n"); |
| 280 |
formatted = mfree(formatted); |
| 281 |
|
| 282 |
table_set_cell_height_max(table, 2); |
| 283 |
ASSERT_OK(table_format(table, &formatted)); |
| 284 |
fputs(formatted, stdout); |
| 285 |
ASSERT_STREQ(formatted, |
| 286 |
"FOO BAR\n" |
| 287 |
"three different lines two lines\n"); |
| 288 |
formatted = mfree(formatted); |
| 289 |
|
| 290 |
table_set_cell_height_max(table, 3); |
| 291 |
ASSERT_OK(table_format(table, &formatted)); |
| 292 |
fputs(formatted, stdout); |
| 293 |
ASSERT_STREQ(formatted, |
| 294 |
"FOO BAR\n" |
| 295 |
"three different lines two lines\n"); |
| 296 |
formatted = mfree(formatted); |
| 297 |
|
| 298 |
table_set_cell_height_max(table, SIZE_MAX); |
| 299 |
ASSERT_OK(table_format(table, &formatted)); |
| 300 |
fputs(formatted, stdout); |
| 301 |
ASSERT_STREQ(formatted, |
| 302 |
"FOO BAR\n" |
| 303 |
"three different lines two lines\n"); |
| 304 |
formatted = mfree(formatted); |
| 305 |
|
| 306 |
ASSERT_OK(table_add_many(table, |
| 307 |
TABLE_STRING, "short", |
| 308 |
TABLE_STRV_WRAPPED, STRV_MAKE("a", "pair"))); |
| 309 |
|
| 310 |
ASSERT_OK(table_add_many(table, |
| 311 |
TABLE_STRV_WRAPPED, STRV_MAKE("short2"), |
| 312 |
TABLE_STRV_WRAPPED, STRV_MAKE("a", "eight", "line", "ćęłł", |
| 313 |
"___5___", "___6___", "___7___", "___8___"))); |
| 314 |
|
| 315 |
table_set_cell_height_max(table, 1); |
| 316 |
ASSERT_OK(table_format(table, &formatted)); |
| 317 |
fputs(formatted, stdout); |
| 318 |
ASSERT_STREQ(formatted, |
| 319 |
"FOO BAR\n" |
| 320 |
"three different… two lines\n" |
| 321 |
"short a pair\n" |
| 322 |
"short2 a eight line ćęłł…\n"); |
| 323 |
formatted = mfree(formatted); |
| 324 |
|
| 325 |
table_set_cell_height_max(table, 2); |
| 326 |
ASSERT_OK(table_format(table, &formatted)); |
| 327 |
fputs(formatted, stdout); |
| 328 |
ASSERT_STREQ(formatted, |
| 329 |
"FOO BAR\n" |
| 330 |
"three different two lines\n" |
| 331 |
"lines \n" |
| 332 |
"short a pair\n" |
| 333 |
"short2 a eight line ćęłł\n" |
| 334 |
" ___5___ ___6___…\n"); |
| 335 |
formatted = mfree(formatted); |
| 336 |
|
| 337 |
table_set_cell_height_max(table, 3); |
| 338 |
ASSERT_OK(table_format(table, &formatted)); |
| 339 |
fputs(formatted, stdout); |
| 340 |
ASSERT_STREQ(formatted, |
| 341 |
"FOO BAR\n" |
| 342 |
"three different two lines\n" |
| 343 |
"lines \n" |
| 344 |
"short a pair\n" |
| 345 |
"short2 a eight line ćęłł\n" |
| 346 |
" ___5___ ___6___\n" |
| 347 |
" ___7___ ___8___\n"); |
| 348 |
formatted = mfree(formatted); |
| 349 |
|
| 350 |
table_set_cell_height_max(table, SIZE_MAX); |
| 351 |
ASSERT_OK(table_format(table, &formatted)); |
| 352 |
fputs(formatted, stdout); |
| 353 |
ASSERT_STREQ(formatted, |
| 354 |
"FOO BAR\n" |
| 355 |
"three different two lines\n" |
| 356 |
"lines \n" |
| 357 |
"short a pair\n" |
| 358 |
"short2 a eight line ćęłł\n" |
| 359 |
" ___5___ ___6___\n" |
| 360 |
" ___7___ ___8___\n"); |
| 361 |
formatted = mfree(formatted); |
| 362 |
} |
| 363 |
|
| 364 |
TEST(json) { |
| 365 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *w = NULL; |
| 366 |
_cleanup_(table_unrefp) Table *t = NULL; |
| 367 |
|
| 368 |
ASSERT_NOT_NULL((t = table_new_raw(4))); |
| 369 |
|
| 370 |
ASSERT_OK(table_add_many(t, |
| 371 |
TABLE_HEADER, "foo bar", |
| 372 |
TABLE_HEADER, "quux", |
| 373 |
TABLE_HEADER, "piep miau", |
| 374 |
TABLE_HEADER, "asdf", |
| 375 |
TABLE_SET_JSON_FIELD_NAME, "asdf-custom")); |
| 376 |
ASSERT_OK(table_set_json_field_name(t, 2, "ZZZ")); |
| 377 |
ASSERT_OK(table_set_json_field_name(t, 2, NULL)); |
| 378 |
ASSERT_OK(table_set_json_field_name(t, 2, "zzz")); |
| 379 |
|
| 380 |
ASSERT_OK(table_add_many(t, |
| 381 |
TABLE_STRING, "v1", |
| 382 |
TABLE_UINT64, UINT64_C(4711), |
| 383 |
TABLE_BOOLEAN, true, |
| 384 |
TABLE_EMPTY)); |
| 385 |
|
| 386 |
ASSERT_OK(table_add_many(t, |
| 387 |
TABLE_STRV, STRV_MAKE("a", "b", "c"), |
| 388 |
TABLE_EMPTY, |
| 389 |
TABLE_MODE, 0755, |
| 390 |
TABLE_EMPTY)); |
| 391 |
|
| 392 |
ASSERT_OK(table_to_json(t, &v)); |
| 393 |
|
| 394 |
ASSERT_OK(sd_json_build(&w, |
| 395 |
SD_JSON_BUILD_ARRAY( |
| 396 |
SD_JSON_BUILD_OBJECT( |
| 397 |
SD_JSON_BUILD_PAIR("foo_bar", JSON_BUILD_CONST_STRING("v1")), |
| 398 |
SD_JSON_BUILD_PAIR_UNSIGNED("quux", 4711), |
| 399 |
SD_JSON_BUILD_PAIR_BOOLEAN("zzz", true), |
| 400 |
SD_JSON_BUILD_PAIR("asdf-custom", SD_JSON_BUILD_NULL)), |
| 401 |
SD_JSON_BUILD_OBJECT( |
| 402 |
SD_JSON_BUILD_PAIR("foo_bar", SD_JSON_BUILD_STRV(STRV_MAKE("a", "b", "c"))), |
| 403 |
SD_JSON_BUILD_PAIR("quux", SD_JSON_BUILD_NULL), |
| 404 |
SD_JSON_BUILD_PAIR_UNSIGNED("zzz", 0755), |
| 405 |
SD_JSON_BUILD_PAIR("asdf-custom", SD_JSON_BUILD_NULL))))); |
| 406 |
|
| 407 |
ASSERT_TRUE(sd_json_variant_equal(v, w)); |
| 408 |
} |
| 409 |
|
| 410 |
TEST(json_mangling) { |
| 411 |
static const struct { |
| 412 |
const char *arg; |
| 413 |
const char *exp; |
| 414 |
} cases[] = { |
| 415 |
/* Not Mangled */ |
| 416 |
{ "foo", "foo" }, |
| 417 |
{ "foo_bar", "foo_bar" }, |
| 418 |
{ "fooBar", "fooBar" }, |
| 419 |
{ "fooBar123", "fooBar123" }, |
| 420 |
{ "foo_bar123", "foo_bar123" }, |
| 421 |
{ ALPHANUMERICAL, ALPHANUMERICAL }, |
| 422 |
{ "_123", "_123" }, |
| 423 |
|
| 424 |
/* Mangled */ |
| 425 |
{ "Foo Bar", "foo_bar" }, |
| 426 |
{ "Foo-Bar", "foo_bar" }, |
| 427 |
{ "Foo@Bar", "foo_bar" }, |
| 428 |
{ "Foo (Bar)", "foo__bar_"}, |
| 429 |
{ "MixedCase ALLCAPS", "mixedCase_ALLCAPS" }, |
| 430 |
{ "_X", "_x" }, |
| 431 |
{ "_Foo", "_foo" }, |
| 432 |
}; |
| 433 |
|
| 434 |
FOREACH_ELEMENT(i, cases) { |
| 435 |
_cleanup_free_ char *ret = NULL; |
| 436 |
ASSERT_NOT_NULL((ret = table_mangle_to_json_field_name(i->arg))); |
| 437 |
printf("\"%s\" -> \"%s\"\n", i->arg, ret); |
| 438 |
ASSERT_STREQ(ret, i->exp); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
TEST(table) { |
| 443 |
_cleanup_(table_unrefp) Table *t = NULL; |
| 444 |
_cleanup_free_ char *formatted = NULL; |
| 445 |
|
| 446 |
ASSERT_NOT_NULL((t = table_new("one", "two", "three", "four"))); |
| 447 |
|
| 448 |
ASSERT_OK(table_set_align_percent(t, TABLE_HEADER_CELL(3), 100)); |
| 449 |
|
| 450 |
ASSERT_OK(table_add_many(t, |
| 451 |
TABLE_STRING, "xxx", |
| 452 |
TABLE_STRING, "yyy", |
| 453 |
TABLE_BOOLEAN, true, |
| 454 |
TABLE_INT, -1)); |
| 455 |
|
| 456 |
ASSERT_OK(table_add_many(t, |
| 457 |
TABLE_STRING, "a long field", |
| 458 |
TABLE_STRING, "yyy", |
| 459 |
TABLE_SET_UPPERCASE, 1, |
| 460 |
TABLE_BOOLEAN, false, |
| 461 |
TABLE_INT, -999999)); |
| 462 |
|
| 463 |
ASSERT_OK(table_format(t, &formatted)); |
| 464 |
printf("%s\n", formatted); |
| 465 |
|
| 466 |
ASSERT_STREQ(formatted, |
| 467 |
"ONE TWO THREE FOUR\n" |
| 468 |
"xxx yyy yes -1\n" |
| 469 |
"a long field YYY no -999999\n"); |
| 470 |
|
| 471 |
formatted = mfree(formatted); |
| 472 |
|
| 473 |
table_set_width(t, 40); |
| 474 |
|
| 475 |
ASSERT_OK(table_format(t, &formatted)); |
| 476 |
printf("%s\n", formatted); |
| 477 |
|
| 478 |
ASSERT_STREQ(formatted, |
| 479 |
"ONE TWO THREE FOUR\n" |
| 480 |
"xxx yyy yes -1\n" |
| 481 |
"a long field YYY no -999999\n"); |
| 482 |
|
| 483 |
formatted = mfree(formatted); |
| 484 |
|
| 485 |
table_set_width(t, 15); |
| 486 |
ASSERT_OK(table_format(t, &formatted)); |
| 487 |
printf("%s\n", formatted); |
| 488 |
|
| 489 |
ASSERT_STREQ(formatted, |
| 490 |
"ONE TWO TH… FO…\n" |
| 491 |
"xxx yyy yes -1\n" |
| 492 |
"a … YYY no -9…\n"); |
| 493 |
|
| 494 |
formatted = mfree(formatted); |
| 495 |
|
| 496 |
table_set_width(t, 5); |
| 497 |
ASSERT_OK(table_format(t, &formatted)); |
| 498 |
printf("%s\n", formatted); |
| 499 |
|
| 500 |
ASSERT_STREQ(formatted, |
| 501 |
"… … … …\n" |
| 502 |
"… … … …\n" |
| 503 |
"… … … …\n"); |
| 504 |
|
| 505 |
formatted = mfree(formatted); |
| 506 |
|
| 507 |
table_set_width(t, 3); |
| 508 |
ASSERT_OK(table_format(t, &formatted)); |
| 509 |
printf("%s\n", formatted); |
| 510 |
|
| 511 |
ASSERT_STREQ(formatted, |
| 512 |
"… … … …\n" |
| 513 |
"… … … …\n" |
| 514 |
"… … … …\n"); |
| 515 |
|
| 516 |
formatted = mfree(formatted); |
| 517 |
|
| 518 |
table_set_width(t, SIZE_MAX); |
| 519 |
ASSERT_OK(table_set_sort(t, (size_t) 0, (size_t) 2, SIZE_MAX)); |
| 520 |
|
| 521 |
ASSERT_OK(table_format(t, &formatted)); |
| 522 |
printf("%s\n", formatted); |
| 523 |
|
| 524 |
ASSERT_STREQ(formatted, |
| 525 |
"ONE TWO THREE FOUR\n" |
| 526 |
"a long field YYY no -999999\n" |
| 527 |
"xxx yyy yes -1\n"); |
| 528 |
|
| 529 |
formatted = mfree(formatted); |
| 530 |
|
| 531 |
table_set_header(t, false); |
| 532 |
|
| 533 |
ASSERT_OK(table_add_many(t, |
| 534 |
TABLE_STRING, "fäää", |
| 535 |
TABLE_STRING, "uuu", |
| 536 |
TABLE_BOOLEAN, true, |
| 537 |
TABLE_INT, 42)); |
| 538 |
|
| 539 |
ASSERT_OK(table_add_many(t, |
| 540 |
TABLE_STRING, "fäää", |
| 541 |
TABLE_STRING, "zzz", |
| 542 |
TABLE_BOOLEAN, false, |
| 543 |
TABLE_INT, 0)); |
| 544 |
|
| 545 |
ASSERT_OK(table_add_many(t, |
| 546 |
TABLE_EMPTY, |
| 547 |
TABLE_SIZE, (uint64_t) 4711, |
| 548 |
TABLE_TIMESPAN, (usec_t) 5*USEC_PER_MINUTE, |
| 549 |
TABLE_INT64, (uint64_t) -123456789)); |
| 550 |
|
| 551 |
ASSERT_OK(table_format(t, &formatted)); |
| 552 |
printf("%s\n", formatted); |
| 553 |
|
| 554 |
ASSERT_STREQ(formatted, |
| 555 |
"a long field YYY no -999999\n" |
| 556 |
"fäää zzz no 0\n" |
| 557 |
"fäää uuu yes 42\n" |
| 558 |
"xxx yyy yes -1\n" |
| 559 |
" 4.6K 5min -123456789\n"); |
| 560 |
|
| 561 |
formatted = mfree(formatted); |
| 562 |
|
| 563 |
ASSERT_OK(table_set_display(t, (size_t) 2, (size_t) 0, (size_t) 2, (size_t) 0, (size_t) 0, SIZE_MAX)); |
| 564 |
|
| 565 |
ASSERT_OK(table_format(t, &formatted)); |
| 566 |
printf("%s\n", formatted); |
| 567 |
|
| 568 |
if (isatty_safe(STDOUT_FILENO)) |
| 569 |
ASSERT_STREQ(formatted, |
| 570 |
"no a long f… no a long f… a long fi…\n" |
| 571 |
"no fäää no fäää fäää\n" |
| 572 |
"yes fäää yes fäää fäää\n" |
| 573 |
"yes xxx yes xxx xxx\n" |
| 574 |
"5min 5min \n"); |
| 575 |
else |
| 576 |
ASSERT_STREQ(formatted, |
| 577 |
"no a long field no a long field a long field\n" |
| 578 |
"no fäää no fäää fäää\n" |
| 579 |
"yes fäää yes fäää fäää\n" |
| 580 |
"yes xxx yes xxx xxx\n" |
| 581 |
"5min 5min \n"); |
| 582 |
} |
| 583 |
|
| 584 |
TEST(tristate) { |
| 585 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *w = NULL; |
| 586 |
_cleanup_(table_unrefp) Table *t = NULL; |
| 587 |
_cleanup_free_ char *formatted = NULL; |
| 588 |
|
| 589 |
ASSERT_NOT_NULL((t = table_new("name", "flag"))); |
| 590 |
|
| 591 |
ASSERT_OK(table_add_many(t, |
| 592 |
TABLE_STRING, "neg", |
| 593 |
TABLE_TRISTATE, -1)); |
| 594 |
ASSERT_OK(table_add_many(t, |
| 595 |
TABLE_STRING, "zero", |
| 596 |
TABLE_TRISTATE, 0)); |
| 597 |
ASSERT_OK(table_add_many(t, |
| 598 |
TABLE_STRING, "pos", |
| 599 |
TABLE_TRISTATE, 1)); |
| 600 |
|
| 601 |
ASSERT_OK(table_format(t, &formatted)); |
| 602 |
printf("%s\n", formatted); |
| 603 |
ASSERT_STREQ(formatted, |
| 604 |
"NAME FLAG\n" |
| 605 |
"neg \n" |
| 606 |
"zero no\n" |
| 607 |
"pos yes\n"); |
| 608 |
formatted = mfree(formatted); |
| 609 |
|
| 610 |
/* Try a non-default ersatz string. */ |
| 611 |
table_set_ersatz_string(t, TABLE_ERSATZ_DASH); |
| 612 |
ASSERT_OK(table_format(t, &formatted)); |
| 613 |
printf("%s\n", formatted); |
| 614 |
ASSERT_STREQ(formatted, |
| 615 |
"NAME FLAG\n" |
| 616 |
"neg -\n" |
| 617 |
"zero no\n" |
| 618 |
"pos yes\n"); |
| 619 |
formatted = mfree(formatted); |
| 620 |
|
| 621 |
/* Sorting: -1 < 0 < 1 */ |
| 622 |
ASSERT_OK(table_set_sort(t, (size_t) 1, SIZE_MAX)); |
| 623 |
ASSERT_OK(table_format(t, &formatted)); |
| 624 |
printf("%s\n", formatted); |
| 625 |
ASSERT_STREQ(formatted, |
| 626 |
"NAME FLAG\n" |
| 627 |
"neg -\n" |
| 628 |
"zero no\n" |
| 629 |
"pos yes\n"); |
| 630 |
formatted = mfree(formatted); |
| 631 |
|
| 632 |
/* JSON: -1 → null, 0 → false, positive → true */ |
| 633 |
ASSERT_OK(table_to_json(t, &v)); |
| 634 |
|
| 635 |
ASSERT_OK(sd_json_build(&w, |
| 636 |
SD_JSON_BUILD_ARRAY( |
| 637 |
SD_JSON_BUILD_OBJECT( |
| 638 |
SD_JSON_BUILD_PAIR("name", JSON_BUILD_CONST_STRING("neg")), |
| 639 |
SD_JSON_BUILD_PAIR("flag", SD_JSON_BUILD_NULL)), |
| 640 |
SD_JSON_BUILD_OBJECT( |
| 641 |
SD_JSON_BUILD_PAIR("name", JSON_BUILD_CONST_STRING("zero")), |
| 642 |
SD_JSON_BUILD_PAIR_BOOLEAN("flag", false)), |
| 643 |
SD_JSON_BUILD_OBJECT( |
| 644 |
SD_JSON_BUILD_PAIR("name", JSON_BUILD_CONST_STRING("pos")), |
| 645 |
SD_JSON_BUILD_PAIR_BOOLEAN("flag", true))))); |
| 646 |
|
| 647 |
ASSERT_TRUE(sd_json_variant_equal(v, w)); |
| 648 |
} |
| 649 |
|
| 650 |
TEST(signed_integers) { |
| 651 |
_cleanup_(table_unrefp) Table *t = NULL; |
| 652 |
_cleanup_free_ char *formatted = NULL; |
| 653 |
|
| 654 |
ASSERT_NOT_NULL((t = table_new("int", "int8", "int16", "int32", "int64"))); |
| 655 |
table_set_width(t, 0); |
| 656 |
|
| 657 |
ASSERT_OK(table_add_many(t, |
| 658 |
TABLE_INT, -1, |
| 659 |
TABLE_INT8, INT8_C(-1), |
| 660 |
TABLE_INT16, INT16_C(-1), |
| 661 |
TABLE_INT32, INT32_C(-1), |
| 662 |
TABLE_INT64, INT64_C(-1))); |
| 663 |
ASSERT_OK(table_add_many(t, |
| 664 |
TABLE_INT, INT_MAX, |
| 665 |
TABLE_INT8, INT8_MAX, |
| 666 |
TABLE_INT16, INT16_MAX, |
| 667 |
TABLE_INT32, INT32_MAX, |
| 668 |
TABLE_INT64, INT64_MAX)); |
| 669 |
ASSERT_OK(table_add_many(t, |
| 670 |
TABLE_INT, INT_MIN, |
| 671 |
TABLE_INT8, INT8_MIN, |
| 672 |
TABLE_INT16, INT16_MIN, |
| 673 |
TABLE_INT32, INT32_MIN, |
| 674 |
TABLE_INT64, INT64_MIN)); |
| 675 |
|
| 676 |
ASSERT_OK(table_format(t, &formatted)); |
| 677 |
printf("%s\n", formatted); |
| 678 |
|
| 679 |
ASSERT_STREQ(formatted, |
| 680 |
"INT INT8 INT16 INT32 INT64\n" |
| 681 |
"-1 -1 -1 -1 -1\n" |
| 682 |
"2147483647 127 32767 2147483647 9223372036854775807\n" |
| 683 |
"-2147483648 -128 -32768 -2147483648 -9223372036854775808\n"); |
| 684 |
|
| 685 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *a = NULL, *b = NULL; |
| 686 |
ASSERT_OK(table_to_json(t, &a)); |
| 687 |
|
| 688 |
table_print_json(t, /* f= */ NULL, SD_JSON_FORMAT_NEWLINE); |
| 689 |
|
| 690 |
ASSERT_OK(sd_json_build(&b, |
| 691 |
SD_JSON_BUILD_ARRAY( |
| 692 |
SD_JSON_BUILD_OBJECT( |
| 693 |
SD_JSON_BUILD_PAIR_INTEGER("int", -1), |
| 694 |
SD_JSON_BUILD_PAIR_INTEGER("int8", -1), |
| 695 |
SD_JSON_BUILD_PAIR_INTEGER("int16", -1), |
| 696 |
SD_JSON_BUILD_PAIR_INTEGER("int32", -1), |
| 697 |
SD_JSON_BUILD_PAIR_INTEGER("int64", -1)), |
| 698 |
SD_JSON_BUILD_OBJECT( |
| 699 |
SD_JSON_BUILD_PAIR_INTEGER("int", INT_MAX), |
| 700 |
SD_JSON_BUILD_PAIR_INTEGER("int8", INT8_MAX), |
| 701 |
SD_JSON_BUILD_PAIR_INTEGER("int16", INT16_MAX), |
| 702 |
SD_JSON_BUILD_PAIR_INTEGER("int32", INT32_MAX), |
| 703 |
SD_JSON_BUILD_PAIR_INTEGER("int64", INT64_MAX)), |
| 704 |
SD_JSON_BUILD_OBJECT( |
| 705 |
SD_JSON_BUILD_PAIR_INTEGER("int", INT_MIN), |
| 706 |
SD_JSON_BUILD_PAIR_INTEGER("int8", INT8_MIN), |
| 707 |
SD_JSON_BUILD_PAIR_INTEGER("int16", INT16_MIN), |
| 708 |
SD_JSON_BUILD_PAIR_INTEGER("int32", INT32_MIN), |
| 709 |
SD_JSON_BUILD_PAIR_INTEGER("int64", INT64_MIN))))); |
| 710 |
sd_json_variant_dump(b, SD_JSON_FORMAT_NEWLINE, stdout, NULL); |
| 711 |
|
| 712 |
ASSERT_TRUE(sd_json_variant_equal(a, b)); |
| 713 |
} |
| 714 |
|
| 715 |
TEST(unsigned_integers) { |
| 716 |
_cleanup_(table_unrefp) Table *t = NULL; |
| 717 |
_cleanup_free_ char *formatted = NULL; |
| 718 |
|
| 719 |
ASSERT_NOT_NULL((t = table_new("uint", "uint8", "uint16", "uint32", "uhex32", "uint64", "uhex64"))); |
| 720 |
table_set_width(t, 0); |
| 721 |
|
| 722 |
ASSERT_OK(table_add_many(t, |
| 723 |
TABLE_UINT, 0, |
| 724 |
TABLE_UINT8, UINT8_C(0), |
| 725 |
TABLE_UINT16, UINT16_C(0), |
| 726 |
TABLE_UINT32, UINT32_C(0), |
| 727 |
TABLE_UINT32_HEX, UINT32_C(0), |
| 728 |
TABLE_UINT64, UINT64_C(0), |
| 729 |
TABLE_UINT64_HEX, UINT64_C(0))); |
| 730 |
ASSERT_OK(table_add_many(t, |
| 731 |
TABLE_UINT, UINT_MAX, |
| 732 |
TABLE_UINT8, UINT8_MAX, |
| 733 |
TABLE_UINT16, UINT16_MAX, |
| 734 |
TABLE_UINT32, UINT32_MAX, |
| 735 |
TABLE_UINT32_HEX, UINT32_MAX, |
| 736 |
TABLE_UINT64, UINT64_MAX, |
| 737 |
TABLE_UINT64_HEX, UINT64_MAX)); |
| 738 |
|
| 739 |
ASSERT_OK(table_format(t, &formatted)); |
| 740 |
printf("%s\n", formatted); |
| 741 |
|
| 742 |
ASSERT_STREQ(formatted, |
| 743 |
"UINT UINT8 UINT16 UINT32 UHEX32 UINT64 UHEX64\n" |
| 744 |
"0 0 0 0 0 0 0\n" |
| 745 |
"4294967295 255 65535 4294967295 ffffffff 18446744073709551615 ffffffffffffffff\n"); |
| 746 |
|
| 747 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *a = NULL, *b = NULL; |
| 748 |
ASSERT_OK(table_to_json(t, &a)); |
| 749 |
|
| 750 |
table_print_json(t, /* f= */ NULL, SD_JSON_FORMAT_NEWLINE); |
| 751 |
|
| 752 |
ASSERT_OK(sd_json_build(&b, |
| 753 |
SD_JSON_BUILD_ARRAY( |
| 754 |
SD_JSON_BUILD_OBJECT( |
| 755 |
SD_JSON_BUILD_PAIR_UNSIGNED("uint", 0), |
| 756 |
SD_JSON_BUILD_PAIR_UNSIGNED("uint8", 0), |
| 757 |
SD_JSON_BUILD_PAIR_UNSIGNED("uint16", 0), |
| 758 |
SD_JSON_BUILD_PAIR_UNSIGNED("uint32", 0), |
| 759 |
SD_JSON_BUILD_PAIR_UNSIGNED("uhex32", 0), |
| 760 |
SD_JSON_BUILD_PAIR_UNSIGNED("uint64", 0), |
| 761 |
SD_JSON_BUILD_PAIR_UNSIGNED("uhex64", 0)), |
| 762 |
SD_JSON_BUILD_OBJECT( |
| 763 |
SD_JSON_BUILD_PAIR_UNSIGNED("uint", UINT_MAX), |
| 764 |
SD_JSON_BUILD_PAIR_UNSIGNED("uint8", UINT8_MAX), |
| 765 |
SD_JSON_BUILD_PAIR_UNSIGNED("uint16", UINT16_MAX), |
| 766 |
SD_JSON_BUILD_PAIR_UNSIGNED("uint32", UINT32_MAX), |
| 767 |
SD_JSON_BUILD_PAIR_UNSIGNED("uhex32", UINT32_MAX), |
| 768 |
SD_JSON_BUILD_PAIR_UNSIGNED("uint64", UINT64_MAX), |
| 769 |
SD_JSON_BUILD_PAIR_UNSIGNED("uhex64", UINT64_MAX))))); |
| 770 |
sd_json_variant_dump(b, SD_JSON_FORMAT_NEWLINE, stdout, NULL); |
| 771 |
|
| 772 |
ASSERT_TRUE(sd_json_variant_equal(a, b)); |
| 773 |
} |
| 774 |
|
| 775 |
TEST(vertical) { |
| 776 |
_cleanup_(table_unrefp) Table *t = NULL; |
| 777 |
_cleanup_free_ char *formatted = NULL; |
| 778 |
|
| 779 |
ASSERT_NOT_NULL((t = table_new_vertical())); |
| 780 |
|
| 781 |
ASSERT_OK(table_add_many(t, |
| 782 |
TABLE_FIELD, "pfft aa", TABLE_STRING, "foo", |
| 783 |
TABLE_FIELD, "uuu o", TABLE_SIZE, UINT64_C(1024), |
| 784 |
TABLE_FIELD, "quux", TABLE_STRING, "asdf", TABLE_SET_JSON_FIELD_NAME, "custom-quux", |
| 785 |
TABLE_FIELD, "lllllllllllo", TABLE_STRING, "jjjjjjjjjjjjjjjjj")); |
| 786 |
|
| 787 |
ASSERT_OK(table_set_json_field_name(t, 1, "DIMPFELMOSER")); |
| 788 |
ASSERT_OK(table_set_json_field_name(t, 1, NULL)); |
| 789 |
ASSERT_OK(table_set_json_field_name(t, 1, "dimpfelmoser")); |
| 790 |
|
| 791 |
ASSERT_OK(table_format(t, &formatted)); |
| 792 |
|
| 793 |
ASSERT_STREQ(formatted, |
| 794 |
" pfft aa: foo\n" |
| 795 |
" uuu o: 1K\n" |
| 796 |
" quux: asdf\n" |
| 797 |
"lllllllllllo: jjjjjjjjjjjjjjjjj\n"); |
| 798 |
|
| 799 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *a = NULL, *b = NULL; |
| 800 |
ASSERT_OK(table_to_json(t, &a)); |
| 801 |
|
| 802 |
ASSERT_OK(sd_json_build(&b, SD_JSON_BUILD_OBJECT( |
| 803 |
SD_JSON_BUILD_PAIR_STRING("pfft_aa", "foo"), |
| 804 |
SD_JSON_BUILD_PAIR_UNSIGNED("dimpfelmoser", 1024), |
| 805 |
SD_JSON_BUILD_PAIR_STRING("custom-quux", "asdf"), |
| 806 |
SD_JSON_BUILD_PAIR_STRING("lllllllllllo", "jjjjjjjjjjjjjjjjj")))); |
| 807 |
|
| 808 |
ASSERT_TRUE(sd_json_variant_equal(a, b)); |
| 809 |
} |
| 810 |
|
| 811 |
TEST(path_basename) { |
| 812 |
_cleanup_(table_unrefp) Table *t = NULL; |
| 813 |
_cleanup_free_ char *formatted = NULL; |
| 814 |
|
| 815 |
ASSERT_NOT_NULL((t = table_new("x"))); |
| 816 |
|
| 817 |
table_set_header(t, false); |
| 818 |
|
| 819 |
ASSERT_OK(table_add_many(t, |
| 820 |
TABLE_PATH_BASENAME, "/foo/bar", |
| 821 |
TABLE_PATH_BASENAME, "/quux/bar", |
| 822 |
TABLE_PATH_BASENAME, "/foo/baz")); |
| 823 |
|
| 824 |
ASSERT_OK(table_format(t, &formatted)); |
| 825 |
|
| 826 |
ASSERT_STREQ(formatted, "bar\nbar\nbaz\n"); |
| 827 |
} |
| 828 |
|
| 829 |
TEST(dup_cell) { |
| 830 |
_cleanup_(table_unrefp) Table *t = NULL; |
| 831 |
_cleanup_free_ char *formatted = NULL; |
| 832 |
|
| 833 |
ASSERT_NOT_NULL((t = table_new("foo", "bar", "x", "baz", ".", "%", "!", "~", "+"))); |
| 834 |
table_set_width(t, 75); |
| 835 |
|
| 836 |
ASSERT_OK(table_add_many(t, |
| 837 |
TABLE_STRING, "hello", |
| 838 |
TABLE_UINT8, UINT8_C(42), |
| 839 |
TABLE_UINT16, UINT16_C(666), |
| 840 |
TABLE_UINT32, UINT32_C(253), |
| 841 |
TABLE_PERCENT, 0, |
| 842 |
TABLE_PATH_BASENAME, "/foo/bar", |
| 843 |
TABLE_STRING, "aaa", |
| 844 |
TABLE_STRING, "bbb", |
| 845 |
TABLE_STRING, "ccc")); |
| 846 |
|
| 847 |
/* Add the second row by duping cells */ |
| 848 |
for (size_t i = 0; i < table_get_columns(t); i++) |
| 849 |
ASSERT_OK(table_dup_cell(t, table_get_cell(t, 1, i))); |
| 850 |
|
| 851 |
/* Another row, but dupe the last three strings from the same cell */ |
| 852 |
ASSERT_OK(table_add_many(t, |
| 853 |
TABLE_STRING, "aaa", |
| 854 |
TABLE_UINT8, UINT8_C(0), |
| 855 |
TABLE_UINT16, UINT16_C(65535), |
| 856 |
TABLE_UINT32, UINT32_C(4294967295), |
| 857 |
TABLE_PERCENT, 100, |
| 858 |
TABLE_PATH_BASENAME, "../")); |
| 859 |
|
| 860 |
for (size_t i = 6; i < table_get_columns(t); i++) |
| 861 |
ASSERT_OK(table_dup_cell(t, table_get_cell(t, 2, 0))); |
| 862 |
|
| 863 |
ASSERT_OK(table_format(t, &formatted)); |
| 864 |
printf("%s\n", formatted); |
| 865 |
ASSERT_STREQ(formatted, |
| 866 |
"FOO BAR X BAZ . % ! ~ +\n" |
| 867 |
"hello 42 666 253 0% bar aaa bbb ccc\n" |
| 868 |
"hello 42 666 253 0% bar aaa bbb ccc\n" |
| 869 |
"aaa 0 65535 4294967295 100% ../ hello hello hello\n"); |
| 870 |
} |
| 871 |
|
| 872 |
TEST(table_bps) { |
| 873 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 874 |
_cleanup_free_ char *formatted = NULL; |
| 875 |
|
| 876 |
ASSERT_NOT_NULL((table = table_new("uint64", "size", "bps"))); |
| 877 |
uint64_t v; |
| 878 |
FOREACH_ARGUMENT(v, |
| 879 |
2500, |
| 880 |
10000000, |
| 881 |
20000000, |
| 882 |
25000000, |
| 883 |
1000000000, |
| 884 |
2000000000, |
| 885 |
2500000000) |
| 886 |
ASSERT_OK(table_add_many(table, |
| 887 |
TABLE_UINT64, v, |
| 888 |
TABLE_SIZE, v, |
| 889 |
TABLE_BPS, v)); |
| 890 |
|
| 891 |
table_set_width(table, 50); |
| 892 |
ASSERT_OK(table_format(table, &formatted)); |
| 893 |
|
| 894 |
printf("%s", formatted); |
| 895 |
ASSERT_STREQ(formatted, |
| 896 |
"UINT64 SIZE BPS\n" |
| 897 |
"2500 2.4K 2.5Kbps\n" |
| 898 |
"10000000 9.5M 10Mbps\n" |
| 899 |
"20000000 19M 20Mbps\n" |
| 900 |
"25000000 23.8M 25Mbps\n" |
| 901 |
"1000000000 953.6M 1Gbps\n" |
| 902 |
"2000000000 1.8G 2Gbps\n" |
| 903 |
"2500000000 2.3G 2.5Gbps\n"); |
| 904 |
} |
| 905 |
|
| 906 |
TEST(table_ansi) { |
| 907 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 908 |
|
| 909 |
ASSERT_NOT_NULL((table = table_new("foo", "bar", "baz", "kkk"))); |
| 910 |
|
| 911 |
ASSERT_OK(table_add_many(table, |
| 912 |
TABLE_STRING, "hallo", |
| 913 |
TABLE_STRING_WITH_ANSI, "knuerz" ANSI_HIGHLIGHT_RED "red" ANSI_HIGHLIGHT_GREEN "green", |
| 914 |
TABLE_STRING_WITH_ANSI, "noansi", |
| 915 |
TABLE_STRING_WITH_ANSI, ANSI_GREY "thisisgrey")); |
| 916 |
|
| 917 |
unsigned saved_columns = columns(); |
| 918 |
_cleanup_free_ char *saved_term = NULL, *saved_color = NULL; |
| 919 |
const char *e; |
| 920 |
|
| 921 |
e = getenv("TERM"); |
| 922 |
if (e) |
| 923 |
ASSERT_NOT_NULL((saved_term = strdup(e))); |
| 924 |
e = getenv("SYSTEMD_COLORS"); |
| 925 |
if (e) |
| 926 |
ASSERT_NOT_NULL((saved_color = strdup(e))); |
| 927 |
|
| 928 |
ASSERT_OK_ERRNO(setenv("COLUMNS", "200", /* overwrite= */ true)); |
| 929 |
ASSERT_OK_ERRNO(setenv("SYSTEMD_COLORS", "24bit", /* overwrite= */ true)); |
| 930 |
ASSERT_OK_ERRNO(setenv("TERM", FALLBACK_TERM, /* overwrite= */ true)); |
| 931 |
reset_terminal_feature_caches(); |
| 932 |
|
| 933 |
_cleanup_free_ char *formatted = NULL; |
| 934 |
ASSERT_OK(table_format(table, &formatted)); |
| 935 |
|
| 936 |
ASSERT_STREQ(formatted, |
| 937 |
ANSI_ADD_UNDERLINE "FOO " ANSI_NORMAL |
| 938 |
ANSI_ADD_UNDERLINE " " ANSI_NORMAL |
| 939 |
ANSI_ADD_UNDERLINE "BAR " ANSI_NORMAL |
| 940 |
ANSI_ADD_UNDERLINE " " ANSI_NORMAL |
| 941 |
ANSI_ADD_UNDERLINE "BAZ " ANSI_NORMAL |
| 942 |
ANSI_ADD_UNDERLINE " " ANSI_NORMAL |
| 943 |
ANSI_ADD_UNDERLINE "KKK " ANSI_NORMAL "\n" |
| 944 |
"hallo knuerz" ANSI_HIGHLIGHT_RED "red" ANSI_HIGHLIGHT_GREEN "green" ANSI_NORMAL |
| 945 |
" noansi" ANSI_NORMAL |
| 946 |
" " ANSI_GREY "thisisgrey" ANSI_NORMAL "\n"); |
| 947 |
|
| 948 |
/* Validate that color is correctly stripped */ |
| 949 |
ASSERT_OK_ERRNO(setenv("SYSTEMD_COLORS", "0", /* overwrite= */ true)); |
| 950 |
reset_terminal_feature_caches(); |
| 951 |
|
| 952 |
formatted = mfree(formatted); |
| 953 |
ASSERT_OK(table_format(table, &formatted)); |
| 954 |
|
| 955 |
ASSERT_STREQ(formatted, |
| 956 |
"FOO BAR BAZ KKK\n" |
| 957 |
"hallo knuerzredgreen noansi thisisgrey\n"); |
| 958 |
|
| 959 |
ASSERT_OK(table_print(table)); |
| 960 |
|
| 961 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL, *jj = NULL; |
| 962 |
|
| 963 |
ASSERT_OK(table_to_json(table, &j)); |
| 964 |
|
| 965 |
ASSERT_OK(sd_json_build(&jj, |
| 966 |
SD_JSON_BUILD_ARRAY( |
| 967 |
SD_JSON_BUILD_OBJECT( |
| 968 |
SD_JSON_BUILD_PAIR_STRING("foo", "hallo"), |
| 969 |
SD_JSON_BUILD_PAIR_STRING("bar", "knuerzredgreen"), |
| 970 |
SD_JSON_BUILD_PAIR_STRING("baz", "noansi"), |
| 971 |
SD_JSON_BUILD_PAIR_STRING("kkk", "thisisgrey"))))); |
| 972 |
ASSERT_TRUE(sd_json_variant_equal(j, jj)); |
| 973 |
|
| 974 |
ASSERT_OK(sd_json_variant_dump(j, SD_JSON_FORMAT_COLOR_AUTO|SD_JSON_FORMAT_PRETTY_AUTO, /* f= */ NULL, /* prefix= */ NULL)); |
| 975 |
|
| 976 |
ASSERT_OK(setenvf("COLUMNS", /* overwrite= */ true, "%u", saved_columns)); |
| 977 |
ASSERT_OK(set_unset_env("SYSTEMD_COLORS", saved_color, /* overwrite= */ true)); |
| 978 |
ASSERT_OK(set_unset_env("TERM", saved_term, /* overwrite= */ true)); |
| 979 |
} |
| 980 |
|
| 981 |
static int intro(void) { |
| 982 |
ASSERT_OK_ERRNO(setenv("SYSTEMD_COLORS", "0", /* overwrite= */ true)); |
| 983 |
ASSERT_OK_ERRNO(setenv("COLUMNS", "40", /* overwrite= */ true)); |
| 984 |
return EXIT_SUCCESS; |
| 985 |
} |
| 986 |
|
| 987 |
DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro); |