aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2021-07-18 13:34:19 +0000
committerDaniel Eklöf <daniel@ekloef.se>2021-07-18 14:14:25 +0000
commitd0a7f999c662759d1704d97d5217736e84f508c1 (patch)
tree3eea88c8a061ec22816d1183bc8253461bc4b832
parentcsi: erase scrollback: cancel selection if it touches the scrollback (diff)
term: add unit test for term_erase_scrollback()
-rw-r--r--terminal.c97
1 files changed, 97 insertions, 0 deletions
diff --git a/terminal.c b/terminal.c
index df6ddbed..b16cf19a 100644
--- a/terminal.c
+++ b/terminal.c
@@ -2074,6 +2074,103 @@ term_erase_scrollback(struct terminal *term)
term_damage_view(term);
}
+UNITTEST
+{
+ const int scrollback_rows = 16;
+ const int term_rows = 5;
+ const int cols = 5;
+
+ struct fdm *fdm = fdm_init();
+ xassert(fdm != NULL);
+
+ struct terminal term = {
+ .fdm = fdm,
+ .rows = term_rows,
+ .cols = cols,
+ .normal = {
+ .rows = xcalloc(scrollback_rows, sizeof(term.normal.rows[0])),
+ .num_rows = scrollback_rows,
+ .num_cols = cols,
+ },
+ .grid = &term.normal,
+ .selection = {
+ .start = {-1, -1},
+ .end = {-1, -1},
+ .kind = SELECTION_NONE,
+ .auto_scroll = {
+ .fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK),
+ },
+ },
+ };
+
+ xassert(term.selection.auto_scroll.fd >= 0);
+
+#define populate_scrollback() do { \
+ for (int i = 0; i < scrollback_rows; i++) { \
+ if (term.normal.rows[i] == NULL) { \
+ struct row *r = xcalloc(1, sizeof(*term.normal.rows[i])); \
+ r->cells = xcalloc(cols, sizeof(r->cells[0])); \
+ term.normal.rows[i] = r; \
+ } \
+ } \
+ } while (0)
+
+ /*
+ * Test case 1 - no selection, just verify all rows except those
+ * on screen have been deleted.
+ */
+
+ populate_scrollback();
+ term.normal.offset = 11;
+ term_erase_scrollback(&term);
+ for (int i = 0; i < scrollback_rows; i++) {
+ if (i >= term.normal.offset && i < term.normal.offset + term_rows)
+ xassert(term.normal.rows[i] != NULL);
+ else
+ xassert(term.normal.rows[i] == NULL);
+ }
+
+ /*
+ * Test case 2 - selection that touches the scrollback. Verify the
+ * selection is cancelled.
+ */
+
+ term.normal.offset = 14; /* Screen covers rows 14,15,0,1,2 */
+
+ /* Selection covers rows 15,0,1,2,3 */
+ term.selection.start = (struct coord){.row = 15};
+ term.selection.end = (struct coord){.row = 19};
+ term.selection.kind = SELECTION_CHAR_WISE;
+
+ populate_scrollback();
+ term_erase_scrollback(&term);
+ xassert(term.selection.start.row < 0);
+ xassert(term.selection.end.row < 0);
+ xassert(term.selection.kind == SELECTION_NONE);
+
+ /*
+ * Test case 3 - selection that does *not* touch the
+ * scrollback. Verify the selection is *not* cancelled.
+ */
+
+ /* Selection covers rows 15,0 */
+ term.selection.start = (struct coord){.row = 15};
+ term.selection.end = (struct coord){.row = 16};
+ term.selection.kind = SELECTION_CHAR_WISE;
+
+ populate_scrollback();
+ term_erase_scrollback(&term);
+ xassert(term.selection.start.row == 15);
+ xassert(term.selection.end.row == 16);
+ xassert(term.selection.kind == SELECTION_CHAR_WISE);
+
+ close(term.selection.auto_scroll.fd);
+ for (int i = 0; i < scrollback_rows; i++)
+ grid_row_free(term.normal.rows[i]);
+ free(term.normal.rows);
+ fdm_destroy(fdm);
+}
+
int
term_row_rel_to_abs(const struct terminal *term, int row)
{