fix: apply change for 2-w at alternate position

This commit is contained in:
Shibo Lyu 2024-04-29 14:32:06 +08:00
parent 6dfe7129c4
commit 4999c5096a
3 changed files with 121 additions and 47 deletions

View file

@ -7,6 +7,11 @@ export function createSection(
{ sx, sy }: SectionPosition, { sx, sy }: SectionPosition,
boardConfig: BoardConfig boardConfig: BoardConfig
): SectionData { ): SectionData {
if (boardConfig.sectionWidth % 2 !== 0)
throw new Error(
"sectionWidth must be multiple of 2 (least common multiples of all character widths)"
);
const offsetX = sx * boardConfig.sectionWidth; const offsetX = sx * boardConfig.sectionWidth;
const offsetY = sy * boardConfig.sectionHeight; const offsetY = sy * boardConfig.sectionHeight;
@ -32,8 +37,10 @@ export function applyChange(change: BoardChange, section: SectionData) {
if (change.ch) { if (change.ch) {
const chWidth = getCharacterWidth(change.ch); const chWidth = getCharacterWidth(change.ch);
section.ch[yInSection][xInSection] = change.ch; const xCharacterOffset = xInSection % 2;
section.width[yInSection][xInSection] = chWidth; const offsetAdjustedXInSection = xInSection - xCharacterOffset;
section.ch[yInSection][offsetAdjustedXInSection] = change.ch;
section.width[yInSection][offsetAdjustedXInSection] = chWidth;
} }
if (change.color) { if (change.color) {
section.color[yInSection][xInSection] = change.color; section.color[yInSection][xInSection] = change.color;

View file

@ -1,9 +1,18 @@
import { assertEquals } from "https://deno.land/std@0.224.0/assert/mod.ts"; import {
assert,
assertEquals,
assertThrows,
} from "https://deno.land/std@0.224.0/assert/mod.ts";
import { createSection } from "../logic/section.ts"; import { applyChange, createSection } from "../logic/section.ts";
import type { SectionData } from "../types/section.ts";
Deno.test("createSection", () => { Deno.test("section", async (t) => {
const section = createSection( let section: SectionData | undefined;
await t.step("createSection non-lcm", () => {
assertThrows(() => {
createSection(
{ sx: 0, sy: 0 }, { sx: 0, sy: 0 },
{ {
xSections: 2, xSections: 2,
@ -16,38 +25,91 @@ Deno.test("createSection", () => {
defaultWidth: 1, defaultWidth: 1,
} }
); );
});
});
await t.step("createSection non-origin section", () => {
section = createSection(
{ sx: 1, sy: 1 },
{
xSections: 2,
ySections: 2,
sectionWidth: 4,
sectionHeight: 3,
defaultCh: " ",
defaultColor: "F",
defaultBgColor: "0",
defaultWidth: 1,
}
);
assertEquals(section.offsetX, 4);
assertEquals(section.offsetY, 3);
});
await t.step("createSection", () => {
section = createSection(
{ sx: 0, sy: 0 },
{
xSections: 2,
ySections: 2,
sectionWidth: 4,
sectionHeight: 3,
defaultCh: " ",
defaultColor: "F",
defaultBgColor: "0",
defaultWidth: 1,
}
);
assertEquals(section.offsetX, 0); assertEquals(section.offsetX, 0);
assertEquals(section.offsetY, 0); assertEquals(section.offsetY, 0);
assertEquals(section.ch.length, 3);
for (const row of section.ch) { function assertSectionContent<T>(
assertEquals(row.length, 3); content: T[][],
for (const ch of row) { rowCount: number,
assertEquals(ch, " "); columnCount: number,
value: T
) {
assertEquals(content.length, rowCount);
for (const row of content) {
assertEquals(row.length, columnCount);
for (const item of row) {
assertEquals(item, value);
}
} }
} }
assertEquals(section.color.length, 3); assertSectionContent(section.ch, 3, 4, " ");
for (const row of section.color) { assertSectionContent(section.color, 3, 4, "F");
assertEquals(row.length, 3); assertSectionContent(section.bgColor, 3, 4, "0");
for (const color of row) { assertSectionContent(section.width, 3, 4, 1);
assertEquals(color, "F"); });
}
}
assertEquals(section.bgColor.length, 3); await t.step("applyChange 1-width", () => {
for (const row of section.bgColor) { assert(section);
assertEquals(row.length, 3);
for (const bgColor of row) {
assertEquals(bgColor, "0");
}
}
assertEquals(section.width.length, 3); applyChange({ x: 0, y: 0, ch: "t" }, section);
for (const row of section.width) { assertEquals(section.ch[0][0], "t");
assertEquals(row.length, 3); assertEquals(section.ch[0][1], " ");
for (const width of row) { assertEquals(section.width[0][0], 1);
assertEquals(width, 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);
});
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);
});
}); });

View file

@ -36,6 +36,11 @@ export interface CharacterPosition {
export interface BoardConfig { export interface BoardConfig {
xSections: number; xSections: number;
ySections: number; ySections: number;
/**
* Width of each section, in display characters.
*
* Note that this must be a multiple of the least common multiple of all possible character display width (returned by `getCharacterWidth`).
*/
sectionWidth: number; sectionWidth: number;
sectionHeight: number; sectionHeight: number;