2020-01-30 13:55:01 +00:00
|
|
|
#include "main.h"
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
|
2020-05-07 19:06:07 +00:00
|
|
|
TEST_CASE("Selectors") {
|
2020-06-16 03:58:43 +00:00
|
|
|
auto nc_ = testing_notcurses();
|
2020-05-17 11:57:21 +00:00
|
|
|
if(!nc_){
|
|
|
|
return;
|
|
|
|
}
|
2020-01-30 13:55:01 +00:00
|
|
|
struct ncplane* n_ = notcurses_stdplane(nc_);
|
|
|
|
REQUIRE(n_);
|
|
|
|
REQUIRE(0 == ncplane_cursor_move_yx(n_, 0, 0));
|
|
|
|
|
|
|
|
SUBCASE("EmptySelector") {
|
2020-05-09 00:58:00 +00:00
|
|
|
struct ncselector_options opts{};
|
2020-05-09 11:51:23 +00:00
|
|
|
struct ncselector* ncs = ncselector_create(n_, 0, 0, &opts);
|
2020-01-30 13:55:01 +00:00
|
|
|
REQUIRE(nullptr != ncs);
|
|
|
|
CHECK(0 == notcurses_render(nc_));
|
2020-01-30 21:40:02 +00:00
|
|
|
CHECK(nullptr == ncselector_selected(ncs));
|
2020-01-31 00:37:21 +00:00
|
|
|
struct ncplane* ncsp = ncselector_plane(ncs);
|
|
|
|
REQUIRE(nullptr != ncsp);
|
|
|
|
int dimy, dimx;
|
|
|
|
ncplane_dim_yx(ncsp, &dimy, &dimx);
|
|
|
|
CHECK(4 == dimy);
|
|
|
|
CHECK(5 == dimx);
|
2020-01-30 13:55:01 +00:00
|
|
|
ncselector_destroy(ncs, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("TitledSelector") {
|
2020-05-09 00:58:00 +00:00
|
|
|
struct ncselector_options opts{};
|
2020-01-30 13:55:01 +00:00
|
|
|
opts.title = strdup("hey hey whaddya say");
|
2020-05-09 11:51:23 +00:00
|
|
|
struct ncselector* ncs = ncselector_create(n_, 0, 0, &opts);
|
2020-01-30 13:55:01 +00:00
|
|
|
REQUIRE(nullptr != ncs);
|
|
|
|
CHECK(0 == notcurses_render(nc_));
|
2020-01-31 00:37:21 +00:00
|
|
|
struct ncplane* ncsp = ncselector_plane(ncs);
|
|
|
|
REQUIRE(nullptr != ncsp);
|
|
|
|
int dimy, dimx;
|
|
|
|
ncplane_dim_yx(ncsp, &dimy, &dimx);
|
|
|
|
CHECK(6 == dimy);
|
|
|
|
CHECK(strlen(opts.title) + 4 == dimx);
|
2020-01-30 13:55:01 +00:00
|
|
|
ncselector_destroy(ncs, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("SecondarySelector") {
|
2020-05-09 00:58:00 +00:00
|
|
|
struct ncselector_options opts{};
|
2020-01-30 13:55:01 +00:00
|
|
|
opts.secondary = strdup("this is not a title, but it's not *not* a title");
|
2020-05-09 11:51:23 +00:00
|
|
|
struct ncselector* ncs = ncselector_create(n_, 0, 0, &opts);
|
2020-01-30 13:55:01 +00:00
|
|
|
REQUIRE(nullptr != ncs);
|
|
|
|
CHECK(0 == notcurses_render(nc_));
|
2020-01-31 00:37:21 +00:00
|
|
|
struct ncplane* ncsp = ncselector_plane(ncs);
|
|
|
|
REQUIRE(nullptr != ncsp);
|
|
|
|
int dimy, dimx;
|
|
|
|
ncplane_dim_yx(ncsp, &dimy, &dimx);
|
|
|
|
CHECK(4 == dimy);
|
|
|
|
CHECK(strlen(opts.secondary) + 2 == dimx);
|
2020-01-30 13:55:01 +00:00
|
|
|
ncselector_destroy(ncs, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("FooterSelector") {
|
2020-05-09 00:58:00 +00:00
|
|
|
struct ncselector_options opts{};
|
2020-01-31 00:37:21 +00:00
|
|
|
opts.footer = strdup("i am a lone footer, little old footer");
|
2020-05-09 11:51:23 +00:00
|
|
|
struct ncselector* ncs = ncselector_create(n_, 0, 0, &opts);
|
2020-01-30 13:55:01 +00:00
|
|
|
REQUIRE(nullptr != ncs);
|
|
|
|
CHECK(0 == notcurses_render(nc_));
|
2020-01-31 00:37:21 +00:00
|
|
|
struct ncplane* ncsp = ncselector_plane(ncs);
|
|
|
|
REQUIRE(nullptr != ncsp);
|
|
|
|
int dimy, dimx;
|
|
|
|
ncplane_dim_yx(ncsp, &dimy, &dimx);
|
|
|
|
CHECK(4 == dimy);
|
|
|
|
CHECK(strlen(opts.footer) + 2 == dimx);
|
2020-01-30 13:55:01 +00:00
|
|
|
ncselector_destroy(ncs, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("PopulatedSelector") {
|
2020-05-09 00:58:00 +00:00
|
|
|
ncselector_item items[] = {
|
2020-02-01 00:51:14 +00:00
|
|
|
{ strdup("op1"), strdup("this is option 1"), 0, 0, },
|
|
|
|
{ strdup("2ndop"), strdup("this is option #2"), 0, 0, },
|
|
|
|
{ strdup("tres"), strdup("option the third"), 0, 0, },
|
2020-07-29 02:56:51 +00:00
|
|
|
{ NULL, NULL, 0, 0, },
|
2020-01-30 13:55:01 +00:00
|
|
|
};
|
2020-05-09 00:58:00 +00:00
|
|
|
struct ncselector_options opts{};
|
2020-01-30 13:55:01 +00:00
|
|
|
opts.items = items;
|
2020-05-09 11:51:23 +00:00
|
|
|
struct ncselector* ncs = ncselector_create(n_, 0, 0, &opts);
|
2020-01-30 13:55:01 +00:00
|
|
|
REQUIRE(nullptr != ncs);
|
|
|
|
CHECK(0 == notcurses_render(nc_));
|
2020-01-31 00:37:21 +00:00
|
|
|
struct ncplane* ncsp = ncselector_plane(ncs);
|
|
|
|
REQUIRE(nullptr != ncsp);
|
|
|
|
int dimy, dimx;
|
|
|
|
ncplane_dim_yx(ncsp, &dimy, &dimx);
|
|
|
|
CHECK(7 == dimy);
|
|
|
|
CHECK(15 < dimx);
|
2020-01-30 13:55:01 +00:00
|
|
|
ncselector_destroy(ncs, nullptr);
|
|
|
|
}
|
|
|
|
|
2020-01-30 21:40:02 +00:00
|
|
|
SUBCASE("EmptySelectorMovement") {
|
2020-05-09 00:58:00 +00:00
|
|
|
struct ncselector_options opts{};
|
2020-05-09 11:51:23 +00:00
|
|
|
struct ncselector* ncs = ncselector_create(n_, 0, 0, &opts);
|
2020-01-30 21:40:02 +00:00
|
|
|
REQUIRE(nullptr != ncs);
|
|
|
|
CHECK(0 == notcurses_render(nc_));
|
|
|
|
auto sel = ncselector_selected(ncs);
|
|
|
|
REQUIRE(nullptr == sel);
|
2020-02-12 14:11:28 +00:00
|
|
|
sel = ncselector_nextitem(ncs);
|
2020-01-30 21:40:02 +00:00
|
|
|
REQUIRE(nullptr == sel);
|
|
|
|
CHECK(0 == notcurses_render(nc_));
|
2020-02-12 14:11:28 +00:00
|
|
|
sel = ncselector_previtem(ncs);
|
2020-01-30 21:40:02 +00:00
|
|
|
REQUIRE(nullptr == sel);
|
|
|
|
CHECK(0 == notcurses_render(nc_));
|
|
|
|
ncselector_destroy(ncs, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("SelectorMovement") {
|
2020-05-09 00:58:00 +00:00
|
|
|
ncselector_item items[] = {
|
2020-02-01 00:51:14 +00:00
|
|
|
{ strdup("op1"), strdup("this is option 1"), 0, 0, },
|
|
|
|
{ strdup("2ndop"), strdup("this is option #2"), 0, 0, },
|
|
|
|
{ strdup("tres"), strdup("option the third"), 0, 0, },
|
2020-07-29 02:56:51 +00:00
|
|
|
{ NULL, NULL, 0, 0, },
|
2020-01-30 21:40:02 +00:00
|
|
|
};
|
2020-05-09 00:58:00 +00:00
|
|
|
struct ncselector_options opts{};
|
2020-01-30 21:40:02 +00:00
|
|
|
opts.items = items;
|
2020-05-09 11:51:23 +00:00
|
|
|
struct ncselector* ncs = ncselector_create(n_, 0, 0, &opts);
|
2020-01-30 21:40:02 +00:00
|
|
|
REQUIRE(nullptr != ncs);
|
|
|
|
auto sel = ncselector_selected(ncs);
|
|
|
|
REQUIRE(nullptr != sel);
|
|
|
|
CHECK(0 == strcmp(sel, items[0].option));
|
2020-01-31 00:37:21 +00:00
|
|
|
CHECK(0 == notcurses_render(nc_));
|
2020-02-12 14:11:28 +00:00
|
|
|
sel = ncselector_nextitem(ncs);
|
2020-01-30 21:40:02 +00:00
|
|
|
REQUIRE(nullptr != sel);
|
|
|
|
CHECK(0 == strcmp(sel, items[1].option));
|
2020-01-31 00:37:21 +00:00
|
|
|
CHECK(0 == notcurses_render(nc_));
|
2020-02-12 14:11:28 +00:00
|
|
|
sel = ncselector_previtem(ncs);
|
2020-01-30 21:40:02 +00:00
|
|
|
REQUIRE(nullptr != sel);
|
|
|
|
CHECK(0 == strcmp(sel, items[0].option));
|
2020-01-31 00:37:21 +00:00
|
|
|
CHECK(0 == notcurses_render(nc_));
|
2020-01-30 21:40:02 +00:00
|
|
|
// wrap around from the top to bottom...
|
2020-02-12 14:11:28 +00:00
|
|
|
sel = ncselector_previtem(ncs);
|
2020-01-30 21:40:02 +00:00
|
|
|
REQUIRE(nullptr != sel);
|
|
|
|
CHECK(0 == strcmp(sel, items[2].option));
|
2020-01-31 00:37:21 +00:00
|
|
|
CHECK(0 == notcurses_render(nc_));
|
2020-01-30 21:40:02 +00:00
|
|
|
// ...and back to the top
|
2020-02-12 14:11:28 +00:00
|
|
|
sel = ncselector_nextitem(ncs);
|
2020-01-30 21:40:02 +00:00
|
|
|
REQUIRE(nullptr != sel);
|
|
|
|
CHECK(0 == strcmp(sel, items[0].option));
|
2020-01-31 00:37:21 +00:00
|
|
|
CHECK(0 == notcurses_render(nc_));
|
|
|
|
ncselector_destroy(ncs, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provide three items, limited to 1 shown at a time
|
|
|
|
SUBCASE("ScrollingSelectorOne") {
|
2020-05-09 00:58:00 +00:00
|
|
|
ncselector_item items[] = {
|
2020-02-01 00:51:14 +00:00
|
|
|
{ strdup("op1"), strdup("this is option 1"), 0, 0, },
|
|
|
|
{ strdup("2ndop"), strdup("this is option #2"), 0, 0, },
|
|
|
|
{ strdup("tres"), strdup("option the third"), 0, 0, },
|
2020-07-29 02:56:51 +00:00
|
|
|
{ NULL, NULL, 0, 0, },
|
2020-01-31 00:37:21 +00:00
|
|
|
};
|
2020-05-09 00:58:00 +00:00
|
|
|
struct ncselector_options opts{};
|
2020-01-31 00:37:21 +00:00
|
|
|
opts.maxdisplay = 1;
|
|
|
|
opts.items = items;
|
2020-05-09 11:51:23 +00:00
|
|
|
struct ncselector* ncs = ncselector_create(n_, 0, 0, &opts);
|
2020-01-31 00:37:21 +00:00
|
|
|
REQUIRE(nullptr != ncs);
|
|
|
|
CHECK(0 == notcurses_render(nc_));
|
|
|
|
auto sel = ncselector_selected(ncs);
|
|
|
|
REQUIRE(nullptr != sel);
|
2020-01-31 00:57:39 +00:00
|
|
|
CHECK(0 == strcmp(sel, items[0].option));
|
|
|
|
CHECK(0 == notcurses_render(nc_));
|
2020-02-12 14:11:28 +00:00
|
|
|
sel = ncselector_nextitem(ncs);
|
2020-01-31 00:57:39 +00:00
|
|
|
REQUIRE(nullptr != sel);
|
|
|
|
CHECK(0 == strcmp(sel, items[1].option));
|
|
|
|
CHECK(0 == notcurses_render(nc_));
|
2020-02-12 14:11:28 +00:00
|
|
|
sel = ncselector_previtem(ncs);
|
2020-01-31 00:57:39 +00:00
|
|
|
REQUIRE(nullptr != sel);
|
|
|
|
CHECK(0 == strcmp(sel, items[0].option));
|
|
|
|
CHECK(0 == notcurses_render(nc_));
|
|
|
|
// wrap around from the top to bottom...
|
2020-02-12 14:11:28 +00:00
|
|
|
sel = ncselector_previtem(ncs);
|
2020-01-31 00:57:39 +00:00
|
|
|
REQUIRE(nullptr != sel);
|
|
|
|
CHECK(0 == strcmp(sel, items[2].option));
|
|
|
|
CHECK(0 == notcurses_render(nc_));
|
|
|
|
// ...and back to the top
|
2020-02-12 14:11:28 +00:00
|
|
|
sel = ncselector_nextitem(ncs);
|
2020-01-31 00:57:39 +00:00
|
|
|
REQUIRE(nullptr != sel);
|
|
|
|
CHECK(0 == strcmp(sel, items[0].option));
|
2020-01-31 00:37:21 +00:00
|
|
|
struct ncplane* ncsp = ncselector_plane(ncs);
|
|
|
|
REQUIRE(nullptr != ncsp);
|
|
|
|
int dimy, dimx;
|
|
|
|
ncplane_dim_yx(ncsp, &dimy, &dimx);
|
|
|
|
CHECK(5 == dimy);
|
|
|
|
ncselector_destroy(ncs, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provide three items, limited to 2 shown at a time
|
|
|
|
SUBCASE("ScrollingSelectorTwo") {
|
2020-05-09 00:58:00 +00:00
|
|
|
ncselector_item items[] = {
|
2020-02-01 00:51:14 +00:00
|
|
|
{ strdup("op1"), strdup("this is option 1"), 0, 0, },
|
|
|
|
{ strdup("2ndop"), strdup("this is option #2"), 0, 0, },
|
|
|
|
{ strdup("tres"), strdup("option the third"), 0, 0, },
|
2020-07-29 02:56:51 +00:00
|
|
|
{ NULL, NULL, 0, 0, },
|
2020-01-31 00:37:21 +00:00
|
|
|
};
|
2020-05-09 00:58:00 +00:00
|
|
|
struct ncselector_options opts{};
|
2020-01-31 00:37:21 +00:00
|
|
|
opts.maxdisplay = 2;
|
|
|
|
opts.items = items;
|
2020-05-09 11:51:23 +00:00
|
|
|
struct ncselector* ncs = ncselector_create(n_, 0, 0, &opts);
|
2020-01-31 00:37:21 +00:00
|
|
|
REQUIRE(nullptr != ncs);
|
|
|
|
CHECK(0 == notcurses_render(nc_));
|
2020-02-12 14:11:28 +00:00
|
|
|
const char* sel = ncselector_selected(ncs);
|
2020-01-31 00:57:39 +00:00
|
|
|
CHECK(0 == strcmp(sel, items[0].option));
|
|
|
|
CHECK(0 == notcurses_render(nc_));
|
2020-02-12 14:11:28 +00:00
|
|
|
sel = ncselector_nextitem(ncs);
|
2020-01-31 00:57:39 +00:00
|
|
|
REQUIRE(nullptr != sel);
|
|
|
|
CHECK(0 == strcmp(sel, items[1].option));
|
|
|
|
CHECK(0 == notcurses_render(nc_));
|
2020-02-12 14:11:28 +00:00
|
|
|
sel = ncselector_previtem(ncs);
|
2020-01-31 00:57:39 +00:00
|
|
|
REQUIRE(nullptr != sel);
|
|
|
|
CHECK(0 == strcmp(sel, items[0].option));
|
|
|
|
CHECK(0 == notcurses_render(nc_));
|
|
|
|
// wrap around from the top to bottom...
|
2020-02-12 14:11:28 +00:00
|
|
|
sel = ncselector_previtem(ncs);
|
2020-01-31 00:57:39 +00:00
|
|
|
REQUIRE(nullptr != sel);
|
|
|
|
CHECK(0 == strcmp(sel, items[2].option));
|
|
|
|
CHECK(0 == notcurses_render(nc_));
|
|
|
|
// ...and back to the top
|
2020-02-12 14:11:28 +00:00
|
|
|
sel = ncselector_nextitem(ncs);
|
2020-01-31 00:57:39 +00:00
|
|
|
REQUIRE(nullptr != sel);
|
|
|
|
CHECK(0 == strcmp(sel, items[0].option));
|
2020-01-31 00:37:21 +00:00
|
|
|
struct ncplane* ncsp = ncselector_plane(ncs);
|
|
|
|
REQUIRE(nullptr != ncsp);
|
|
|
|
int dimy, dimx;
|
|
|
|
ncplane_dim_yx(ncsp, &dimy, &dimx);
|
|
|
|
CHECK(6 == dimy);
|
2020-01-30 21:40:02 +00:00
|
|
|
ncselector_destroy(ncs, nullptr);
|
|
|
|
}
|
|
|
|
|
2020-01-30 13:55:01 +00:00
|
|
|
CHECK(0 == notcurses_stop(nc_));
|
|
|
|
}
|