1 /*
2 lib - tty function testing
3
4 Copyright (C) 2011-2025
5 Free Software Foundation, Inc.
6
7 This file is part of the Midnight Commander.
8
9 The Midnight Commander is free software: you can redistribute it
10 and/or modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation, either version 3 of the License,
12 or (at your option) any later version.
13
14 The Midnight Commander is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23 #define TEST_SUITE_NAME "/lib"
24
25 #include "tests/mctest.h"
26
27 #include <stdio.h>
28
29 #include "lib/strutil.h"
30 #include "lib/util.h"
31
32 #include "lib/tty/tty.h"
33
34 /* --------------------------------------------------------------------------------------------- */
35 /* @CapturedValue */
36 static int my_exit__status__captured;
37
38 /* @Mock */
39 void
40 my_exit (int status)
/* ![[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)
*/
41 {
42 my_exit__status__captured = status;
43 }
44
45 /* --------------------------------------------------------------------------------------------- */
46
47 START_TEST (test_tty_check_term_unset)
/* ![[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)
*/
48 {
49 // given
50 setenv ("TERM", "", 1);
51
52 // when
53 tty_check_xterm_compat (FALSE);
54
55 // then
56 ck_assert_int_eq (my_exit__status__captured, 1);
57 }
58 END_TEST
59
60 /* --------------------------------------------------------------------------------------------- */
61
62 START_TEST (test_tty_check_term_non_xterm)
/* ![[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)
*/
63 {
64 // given
65 setenv ("TERM", "gnome-terminal", 1);
66
67 // when
68 const gboolean actual_result_force_false = tty_check_xterm_compat (FALSE);
69 const gboolean actual_result_force_true = tty_check_xterm_compat (TRUE);
70
71 // then
72 ck_assert_int_eq (my_exit__status__captured, 0);
73 ck_assert_int_eq (actual_result_force_false, 0);
74 ck_assert_int_eq (actual_result_force_true, 1);
75 }
76 END_TEST
77 /* --------------------------------------------------------------------------------------------- */
78
79 START_TEST (test_tty_check_term_xterm_like)
/* ![[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)
*/
80 {
81 // given
82 setenv ("TERM", "alacritty-terminal", 1);
83
84 // when
85 const gboolean actual_result_force_false = tty_check_xterm_compat (FALSE);
86 const gboolean actual_result_force_true = tty_check_xterm_compat (TRUE);
87
88 // then
89 ck_assert_int_eq (my_exit__status__captured, 0);
90 ck_assert_int_eq (actual_result_force_false, 1);
91 ck_assert_int_eq (actual_result_force_true, 1);
92 }
93 END_TEST
94
95 /* --------------------------------------------------------------------------------------------- */
96
97 int
98 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)
*/
99 {
100 TCase *tc_core;
101
102 tc_core = tcase_create ("Core");
103
104 // Add new tests here: ***************
105 tcase_add_test (tc_core, test_tty_check_term_unset);
106 tcase_add_test (tc_core, test_tty_check_term_non_xterm);
107 tcase_add_test (tc_core, test_tty_check_term_xterm_like);
108 // ***********************************
109
110 return mctest_run_all (tc_core);
111 }
112
113 /* --------------------------------------------------------------------------------------------- */