diff options
Diffstat (limited to 'test/behaviour/panes.zig')
| -rw-r--r-- | test/behaviour/panes.zig | 541 |
1 files changed, 541 insertions, 0 deletions
diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig index a46f8432..0c0edbf9 100644 --- a/test/behaviour/panes.zig +++ b/test/behaviour/panes.zig @@ -363,3 +363,544 @@ test "panes: a Mini that cannot read its file leaves the panes, the last Mini's try s.expect("/index", index); try s.expect(s.at("/pane/{d}/body", .{mini}), body); } + +test "panes: Mini with no argument is wrong #args, EINVAL, as every word that needs one" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/ctl", .{try s.focused()}), "Mini\n")); + try std.testing.expectEqualStrings("wrong #args in control message \"Mini\"", s.said); +} + +test "panes: the ctl word name with nothing after it says it names nothing" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/ctl", .{try s.focused()}), "name \n")); + try std.testing.expectEqualStrings("invalid file name: name takes the name, and there is none", s.said); +} + +test "panes: ctl reads acme's fields in acme's order, undo and redo flags included, then pardes's own" { + var s = try Session.initFile(std.testing.allocator, "one\n"); + defer s.deinit(); + const n = try s.focused(); + const ctl = s.at("/pane/{d}/ctl", .{n}); + const field = struct { + fn at(text: []const u8, i: usize) []const u8 { + var it = std.mem.tokenizeScalar(u8, text, ' '); + for (0..i) |_| _ = it.next(); + return it.next().?; + } + }.at; + try std.testing.expectEqualStrings("0", field(try s.read(ctl), 8)); // nothing to undo + try std.testing.expectEqualStrings("0", field(try s.read(ctl), 9)); // nothing to redo + const current = field(try s.read(ctl), 10); + try std.testing.expect(std.mem.eql(u8, current, "current") or std.mem.eql(u8, current, "notcurrent")); + try s.write(s.at("/pane/{d}/body", .{n}), "two\n"); + try std.testing.expectEqualStrings("1", field(try s.read(ctl), 8)); + try s.write(ctl, "Undo\n"); + try std.testing.expectEqualStrings("1", field(try s.read(ctl), 9)); +} + +/// A fresh directory's absolute path, kept until deinit. +fn tmpPath(s: *Session, tmp: *std.testing.TmpDir) ![]const u8 { + var buf: [4096]u8 = undefined; + return s.at("{s}", .{buf[0..try tmp.dir.realPath(std.testing.io, &buf)]}); +} + +/// The `i`th blank-separated field of a pane's ctl read. +fn ctlField(s: *Session, n: u32, i: usize) ![]const u8 { + var it = std.mem.tokenizeScalar(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), ' '); + for (0..i) |_| _ = it.next(); + return it.next() orelse error.NoField; +} + +test "panes: get reloads the pane from disk and reports a missing file without losing dirty contents" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "note.txt", .data = "from disk\n" }); + var s = try Session.initFile(std.testing.allocator, "in memory\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const ctl = s.at("/pane/{d}/ctl", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/note.txt\n", .{dir})); + try s.write(ctl, "get\n"); + try s.expect(body, "from disk\n"); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); + try std.testing.expectEqualStrings("1", try ctlField(&s, n, 8)); // an undo step + // Renamed but never edited, it was clean: nothing thrown away, nothing + // logged as reloaded over edits. + try std.testing.expect(!has(try s.read("/log"), s.at("changed {d} reloaded\n", .{n}))); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/missing.txt\n", .{dir})); + try s.write(body, "unsaved\n"); + // Unsaved text is asked about once, as acme's get asks winclean. + try std.testing.expectEqual(@as(u16, 5), try s.refused(ctl, "get\n")); + try std.testing.expect(std.mem.endsWith(u8, s.said, ": Modified (get again to discard)")); + try std.testing.expect(has(try s.read("/log"), s.at("unsaved {d} {s}/missing.txt\n", .{ n, dir }))); + try std.testing.expectEqual(@as(u16, 2), try s.refused(ctl, "get\n")); + try s.expect(body, "from disk\nunsaved\n"); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "1\n"); +} + +test "panes: Get reads the pane's file again, refusing unsaved edits once, and Get file loads that file into the pane" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "note.txt", .data = "from disk\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "other.txt", .data = "the other\n" }); + var s = try Session.initFile(std.testing.allocator, "in memory\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const exec = s.at("/pane/{d}/exec", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/note.txt\n", .{dir})); + try s.write(exec, "Get\n"); + try s.expect(body, "from disk\n"); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); + // Edited: asked once, then read again. + try s.write(body, "unsaved\n"); + try std.testing.expect(try s.refused(exec, "Get\n") != 0); + try s.expect(body, "from disk\nunsaved\n"); + try s.write(exec, "Get\n"); + try s.expect(body, "from disk\n"); + // Get file: that file, by a name from the pane's directory. + try s.write(exec, "Get other.txt\n"); + try s.expect(body, "the other\n"); + try s.expect(s.at("/pane/{d}/name", .{n}), s.at("{s}/other.txt\n", .{dir})); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); +} + +test "panes: Undo after a load under another name (Get file, Edit e) puts the old name back with the old text and its clean state; Putall then writes nothing over the loaded file" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "other.txt", .data = "other\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "keep.txt", .data = "keep\n" }); + for ([_][]const u8{ "Get keep.txt\n", "Edit e keep.txt\n" }) |load| { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const exec = s.at("/pane/{d}/exec", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + const name = s.at("/pane/{d}/name", .{n}); + const dirty = s.at("/pane/{d}/dirty", .{n}); + try s.write(name, s.at("{s}/other.txt\n", .{dir})); + try s.write(exec, "Get\n"); + try s.expect(body, "other\n"); + try s.expect(dirty, "0\n"); + // Loaded: keep.txt's text under its name. + try s.write(exec, load); + try s.expect(body, "keep\n"); + try s.expect(name, s.at("{s}/keep.txt\n", .{dir})); + if (std.mem.startsWith(u8, load, "Get")) try s.expect(dirty, "0\n"); + // Undo: other.txt's text, other.txt's name, clean as it was. + try s.write(exec, "Undo\n"); + try s.expect(body, "other\n"); + try s.expect(name, s.at("{s}/other.txt\n", .{dir})); + try s.expect(dirty, "0\n"); + // Putall saves nothing, and nothing lands on keep.txt. + try s.write("/ctl", "Putall\n"); + try std.testing.expectEqual(@as(usize, 0), s.saved); + // Redo: keep.txt again. + try s.write(exec, "Redo\n"); + try s.expect(name, s.at("{s}/keep.txt\n", .{dir})); + try s.expect(body, "keep\n"); + } +} + +test "panes: a Get file that fails changes nothing: no such file, a directory, or unsaved edits asked about once" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "here.txt", .data = "here\n" }); + try tmp.dir.createDirPath(std.testing.io, "sub"); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const exec = s.at("/pane/{d}/exec", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + const name = s.at("/pane/{d}/name", .{n}); + const here = s.at("{s}/here.txt\n", .{dir}); + try s.write(name, here); + try s.write(exec, "Get\n"); + for ([_][]const u8{ "Get nosuch.txt\n", "Get sub\n" }) |line| { + try std.testing.expect(try s.refused(exec, line) != 0); + try s.expect(name, here); + try s.expect(body, "here\n"); + } + // No undo step left by them: one Undo takes back the first Get. + try s.write(exec, "Undo\n"); + try s.expect(body, "x\n"); + try s.write(exec, "Redo\n"); + // Edited, the first Get asks and renames nothing. + try s.write(body, "more\n"); + try std.testing.expect(try s.refused(exec, "Get other.txt\n") != 0); + try s.expect(name, here); + try s.expect(body, "here\nmore\n"); +} + +/// The serials /index lists with a name ending `suffix`, in its order. +fn panesNamed(s: *Session, suffix: []const u8) ![]u32 { + var out: std.ArrayList(u32) = .empty; + var rows = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); + while (rows.next()) |line| { + const name = line[0 .. std.mem.lastIndexOfScalar(u8, line, ' ') orelse continue]; + if (std.mem.endsWith(u8, name, suffix)) try out.append(s.arena.allocator(), try std.fmt.parseInt(u32, line[0..std.mem.indexOfScalar(u8, line, ' ').?], 10)); + } + return out.items; +} + +test "panes: Zerox makes a second pane on one buffer: one text, undo and dirty state, its own cursor; Del of one asks nothing" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var s = try Session.initFile(std.testing.allocator, "one\ntwo\nthree\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const a = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{a}), s.at("{s}/z.txt\n", .{dir})); + try s.write(s.at("/pane/{d}/exec", .{a}), "Zerox\n"); + // Both in the index, each its serial, one name. + const twins = try panesNamed(&s, "/z.txt"); + try std.testing.expectEqual(@as(usize, 2), twins.len); + const b = if (twins[0] == a) twins[1] else twins[0]; + // b's cursor on `three`; a line written above it in a moves it down. + try s.write(s.at("/pane/{d}/dot", .{b}), "9 9"); + try s.write(s.at("/pane/{d}/addr", .{a}), "0"); + try s.write(s.at("/pane/{d}/data", .{a}), "zero\n"); + try s.expect(s.at("/pane/{d}/body", .{b}), "zero\none\ntwo\nthree\n"); + try std.testing.expectEqual(.{ @as(u32, 14), @as(u32, 14) }, try s.range(s.at("/pane/{d}/dot", .{b}))); + try s.expect(s.at("/pane/{d}/dirty", .{a}), "1\n"); + try s.expect(s.at("/pane/{d}/dirty", .{b}), "1\n"); + // A write through b is a's too; Undo in a takes it back from both. + try s.write(s.at("/pane/{d}/body", .{b}), "four\n"); + try s.expect(s.at("/pane/{d}/body", .{a}), "zero\none\ntwo\nthree\nfour\n"); + try s.write(s.at("/pane/{d}/exec", .{a}), "Undo\n"); + try s.expect(s.at("/pane/{d}/body", .{b}), "zero\none\ntwo\nthree\n"); + // Del of a asks nothing: b holds the text. + try s.write(s.at("/pane/{d}/ctl", .{a}), "Del\n"); + try std.testing.expectEqualSlices(u32, &.{b}, try panesNamed(&s, "/z.txt")); + try s.expect(s.at("/pane/{d}/dirty", .{b}), "1\n"); + try s.expect(s.at("/pane/{d}/body", .{b}), "zero\none\ntwo\nthree\n"); + // Alone now, it is no twin; Del asks about it as about any pane. + try std.testing.expect(try s.refused(s.at("/pane/{d}/ctl", .{b}), "Del\n") != 0); + try std.testing.expectEqualSlices(u32, &.{b}, try panesNamed(&s, "/z.txt")); +} + +test "panes: a twin renamed logs a rename for each twin" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const a = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{a}), "Zerox\n"); + const twins = try panesNamed(&s, "/test.txt"); + try std.testing.expectEqual(@as(usize, 2), twins.len); + try s.write(s.at("/pane/{d}/name", .{a}), "/tmp/pardes-twin-renamed.txt\n"); + for (twins) |n| try std.testing.expect(has(try s.read("/log"), s.at("rename {d} /tmp/pardes-twin-renamed.txt\n", .{n}))); +} + +test "panes: small refusals: one name an open, small scratches loaded over unasked, twins saved once, and errnos in words a mount reads" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "noread.txt", .data = "secret\n" }); + try tmp.dir.createDirPath(std.testing.io, "sub"); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const name = s.at("/pane/{d}/name", .{n}); + const exec = s.at("/pane/{d}/exec", .{n}); + // One name an open: a second line, in the write or after, is refused. + try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "/tmp/pardes-a\nb")); + const f = try s.hold(name, 1); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(f, "/tmp/pardes-one.txt\n")); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(f, "b")); + s.clunk(f); + try s.expect(name, "/tmp/pardes-one.txt\n"); + // Errnos a mount reads from the words. + try s.write(name, s.at("{s}/f.txt\n", .{dir})); + try s.write(exec, "Get\n"); + try std.testing.expectEqual(@as(u16, 21), try s.refused(exec, "Get sub\n")); // EISDIR + const noread = s.at("{s}/noread.txt\x00", .{dir}); + _ = std.c.chmod(noread[0 .. noread.len - 1 :0], 0); + try std.testing.expectEqual(@as(u16, 13), try s.refused(exec, "Get noread.txt\n")); // EACCES + try std.testing.expectEqual(@as(u16, 20), try s.refused(exec, s.at("Incl {s}/f.txt\n", .{dir}))); // ENOTDIR + _ = try s.refused(s.at("/pane/{d}/look", .{n}), "/dev/zero\n"); + try std.testing.expect(has(try s.read("/log"), "not a regular file")); + // Twins saved once by Putall. + try s.write(s.at("/pane/{d}/body", .{n}), "edited\n"); + try s.write(exec, "Zerox\n"); + const saved = s.saved; + try s.write("/ctl", "Putall\n"); + try std.testing.expectEqual(saved + 1, s.saved); + // A scratch of 2 bytes is loaded over unasked. + try s.write(exec, "New\n"); + const scratch = try s.focused(); + try s.write(s.at("/pane/{d}/body", .{scratch}), "hi"); + try s.write(s.at("/pane/{d}/exec", .{scratch}), s.at("Get {s}/f.txt\n", .{dir})); + try s.expect(s.at("/pane/{d}/body", .{scratch}), "f\n"); +} + +test "panes: Get of a file not there says so without asking about unsaved edits" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/body", .{n}), "unsaved\n"); + _ = try s.refused(s.at("/pane/{d}/exec", .{n}), "Get /tmp/pardes-surely-no-such-file.txt\n"); + const log = try s.read("/log"); + try std.testing.expect(has(log, "no such file")); + try std.testing.expect(!has(log, "again to discard")); + // Not asked, so the next Get of a file there is asked about, not run. + try std.testing.expect(try s.refused(s.at("/pane/{d}/exec", .{n}), s.at("Get {s}/f.txt\n", .{dir})) != 0); + try s.expect(s.at("/pane/{d}/body", .{n}), "x\nunsaved\n"); +} + +test "panes: a pane's ctl takes acme's words: done by the files that replace them, or refused with why" { + var s = try Session.initFile(std.testing.allocator, "one two\n"); + defer s.deinit(); + const n = try s.focused(); + const ctl = s.at("/pane/{d}/ctl", .{n}); + const dirty = s.at("/pane/{d}/dirty", .{n}); + try s.write(ctl, "name /tmp/pardes-acme-ctl.txt\n"); + try s.expect(s.at("/pane/{d}/name", .{n}), "/tmp/pardes-acme-ctl.txt\n"); + try s.write(ctl, "dirty\n"); + try s.expect(dirty, "1\n"); + try s.write(ctl, "clean\n"); + try s.expect(dirty, "0\n"); + try s.write(s.at("/pane/{d}/addr", .{n}), "/two/"); + try s.write(ctl, "dot=addr\nshow\nnomark\nmark\nlimit=addr\n"); + try std.testing.expectEqual(@as(u32, 4), (try s.range(s.at("/pane/{d}/dot", .{n})))[0]); + try std.testing.expectEqual(.{ @as(u32, 4), @as(u32, 7) }, try s.range(s.at("/pane/{d}/limit", .{n}))); + try s.write(ctl, "cleartag\n"); + for ([_][]const u8{ "menu\n", "nomenu\n", "dump echo hi\n", "dumpdir /tmp\n", "font Go Mono\n" }) |line| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, line)); + try std.testing.expect(std.mem.startsWith(u8, s.said, "invalid: acme's ")); + } + // Checked whole: a refused word anywhere and nothing ran. + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "dirty\nmenu\n")); + try s.expect(dirty, "0\n"); +} + +test "panes: +Unsaved drops a pane once it has closed" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const a = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{a}), "/tmp/pardes-unsaved-a.txt\n"); + try s.write(s.at("/pane/{d}/body", .{a}), "edited\n"); + const b = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{b}), "/tmp/pardes-unsaved-b.txt\n"); + try s.write(s.at("/pane/{d}/body", .{b}), "edited\n"); + try std.testing.expect(try s.refused("/ctl", "Exit\n") != 0); + const list = s.at("/pane/{d}/body", .{(try panesNamed(&s, "/+Unsaved"))[0]}); + try std.testing.expect(has(try s.read(list), "/tmp/pardes-unsaved-a.txt: Modified")); + // a closes (discarded): its row goes. + _ = try s.refused(s.at("/pane/{d}/ctl", .{a}), "Del\n"); + try s.write(s.at("/pane/{d}/ctl", .{a}), "Del\n"); + try std.testing.expectEqual(@as(usize, 0), (try panesNamed(&s, "/pardes-unsaved-a.txt")).len); + try std.testing.expect(!has(try s.read(list), "/tmp/pardes-unsaved-a.txt: Modified")); +} + +test "panes: name refuses a directory, EISDIR, and leaves the name as it was" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const name = s.at("/pane/{d}/name", .{try s.focused()}); + for ([_][]const u8{ "/\n", "/tmp/\n", "sub/\n", "/tmp\n" }) |dir| { + try std.testing.expectEqual(@as(u16, 21), try s.refused(name, dir)); + try std.testing.expect(std.mem.indexOf(u8, s.said, "is a directory") != null); + } + try s.expect(name, "/test.txt\n"); +} + +test "panes: a write of two lines to name is refused EINVAL, on a held open or not" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const name = s.at("/pane/{d}/name", .{try s.focused()}); + try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "/tmp/a\n/tmp/b\n")); + const h = try s.hold(name, 1); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(h, "/tmp/a\n/tmp/b\n")); + s.clunk(h); + try s.expect(name, "/test.txt\n"); + // Line by line, as bash writes it: the second line is refused. + const h2 = try s.hold(name, 1); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(h2, "/tmp/pardes-a\n")); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(h2, "/tmp/pardes-b\n")); + s.clunk(h2); + try s.expect(name, "/tmp/pardes-a\n"); +} + +test "panes: a name with a component over 255 bytes is refused by the write that holds it" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const name = s.at("/pane/{d}/name", .{try s.focused()}); + try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "/tmp/" ++ &@as([256]u8, @splat('c')) ++ "/f.txt\n")); + try std.testing.expectEqualStrings("invalid file name: a component over 255 bytes", s.said); + const longest = "/tmp/" ++ &@as([255]u8, @splat('c')); + try s.write(name, longest ++ "\n"); + // Held with no newline, it is refused by that write, and the close + // has nothing left to apply. + const held = try s.hold(name, 1); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(held, "/tmp/" ++ &@as([256]u8, @splat('e')))); + s.clunk(held); + try s.expect(name, longest ++ "\n"); +} + +test "panes: a name cut across writes is one name, applied once at its newline or its close" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const name = s.at("/pane/{d}/name", .{try s.focused()}); + const h = try s.hold(name, 1); + _ = try s.writeFid(h, "/tmp/pardes-na"); + try s.expect(name, "/test.txt\n"); + _ = try s.writeFid(h, "me.txt\n"); + try s.expect(name, "/tmp/pardes-name.txt\n"); + s.clunk(h); + // No newline: held, and applied at the close. + const h3 = try s.hold(name, 1); + _ = try s.writeFid(h3, "/tmp/pardes-closed.txt"); + try s.expect(name, "/tmp/pardes-name.txt\n"); + s.clunk(h3); + try s.expect(name, "/tmp/pardes-closed.txt\n"); +} + +test "panes: name reads the file name, and a write promotes a scratch without touching its body or its file" { + const gpa = std.testing.allocator; + var s = try Session.initFile(gpa, "opener\n"); + defer s.deinit(); + const n = try s.newPane(); + const name = s.at("/pane/{d}/name", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + const scratch = try s.read(name); + try std.testing.expect(std.mem.endsWith(u8, scratch, "/+New\n")); + try std.testing.expectEqual(@as(u64, scratch.len), (try s.stat(name)).size); + try s.write(body, "scratch "); + try s.write(body, "work\n"); + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "renamed.zig", .data = "existing target\n" }); + var dir_buf: [4096]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + const path = s.at("{s}/renamed.zig", .{dir}); + try s.write(name, s.at("{s}\n", .{path})); + try s.expect(body, "scratch work\n"); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "1\n"); + try s.expect(name, s.at("{s}\n", .{path})); + var target_buf: [64]u8 = undefined; + try std.testing.expectEqualStrings("existing target\n", try tmp.dir.readFile(std.testing.io, "renamed.zig", &target_buf)); + try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "\n")); + // One name, and what no file name holds: a second line, a NUL. + for ([_][2][]const u8{ + .{ "two\nlines\n", "invalid file name: one name a write, on one line" }, + .{ "nul\x00.zig\n", "bad character in file name: a NUL" }, + }) |c| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(name, c[0])); + try std.testing.expectEqualStrings(c[1], s.said); + } + // Any other byte a file name holds is taken, and reads back as written: + // blanks at its ends, a tab, a control byte, DEL, C1, bytes not UTF-8. + for ([_][]const u8{ " lead.zig ", "tab\t.zig", "c\x01.zig", "del\x7f.zig", "c1\xc2\x85.zig", "bad\xff.zig" }) |odd| { + try s.write(name, s.at("{s}\n", .{odd})); + const read_back = std.mem.trimEnd(u8, try s.read(name), "\n"); + try std.testing.expect(std.mem.endsWith(u8, read_back, odd)); + try s.write(name, read_back); + try s.expect(name, s.at("{s}\n", .{read_back})); + } + // A name that is one line and UTF-8 is taken, é and all. + try s.write(name, "caf\xc3\xa9.zig\n"); + try std.testing.expect(std.mem.endsWith(u8, try s.read(name), "/caf\xc3\xa9.zig\n")); + try s.write(name, "two words.zig\n"); + try s.expect(name, s.at("{s}/two words.zig\n", .{dir})); + // A rename is no edit: Undo takes back the last write. + try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n"); + try s.expect(body, "scratch "); +} + +test "panes: relative names go from the file's directory and can name a new target" { + var s = try Session.initFile(std.testing.allocator, "retained body\n"); + defer s.deinit(); + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "note.txt", .data = "from pane directory\n" }); + var dir_buf: [4096]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + const n = try s.focused(); + const name = s.at("/pane/{d}/name", .{n}); + try s.write(name, s.at("{s}/old.txt\n", .{dir})); + try s.write(name, "child/../note.txt\n"); + try s.write(s.at("/pane/{d}/ctl", .{n}), "get\n"); + try s.expect(s.at("/pane/{d}/body", .{n}), "from pane directory\n"); + try s.write(name, "./created.txt\n"); + try s.expect(name, s.at("{s}/created.txt\n", .{dir})); + // A name alone is no edit. + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); +} + +test "panes: relative names follow a pane/new scratch's session directory, and a virtual one" { + var s = try Session.initFile(std.testing.allocator, "source body\n"); + defer s.deinit(); + const source = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{source}), "/elsewhere/source.zig\n"); + // pane/new's scratch is the session's, not the active pane's. + const n = try s.newPane(); + const name = s.at("/pane/{d}/name", .{n}); + const session = std.fs.path.dirname(std.mem.trimEnd(u8, try s.read(name), "\n")).?; + try std.testing.expect(!std.mem.eql(u8, session, "/elsewhere")); + const up = std.fs.path.dirname(session) orelse ""; + try s.write(name, "../out/./notes.txt\n"); + try s.expect(name, s.at("{s}/out/notes.txt\n", .{up})); + try s.write(name, "again.txt\n"); + try s.expect(name, s.at("{s}/out/again.txt\n", .{up})); + try s.write(name, s.at("/virtual/pane/{d}/./body\n", .{source})); + try s.write(s.at("/pane/{d}/ctl", .{n}), "get\n"); + try s.expect(s.at("/pane/{d}/body", .{n}), "source body\n"); + try s.write(name, "./body\n"); + try s.expect(name, s.at("/virtual/pane/{d}/body\n", .{source})); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); +} + +test "panes: errors appends to one +Errors pane for the pane's directory" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), "+Errors") == null); + try s.write(s.at("/pane/{d}/errors", .{n}), "boom\n"); + try s.write(s.at("/pane/{d}/errors", .{n}), "again\n"); + const index = try s.read("/index"); + try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, index, " /+Errors ")); + const at = std.mem.indexOf(u8, index, " /+Errors ").?; + const line = index[(if (std.mem.lastIndexOfScalar(u8, index[0..at], '\n')) |i| i + 1 else 0)..]; + const serial = line[0..std.mem.indexOfScalar(u8, line, ' ').?]; + try s.expect(s.at("/pane/{s}/body", .{serial}), "boom\nagain\n"); +} + +test "panes: truncating body empties it, and a stat says the length after" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const body = s.at("/pane/{d}/body", .{try s.focused()}); + const h = try s.hold(body, 1); + try s.truncate(h); + try std.testing.expectEqual(@as(u64, 0), (try s.stat(body)).size); + try s.expect(body, ""); + s.clunk(h); + try s.write(body, "new text\n"); + try std.testing.expectEqual(@as(u64, 9), (try s.stat(body)).size); +} + +test "panes: a rename colours a source its new name's language names, its text unchanged" { + if (!@import("pardes_config").syntax_highlighting) return error.SkipZigTest; + var s = try Session.initFile(std.testing.allocator, "fn check() void {}\n"); + defer s.deinit(); + const n = try s.focused(); + const x, const y = try s.where("fn check"); + const screen = try s.screen(); + const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]); + const plain = try s.styles(); + try std.testing.expectEqualStrings(plain[y * cols + x], plain[y * cols + x + 3]); + try s.write(s.at("/pane/{d}/name", .{n}), "renamed.zig\n"); + const coloured = try s.styles(); + try std.testing.expectEqualStrings(coloured[y * cols + x], coloured[y * cols + x + 1]); + try std.testing.expect(!std.mem.eql(u8, coloured[y * cols + x], plain[y * cols + x])); + try s.expect(s.at("/pane/{d}/body", .{n}), "fn check() void {}\n"); +} |
