1 /*
2 lib/terminal - tests for terminal emulation functions
3
4 Copyright (C) 2013-2025
5 Free Software Foundation, Inc.
6
7 Written by:
8 Johannes Altmanninger, 2025
9
10 This file is part of the Midnight Commander.
11
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
16
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <https://www.gnu.org/licenses/>.
24 */
25
26 #define TEST_SUITE_NAME "/lib/terminal"
27
28 #include "tests/mctest.h"
29
30 #include <string.h>
31
32 #include "lib/global.h" // include <glib.h>
33 #include "lib/terminal.h"
34 #include "lib/strutil.h"
35
36 /* --------------------------------------------------------------------------------------------- */
37
38 static void
39 setup (void)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
40 {
41 str_init_strings (NULL);
42 }
43
44 static void
45 teardown (void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
46 {
47 str_uninit_strings ();
48 }
49
50 /* --------------------------------------------------------------------------------------------- */
51
52 START_TEST (test_parse_csi)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
53 {
54 const char *s = &"\x1b[=5uRest"[2];
55 const char *end = s + strlen (s);
56 const gboolean ok = parse_csi (NULL, &s, end);
57
58 ck_assert_msg (ok, "failed to parse CSI");
59 ck_assert_str_eq (s, "Rest");
60 }
61 END_TEST
62
63 /* --------------------------------------------------------------------------------------------- */
64
65 START_TEST (test_strip_ctrl_codes)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
66 {
67 // clang-format off
68 char *s = g_strdup (ESC_STR "]0;~\a" ESC_STR "[30m" ESC_STR "(B" ESC_STR "[m"
69 ESC_STR "]133;A;special_key=1\a$ " ESC_STR "[K" ESC_STR "[?2004h"
70 ESC_STR "[>4;1m" ESC_STR "[=5u" ESC_STR "=" ESC_STR "[?2004l"
71 ESC_STR "[>4;0m" ESC_STR "[=0u" ESC_STR ">" ESC_STR "[?2004h"
72 ESC_STR "[>4;1m" ESC_STR "[=5u" ESC_STR "=" ESC_STR "[?2004l"
73 ESC_STR "[>4;0m" ESC_STR "[=0u" ESC_STR ">" ESC_STR "[?2004h"
74 ESC_STR "[>4;1m" ESC_STR "[=5u" ESC_STR "=");
75 // clang-format on
76 char *actual = strip_ctrl_codes (s);
77 const char *expected = "$ ";
78
79 ck_assert_str_eq (actual, expected);
80 g_free (s);
81 }
82 END_TEST
83
84 /* --------------------------------------------------------------------------------------------- */
85
86 int
87 main (void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
88 {
89 TCase *tc_core;
90
91 tc_core = tcase_create ("Core");
92
93 tcase_add_checked_fixture (tc_core, setup, teardown);
94
95 // Add new tests here: ***************
96 tcase_add_test (tc_core, test_parse_csi);
97 tcase_add_test (tc_core, test_strip_ctrl_codes);
98 // ***********************************
99
100 return mctest_run_all (tc_core);
101 }
102
103 /* --------------------------------------------------------------------------------------------- */