]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-strip-tab-ansi.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / test / test-strip-tab-ansi.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <stdio.h>
4
5 #include "alloc-util.h"
6 #include "pretty-print.h"
7 #include "string-util.h"
8 #include "terminal-util.h"
9 #include "util.h"
10
11 int main(int argc, char *argv[]) {
12 _cleanup_free_ char *urlified = NULL, *q = NULL, *qq = NULL;
13 char *p, *z;
14
15 assert_se(p = strdup("\tFoobar\tbar\twaldo\t"));
16 assert_se(strip_tab_ansi(&p, NULL, NULL));
17 fprintf(stdout, "<%s>\n", p);
18 assert_se(streq(p, " Foobar bar waldo "));
19 free(p);
20
21 assert_se(p = strdup(ANSI_HIGHLIGHT "Hello" ANSI_NORMAL ANSI_HIGHLIGHT_RED " world!" ANSI_NORMAL));
22 assert_se(strip_tab_ansi(&p, NULL, NULL));
23 fprintf(stdout, "<%s>\n", p);
24 assert_se(streq(p, "Hello world!"));
25 free(p);
26
27 assert_se(p = strdup("\x1B[\x1B[\t\x1B[" ANSI_HIGHLIGHT "\x1B[" "Hello" ANSI_NORMAL ANSI_HIGHLIGHT_RED " world!" ANSI_NORMAL));
28 assert_se(strip_tab_ansi(&p, NULL, NULL));
29 assert_se(streq(p, "\x1B[\x1B[ \x1B[\x1B[Hello world!"));
30 free(p);
31
32 assert_se(p = strdup("\x1B[waldo"));
33 assert_se(strip_tab_ansi(&p, NULL, NULL));
34 assert_se(streq(p, "\x1B[waldo"));
35 free(p);
36
37 assert_se(terminal_urlify_path("/etc/fstab", "i am a fabulous link", &urlified) >= 0);
38 assert_se(p = strjoin("something ", urlified, " something-else"));
39 assert_se(q = strdup(p));
40 printf("<%s>\n", p);
41 assert_se(strip_tab_ansi(&p, NULL, NULL));
42 printf("<%s>\n", p);
43 assert_se(streq(p, "something i am a fabulous link something-else"));
44 p = mfree(p);
45
46 /* Truncate the formatted string in the middle of an ANSI sequence (in which case we shouldn't touch the
47 * incomplete sequence) */
48 z = strstr(q, "fstab");
49 if (z) {
50 *z = 0;
51 assert_se(qq = strdup(q));
52 assert_se(strip_tab_ansi(&q, NULL, NULL));
53 assert_se(streq(q, qq));
54 }
55
56 return 0;
57 }