Skip to content

Commit 2b2e1ef

Browse files
author
Jakub Horký
committed
tests/lib/tty.c: Add key mocking support
Make functions tty_lowlevel_getch() and getch_with_timeout() mockable by defining them as weak. Fake key input can be passed by mock_input(). Signed-off-by: Jakub Horký <jakub.git@horky.net>
1 parent db05396 commit 2b2e1ef

6 files changed

Lines changed: 118 additions & 24 deletions

File tree

lib/tty/key.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,26 +1149,6 @@ correct_key_code (int code)
11491149

11501150
/* --------------------------------------------------------------------------------------------- */
11511151

1152-
static int
1153-
getch_with_timeout (unsigned int delay_us)
1154-
{
1155-
fd_set Read_FD_Set;
1156-
int c;
1157-
struct timeval time_out;
1158-
1159-
time_out.tv_sec = delay_us / G_USEC_PER_SEC;
1160-
time_out.tv_usec = delay_us % G_USEC_PER_SEC;
1161-
tty_nodelay (TRUE);
1162-
FD_ZERO (&Read_FD_Set);
1163-
FD_SET (input_fd, &Read_FD_Set);
1164-
select (input_fd + 1, &Read_FD_Set, NULL, NULL, &time_out);
1165-
c = tty_lowlevel_getch ();
1166-
tty_nodelay (FALSE);
1167-
return c;
1168-
}
1169-
1170-
/* --------------------------------------------------------------------------------------------- */
1171-
11721152
static void
11731153
learn_store_key (GString *buffer, int c)
11741154
{
@@ -1309,6 +1289,26 @@ lookup_keycode (const int code, int *idx)
13091289
/* This has to be called before init_slang or whatever routine
13101290
calls any define_sequence */
13111291

1292+
int
1293+
getch_with_timeout (unsigned int delay_us)
1294+
{
1295+
fd_set Read_FD_Set;
1296+
int c;
1297+
struct timeval time_out;
1298+
1299+
time_out.tv_sec = delay_us / G_USEC_PER_SEC;
1300+
time_out.tv_usec = delay_us % G_USEC_PER_SEC;
1301+
tty_nodelay (TRUE);
1302+
FD_ZERO (&Read_FD_Set);
1303+
FD_SET (input_fd, &Read_FD_Set);
1304+
select (input_fd + 1, &Read_FD_Set, NULL, NULL, &time_out);
1305+
c = tty_lowlevel_getch ();
1306+
tty_nodelay (FALSE);
1307+
return c;
1308+
}
1309+
1310+
/* --------------------------------------------------------------------------------------------- */
1311+
13121312
void
13131313
init_key (void)
13141314
{

lib/tty/key.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ char *tty_keycode_to_keyname (const int keycode);
8080
int tty_get_event (struct Gpm_Event *event, gboolean redo_event, gboolean block);
8181
gboolean is_idle (void);
8282
int tty_getch (void);
83+
MC_MOCKABLE int getch_with_timeout (unsigned int delay_us);
8384

8485
/* While waiting for input, the program can select on more than one file */
8586
typedef int (*select_fn) (int fd, void *info);

lib/tty/tty-internal.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ extern int sigwinch_pipe[2];
3535

3636
/*** declarations of public functions ************************************************************/
3737

38+
void load_terminfo_keys (void);
39+
3840
void tty_create_winch_pipe (void);
3941
void tty_destroy_winch_pipe (void);
4042

4143
char *mc_tty_normalize_from_utf8 (const char *str);
4244
void tty_init_xterm_support (gboolean is_xterm);
43-
int tty_lowlevel_getch (void);
45+
MC_MOCKABLE int tty_lowlevel_getch (void);
4446

4547
void tty_colorize_area (int y, int x, int rows, int cols, int color);
4648

lib/tty/tty-slang.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,11 @@ do_define_key (int code, const char *strcap)
210210
define_sequence (code, seq, MCKEY_NOACTION);
211211
}
212212

213+
/* --------------------------------------------------------------------------------------------- */
214+
/*** public functions ****************************************************************************/
213215
/* --------------------------------------------------------------------------------------------- */
214216

215-
static void
217+
void
216218
load_terminfo_keys (void)
217219
{
218220
int i;
@@ -221,8 +223,6 @@ load_terminfo_keys (void)
221223
do_define_key (key_table[i].key_code, key_table[i].key_name);
222224
}
223225

224-
/* --------------------------------------------------------------------------------------------- */
225-
/*** public functions ****************************************************************************/
226226
/* --------------------------------------------------------------------------------------------- */
227227

228228
int

tests/lib/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ terminal_SOURCES = \
5151

5252
tty_SOURCES = \
5353
tty.c
54+
tty_LDADD = $(SLANG_LIBS)
5455

5556
utilunix__mc_pstream_get_string_SOURCES = \
5657
utilunix__mc_pstream_get_string.c

tests/lib/tty.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,46 @@
3030
#include "lib/util.h"
3131

3232
#include "lib/tty/tty.h"
33+
#include "lib/tty/tty-internal.h"
34+
#include "lib/tty/key.h"
35+
36+
static int mock_input_buf[1000];
37+
static int *mock_input_ptr;
38+
39+
static void
40+
mock_input (const char *charinput)
41+
{
42+
mock_input_ptr = mock_input_buf;
43+
44+
while (*charinput != '\0')
45+
*mock_input_ptr++ = (*charinput++ & 0xFF);
46+
*mock_input_ptr = '\0';
47+
48+
mock_input_ptr = mock_input_buf;
49+
}
50+
51+
/* @Mock */
52+
int
53+
tty_lowlevel_getch (void)
54+
{
55+
int key = *mock_input_ptr;
56+
if (key != '\0')
57+
{
58+
mock_input_ptr++;
59+
return key;
60+
}
61+
else
62+
return -1;
63+
}
64+
65+
/* @Mock */
66+
int
67+
getch_with_timeout (unsigned int delay_us)
68+
{
69+
(void) (delay_us);
70+
71+
return tty_lowlevel_getch ();
72+
}
3373

3474
/* --------------------------------------------------------------------------------------------- */
3575
/* @CapturedValue */
@@ -74,6 +114,7 @@ START_TEST (test_tty_check_term_non_xterm)
74114
ck_assert_int_eq (actual_result_force_true, 1);
75115
}
76116
END_TEST
117+
77118
/* --------------------------------------------------------------------------------------------- */
78119

