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