diff options
Diffstat (limited to 'tests/test_tui.c')
| -rw-r--r-- | tests/test_tui.c | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/tests/test_tui.c b/tests/test_tui.c index 98a1ac2..9c47c43 100644 --- a/tests/test_tui.c +++ b/tests/test_tui.c @@ -203,6 +203,13 @@ static void test_form_nav(void) { CHECK(TUI_NAV_NONE != TUI_FORM_SUBMIT); CHECK(tui_form_next(1, 3, 'x') == TUI_NAV_NONE); + CHECK(tui_form_next(2, 5, KEY_HOME) == 0); + CHECK(tui_form_next(2, 5, KEY_END) == 4); + CHECK(tui_form_next(0, 5, KEY_NPAGE) == 4); + CHECK(tui_form_next(0, 25, KEY_NPAGE) == 10); + CHECK(tui_form_next(12, 25, KEY_PPAGE) == 2); + CHECK(tui_form_next(3, 25, KEY_PPAGE) == 0); + CHECK(tui_form_next(3, 25, KEY_HOME) == 0); CHECK(tui_form_next(0, 3, KEY_UP) == TUI_NAV_NONE); CHECK(tui_form_next(0, 3, KEY_DOWN) == 1); CHECK(tui_form_next(2, 3, KEY_DOWN) == TUI_NAV_NONE); @@ -306,6 +313,84 @@ static void test_rowtable(void) CHECK(t.nrows == 1); /* never below one row */ } +static void test_rt_fields(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.focus == -1); + struct tui_form_field f[2]; + memset(f, 0, sizeof f); + f[0].label = "Datum"; + f[1].label = "Text"; + tui_rt_set_fields(&t, f, 2); + CHECK(t.fields == f && t.nfields == 2 && t.focus == 0); + tui_rt_set_fields(&t, NULL, 0); + CHECK(t.fields == NULL && t.nfields == 0 && t.focus == -1); +} + +static void test_rt_focus(void) +{ + int row, col; + + /* fields 0..2, then a 2-row table with 3 editable cells each */ + row = 0; + col = 0; + CHECK(tui_rt_focus_step(0, 3, &row, &col, 2, 3, 1) == 1); + CHECK(tui_rt_focus_step(1, 3, &row, &col, 2, 3, 1) == 2); + CHECK(tui_rt_focus_step(2, 3, &row, &col, 2, 3, 1) == -1); + CHECK(row == 0 && col == 0); + CHECK(tui_rt_focus_step(-1, 3, &row, &col, 2, 3, 1) == -1); + CHECK(row == 0 && col == 1); + CHECK(tui_rt_focus_step(-1, 3, &row, &col, 2, 3, 1) == -1); + CHECK(row == 0 && col == 2); + CHECK(tui_rt_focus_step(-1, 3, &row, &col, 2, 3, 1) == -1); + CHECK(row == 1 && col == 0); + /* after the last cell Tab wraps back to the first field */ + row = 1; + col = 2; + CHECK(tui_rt_focus_step(-1, 3, &row, &col, 2, 3, 1) == 0); + /* Shift-Tab from the first field lands on the last cell */ + CHECK(tui_rt_focus_step(0, 3, &row, &col, 2, 3, -1) == -1); + CHECK(row == 1 && col == 2); + CHECK(tui_rt_focus_step(-1, 3, &row, &col, 2, 3, -1) == -1); + CHECK(row == 1 && col == 1); + /* Shift-Tab from the first cell wraps back to the last field */ + row = 0; + col = 0; + CHECK(tui_rt_focus_step(-1, 3, &row, &col, 2, 3, -1) == 2); + /* without fields the table still cycles through its cells */ + row = 0; + col = 0; + CHECK(tui_rt_focus_step(-1, 0, &row, &col, 2, 3, -1) == -1); + CHECK(row == 1 && col == 2); + CHECK(tui_rt_focus_step(-1, 0, &row, &col, 2, 3, 1) == -1); + CHECK(row == 0 && col == 0); + /* an empty table cycles only through the fields */ + row = 0; + col = 0; + CHECK(tui_rt_focus_step(2, 3, &row, &col, 0, 0, 1) == 0); + CHECK(tui_rt_focus_step(0, 3, &row, &col, 0, 0, -1) == 2); +} + +static void test_rt_footer(void) +{ + CHECK(tui_rt_footer_row(24) == 22); + CHECK(tui_rt_footer_row(4) == 2); + CHECK(tui_rt_footer_row(3) == 2); + CHECK(tui_rt_footer_row(2) == 2); + char f[64]; + snprintf(f, sizeof f, "Bilagor: a\nSumma 0,00 I BALANS"); + tui_rt_footer_flatten(f); + CHECK(strcmp(f, "Bilagor: a Summa 0,00 I BALANS") == 0); + snprintf(f, sizeof f, "en enda rad"); + tui_rt_footer_flatten(f); + CHECK(strcmp(f, "en enda rad") == 0); + f[0] = '\0'; + tui_rt_footer_flatten(f); + CHECK(f[0] == '\0'); +} + int main(void) { test_disp_width(); @@ -316,6 +401,9 @@ int main(void) test_nav(); test_form_nav(); test_rowtable(); + test_rt_fields(); + test_rt_focus(); + test_rt_footer(); printf("test_tui: %d checks, %d failures\n", checks, failures); return failures ? 1 : 0; } |
