From 9a4927f9b2a93d87b1c33f40c0f630ad3ecb3f7d Mon Sep 17 00:00:00 2001 From: Shibo Lyu Date: Sat, 4 May 2024 22:17:19 +0800 Subject: [PATCH] test: more tests --- .zed/settings.json | 19 ++++++++++++++++--- .zed/tasks.json | 9 +++++++++ logic/board.ts | 9 ++++++++- logic/section.ts | 6 ++++++ tests/board.test.ts | 41 +++++++++++++++++++++++++++++++++++++++-- tests/section.test.ts | 8 ++++++++ 6 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 .zed/tasks.json diff --git a/.zed/settings.json b/.zed/settings.json index 0ac1e6c..14b3761 100644 --- a/.zed/settings.json +++ b/.zed/settings.json @@ -5,10 +5,23 @@ { "languages": { "TypeScript": { - "language_servers": ["deno", "!typescript-language-server", "!eslint"] + "language_servers": ["deno", "!typescript-language-server", "!eslint"], + "formatter": { + "external": { + "command": "deno", + "arguments": ["fmt", "-"] + } + } }, "TSX": { - "language_servers": ["deno", "!typescript-language-server", "!eslint"] + "language_servers": ["deno", "!typescript-language-server", "!eslint"], + "formatter": { + "external": { + "command": "deno", + "arguments": ["fmt", "-"] + } + } } - } + }, + "format_on_save": "on" } diff --git a/.zed/tasks.json b/.zed/tasks.json new file mode 100644 index 0000000..4868825 --- /dev/null +++ b/.zed/tasks.json @@ -0,0 +1,9 @@ +// Static tasks configuration. +// +// Example: +[ + { + "label": "Test", + "command": "deno test" + } +] diff --git a/logic/board.ts b/logic/board.ts index 0d4c225..01da3dc 100644 --- a/logic/board.ts +++ b/logic/board.ts @@ -5,7 +5,8 @@ import type { CharacterPosition, FullBoard, } from "../types/board.ts"; -import { createSection } from "./section.ts"; +import { applyChange, createSection } from "./section.ts"; +import { BoardChange } from "../types/change.ts"; export function createBoard(config: BoardConfig): BoardData { const sections: SectionData[][] = Array(config.ySections) @@ -29,6 +30,12 @@ export function locateSection( }; } +export function applyChangeOnBoard(change: BoardChange, board: BoardData) { + const { sx, sy } = locateSection(change, board.config); + const section = board.sections[sy][sx]; + applyChange(change, section); +} + export function renderFullBoard(data: BoardData): FullBoard { const totalLineCount = data.config.sectionHeight * data.config.ySections; const lineLength = data.config.sectionWidth * data.config.xSections; diff --git a/logic/section.ts b/logic/section.ts index e2d99f0..b9b37bb 100644 --- a/logic/section.ts +++ b/logic/section.ts @@ -36,6 +36,12 @@ export function applyChange(change: BoardChange, section: SectionData) { const xInSection = change.x - section.offsetX; const yInSection = change.y - section.offsetY; + const validX = xInSection >= 0 && xInSection < section.ch[0].length; + const validY = yInSection >= 0 && yInSection < section.ch.length; + if (!validX || !validY) { + throw new Error("Change does not belong to this section"); + } + if (change.ch) { const chWidth = getCharacterWidth(change.ch); const xCharacterOffset = xInSection % 2; diff --git a/tests/board.test.ts b/tests/board.test.ts index dc85e66..c3252a0 100644 --- a/tests/board.test.ts +++ b/tests/board.test.ts @@ -4,8 +4,10 @@ import { } from "https://deno.land/std@0.224.0/assert/mod.ts"; import { createBoard, renderFullBoard } from "../logic/board.ts"; -import { BoardData } from "../mod.ts"; +import { BoardData } from "../types/board.ts"; import { checkFullBoard } from "./checkFullBoard.ts"; +import { locateSection } from "../logic/board.ts"; +import { applyChangeOnBoard } from "../logic/board.ts"; Deno.test("board", async (t) => { let board: BoardData | undefined; @@ -35,11 +37,46 @@ Deno.test("board", async (t) => { assertEquals(board.sections[1][1].offsetY, 3); }); + await t.step("locateSection", () => { + assert(board); + + const { sx, sy } = locateSection({ x: 0, y: 0 }, board.config); + assertEquals(sx, 0); + assertEquals(sy, 0); + + const { sx: sx2, sy: sy2 } = locateSection({ x: 4, y: 0 }, board.config); + assertEquals(sx2, 1); + assertEquals(sy2, 0); + }); + + await t.step("applyChangeOnBoard", () => { + assert(board); + + applyChangeOnBoard({ x: 0, y: 0, ch: "A" }, board); + applyChangeOnBoard({ x: 4, y: 0, ch: "B" }, board); + applyChangeOnBoard({ x: 0, y: 3, ch: "C" }, board); + applyChangeOnBoard({ x: 4, y: 3, ch: "D" }, 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"); + + applyChangeOnBoard({ x: 0, y: 1, ch: "你" }, board); + applyChangeOnBoard({ x: 4, y: 2, ch: "好" }, board); + applyChangeOnBoard({ x: 0, y: 4, ch: "嗎" }, board); + applyChangeOnBoard({ x: 4, y: 4, ch: "嘛" }, board); + + 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], "嘛"); + }); + await t.step("renderFullBoard", () => { assert(board); const rendered = renderFullBoard(board); - checkFullBoard(rendered); }); }); diff --git a/tests/section.test.ts b/tests/section.test.ts index 0984ad2..8a5a139 100644 --- a/tests/section.test.ts +++ b/tests/section.test.ts @@ -112,4 +112,12 @@ Deno.test("section", async (t) => { assertEquals(section.ch[0][1], " "); assertEquals(section.width[0][0], 2); }); + + await t.step("applyChange incorrect section", () => { + assertThrows(() => { + assert(section); + + applyChange({ x: 6, y: 3, ch: "あ" }, section); + }); + }); });