From 985ae80d7f82cef7c273fae953c73f8bd909ebef Mon Sep 17 00:00:00 2001 From: Anders Betts Date: Sat, 19 Sep 2026 23:17:45 +0200 Subject: tui: backspace deletes backwards while editing; esc restores the value --- tests/test_tui.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'tests') diff --git a/tests/test_tui.c b/tests/test_tui.c index f4405b4..93aece0 100644 --- a/tests/test_tui.c +++ b/tests/test_tui.c @@ -104,6 +104,42 @@ static void test_ledit(void) CHECK(tui_le_key(&e, KEY_UP) == 0); /* not an editing key */ } +static void test_field_fresh(void) +{ + char buf[16]; + size_t pos = (size_t)-1; + int fresh = 1; + + /* fresh + backspace deletes the last character, it does not clear */ + snprintf(buf, sizeof buf, "abc"); + CHECK(tui_field_edit(buf, sizeof buf, &pos, KEY_BACKSPACE, &fresh)); + CHECK(strcmp(buf, "ab") == 0); + + /* the first printable still replaces the whole prefilled value */ + snprintf(buf, sizeof buf, "abc"); + pos = (size_t)-1; + fresh = 1; + CHECK(tui_field_edit(buf, sizeof buf, &pos, 'x', &fresh)); + CHECK(strcmp(buf, "x") == 0); + + /* movement keeps the content, clears fresh; the next printable is + inserted at the caret */ + snprintf(buf, sizeof buf, "abc"); + pos = (size_t)-1; + fresh = 1; + CHECK(tui_field_edit(buf, sizeof buf, &pos, KEY_LEFT, &fresh)); + CHECK(strcmp(buf, "abc") == 0 && pos == 2 && fresh == 0); + CHECK(tui_field_edit(buf, sizeof buf, &pos, 'x', &fresh)); + CHECK(strcmp(buf, "abxc") == 0); + + /* fresh + delete removes the character at the caret, no clear */ + snprintf(buf, sizeof buf, "abc"); + pos = 0; + fresh = 1; + CHECK(tui_field_edit(buf, sizeof buf, &pos, KEY_DC, &fresh)); + CHECK(strcmp(buf, "bc") == 0); +} + static void date_feed(char *buf, size_t cap, const char *digits) { size_t pos = (size_t)-1; @@ -139,6 +175,18 @@ static void test_date_field(void) fresh = 0; tui_date_field_edit(buf, sizeof buf, &pos, '9', &fresh); CHECK(strcmp(buf, "2026-06-09") == 0); + + /* fresh + backspace deletes the last digit; fresh + ^U clears */ + snprintf(buf, sizeof buf, "2026-06-04"); + pos = (size_t)-1; + fresh = 1; + CHECK(tui_date_field_edit(buf, sizeof buf, &pos, KEY_BACKSPACE, &fresh)); + CHECK(strcmp(buf, "2026-06-0") == 0); + snprintf(buf, sizeof buf, "2026-06-04"); + pos = (size_t)-1; + fresh = 1; + CHECK(tui_date_field_edit(buf, sizeof buf, &pos, 21, &fresh)); + CHECK(strcmp(buf, "") == 0); } static void nav_init(struct tui_list_nav *v, int n) @@ -479,6 +527,7 @@ int main(void) test_formats(); test_parse_kr(); test_ledit(); + test_field_fresh(); test_date_field(); test_styles(); test_markup(); -- cgit v1.3