test: more tests

This commit is contained in:
Shibo Lyu 2024-05-04 22:17:19 +08:00
parent 302d6cef3e
commit 9a4927f9b2
6 changed files with 86 additions and 6 deletions

View file

@ -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"
}

9
.zed/tasks.json Normal file
View file

@ -0,0 +1,9 @@
// Static tasks configuration.
//
// Example:
[
{
"label": "Test",
"command": "deno test"
}
]

View file

@ -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;

View file

@ -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;

View file

@ -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);
});
});

View file

@ -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);
});
});
});