|
1 | 1 | import { beforeEach, describe, expect, it } from "./suite.ts"; |
2 | 2 | import { createTerm, type Term } from "../term.ts"; |
3 | | -import { close, fixed, grow, open, rgba, text } from "../ops.ts"; |
| 3 | +import { |
| 4 | + close, |
| 5 | + fixed, |
| 6 | + grow, |
| 7 | + type Op, |
| 8 | + open, |
| 9 | + rgba, |
| 10 | + snapshot, |
| 11 | + text, |
| 12 | +} from "../ops.ts"; |
4 | 13 | import { print } from "./print.ts"; |
5 | 14 |
|
6 | 15 | const decode = (bytes: Uint8Array) => new TextDecoder().decode(bytes); |
@@ -191,6 +200,99 @@ describe("term", () => { |
191 | 200 | }); |
192 | 201 | }); |
193 | 202 |
|
| 203 | + describe("snapshot", () => { |
| 204 | + it("produces identical output to direct ops", async () => { |
| 205 | + let ops = [ |
| 206 | + open("root", { |
| 207 | + layout: { width: grow(), height: grow(), direction: "ttb" }, |
| 208 | + bg: rgba(0, 0, 128), |
| 209 | + }), |
| 210 | + open("child", { |
| 211 | + layout: { |
| 212 | + width: grow(), |
| 213 | + padding: { left: 1 }, |
| 214 | + direction: "ttb", |
| 215 | + }, |
| 216 | + border: { |
| 217 | + color: rgba(255, 255, 255), |
| 218 | + left: 1, |
| 219 | + right: 1, |
| 220 | + top: 1, |
| 221 | + bottom: 1, |
| 222 | + }, |
| 223 | + }), |
| 224 | + text("snapshot test"), |
| 225 | + close(), |
| 226 | + close(), |
| 227 | + ]; |
| 228 | + |
| 229 | + let direct = await createTerm({ width: 40, height: 10 }); |
| 230 | + let snapped = await createTerm({ width: 40, height: 10 }); |
| 231 | + |
| 232 | + let expected = direct.render(ops, { mode: "line" }).output; |
| 233 | + let actual = snapped.render([snapshot(ops)], { mode: "line" }).output; |
| 234 | + |
| 235 | + expect(decode(actual)).toEqual(decode(expected)); |
| 236 | + }); |
| 237 | + |
| 238 | + it("renders inside another element", async () => { |
| 239 | + let child = snapshot([ |
| 240 | + open("child", { |
| 241 | + layout: { width: grow(), direction: "ttb" }, |
| 242 | + }), |
| 243 | + text("inner"), |
| 244 | + close(), |
| 245 | + ]); |
| 246 | + |
| 247 | + let direct = await createTerm({ width: 20, height: 5 }); |
| 248 | + let snapped = await createTerm({ width: 20, height: 5 }); |
| 249 | + |
| 250 | + let wrapper = (content: Op[]) => [ |
| 251 | + open("root", { |
| 252 | + layout: { |
| 253 | + width: grow(), |
| 254 | + height: grow(), |
| 255 | + direction: "ttb", |
| 256 | + padding: { left: 1, top: 1 }, |
| 257 | + }, |
| 258 | + border: { |
| 259 | + color: rgba(255, 255, 255), |
| 260 | + left: 1, |
| 261 | + right: 1, |
| 262 | + top: 1, |
| 263 | + bottom: 1, |
| 264 | + }, |
| 265 | + }), |
| 266 | + ...content, |
| 267 | + close(), |
| 268 | + ]; |
| 269 | + |
| 270 | + let expected = direct.render( |
| 271 | + wrapper([ |
| 272 | + open("child", { |
| 273 | + layout: { width: grow(), direction: "ttb" }, |
| 274 | + }), |
| 275 | + text("inner"), |
| 276 | + close(), |
| 277 | + ]), |
| 278 | + { mode: "line" }, |
| 279 | + ).output; |
| 280 | + |
| 281 | + let actual = snapped.render( |
| 282 | + wrapper([child]), |
| 283 | + { mode: "line" }, |
| 284 | + ).output; |
| 285 | + |
| 286 | + expect(decode(actual)).toEqual(decode(expected)); |
| 287 | + expect(trim(print(decode(actual), 20, 5))).toEqual(` |
| 288 | +┌──────────────────┐ |
| 289 | +│inner │ |
| 290 | +│ │ |
| 291 | +│ │ |
| 292 | +└──────────────────┘`.trim()); |
| 293 | + }); |
| 294 | + }); |
| 295 | + |
194 | 296 | describe("row offset", () => { |
195 | 297 | it("renders two frames at the offset position", async () => { |
196 | 298 | let term = await createTerm({ width: 20, height: 5 }); |
|
0 commit comments