chiark / gitweb /
1b5e040a888ef729a073e170f64803506a732fa4
[elogind.git] / src / test / test-strip-tab-ansi.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2012 Lennart Poettering
6 ***/
7
8 #include <stdio.h>
9
10 //#include "alloc-util.h"
11 #include "string-util.h"
12 #include "terminal-util.h"
13 #include "util.h"
14
15 int main(int argc, char *argv[]) {
16         _cleanup_free_ char *urlified = NULL, *q = NULL, *qq = NULL;
17         char *p, *z;
18
19         assert_se(p = strdup("\tFoobar\tbar\twaldo\t"));
20         assert_se(strip_tab_ansi(&p, NULL, NULL));
21         fprintf(stdout, "<%s>\n", p);
22         assert_se(streq(p, "        Foobar        bar        waldo        "));
23         free(p);
24
25         assert_se(p = strdup(ANSI_HIGHLIGHT "Hello" ANSI_NORMAL ANSI_HIGHLIGHT_RED " world!" ANSI_NORMAL));
26         assert_se(strip_tab_ansi(&p, NULL, NULL));
27         fprintf(stdout, "<%s>\n", p);
28         assert_se(streq(p, "Hello world!"));
29         free(p);
30
31         assert_se(p = strdup("\x1B[\x1B[\t\x1B[" ANSI_HIGHLIGHT "\x1B[" "Hello" ANSI_NORMAL ANSI_HIGHLIGHT_RED " world!" ANSI_NORMAL));
32         assert_se(strip_tab_ansi(&p, NULL, NULL));
33         assert_se(streq(p, "\x1B[\x1B[        \x1B[\x1B[Hello world!"));
34         free(p);
35
36         assert_se(p = strdup("\x1B[waldo"));
37         assert_se(strip_tab_ansi(&p, NULL, NULL));
38         assert_se(streq(p, "\x1B[waldo"));
39         free(p);
40
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
50         /* Truncate the formatted string in the middle of an ANSI sequence (in which case we shouldn't touch the
51          * incomplete sequence) */
52         z = strstr(q, "fstab");
53         if (z) {
54                 *z = 0;
55                 assert_se(qq = strdup(q));
56                 assert_se(strip_tab_ansi(&q, NULL, NULL));
57                 assert_se(streq(q, qq));
58         }
59
60         return 0;
61 }