//! Test helpers for drawing widgets to an in-memory screen. const std = @import("std"); const testing = std.testing; const vaxis = @import("vaxis"); /// A screen to draw into and read back. Do not move it after `init`. pub const Fixture = struct { screen: vaxis.Screen, win: vaxis.Window, pub fn init(self: *Fixture, rows: u16, cols: u16) !void { self.screen = try vaxis.Screen.init(testing.allocator, .{ .rows = rows, .cols = cols, .x_pixel = 0, .y_pixel = 0, }); self.win = .{ .x_off = 0, .y_off = 0, .parent_x_off = 0, .parent_y_off = 0, .width = cols, .height = rows, .screen = &self.screen, }; } pub fn deinit(self: *Fixture) void { self.screen.deinit(testing.allocator); } pub fn row(self: *Fixture, line: u16, arena: std.mem.Allocator) ![]const u8 { var text: std.ArrayList(u8) = .empty; var col: u16 = 0; while (col < self.win.width) : (col += 1) { const cell = self.screen.readCell(col, line).?; try text.appendSlice(arena, cell.char.grapheme); } return text.items; } pub fn style(self: *Fixture, col: u16, line: u16) vaxis.Style { return self.screen.readCell(col, line).?.style; } }; /// The cell at a point of a surface tree, from the topmost child that covers it. pub fn cellAt(surface: vaxis.vxfw.Surface, col: u16, row: u16) ?vaxis.Cell { var i = surface.children.len; while (i > 0) { i -= 1; const child = surface.children[i]; if (row < child.origin.row or col < child.origin.col) { continue; } const r: u16 = @intCast(row - child.origin.row); const c: u16 = @intCast(col - child.origin.col); if (r >= child.surface.size.height or c >= child.surface.size.width) { continue; } if (cellAt(child.surface, c, r)) |cell| { return cell; } } if (surface.buffer.len == 0) { return null; } return surface.readCell(col, row); }