From ed630502fa43ece1074296a705253d292c33bf2d Mon Sep 17 00:00:00 2001 From: Viktor Podzigun Date: Mon, 25 Mar 2024 10:23:41 +0100 Subject: [PATCH] Improved test coverage --- src/TextInput.mjs | 36 ++++++++++++++++++++++++++---------- test/TextInput.test.mjs | 7 +++++++ 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/TextInput.mjs b/src/TextInput.mjs index ed36de5..499fccb 100644 --- a/src/TextInput.mjs +++ b/src/TextInput.mjs @@ -60,23 +60,38 @@ const TextInput = (props) => { */ function move(el, value, cm, ts) { const charStart = value.charStartPos(offset + cursorX); + + /** + * @param {number} [dx] + * @returns {number[]} + */ + function moveLeft(dx) { + const ldx = dx ?? Math.max(charStart.lcw, 1); + return [cursorX - ldx, cursorX === 0 ? offset - ldx : offset]; + } + + /** + * @param {number} [dx] + * @returns {number[]} + */ + //prettier-ignore + function moveRight(dx) { + const rdx = dx ?? Math.max(charStart.rcw, 1); + return [cursorX + rdx, cursorX === /** @type {number} */ (el.width) - 1 ? offset + rdx : offset]; + } + const [posX, idx] = (() => { //prettier-ignore switch (cm.move) { - case "At": return [cm.pos, offset]; - case "Home": return [0, 0]; - case "End": return [value.strWidth(), value.strWidth() - /** @type {number} */ (el.width) + 1]; - case "Left": - const ldx = cm.dx ?? Math.max(charStart.lcw, 1); - return [cursorX - ldx, cursorX === 0 ? offset - ldx : offset]; - case "Right": - const rdx = cm.dx ?? Math.max(charStart.rcw, 1); - return [cursorX + rdx, cursorX === /** @type {number} */ (el.width) - 1 ? offset + rdx : offset]; + case "At": return [cm.pos, offset]; + case "Home": return [0, 0]; + case "End": return [value.strWidth(), value.strWidth() - /** @type {number} */ (el.width) + 1]; + case "Left": return moveLeft(cm.dx); + case "Right": return moveRight(cm.dx); } })(); const newOffset = Math.min(Math.max(idx, 0), value.strWidth()); - const newPos = Math.min( Math.max(posX, 0), Math.min( @@ -84,6 +99,7 @@ const TextInput = (props) => { Math.max(value.strWidth() - newOffset, 0) ) ); + if (newPos !== cursorX) { el.screen.program.omove( /** @type {number} */ (el.aleft) + newPos, diff --git a/test/TextInput.test.mjs b/test/TextInput.test.mjs index c2c7fdd..245b001 100644 --- a/test/TextInput.test.mjs +++ b/test/TextInput.test.mjs @@ -550,6 +550,13 @@ describe("TextInput.test.mjs", () => { //when & then check(true, "right", offset + 1, cursorX - 1, value); + check(true, "right", offset, cursorX, value); + for (let i = 0; i < 8; i++) { + check(true, "left", offset, cursorX - 1, value); + } + check(true, "left", offset - 1, cursorX, value); + check(true, "left", offset - 1, cursorX, value); + check(true, "right", offset, cursorX + 1, value); check(true, "S-home", 0, 0, value, 0, currIdx()); check(true, "right", offset, cursorX + 1, value); check(true, "S-right", offset, cursorX + 1, value, currIdx(), currIdx() + 1);