]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-strip-tab-ansi.c
test: Use TEST macro
[thirdparty/systemd.git] / src / test / test-strip-tab-ansi.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
e8bc0ea2
LP
2
3#include <stdio.h>
4
695a944c 5#include "alloc-util.h"
294bf0c3 6#include "pretty-print.h"
07630cea 7#include "string-util.h"
288a74cc 8#include "terminal-util.h"
4f7452a8 9#include "tests.h"
07630cea 10#include "util.h"
e8bc0ea2 11
4f7452a8 12TEST(strip_tab_ansi) {
695a944c
LP
13 _cleanup_free_ char *urlified = NULL, *q = NULL, *qq = NULL;
14 char *p, *z;
e8bc0ea2
LP
15
16 assert_se(p = strdup("\tFoobar\tbar\twaldo\t"));
b4766d5f 17 assert_se(strip_tab_ansi(&p, NULL, NULL));
e8bc0ea2
LP
18 fprintf(stdout, "<%s>\n", p);
19 assert_se(streq(p, " Foobar bar waldo "));
20 free(p);
21
1fc464f6 22 assert_se(p = strdup(ANSI_HIGHLIGHT "Hello" ANSI_NORMAL ANSI_HIGHLIGHT_RED " world!" ANSI_NORMAL));
b4766d5f 23 assert_se(strip_tab_ansi(&p, NULL, NULL));
e8bc0ea2
LP
24 fprintf(stdout, "<%s>\n", p);
25 assert_se(streq(p, "Hello world!"));
26 free(p);
27
1fc464f6 28 assert_se(p = strdup("\x1B[\x1B[\t\x1B[" ANSI_HIGHLIGHT "\x1B[" "Hello" ANSI_NORMAL ANSI_HIGHLIGHT_RED " world!" ANSI_NORMAL));
b4766d5f 29 assert_se(strip_tab_ansi(&p, NULL, NULL));
e8bc0ea2
LP
30 assert_se(streq(p, "\x1B[\x1B[ \x1B[\x1B[Hello world!"));
31 free(p);
32
33 assert_se(p = strdup("\x1B[waldo"));
b4766d5f 34 assert_se(strip_tab_ansi(&p, NULL, NULL));
e8bc0ea2
LP
35 assert_se(streq(p, "\x1B[waldo"));
36 free(p);
37
62a3fc6d
ZJS
38 assert_se(p = strdup("\r\rwaldo"));
39 assert_se(strip_tab_ansi(&p, NULL, NULL));
40 assert_se(streq(p, "\r\rwaldo"));
41 free(p);
42
43 assert_se(p = strdup("waldo\r\r"));
44 assert_se(strip_tab_ansi(&p, NULL, NULL));
45 assert_se(streq(p, "waldo"));
46 free(p);
47
48 assert_se(p = strdup("waldo\r\r\n\r\n"));
49 assert_se(strip_tab_ansi(&p, NULL, NULL));
50 assert_se(streq(p, "waldo\n\n"));
51 free(p);
52
695a944c
LP
53 assert_se(terminal_urlify_path("/etc/fstab", "i am a fabulous link", &urlified) >= 0);
54 assert_se(p = strjoin("something ", urlified, " something-else"));
55 assert_se(q = strdup(p));
56 printf("<%s>\n", p);
57 assert_se(strip_tab_ansi(&p, NULL, NULL));
58 printf("<%s>\n", p);
59 assert_se(streq(p, "something i am a fabulous link something-else"));
60 p = mfree(p);
61
62 /* Truncate the formatted string in the middle of an ANSI sequence (in which case we shouldn't touch the
63 * incomplete sequence) */
64 z = strstr(q, "fstab");
65 if (z) {
66 *z = 0;
67 assert_se(qq = strdup(q));
68 assert_se(strip_tab_ansi(&q, NULL, NULL));
69 assert_se(streq(q, qq));
70 }
e8bc0ea2 71}
4f7452a8
JJ
72
73DEFINE_TEST_MAIN(LOG_INFO);