From 91db478fe9d8a015359827f4d089b28ac9ceccfc Mon Sep 17 00:00:00 2001 From: Shibo Lyu Date: Sun, 29 Dec 2024 22:32:59 +0800 Subject: [PATCH] fix: applying 1-width at odd location --- logic/section.ts | 2 +- tests/board.test.ts | 9 +++++++-- tests/section.test.ts | 23 ++++++++++++++--------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/logic/section.ts b/logic/section.ts index d935dc3..f2463ff 100644 --- a/logic/section.ts +++ b/logic/section.ts @@ -44,7 +44,7 @@ export function applyChange(change: BoardChange, section: SectionData) { if (change.ch) { const chWidth = getCharacterWidth(change.ch); - const xCharacterOffset = xInSection % 2; + const xCharacterOffset = xInSection % chWidth; const offsetAdjustedXInSection = xInSection - xCharacterOffset; section.ch[yInSection][offsetAdjustedXInSection] = change.ch; section.width[yInSection][offsetAdjustedXInSection] = chWidth; diff --git a/tests/board.test.ts b/tests/board.test.ts index ecedc47..7e15725 100644 --- a/tests/board.test.ts +++ b/tests/board.test.ts @@ -51,11 +51,13 @@ Deno.test("board", async (t) => { applyChangeOnBoard({ x: 4, y: 0, ch: "B" }, board); applyChangeOnBoard({ x: 0, y: 3, ch: "C" }, board); applyChangeOnBoard({ x: 4, y: 3, ch: "D" }, board); + applyChangeOnBoard({ x: 5, y: 3, ch: "E" }, board); assertEquals(board.sections[0][0].ch[0][0], "A"); assertEquals(board.sections[0][1].ch[0][0], "B"); assertEquals(board.sections[1][0].ch[0][0], "C"); assertEquals(board.sections[1][1].ch[0][0], "D"); + assertEquals(board.sections[1][1].ch[0][1], "E"); applyChangeOnBoard({ x: 0, y: 1, ch: "你" }, board); applyChangeOnBoard({ x: 4, y: 2, ch: "好" }, board); @@ -65,7 +67,10 @@ Deno.test("board", async (t) => { assertEquals(board.sections[0][0].ch[1][0], "你"); assertEquals(board.sections[0][1].ch[2][0], "好"); assertEquals(board.sections[1][0].ch[1][0], "嗎"); - assertEquals(board.sections[1][1].ch[2][0], "嘛"); + assertEquals(board.sections[1][1].ch[2], ["嘛", "E", " ", " "]); + + applyChangeOnBoard({ x: 5, y: 4, ch: "啊" }, board); + assertEquals(board.sections[1][1].ch[2], ["啊", "E", " ", " "]); }); await t.step("getSectionOnBoard: existing section", () => { @@ -74,7 +79,7 @@ Deno.test("board", async (t) => { const section = getSectionOnBoard({ sx: 1, sy: 1 }, board, { readOnly: true, }); - assertEquals(section.ch[0][0], "嘛"); + assertEquals(section.ch[0][0], "啊"); assertEquals(section.color[0][0], "F"); assertEquals(section.bgColor[0][0], "0"); assertEquals(section.width[0][0], 2); diff --git a/tests/section.test.ts b/tests/section.test.ts index 8a5a139..07999f7 100644 --- a/tests/section.test.ts +++ b/tests/section.test.ts @@ -90,27 +90,32 @@ Deno.test("section", async (t) => { assert(section); applyChange({ x: 0, y: 0, ch: "t" }, section); - assertEquals(section.ch[0][0], "t"); - assertEquals(section.ch[0][1], " "); - assertEquals(section.width[0][0], 1); + assertEquals(section.ch[0], ["t", " ", " ", " "]); + assertEquals(section.width[0], [1, 1, 1, 1]); + }); + + await t.step("applyChange 1-width at odd position", () => { + assert(section); + + applyChange({ x: 1, y: 0, ch: "t" }, section); + assertEquals(section.ch[0], ["t", "t", " ", " "]); + assertEquals(section.width[0], [1, 1, 1, 1]); }); await t.step("applyChange 2-width at a correct position", () => { assert(section); applyChange({ x: 0, y: 0, ch: "あ" }, section); - assertEquals(section.ch[0][0], "あ"); - assertEquals(section.ch[0][1], " "); - assertEquals(section.width[0][0], 2); + assertEquals(section.ch[0], ["あ", "t", " ", " "]); + assertEquals(section.width[0], [2, 1, 1, 1]); }); await t.step("applyChange 2-width at an alternate position", () => { assert(section); applyChange({ x: 1, y: 0, ch: "あ" }, section); - assertEquals(section.ch[0][0], "あ"); - assertEquals(section.ch[0][1], " "); - assertEquals(section.width[0][0], 2); + assertEquals(section.ch[0], ["あ", "t", " ", " "]); + assertEquals(section.width[0], [2, 1, 1, 1]); }); await t.step("applyChange incorrect section", () => {