mirror of
https://github.com/TextPlace/CoreTextPlace.git
synced 2025-05-01 05:01:11 +00:00
test: board
This commit is contained in:
parent
b2158a63c2
commit
302d6cef3e
3 changed files with 150 additions and 3 deletions
|
@ -8,9 +8,13 @@ import type {
|
|||
import { createSection } from "./section.ts";
|
||||
|
||||
export function createBoard(config: BoardConfig): BoardData {
|
||||
const sections: SectionData[][] = Array(config.ySections).map((_, sy) =>
|
||||
Array(config.xSections).map((_, sx) => createSection({ sx, sy }, config))
|
||||
);
|
||||
const sections: SectionData[][] = Array(config.ySections)
|
||||
.fill(0)
|
||||
.map((_, sy) =>
|
||||
Array(config.xSections)
|
||||
.fill(0)
|
||||
.map((_, sx) => createSection({ sx, sy }, config))
|
||||
);
|
||||
|
||||
return { config, sections };
|
||||
}
|
||||
|
|
45
tests/board.test.ts
Normal file
45
tests/board.test.ts
Normal file
|
@ -0,0 +1,45 @@
|
|||
import {
|
||||
assert,
|
||||
assertEquals,
|
||||
} from "https://deno.land/std@0.224.0/assert/mod.ts";
|
||||
|
||||
import { createBoard, renderFullBoard } from "../logic/board.ts";
|
||||
import { BoardData } from "../mod.ts";
|
||||
import { checkFullBoard } from "./checkFullBoard.ts";
|
||||
|
||||
Deno.test("board", async (t) => {
|
||||
let board: BoardData | undefined;
|
||||
|
||||
await t.step("createBoard", () => {
|
||||
board = createBoard({
|
||||
xSections: 2,
|
||||
ySections: 2,
|
||||
sectionWidth: 4,
|
||||
sectionHeight: 3,
|
||||
defaultCh: " ",
|
||||
defaultColor: "F",
|
||||
defaultBgColor: "0",
|
||||
defaultWidth: 1,
|
||||
});
|
||||
|
||||
assertEquals(board.sections.length, 2);
|
||||
assertEquals(board.sections[0].length, 2);
|
||||
assertEquals(board.sections[0][0].offsetX, 0);
|
||||
assertEquals(board.sections[0][0].offsetY, 0);
|
||||
assertEquals(board.sections[0][1].offsetX, 4);
|
||||
assertEquals(board.sections[0][1].offsetY, 0);
|
||||
assertEquals(board.sections[1].length, 2);
|
||||
assertEquals(board.sections[1][0].offsetX, 0);
|
||||
assertEquals(board.sections[1][0].offsetY, 3);
|
||||
assertEquals(board.sections[1][1].offsetX, 4);
|
||||
assertEquals(board.sections[1][1].offsetY, 3);
|
||||
});
|
||||
|
||||
await t.step("renderFullBoard", () => {
|
||||
assert(board);
|
||||
|
||||
const rendered = renderFullBoard(board);
|
||||
|
||||
checkFullBoard(rendered);
|
||||
});
|
||||
});
|
98
tests/checkFullBoard.ts
Normal file
98
tests/checkFullBoard.ts
Normal file
|
@ -0,0 +1,98 @@
|
|||
import { getCharacterWidth } from "../logic/character.ts";
|
||||
import { FullBoard } from "../types/board.ts";
|
||||
|
||||
function isCorrectWidth(cWdRaw: string, cCh: string): boolean {
|
||||
return getCharacterWidth(cCh).toString() === cWdRaw;
|
||||
}
|
||||
|
||||
function isValidColor(color: string): boolean {
|
||||
return /^[0-9A-F]$/.test(color);
|
||||
}
|
||||
|
||||
export function checkFullBoard(board: FullBoard) {
|
||||
let chLine = "";
|
||||
let colorLine = "";
|
||||
let bgColorLine = "";
|
||||
let widthLine = "";
|
||||
let lines = 0;
|
||||
const ch = [...board.ch];
|
||||
const chLength = ch.length;
|
||||
|
||||
let unsafeCurrentOffset = 0;
|
||||
|
||||
for (let i = 0; i < chLength; i++) {
|
||||
const cCh = ch[i];
|
||||
const cCo = board.color[i];
|
||||
const cBg = board.bg_color[i];
|
||||
const cWdRaw = board.width[i];
|
||||
const cWd = parseInt(cWdRaw);
|
||||
|
||||
const printSituation = () => {
|
||||
console.error(
|
||||
"offset:",
|
||||
i,
|
||||
"offset (unsafe):",
|
||||
unsafeCurrentOffset,
|
||||
"cCh:",
|
||||
JSON.stringify(cCh),
|
||||
"cCo:",
|
||||
JSON.stringify(cCo),
|
||||
"cBg:",
|
||||
JSON.stringify(cBg),
|
||||
"cWd:",
|
||||
JSON.stringify(cWdRaw),
|
||||
);
|
||||
console.error("ch: ", chLine);
|
||||
console.error("color: ", colorLine);
|
||||
console.error("bg_color:", bgColorLine);
|
||||
console.error("width: ", widthLine);
|
||||
};
|
||||
|
||||
if (cCh === "\n") {
|
||||
if (cCo !== "\n" || cBg !== "\n" || cWdRaw !== "\n") {
|
||||
printSituation();
|
||||
throw new Error("cCh is newline while at least one other field aren't");
|
||||
}
|
||||
|
||||
if (colorLine.length !== board.w) {
|
||||
printSituation();
|
||||
throw new Error("color line length error");
|
||||
}
|
||||
if (bgColorLine.length !== board.w) {
|
||||
printSituation();
|
||||
throw new Error("bg color line length error");
|
||||
}
|
||||
if (widthLine.length !== board.w) {
|
||||
printSituation();
|
||||
throw new Error("width line length error");
|
||||
}
|
||||
|
||||
chLine = "";
|
||||
colorLine = "";
|
||||
bgColorLine = "";
|
||||
widthLine = "";
|
||||
lines++;
|
||||
unsafeCurrentOffset += cCh.length;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!isValidColor(cCo) || !isValidColor(cBg)) {
|
||||
printSituation();
|
||||
throw new Error("cCo or cBg is not valid");
|
||||
}
|
||||
|
||||
if (!isCorrectWidth(cWdRaw, cCh)) {
|
||||
printSituation();
|
||||
throw new Error("cWd is wrong");
|
||||
}
|
||||
|
||||
chLine += cCh;
|
||||
colorLine += cCo.padEnd(cWd);
|
||||
bgColorLine += cBg.padEnd(cWd);
|
||||
widthLine += cWdRaw.padEnd(cWd);
|
||||
unsafeCurrentOffset += cCh.length;
|
||||
}
|
||||
|
||||
if (lines + 1 !== board.h) throw new Error("board height error");
|
||||
}
|
Loading…
Add table
Reference in a new issue