chiark / gitweb /
Prep v239: Do not test terminal_urlify_path()
[elogind.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 #if 0 /// urlified is UNNEEDED by elogind
12         _cleanup_free_ char *urlified = NULL, *q = NULL, *qq = NULL;
13 #else
14         _cleanup_free_ char *q = NULL, *qq = NULL;
15 #endif // 0
16         char *p, *z;
17
18         assert_se(p = strdup("\tFoobar\tbar\twaldo\t"));
19         assert_se(strip_tab_ansi(&p, NULL, NULL));
20         fprintf(stdout, "<%s>\n", p);
21         assert_se(streq(p, "        Foobar        bar        waldo        "));
22         free(p);
23
24         assert_se(p = strdup(ANSI_HIGHLIGHT "Hello" ANSI_NORMAL ANSI_HIGHLIGHT_RED " world!" ANSI_NORMAL));
25         assert_se(strip_tab_ansi(&p, NULL, NULL));
26         fprintf(stdout, "<%s>\n", p);
27         assert_se(streq(p, "Hello world!"));
28         free(p);
29
30         assert_se(p = strdup("\x1B[\x1B[\t\x1B[" ANSI_HIGHLIGHT "\x1B[" "Hello" ANSI_NORMAL ANSI_HIGHLIGHT_RED " world!" ANSI_NORMAL));
31         assert_se(strip_tab_ansi(&p, NULL, NULL));
32         assert_se(streq(p, "\x1B[\x1B[        \x1B[\x1B[Hello world!"));
33         free(p);
34
35         assert_se(p = strdup("\x1B[waldo"));
36         assert_se(strip_tab_ansi(&p, NULL, NULL));
37         assert_se(streq(p, "\x1B[waldo"));
38         free(p);
39
40 #if 0 /// UNNEEDED by elogind
41         assert_se(terminal_urlify_path("/etc/fstab", "i am a fabulous link", &urlified) >= 0);
42         assert_se(p = strjoin("something ", urlified, " something-else"));
43         assert_se(q = strdup(p));
44         printf("<%s>\n", p);
45         assert_se(strip_tab_ansi(&p, NULL, NULL));
46         printf("<%s>\n", p);
47         assert_se(streq(p, "something i am a fabulous link something-else"));
48         p = mfree(p);
49 #endif // 0
50
51         /* Truncate the formatted string in the middle of an ANSI sequence (in which case we shouldn't touch the
52          * incomplete sequence) */
53         z = strstr(q, "fstab");
54         if (z) {
55                 *z = 0;
56                 assert_se(qq = strdup(q));
57                 assert_se(strip_tab_ansi(&q, NULL, NULL));
58                 assert_se(streq(q, qq));
59         }
60
61         return 0;
62 }