Skip to content

Commit

Permalink
Improved test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-podzigun committed Mar 25, 2024
1 parent 153c08d commit c72ccdc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/TextInput.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,50 @@ const TextInput = (props) => {
*/
function move(el, value, cm, ts) {
const charStart = value.charStartPos(offset + cursorX);
const [posX, idx] = (() => {
//prettier-ignore

/**
* @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];
}

/**
* @returns {number[]}
*/
//prettier-ignore
function doMove() {
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 "Left": return moveLeft(cm.dx);
case "Right":return moveRight(cm.dx);
}
})();
}

const [posX, idx] = doMove();
const newOffset = Math.min(Math.max(idx, 0), value.strWidth());

const newPos = Math.min(
Math.max(posX, 0),
Math.min(
Math.max(/** @type {number} */ (el.width) - 1, 0),
Math.max(value.strWidth() - newOffset, 0)
)
);

if (newPos !== cursorX) {
el.screen.program.omove(
/** @type {number} */ (el.aleft) + newPos,
Expand Down
7 changes: 7 additions & 0 deletions test/TextInput.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c72ccdc

Please sign in to comment.