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