textui/ui.test.ts

58 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-07-19 22:48:00 +00:00
import { BufferLocation, Char, Color, SPACE } from "./base.ts";
2021-08-26 18:12:36 +00:00
import { describe, expect, it } from "./deps.testing.ts";
2021-06-24 22:41:34 +00:00
import { Display } from "./display.ts";
import { TextUI } from "./ui.ts";
describe(TextUI, () => {
it("maps a full RGB color palette directly", async () => {
const palette = [
{ r: 0.0, g: 0.0, b: 0.0 },
{ r: 1.0, g: 0.0, b: 0.0 },
];
const display = new PaletteTestDisplay(palette);
2021-06-28 18:21:32 +00:00
const ui = new TextUI(display, { palette });
await ui.init();
2021-06-24 22:41:34 +00:00
ui.drawing.color(1, 0).text("x", { x: 0, y: 0 });
await ui.flush();
expect(display.last).toEqual({ ch: "x", bg: 0, fg: 1 });
});
2021-06-27 21:11:49 +00:00
it("finds the best match for a color in a limited palette", async () => {
const app_palette = [
{ r: 0.0, g: 0.0, b: 1.0 },
{ r: 0.0, g: 0.0, b: 0.0 },
];
const display_palette = [
{ r: 1.0, g: 1.0, b: 1.0 },
{ r: 0.1, g: 0.2, b: 0.8 },
{ r: 0.1, g: 0.2, b: 0.1 },
];
const display = new PaletteTestDisplay(display_palette);
2021-06-28 18:21:32 +00:00
const ui = new TextUI(display, { palette: app_palette });
await ui.init();
2021-06-27 21:11:49 +00:00
ui.drawing.color(0, 1).text("x", { x: 0, y: 0 });
await ui.flush();
expect(display.last).toEqual({ ch: "x", bg: 2, fg: 1 });
});
2021-06-24 22:41:34 +00:00
});
2021-06-28 18:21:32 +00:00
class PaletteTestDisplay extends Display {
2021-06-24 22:41:34 +00:00
last = SPACE;
constructor(public palette: Color[]) {
2021-06-28 18:21:32 +00:00
super();
2021-06-24 22:41:34 +00:00
}
2021-07-19 22:48:00 +00:00
async getSize() {
return { w: 1, h: 1 };
}
2021-06-24 22:41:34 +00:00
async setupPalette(colors: readonly Color[]): Promise<readonly Color[]> {
return this.palette;
}
async setChar(at: BufferLocation, char: Char): Promise<void> {
this.last = char;
}
}