textui/base.test.ts

47 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-06-28 20:42:42 +00:00
import { BufferDrawing, BufferLocation, CharBuffer } from "./base.ts";
2021-08-26 18:12:36 +00:00
import { describe, expect, it } from "./deps.testing.ts";
2021-05-13 22:04:47 +00:00
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 });
});
2021-06-28 20:42:42 +00:00
it("keeps track of dirty lines", async () => {
const buffer = new CharBuffer({ w: 16, h: 3 });
2021-07-19 22:48:00 +00:00
expect(buffer.isDirty({ x: 0, y: 0 })).toBe(true);
buffer.setDirty(false);
2021-06-28 20:42:42 +00:00
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([]);
});
2021-05-13 22:04:47 +00:00
});
describe(BufferDrawing, () => {
it("draws text", () => {
const buffer = new CharBuffer({ w: 4, h: 2 });
2021-06-24 22:41:34 +00:00
const drawing = new BufferDrawing(buffer, []);
2021-05-13 22:04:47 +00:00
drawing.text("testing", { x: -1, y: 0 });
drawing.text("so", { x: 1, y: 1 });
expect(buffer.toString()).toEqual("esti so ");
});
});