diff options
Diffstat (limited to 'tests/test_tui.c')
| -rw-r--r-- | tests/test_tui.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/tests/test_tui.c b/tests/test_tui.c index 93aece0..15a44c7 100644 --- a/tests/test_tui.c +++ b/tests/test_tui.c @@ -351,6 +351,88 @@ static void test_form_nav(void) CHECK(tui_form_next(1, 3, 'x') == TUI_NAV_NONE); } +/* Fields 0..2, then six action rows: 0 heading, 1/2 selectable, 3 disabled + but selectable, 4 heading, 5 selectable. Focus 3..8 is actions 0..5. */ +static const struct tui_form_action ACTS[6] = { + { "Bokslut bokfört: nej", -1, NULL }, + { "Årsredovisning (K2)", 1, NULL }, + { "Inkomstdeklaration (INK2/SRU)", 1, NULL }, + { "Bokför bokslut", 0, "året är stängt" }, + { "rubrik igen", -1, NULL }, + { "Bokslutsplan (torrkörning)", 1, NULL }, +}; + +static void test_form_actions(void) +{ + const int nf = 3; + + CHECK(TUI_FORM_ACTION > 0); + CHECK(TUI_FORM_ACTION != TUI_FORM_BACK); + CHECK(TUI_FORM_ACTION != TUI_FORM_REFRESH); + CHECK(TUI_FORM_ACTION != TUI_FORM_SUBMIT); + CHECK(TUI_FORM_ACTION + 5 > nf); + + /* initial focus: first field, or the first selectable action */ + CHECK(tui_form_act_first(nf, ACTS, 6) == 0); + CHECK(tui_form_act_first(0, ACTS, 6) == 1); + static const struct tui_form_action HEADS[2] = { + { "rubrik", -1, NULL }, + { "rubrik", -1, NULL }, + }; + CHECK(tui_form_act_first(0, HEADS, 2) == -1); + CHECK(tui_form_act_first(4, ACTS, 6) == 0); + + /* the ring runs fields -> actions -> back, skipping headings */ + CHECK(tui_form_act_step(0, nf, ACTS, 6, 1) == 1); + CHECK(tui_form_act_step(1, nf, ACTS, 6, 1) == 2); + CHECK(tui_form_act_step(2, nf, ACTS, 6, 1) == 4); /* skip focus 3 */ + CHECK(tui_form_act_step(4, nf, ACTS, 6, 1) == 5); + CHECK(tui_form_act_step(5, nf, ACTS, 6, 1) == 6); /* disabled, selectable */ + CHECK(tui_form_act_step(6, nf, ACTS, 6, 1) == 8); /* skip focus 7 */ + CHECK(tui_form_act_step(8, nf, ACTS, 6, 1) == 0); /* wrap */ + CHECK(tui_form_act_step(0, nf, ACTS, 6, -1) == 8); /* wrap back */ + CHECK(tui_form_act_step(8, nf, ACTS, 6, -1) == 6); + CHECK(tui_form_act_step(6, nf, ACTS, 6, -1) == 5); + CHECK(tui_form_act_step(5, nf, ACTS, 6, -1) == 4); + CHECK(tui_form_act_step(4, nf, ACTS, 6, -1) == 2); /* skip focus 3 */ + /* an out-of-range focus snaps back into the ring */ + CHECK(tui_form_act_step(99, nf, ACTS, 6, 1) == 1); + + /* keys: Tab/Shift-Tab/arrows walk the ring, Home/End the ends */ + CHECK(tui_form_act_key(0, nf, ACTS, 6, '\t') == 1); + CHECK(tui_form_act_key(0, nf, ACTS, 6, KEY_BTAB) == 8); + CHECK(tui_form_act_key(0, nf, ACTS, 6, KEY_UP) == 8); + CHECK(tui_form_act_key(8, nf, ACTS, 6, KEY_DOWN) == 0); + CHECK(tui_form_act_key(4, nf, ACTS, 6, KEY_HOME) == 0); + CHECK(tui_form_act_key(0, nf, ACTS, 6, KEY_END) == 8); + CHECK(tui_form_act_key(0, nf, ACTS, 6, KEY_NPAGE) == 4); + CHECK(tui_form_act_key(8, nf, ACTS, 6, KEY_PPAGE) == 4); + CHECK(tui_form_act_key(0, nf, ACTS, 6, KEY_F(5)) == TUI_FORM_REFRESH); + CHECK(tui_form_act_key(5, nf, ACTS, 6, TUI_KEY_CTRL_ENTER) == + TUI_FORM_SUBMIT); + CHECK(tui_form_act_key(5, nf, ACTS, 6, 27) == TUI_FORM_BACK); + CHECK(tui_form_act_key(5, nf, ACTS, 6, 'x') == TUI_NAV_NONE); + + /* labels: plain, disabled with reason, heading, fallback reason */ + char buf[128]; + tui_form_act_label(&ACTS[1], buf, sizeof buf); + CHECK(strcmp(buf, "Årsredovisning (K2)") == 0); + tui_form_act_label(&ACTS[3], buf, sizeof buf); + CHECK(strcmp(buf, "Bokför bokslut (året är stängt)") == 0); + tui_form_act_label(&ACTS[0], buf, sizeof buf); + CHECK(strcmp(buf, "Bokslut bokfört: nej") == 0); + const struct tui_form_action no_reason = { "Stängd", 0, NULL }; + tui_form_act_label(&no_reason, buf, sizeof buf); + CHECK(strcmp(buf, "Stängd (otillgänglig)") == 0); + tui_form_act_label(NULL, buf, sizeof buf); + CHECK(buf[0] == '\0'); + + /* without fields the ring cycles the selectable actions only */ + CHECK(tui_form_act_step(0, 0, ACTS, 6, 1) == 1); + CHECK(tui_form_act_step(5, 0, ACTS, 6, 1) == 1); /* wraps past the head */ + CHECK(tui_form_act_step(1, 0, ACTS, 6, -1) == 5); +} + static char rt_cells[8][3][64]; static void rt_cb(void *ud, int row, int col, char **buf, size_t *cap) @@ -534,6 +616,7 @@ int main(void) test_nav(); test_nav_sections(); test_form_nav(); + test_form_actions(); test_rowtable(); test_rt_fields(); test_rt_focus(); |
