1 /* $OpenBSD: test_get.c,v 1.5 2017/07/07 23:55:21 bluhm Exp $ */
2 /*
3 * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #include <err.h>
19
20 #include "keymacro.c"
21
22 /*
23 * Glue for unit tests of libedit/keymacro.c.
24 * Rather than linking in all the various libedit modules,
25 * provide dummies for those functions called in keymacro.c.
26 * Most aren't actually called in keymacro_get().
27 */
28
29 #define EL EditLine *el __attribute__((__unused__))
30 #define UU __attribute__((__unused__))
31
ct_encode_char(char * dst UU,size_t len UU,wchar_t c UU)32 ssize_t ct_encode_char(char *dst UU, size_t len UU, wchar_t c UU) { return -1; }
33
ct_encode_string(const wchar_t * s UU,ct_buffer_t * conv UU)34 char *ct_encode_string(const wchar_t *s UU, ct_buffer_t *conv UU)
35 {
36 return NULL;
37 }
38
39 ssize_t
ct_visual_char(wchar_t * dst,size_t len UU,wchar_t c)40 ct_visual_char(wchar_t *dst, size_t len UU, wchar_t c)
41 {
42 *dst = c;
43 return 1;
44 }
45
46 int
el_wgetc(EL,wchar_t * cp)47 el_wgetc(EL, wchar_t *cp) {
48 static const wchar_t *const input_buffer = L"adalixi";
49 static const wchar_t *input_ptr = input_buffer;
50
51 *cp = *input_ptr;
52 if (*cp == '\0') {
53 input_ptr = input_buffer;
54 return 0;
55 } else {
56 input_ptr++;
57 return 1;
58 }
59 }
60
61 #undef EL
62 #undef UU
63
64 /*
65 * Unit test steering program for editline/keymacro.c, keymacro_get().
66 */
67
68 int
main()69 main()
70 {
71 EditLine el;
72 keymacro_value_t val;
73 wchar_t repl[] = L"repl";
74 wchar_t ch;
75 int irc;
76
77 if (keymacro_init(&el) == -1)
78 err(1, "keymacro_init");
79 keymacro_add(&el, L"ad", keymacro_map_cmd(&el, VI_ADD), XK_CMD);
80 keymacro_add(&el, L"al", keymacro_map_str(&el, repl), XK_STR);
81 keymacro_add(&el, L"in", keymacro_map_cmd(&el, ED_INSERT), XK_CMD);
82
83 if (el_wgetc(&el, &ch) == 0)
84 errx(1, "el_wgetc ad");
85 irc = keymacro_get(&el, &ch, &val);
86 if (irc != XK_CMD)
87 errx(1, "ad %d != XK_CMD", irc);
88 if (val.cmd != VI_ADD)
89 errx(1, "ad %u != VI_ADD", val.cmd);
90 if (ch != L'd')
91 errx(1, "ad %lc != d", ch);
92
93 if (el_wgetc(&el, &ch) == 0)
94 errx(1, "el_wgetc al");
95 irc = keymacro_get(&el, &ch, &val);
96 if (irc != XK_STR)
97 errx(1, "al %d != XK_STR", irc);
98 if (wcscmp(val.str, L"repl") != 0)
99 errx(1, "al %ls != repl", val.str);
100 if (ch != L'\0')
101 errx(1, "al %lc != 0", ch);
102
103 if (el_wgetc(&el, &ch) == 0)
104 errx(1, "el_wgetc ix");
105 irc = keymacro_get(&el, &ch, &val);
106 if (irc != XK_STR)
107 errx(1, "ix %d != XK_STR", irc);
108 if (val.str != NULL)
109 errx(1, "ix %ls != NULL", val.str);
110 if (ch != L'x')
111 errx(1, "ix %lc != x", ch);
112
113 if (el_wgetc(&el, &ch) == 0)
114 errx(1, "el_wgetc i");
115 irc = keymacro_get(&el, &ch, &val);
116 if (irc != XK_NOD)
117 errx(1, "eof %d != XK_NOD", irc);
118 return 0;
119 }
120