summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/behaviour.zig9
-rw-r--r--test/behaviour/edit.zig21
-rw-r--r--test/behaviour/other_files.zig23
-rw-r--r--test/behaviour/panes.zig40
-rw-r--r--test/session.zig179
5 files changed, 272 insertions, 0 deletions
diff --git a/test/behaviour.zig b/test/behaviour.zig
new file mode 100644
index 00000000..31da19a1
--- /dev/null
+++ b/test/behaviour.zig
@@ -0,0 +1,9 @@
+//! The behavioural tests (docs/next-tests.md): a file a section of the
+//! book, named by its label (`other-files` is other_files.zig), each test
+//! named by the label first. This module imports test/session.zig alone,
+//! so a test sees the session as a 9P client does.
+test {
+ _ = @import("behaviour/edit.zig");
+ _ = @import("behaviour/other_files.zig");
+ _ = @import("behaviour/panes.zig");
+}
diff --git a/test/behaviour/edit.zig b/test/behaviour/edit.zig
new file mode 100644
index 00000000..e95471c6
--- /dev/null
+++ b/test/behaviour/edit.zig
@@ -0,0 +1,21 @@
+//! docs/typ/reference.typ, Edit <edit>.
+const std = @import("std");
+const Session = @import("session").Session;
+
+test "edit: a write of an Edit block carries it whole, to a pane's ctl, the root's ctl and exec" {
+ var s = try Session.initFile(std.testing.allocator, "foo x\nfoo y\n");
+ defer s.deinit();
+ const n = try s.focused();
+ const body = s.at("/pane/{d}/body", .{n});
+ try s.write(s.at("/pane/{d}/ctl", .{n}), "Edit ,x/foo/{\ni/[/\na/]/\n}\n");
+ try s.expect(body, "[foo] x\n[foo] y\n");
+ try s.write("/ctl", "Edit ,x/\\[/{\nc/</\n}\n");
+ try s.expect(body, "<foo] x\n<foo] y\n");
+ try s.write(s.at("/pane/{d}/exec", .{n}), "Edit $a\nend\n.\n");
+ try s.expect(body, "<foo] x\n<foo] y\nend\n");
+ // A group still open when its open closes fails there, an err in the
+ // log, and nothing after it runs as a line.
+ try s.write(s.at("/pane/{d}/ctl", .{n}), "Edit ,x/foo/{\nd\n");
+ try s.expect(body, "<foo] x\n<foo] y\nend\n");
+ try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "unmatched") != null);
+}
diff --git a/test/behaviour/other_files.zig b/test/behaviour/other_files.zig
new file mode 100644
index 00000000..b97affc4
--- /dev/null
+++ b/test/behaviour/other_files.zig
@@ -0,0 +1,23 @@
+//! docs/typ/reference.typ, Other files <other-files>.
+const std = @import("std");
+const Session = @import("session").Session;
+
+test "other-files: kbdin types at the pane with the keyboard and mousein clicks, each done before its write is answered" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const body = try std.fmt.allocPrint(s.arena.allocator(), "/pane/{d}/body", .{try s.focused()});
+ try s.keys("ihello \x1b");
+ try s.expect(body, "hello x\n");
+ // Plan 9's Khome rune, then text before it.
+ try s.keys("A!\x1b\u{F00D}i>\x1b");
+ try s.expect(body, ">hello x!\n");
+ // A left click on the second l, then text there.
+ try s.click(">hello", 4, 1);
+ try s.keys("i#\x1b");
+ try s.expect(body, ">hel#lo x!\n");
+ // Refused, in words, as rio and devmouse refuse.
+ try std.testing.expectEqual(@as(u16, 22), try s.refused("/mousein", "m 1 2\n"));
+ try std.testing.expectEqual(@as(u16, 22), try s.refused("/mousein", "x 1 2 0\n"));
+ try std.testing.expectEqual(@as(u16, 22), try s.refused("/kbdin", "\xff"));
+ try std.testing.expectError(error.Refused, s.read("/kbdin"));
+}
diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig
new file mode 100644
index 00000000..45b7152c
--- /dev/null
+++ b/test/behaviour/panes.zig
@@ -0,0 +1,40 @@
+//! docs/typ/reference.typ, Panes <panes>.
+const std = @import("std");
+const Session = @import("session").Session;
+
+test "panes: a collapsed pane's ctl says so" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const first = try s.focused();
+ _ = try s.newPane();
+ try s.write(s.at("/pane/{d}/ctl", .{first}), "Collapse\n");
+ try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{first})), " collapsed "));
+}
+
+test "panes: the ctl word name refuses a leading blank, as the name file does" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const ctl = s.at("/pane/{d}/ctl", .{try s.focused()});
+ try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "name /tmp/lead.txt\n"));
+ try std.testing.expectEqualStrings("bad character in file name: a blank at its start", s.said);
+ try s.write(ctl, "name /tmp/ok.txt\n");
+ try s.expect(s.at("/pane/{d}/name", .{try s.focused()}), "/tmp/ok.txt\n");
+}
+
+test "panes: Putall saves every pane with unsaved edits to a file, one each, and leaves the rest" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "clean.txt", .data = "clean\n" });
+ var dir_buf: [4096]u8 = undefined;
+ const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const first = try s.focused();
+ try s.write(s.at("/pane/{d}/name", .{first}), "/tmp/pardes-putall-a.txt\n");
+ try s.write(s.at("/pane/{d}/body", .{first}), "edited\n");
+ // A file opened from disk, unedited.
+ try s.write(s.at("/pane/{d}/look", .{first}), s.at("{s}/clean.txt\n", .{dir}));
+ try std.testing.expectEqual(@as(usize, 0), s.saved);
+ try s.write("/ctl", "Putall\n");
+ try std.testing.expectEqual(@as(usize, 1), s.saved);
+}
diff --git a/test/session.zig b/test/session.zig
new file mode 100644
index 00000000..28557322
--- /dev/null
+++ b/test/session.zig
@@ -0,0 +1,179 @@
+//! T1 of docs/next-tests.md: a session driven as a 9P client drives one,
+//! in process. Each call walks a path from the root, opens, reads or writes,
+//! and clunks, through `serveFs` alone. The core is opaque here, so a
+//! behavioural test (test/behaviour/, whose module imports only this) has
+//! no field to reach: what it can see is what a client can.
+const std = @import("std");
+const pardes = @import("pardes");
+
+const Pardes = pardes.Pardes;
+const Req = pardes.ctlfs.Req;
+const Reply = pardes.ctlfs.Reply;
+
+pub const Session = struct {
+ core: *anyopaque,
+ /// What the calls answered, kept until deinit.
+ arena: std.heap.ArenaAllocator,
+ /// Saves the core asked its host for (a host would perform them).
+ saved: usize = 0,
+ /// The words of the last refusal `refused` answered.
+ said: []const u8 = "",
+ tag: u64 = 0,
+
+ /// A session showing one file pane holding `text`.
+ pub fn initFile(gpa: std.mem.Allocator, text: []const u8) !Session {
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 });
+ errdefer p.deinit();
+ _ = try p.setTestFile(text);
+ var s: Session = .{ .core = p, .arena = .init(gpa) };
+ s.drain();
+ return s;
+ }
+
+ /// A session showing its terminal pane, as `pardes` starts.
+ pub fn initTerm(gpa: std.mem.Allocator) !Session {
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 });
+ var s: Session = .{ .core = p, .arena = .init(gpa) };
+ s.drain();
+ return s;
+ }
+
+ pub fn deinit(s: *Session) void {
+ pardesOf(s).deinit();
+ s.arena.deinit();
+ }
+
+ /// The whole file at `path` (`/index`, `/pane/3/body`).
+ pub fn read(s: *Session, path: []const u8) ![]const u8 {
+ const node, const h = try s.open(path, 0);
+ defer _ = s.serve(.{ .tag = 0, .op = .release, .node = node, .handle = h, .opened = true });
+ var out: std.ArrayList(u8) = .empty;
+ while (true) {
+ const r = s.serve(.{ .tag = 0, .op = .read, .node = node, .handle = h, .off = out.items.len, .size = 1 << 16 });
+ if (r.status != .ok) return refusal(path, r);
+ const bytes = pardesOf(s).fsPayload(r);
+ if (bytes.len == 0) break;
+ try out.appendSlice(s.arena.allocator(), bytes);
+ }
+ return out.items;
+ }
+
+ /// `data` written to `path` in one write, the open clunked after.
+ pub fn write(s: *Session, path: []const u8, data: []const u8) !void {
+ const errno = try s.refused(path, data);
+ if (errno != 0) return error.Refused;
+ }
+
+ /// Like `write`, answering the refusal's errno, 0 when it was taken.
+ pub fn refused(s: *Session, path: []const u8, data: []const u8) !u16 {
+ const node, const h = try s.open(path, 1);
+ const w = s.serve(.{ .tag = 0, .op = .write, .node = node, .handle = h, .data = data });
+ // The clunk can refuse too (an Edit block left open), as close(2) does.
+ const c = s.serve(.{ .tag = 0, .op = .release, .node = node, .handle = h, .opened = true });
+ const r = if (w.status != .ok) w else c;
+ if (r.status == .ok) return 0;
+ s.said = try s.arena.allocator().dupe(u8, r.ename);
+ return r.errno;
+ }
+
+ /// A path built from `args` (`s.at("/pane/{d}/body", .{n})`), kept
+ /// until deinit.
+ pub fn at(s: *Session, comptime fmt: []const u8, args: anytype) []const u8 {
+ return std.fmt.allocPrint(s.arena.allocator(), fmt, args) catch @panic("out of memory");
+ }
+
+ /// A new pane, made by opening /pane/new; its serial.
+ pub fn newPane(s: *Session) !u32 {
+ return std.fmt.parseInt(u32, std.mem.trim(u8, try s.read("/pane/new"), "\n"), 10);
+ }
+
+ pub fn expect(s: *Session, path: []const u8, want: []const u8) !void {
+ try std.testing.expectEqualStrings(want, try s.read(path));
+ }
+
+ /// Keys typed at the pane with the keyboard (`/kbdin`'s runes).
+ pub fn keys(s: *Session, runes: []const u8) !void {
+ try s.write("/kbdin", runes);
+ }
+
+ /// A click (Plan 9's buttons: 1 left, 2 middle, 4 right) on the first
+ /// place `text` is drawn, `into` cells into it, found as a user finds it.
+ pub fn click(s: *Session, text: []const u8, into: usize, buttons: u8) !void {
+ const col, const row = try s.find(text);
+ var line: [64]u8 = undefined;
+ const x = col + into;
+ try s.write("/mousein", try std.fmt.bufPrint(&line, "m {d} {d} {d}\nm {d} {d} 0\n", .{ x, row, buttons, x, row }));
+ }
+
+ /// The screen a frame draws, a line a row.
+ pub fn screen(s: *Session) ![]const u8 {
+ const a = s.arena.allocator();
+ const json = try std.json.parseFromSliceLeaky(std.json.Value, a, try s.read("/screen"), .{});
+ const cols: usize = @intCast(json.object.get("cols").?.integer);
+ const cells = json.object.get("cells").?.array.items;
+ var out: std.ArrayList(u8) = .empty;
+ for (cells, 0..) |cell, i| {
+ if (i > 0 and i % cols == 0) try out.append(a, '\n');
+ const g = cell.array.items[0].string;
+ try out.appendSlice(a, if (g.len == 0) " " else g);
+ }
+ return out.items;
+ }
+
+ /// The serial of the pane with the keyboard.
+ pub fn focused(s: *Session) !u32 {
+ return std.fmt.parseInt(u32, std.mem.trim(u8, try s.read("/focus"), "\n"), 10);
+ }
+
+ // shortcut: a grapheme is taken as one cell wide, so text past a wide
+ // character is found a column off; count widths when a test needs it.
+ fn find(s: *Session, text: []const u8) !struct { usize, usize } {
+ var rows = std.mem.splitScalar(u8, try s.screen(), '\n');
+ var row: usize = 0;
+ while (rows.next()) |line| : (row += 1) {
+ if (std.mem.indexOf(u8, line, text)) |i| return .{ try std.unicode.utf8CountCodepoints(line[0..i]), row };
+ }
+ std.debug.print("not on the screen: {s}\n", .{text});
+ return error.NotOnScreen;
+ }
+
+ fn open(s: *Session, path: []const u8, omode: u8) !struct { u64, u32 } {
+ var node: u64 = pardes.ctlfs.root;
+ var parts = std.mem.tokenizeScalar(u8, path, '/');
+ while (parts.next()) |name| {
+ const r = s.serve(.{ .tag = 0, .op = .lookup, .node = node, .data = name });
+ if (r.status != .ok) return refusal(path, r);
+ node = r.attr.node;
+ }
+ const r = s.serve(.{ .tag = 0, .op = .open, .node = node, .omode = omode });
+ if (r.status != .ok) return refusal(path, r);
+ return .{ node, r.handle };
+ }
+
+ fn serve(s: *Session, req: Req) Reply {
+ var q = req;
+ s.tag += 1;
+ q.tag = s.tag;
+ const r = pardesOf(s).serveFs(q);
+ // As the server does (src/9p_io.zig): a clunk's held lines run then.
+ if (q.op == .release) pardes.ctlfs.runClosedLines(pardesOf(s));
+ s.drain();
+ return r;
+ }
+
+ fn drain(s: *Session) void {
+ while (pardesOf(s).nextEffect()) |e| switch (e) {
+ .save_file, .save_text => s.saved += 1,
+ else => {},
+ };
+ }
+
+ fn pardesOf(s: *Session) *Pardes {
+ return @ptrCast(@alignCast(s.core));
+ }
+};
+
+fn refusal(path: []const u8, r: Reply) error{Refused} {
+ std.debug.print("{s}: refused, errno {d}: {s}\n", .{ path, r.errno, r.ename });
+ return error.Refused;
+}