79120
START_TEST (test_tty_check_term_xterm_like)
@@ -94,6 +135,54 @@ END_TEST
94135

95136
/* --------------------------------------------------------------------------------------------- */
96137

138+
START_TEST (test_tty_get_key_code)
139+
{
140+
mc_global.tty.xterm_flag = TRUE;
141+
142+
setenv ("TERM", "xterm", 1);
143+
init_key ();
144+
#ifdef HAVE_SLANG
145+
SLtt_get_terminfo ();
146+
load_terminfo_keys ();
147+
#endif
148+
149+
mock_input ("\x1b[1;2A");
150+
ck_assert_int_eq (get_key_code (0), KEY_M_SHIFT | KEY_UP);
151+
ck_assert_int_eq (get_key_code (0), -1);
152+
ck_assert_int_eq (get_key_code (0), -1);
153+
154+
mock_input ("😊FG");
155+
ck_assert_int_eq (get_key_code (0), 0xF0);
156+
ck_assert_int_eq (get_key_code (0), 0x9F);
157+
ck_assert_int_eq (get_key_code (0), 0x98);
158+
ck_assert_int_eq (get_key_code (0), 0x8A);
159+
ck_assert_int_eq (get_key_code (0), 'F');
160+
ck_assert_int_eq (get_key_code (0), 'G');
161+
ck_assert_int_eq (get_key_code (0), -1);
162+
ck_assert_int_eq (get_key_code (0), -1);
163+
164+
mock_input ("ц\x1b[1;2A=5ů§a");
165+
ck_assert_int_eq (get_key_code (0), 0xD1); // 'ц'
166+
ck_assert_int_eq (get_key_code (0), 0x86);
167+
ck_assert_int_eq (get_key_code (0), KEY_M_SHIFT | KEY_UP);
168+
ck_assert_int_eq (get_key_code (0), '=');
169+
ck_assert_int_eq (get_key_code (0), '5');
170+
ck_assert_int_eq (get_key_code (0), 0xC5); // 'ů'
171+
ck_assert_int_eq (get_key_code (0), 0xAF);
172+
ck_assert_int_eq (get_key_code (0), 0xC2); // '§'
173+
ck_assert_int_eq (get_key_code (0), 0xA7);
174+
ck_assert_int_eq (get_key_code (0), 'a');
175+
ck_assert_int_eq (get_key_code (0), -1);
176+
ck_assert_int_eq (get_key_code (0), -1);
177+
178+
mock_input ("\x1b[u");
179+
ck_assert_int_eq (get_key_code (0), -1);
180+
ck_assert_int_eq (get_key_code (0), -1);
181+
}
182+
END_TEST
183+
184+
/* --------------------------------------------------------------------------------------------- */
185+
97186
int
98187
main (void)
99188
{
@@ -105,6 +194,7 @@ main (void)
105194
tcase_add_test (tc_core, test_tty_check_term_unset);
106195
tcase_add_test (tc_core, test_tty_check_term_non_xterm);
107196
tcase_add_test (tc_core, test_tty_check_term_xterm_like);
197+
tcase_add_test (tc_core, test_tty_get_key_code);
108198
// ***********************************
109199

110200
return mctest_run_all (tc_core);

0 commit comments

Comments
 (0)