From 84c4fdcf138ad6ef4f7edfea76f2318b7a7a5e3e Mon Sep 17 00:00:00 2001 From: Anders Betts Date: Sat, 19 Sep 2026 22:03:06 +0200 Subject: tui: row table widget (dynamic rows, trailing blank row) + tests --- tests/test_tui.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'tests') diff --git a/tests/test_tui.c b/tests/test_tui.c index e056c47..b2462e8 100644 --- a/tests/test_tui.c +++ b/tests/test_tui.c @@ -207,6 +207,61 @@ static void test_form_nav(void) CHECK(tui_form_next(1, 3, 'x') == TUI_NAV_NONE); } + +static char rt_cells[8][3][64]; +static void rt_cb(void *ud, int row, int col, char **buf, size_t *cap) +{ + (void)ud; + *buf = rt_cells[row][col]; + *cap = sizeof rt_cells[row][col]; +} +static void rt_info_cb(void *ud, int row, char *buf, size_t n) +{ + (void)ud; + snprintf(buf, n, "info%d", row); +} +static const struct tui_rt_col RT_COLS[3] = { + { "A", 2, 8, TUI_RT_EDIT }, + { "N", 12, 8, TUI_RT_INFO }, + { "B", 22, 8, TUI_RT_EDIT }, +}; + +static void test_rowtable(void) +{ + struct tui_rt t; + memset(rt_cells, 0, sizeof rt_cells); + tui_rt_init(&t, 1, 8, 3, RT_COLS, 6, rt_cb, rt_info_cb, NULL); + CHECK(t.nrows == 1 && t.neditable == 2 && t.row == 0 && t.col == 0); + CHECK(tui_rt_blank_row(&t, 0)); + CHECK(tui_rt_move(&t, 0, 1) && t.col == 1); + CHECK(tui_rt_move(&t, 0, 1) && t.col == 0); + CHECK(tui_rt_move(&t, -1, 0) == 0); + CHECK(tui_rt_move(&t, 1, 0) == 0); + + /* data in the blank row appends a new blank row */ + snprintf(rt_cells[0][0], 64, "1930"); + CHECK(tui_rt_normalize(&t) == 2); + snprintf(rt_cells[1][0], 64, "x"); + CHECK(tui_rt_normalize(&t) == 3); + /* two trailing blank rows collapse into one */ + t.nrows = 5; + CHECK(tui_rt_normalize(&t) == 3); + + /* ^X clears the row and re-normalizes */ + t.row = 1; + t.col = 0; + CHECK(tui_rt_clear(&t)); + CHECK(t.nrows == 2); + CHECK(rt_cells[1][0][0] == '\0'); + CHECK(tui_rt_clear(&t)); + CHECK(t.nrows == 2); /* the second clear only hit the blank row */ + t.row = 0; + CHECK(tui_rt_clear(&t)); + CHECK(t.nrows == 1); + tui_rt_clear(&t); + CHECK(t.nrows == 1); /* never below one row */ +} + int main(void) { test_disp_width(); @@ -216,6 +271,7 @@ int main(void) test_date_field(); test_nav(); test_form_nav(); + test_rowtable(); printf("test_tui: %d checks, %d failures\n", checks, failures); return failures ? 1 : 0; } -- cgit v1.3