From a71b4d629bf723dcb6d9e61463f50a2fe9c66601 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 10 Apr 2021 12:56:41 +0200 Subject: Add more tests and fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libterminput.c | 11 ++-- test.c | 188 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+), 7 deletions(-) diff --git a/libterminput.c b/libterminput.c index 0b2b22d..960967d 100644 --- a/libterminput.c +++ b/libterminput.c @@ -86,7 +86,7 @@ again: input->mods = ctx->mods | LIBTERMINPUT_CTRL; ctx->mods = 0; return 1; - } else if ((c & 0xC0) == 0xC0) { + } else if ((c & 0xC0) == 0xC0 && c != 0xFF) { /* Beginning of multibyte-character */ ctx->n = 0; for (tc = c; tc & 0x80; tc <<= 1) @@ -105,11 +105,8 @@ again: /* 8th bit set to signify META */ c ^= 0x80; ctx->mods |= LIBTERMINPUT_META; - if (c == 033) - goto single_byte; goto again; } else { - single_byte: /* Single-byte-character */ input->symbol[0] = (char)c; input->symbol[1] = '\0'; @@ -800,7 +797,7 @@ libterminput_read(int fd, union libterminput_input *input, struct libterminput_s } again: - if (!*ret.symbol) { /* TODO test */ + if (!*ret.symbol) { /* Incomplete input */ if (ctx->meta < 3) { /* Up to two Meta/ESC, wait until a third or something else is read */ @@ -811,7 +808,7 @@ again: input->type = LIBTERMINPUT_KEYPRESS; input->keypress.key = LIBTERMINPUT_ESC; input->keypress.times = 3; - input->keypress.mods = ret.mods; + input->keypress.mods = 0; input->keypress.symbol[0] = '\0'; ctx->meta -= 3; } else if (*ctx->key) { @@ -902,7 +899,7 @@ again: /* ESC [ or ESC 0 is used as the beginning of most special keys */ strcpy(ctx->key, ret.symbol); input->type = LIBTERMINPUT_NONE; - } else { /* TODO test */ + } else { /* Character input and single-byte special keys */ input->type = LIBTERMINPUT_KEYPRESS; input->keypress.mods = ret.mods; diff --git a/test.c b/test.c index 86d9bfa..223353f 100644 --- a/test.c +++ b/test.c @@ -126,6 +126,17 @@ main(void) exit(1);\ } while (0) +#define TYPE_MEM(STR, LEN, T)\ + do {\ + alarm(5);\ + if (LEN)\ + TEST(write(fds[1], STR, (LEN)) == (LEN));\ + do {\ + TEST(libterminput_read(fds[0], &input, &ctx) == 1);\ + } while (input.type == LIBTERMINPUT_NONE && libterminput_is_ready(&input, &ctx));\ + TEST(input.type == (T));\ + } while (0) + #define TYPE(STR, T)\ do {\ alarm(5);\ @@ -210,6 +221,29 @@ main(void) KEYNUM_("\033\033[", NUM, ";8", (KEY), (MODS) | LIBTERMINPUT_SHIFT | LIBTERMINPUT_META | LIBTERMINPUT_CTRL);\ } while (0) +#define KEYPRESS_SPECIAL_CHAR(CHAR, KEY)\ + do {\ + buffer[0] = (CHAR);\ + buffer[1] = '\0';\ + TYPE(buffer, LIBTERMINPUT_KEYPRESS);\ + TEST(input.keypress.mods == 0);\ + TEST(input.keypress.times == 1);\ + TEST(input.keypress.key == (KEY));\ + buffer[0] = '\033';\ + buffer[1] = (CHAR);\ + buffer[2] = '\0';\ + TYPE(buffer, LIBTERMINPUT_KEYPRESS);\ + TEST(input.keypress.mods == LIBTERMINPUT_META);\ + TEST(input.keypress.times == 1);\ + TEST(input.keypress.key == (KEY));\ + buffer[0] = (char)((CHAR) | 0x80);\ + buffer[1] = '\0';\ + TYPE(buffer, LIBTERMINPUT_KEYPRESS);\ + TEST(input.keypress.mods == LIBTERMINPUT_META);\ + TEST(input.keypress.times == 1);\ + TEST(input.keypress.key == (KEY));\ + } while (0) + char buffer[512], numbuf[3 * sizeof(int) + 2]; struct libterminput_state ctx; union libterminput_input input; @@ -281,6 +315,160 @@ main(void) TEST(input.position.x == 93); libterminput_clear_flags(&ctx, LIBTERMINPUT_AWAITING_CURSOR_POSITION); + TYPE("\033", LIBTERMINPUT_NONE); + TYPE("\033", LIBTERMINPUT_NONE); + TYPE("\033", LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_ESC); + TEST(input.keypress.mods == 0); + TEST(input.keypress.times == 3); + TYPE(NULL, LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_ESC); + TEST(input.keypress.mods == 0); + TEST(input.keypress.times == 2); + TYPE(NULL, LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_ESC); + TEST(input.keypress.mods == 0); + TEST(input.keypress.times == 1); + + TYPE("\033", LIBTERMINPUT_NONE); + TYPE("\033", LIBTERMINPUT_NONE); + TYPE("\033", LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_ESC); + TEST(input.keypress.mods == 0); + TEST(input.keypress.times == 3); + TYPE(NULL, LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_ESC); + TEST(input.keypress.mods == 0); + TEST(input.keypress.times == 2); + TYPE(NULL, LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_ESC); + TEST(input.keypress.mods == 0); + TEST(input.keypress.times == 1); + + KEYPRESS_SPECIAL_CHAR('\x7F', LIBTERMINPUT_ERASE); + KEYPRESS_SPECIAL_CHAR('\b', LIBTERMINPUT_ERASE); + KEYPRESS_SPECIAL_CHAR('\t', LIBTERMINPUT_TAB); + KEYPRESS_SPECIAL_CHAR('\n', LIBTERMINPUT_ENTER); + libterminput_set_flags(&ctx, LIBTERMINPUT_ESC_ON_BLOCK); + KEYPRESS_SPECIAL_CHAR('\033', LIBTERMINPUT_ESC); + libterminput_clear_flags(&ctx, LIBTERMINPUT_ESC_ON_BLOCK); + + TYPE("text", LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_SYMBOL); + TEST(input.keypress.mods == 0); + TEST(input.keypress.times == 1); + TEST(input.keypress.symbol[0] == 't'); + TEST(input.keypress.symbol[1] == '\0'); + TYPE(NULL, LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_SYMBOL); + TEST(input.keypress.mods == 0); + TEST(input.keypress.times == 1); + TEST(input.keypress.symbol[0] == 'e'); + TEST(input.keypress.symbol[1] == '\0'); + TYPE(NULL, LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_SYMBOL); + TEST(input.keypress.mods == 0); + TEST(input.keypress.times == 1); + TEST(input.keypress.symbol[0] == 'x'); + TEST(input.keypress.symbol[1] == '\0'); + TYPE(NULL, LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_SYMBOL); + TEST(input.keypress.mods == 0); + TEST(input.keypress.times == 1); + TEST(input.keypress.symbol[0] == 't'); + TEST(input.keypress.symbol[1] == '\0'); + + TYPE("åäö", LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_SYMBOL); + TEST(input.keypress.mods == 0); + TEST(input.keypress.times == 1); + TEST(!strcmp(input.keypress.symbol, "å")); + TYPE(NULL, LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_SYMBOL); + TEST(input.keypress.mods == 0); + TEST(input.keypress.times == 1); + TEST(!strcmp(input.keypress.symbol, "ä")); + TYPE(NULL, LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_SYMBOL); + TEST(input.keypress.mods == 0); + TEST(input.keypress.times == 1); + TEST(!strcmp(input.keypress.symbol, "ö")); + + TYPE("\033z", LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_SYMBOL); + TEST(input.keypress.mods == LIBTERMINPUT_META); + TEST(input.keypress.times == 1); + TEST(!strcmp(input.keypress.symbol, "z")); + TYPE("\033ö", LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_SYMBOL); + TEST(input.keypress.mods == LIBTERMINPUT_META); + TEST(input.keypress.times == 1); + TEST(!strcmp(input.keypress.symbol, "ö")); + + buffer[0] = '-'; + buffer[0] |= 0x80; + buffer[1] = 0; + TYPE(buffer, LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_SYMBOL); + TEST(input.keypress.mods == LIBTERMINPUT_META); + TEST(input.keypress.times == 1); + TEST(!strcmp(input.keypress.symbol, "-")); + + buffer[0] = 'Y'; + buffer[0] -= '@'; + buffer[1] = 0; + TYPE(buffer, LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_SYMBOL); + TEST(input.keypress.mods == LIBTERMINPUT_CTRL); + TEST(input.keypress.times == 1); + TEST(!strcmp(input.keypress.symbol, "Y")); + + buffer[0] = '\033'; + buffer[1] = 'Y'; + buffer[1] -= '@'; + buffer[2] = 0; + TYPE(buffer, LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_SYMBOL); + TEST(input.keypress.mods == LIBTERMINPUT_CTRL | LIBTERMINPUT_META); + TEST(input.keypress.times == 1); + TEST(!strcmp(input.keypress.symbol, "Y")); + + buffer[0] = 'Y'; + buffer[0] -= '@'; + buffer[0] |= 0x80; + buffer[1] = 0; + TYPE(buffer, LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_SYMBOL); + TEST(input.keypress.mods == LIBTERMINPUT_CTRL | LIBTERMINPUT_META); + TEST(input.keypress.times == 1); + TEST(!strcmp(input.keypress.symbol, "Y")); + + buffer[0] = '\033'; + buffer[1] = 'Y'; + buffer[1] -= '@'; + buffer[1] |= 0x80; + buffer[2] = 0; + TYPE(buffer, LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_SYMBOL); + TEST(input.keypress.mods == LIBTERMINPUT_CTRL | LIBTERMINPUT_META); + TEST(input.keypress.times == 1); + TEST(!strcmp(input.keypress.symbol, "Y")); + + buffer[0] = 0; + TYPE_MEM(buffer, 1, LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_SYMBOL); + TEST(input.keypress.mods == LIBTERMINPUT_CTRL); + TEST(input.keypress.times == 1); + TEST(!strcmp(input.keypress.symbol, " ")); + + buffer[0] = '\033'; + buffer[1] = 0; + TYPE_MEM(buffer, 2, LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_SYMBOL); + TEST(input.keypress.mods == LIBTERMINPUT_CTRL | LIBTERMINPUT_META); + TEST(input.keypress.times == 1); + TEST(!strcmp(input.keypress.symbol, " ")); + close(fds[1]); TEST(libterminput_read(fds[0], &input, &ctx) == 0); close(fds[0]); -- cgit v1.2.3-70-g09d2