diff options
Diffstat (limited to 'test/session.zig')
| -rw-r--r-- | test/session.zig | 128 |
1 files changed, 118 insertions, 10 deletions
diff --git a/test/session.zig b/test/session.zig index f3d96918..16a131fd 100644 --- a/test/session.zig +++ b/test/session.zig @@ -183,20 +183,62 @@ pub const Session = struct { return @ptrCast(@alignCast(s.core)); } - // panes: - /// Where `text` is first drawn on the screen: its column and row. - pub fn where(s: *Session, text: []const u8) !struct { usize, usize } { - return s.find(text); + // ninep: + + /// An open kept until `clunk`, as an event reader or a lock holder + /// keeps one. + pub const Fid = struct { node: u64, handle: u32 }; + + /// `path` opened (omode 0 read, 1 write, 2 both) and held. + pub fn hold(s: *Session, path: []const u8, omode: u8) !Fid { + const node, const h = try s.open(path, omode); + return .{ .node = node, .handle = h }; } - /// The cell the screen's cursor is drawn on, its column and row. - pub fn cursor(s: *Session) !struct { usize, usize } { - const json = try std.json.parseFromSliceLeaky(std.json.Value, s.arena.allocator(), try s.read("/screen"), .{}); - const c = json.object.get("cursor").?; - if (c != .object) return error.NoCursor; - return .{ @intCast(c.object.get("x").?.integer), @intCast(c.object.get("y").?.integer) }; + /// One read on a held open (an event record, an answer). + pub fn readFid(s: *Session, f: Fid) ![]const u8 { + const r = s.serve(.{ .tag = 0, .op = .read, .node = f.node, .handle = f.handle, .size = 1 << 16 }); + if (r.status != .ok) return refusal("a held open", r); + return s.arena.allocator().dupe(u8, pardesOf(s).fsPayload(r)); + } + + /// One write on a held open: 0 when taken, else its errno, its words in `said`. + pub fn writeFid(s: *Session, f: Fid, data: []const u8) !u16 { + const r = s.serve(.{ .tag = 0, .op = .write, .node = f.node, .handle = f.handle, .data = data }); + if (r.status == .ok) return 0; + s.said = try s.arena.allocator().dupe(u8, r.ename); + return r.errno; } + pub fn clunk(s: *Session, f: Fid) void { + _ = s.serve(.{ .tag = 0, .op = .release, .node = f.node, .handle = f.handle, .opened = true }); + } + + /// A held open's file truncated, as an OTRUNC open (`> data`) does. + pub fn truncate(s: *Session, f: Fid) !void { + const r = s.serve(.{ .tag = 0, .op = .setattr, .node = f.node, .truncate = true }); + if (r.status != .ok) return refusal("truncate", r); + } + + /// One read of `size` bytes at `off`, on an open of its own. + pub fn readAt(s: *Session, path: []const u8, off: u64, size: u32) ![]const u8 { + const f = try s.hold(path, 0); + defer s.clunk(f); + const r = s.serve(.{ .tag = 0, .op = .read, .node = f.node, .handle = f.handle, .off = off, .size = size }); + if (r.status != .ok) return refusal(path, r); + return s.arena.allocator().dupe(u8, pardesOf(s).fsPayload(r)); + } + + /// What a stat of `path` answers: its size, mode and qid version. + pub fn stat(s: *Session, path: []const u8) !struct { size: u64, mode: u16, version: u32 } { + const f = try s.hold(path, 0); + defer s.clunk(f); + const r = s.serve(.{ .tag = 0, .op = .getattr, .node = f.node }); + if (r.status != .ok) return refusal(path, r); + return .{ .size = r.attr.size, .mode = r.attr.mode, .version = r.attr.version }; + } + + // panes: /// `data` written to `path` truncated first, as a shell's `>` does. pub fn replace(s: *Session, path: []const u8, data: []const u8) !void { const node, const h = try s.open(path, 1); @@ -206,6 +248,72 @@ pub const Session = struct { const w = s.serve(.{ .tag = 0, .op = .write, .node = node, .handle = h, .data = data }); if (w.status != .ok) return refusal(path, w); } + + // core: + + /// /layout as its columns, left to right, `column:pane,pane` each and + /// a blank between (`1:2,5 3:`, column 3 empty). + pub fn columns(s: *Session) ![]const u8 { + const a = s.arena.allocator(); + var out: std.ArrayList(u8) = .empty; + var lines = std.mem.tokenizeScalar(u8, try s.read("/layout"), '\n'); + while (lines.next()) |line| { + if (std.mem.startsWith(u8, line, "active ")) continue; + var words = std.mem.tokenizeScalar(u8, line, ' '); + if (out.items.len > 0) try out.append(a, ' '); + try out.print(a, "{s}:", .{words.next().?}); + for (0..5) |_| _ = words.next(); // index x width current empty|full + var sep: []const u8 = ""; + while (words.next()) |pane| : (sep = ",") try out.print(a, "{s}{s}", .{ sep, pane }); + } + return out.items; + } + + /// The serial of the `i`th column from the left. + pub fn column(s: *Session, i: usize) !u32 { + var cols = std.mem.tokenizeScalar(u8, try s.columns(), ' '); + for (0..i) |_| _ = cols.next(); + const col = cols.next() orelse return error.NoColumn; + return std.fmt.parseInt(u32, col[0..std.mem.indexOfScalar(u8, col, ':').?], 10); + } + + /// Where /screen draws the cursor, column and row; null when it draws none. + pub fn cursor(s: *Session) !?struct { usize, usize } { + const json = try std.json.parseFromSliceLeaky(std.json.Value, s.arena.allocator(), try s.read("/screen"), .{}); + const c = switch (json.object.get("cursor").?) { + .object => |o| o, + else => return null, + }; + return .{ @intCast(c.get("x").?.integer), @intCast(c.get("y").?.integer) }; + } + + /// Where `text` is first drawn on /screen, column and row. + pub fn where(s: *Session, text: []const u8) !struct { usize, usize } { + return s.find(text); + } + + // pdf: + + /// A Look of `path` at the root (one relative to the working directory + /// made absolute first); the serial of the pane with the keyboard after. + /// A PDF in a build without them (-Dmupdf=false: no PdfTint in + /// /commands) skips the test. + pub fn lookFile(s: *Session, path: []const u8) !u32 { + if (std.mem.endsWith(u8, path, ".pdf") and std.mem.indexOf(u8, try s.read("/commands"), "PdfTint") == null) return error.SkipZigTest; + var buf: [4096]u8 = undefined; + const abs = if (std.fs.path.isAbsolute(path)) path else s.at("{s}/{s}", .{ buf[0..try std.Io.Dir.cwd().realPath(std.testing.io, &buf)], path }); + try s.write("/look", s.at("{s}\n", .{abs})); + return s.focused(); + } + + /// The page a PDF pane shows and its pages, from its tag's `[page/pages]`. + pub fn pdfPage(s: *Session, n: u32) !struct { u32, u32 } { + const tag = try s.read(s.at("/pane/{d}/tag", .{n})); + const lo = std.mem.indexOf(u8, tag, " [") orelse return error.NoPage; + const close = std.mem.indexOfScalarPos(u8, tag, lo, ']') orelse return error.NoPage; + var it = std.mem.splitScalar(u8, tag[lo + 2 .. close], '/'); + return .{ try std.fmt.parseInt(u32, it.next().?, 10), try std.fmt.parseInt(u32, it.next() orelse return error.NoPage, 10) }; + } }; fn refusal(path: []const u8, r: Reply) error{Refused} { |
