textui/base.test.ts

46 lines
1.7 KiB
TypeScript

import { BufferDrawing, BufferLocation, CharBuffer } from "./base.ts";
import { describe, expect, it } from "./testing.ts";
describe(CharBuffer, () => {
it("initializes empty, sets and gets characters", () => {
const buffer = new CharBuffer({ w: 3, h: 2 });
expect(buffer.toString()).toEqual(" ");
buffer.set({ x: 2, y: 0 }, { ch: "x", fg: 1, bg: 4 });
buffer.set({ x: 1, y: 1 }, { ch: "y", fg: 2, bg: 5 });
expect(buffer.toString()).toEqual(" x y ");
expect(buffer.get({ x: 0, y: 0 })).toEqual({ ch: " ", fg: 0, bg: 0 });
expect(buffer.get({ x: 1, y: 1 })).toEqual({ ch: "y", fg: 2, bg: 5 });
});
it("keeps track of dirty lines", async () => {
const buffer = new CharBuffer({ w: 16, h: 3 });
expect(buffer.isDirty({ x: 0, y: 0 })).toBe(true);
buffer.setDirty(false);
expect(buffer.isDirty({ x: 0, y: 0 })).toBe(false);
buffer.set({ x: 0, y: 0 }, { ch: "x", bg: 0, fg: 0 });
expect(buffer.isDirty({ x: 0, y: 0 })).toBe(true);
expect(buffer.isDirty({ x: 1, y: 0 })).toBe(true);
expect(buffer.isDirty({ x: 2, y: 0 })).toBe(false);
expect(buffer.isDirty({ x: 0, y: 1 })).toBe(false);
let calls: BufferLocation[] = [];
await buffer.forEachDirty(async (at) => {
calls.push(at);
});
expect(calls).toEqual([{ x: 0, y: 0 }, { x: 1, y: 0 }]);
calls = [];
await buffer.forEachDirty(async (at) => {
calls.push(at);
});
expect(calls).toEqual([]);
});
});
describe(BufferDrawing, () => {
it("draws text", () => {
const buffer = new CharBuffer({ w: 4, h: 2 });
const drawing = new BufferDrawing(buffer, []);
drawing.text("testing", { x: -1, y: 0 });
drawing.text("so", { x: 1, y: 1 });
expect(buffer.toString()).toEqual("esti so ");
});
});