diff options
Diffstat (limited to 'test/behaviour')
| -rw-r--r-- | test/behaviour/addresses.zig | 91 | ||||
| -rw-r--r-- | test/behaviour/flags.zig | 60 | ||||
| -rw-r--r-- | test/behaviour/look_and_exec.zig | 10 | ||||
| -rw-r--r-- | test/behaviour/panes.zig | 185 | ||||
| -rw-r--r-- | test/behaviour/tags.zig | 53 |
5 files changed, 399 insertions, 0 deletions
diff --git a/test/behaviour/addresses.zig b/test/behaviour/addresses.zig index a1474c3d..dde36f59 100644 --- a/test/behaviour/addresses.zig +++ b/test/behaviour/addresses.zig @@ -63,3 +63,94 @@ test "addresses: sel reads the selection, and a write to sel replaces it and is try s.expect(body, "ab\ntwo\n"); try s.expect(sel, "ab"); } + +test "addresses: a 9P write moves dot only as acme's textinsert does" { + // dot is `cd` of `abcdef\n` (2 4); two bytes go in at `at`. + for ([_][3]u32{ + .{ 0, 4, 6 }, // before: shifted + .{ 2, 2, 6 }, // at its start: grown + .{ 3, 2, 6 }, // inside: grown + .{ 4, 2, 4 }, // at its end: left + .{ 6, 2, 4 }, // after: left + }) |c| { + var s = try Session.initFile(std.testing.allocator, "abcdef\n"); + defer s.deinit(); + const n = try s.focused(); + const dot = s.at("/pane/{d}/dot", .{n}); + try s.write(dot, "2 4"); + try s.write(s.at("/pane/{d}/addr", .{n}), s.at("#{d}", .{c[0]})); + try s.write(s.at("/pane/{d}/data", .{n}), "XX"); + try expectEqual(.{ c[1], c[2] }, try s.range(dot)); + } + // A body append leaves dot, and a sel write after it replaces dot, not + // the text appended. + var s = try Session.initFile(std.testing.allocator, "abcdef\n"); + defer s.deinit(); + const n = try s.focused(); + const dot = s.at("/pane/{d}/dot", .{n}); + try s.write(dot, "2 4"); + try s.write(s.at("/pane/{d}/body", .{n}), "more\n"); + try expectEqual(.{ @as(u32, 2), @as(u32, 4) }, try s.range(dot)); + try s.write(s.at("/pane/{d}/sel", .{n}), "YY"); + try s.expect(s.at("/pane/{d}/body", .{n}), "abYYef\nmore\n"); +} + +test "addresses: a pair out of order or out of range is refused as the address form is, not clamped" { + var s = try Session.initFile(std.testing.allocator, "abcdef\n"); + defer s.deinit(); + const addr = s.at("/pane/{d}/addr", .{try s.focused()}); + for ([_][2][]const u8{ + .{ "5 2", "addresses out of order" }, + .{ "#5,#2", "addresses out of order" }, + .{ "2 99", "address out of range" }, + .{ "#2,#99", "address out of range" }, + }) |c| { + try expectEqual(@as(u16, 22), try s.refused(addr, c[0])); + try std.testing.expectEqualStrings(c[1], s.said); + } + try s.write(addr, "2 4"); + try expectEqual(.{ @as(u32, 2), @as(u32, 4) }, try s.range(addr)); +} + +test "addresses: every address lands on a rune, and a combining mark and a lone \\r are places of their own" { + // e, U+0301 (two bytes, 1-2), x, \r, \n + var s = try Session.initFile(std.testing.allocator, "e\u{0301}x\r\n"); + defer s.deinit(); + const n = try s.focused(); + const addr = s.at("/pane/{d}/addr", .{n}); + for ([_]struct { []const u8, u32, u32 }{ + .{ "#1,#3", 1, 3 }, // the mark alone + .{ "#2", 1, 1 }, // inside it: back to its start + .{ "#4,#5", 4, 5 }, // the \r alone + .{ "1:2", 1, 1 }, // line:col the same + .{ "1:3", 1, 1 }, + .{ "0/\xCC/", 1, 3 }, // a match inside a rune covers it + }) |c| { + try s.write(addr, c[0]); + try expectEqual(.{ c[1], c[2] }, try s.range(addr)); + } + // Copied to dot, it reads back the same. + const dot = s.at("/pane/{d}/dot", .{n}); + try s.write(addr, "#1,#3"); + try s.write(dot, " 1 3 "); + try s.expect(dot, " 1 3 "); + try s.write(addr, "#4,#5"); + try s.expect(s.at("/pane/{d}/xdata", .{n}), "\r"); +} + +test "addresses: truncating data or xdata deletes only the addressed range, so > data replaces it" { + var s = try Session.initFile(std.testing.allocator, ""); + defer s.deinit(); + const n = try s.focused(); + const body = s.at("/pane/{d}/body", .{n}); + for ([_][]const u8{ "data", "xdata" }) |file| { + try s.replace(body, "hello world\nsecond line\n"); + try s.write(s.at("/pane/{d}/addr", .{n}), "#0,#5"); + const h = try s.hold(s.at("/pane/{d}/{s}", .{ n, file }), 1); + try s.truncate(h); + try s.expect(body, " world\nsecond line\n"); + try expectEqual(@as(u16, 0), try s.writeFid(h, "HOWDY")); + s.clunk(h); + try s.expect(body, "HOWDY world\nsecond line\n"); + } +} diff --git a/test/behaviour/flags.zig b/test/behaviour/flags.zig index 656d219f..0c8b6660 100644 --- a/test/behaviour/flags.zig +++ b/test/behaviour/flags.zig @@ -40,3 +40,63 @@ test "flags: dirty, mark and scroll read and write the state acme kept behind ct try t.expect(term_dirty, "0\n"); try std.testing.expectEqual(@as(u16, 22), try t.refused(term_dirty, "1")); } + +test "flags: a > into an empty body is one undo step, as into any other" { + var s = try Session.initFile(std.testing.allocator, ""); + defer s.deinit(); + const n = try s.focused(); + const body = s.at("/pane/{d}/body", .{n}); + try s.replace(body, "fresh\n"); + try s.expect(body, "fresh\n"); + try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n"); + try s.expect(body, ""); +} + +test "flags: a write that changes nothing, an empty range deleted or a range replaced by itself, is no undo step and no new version" { + var s = try Session.initFile(std.testing.allocator, "abc\ndef\n"); + defer s.deinit(); + const n = try s.focused(); + const body = s.at("/pane/{d}/body", .{n}); + const addr = s.at("/pane/{d}/addr", .{n}); + const data = s.at("/pane/{d}/data", .{n}); + const ctl = s.at("/pane/{d}/ctl", .{n}); + // One real change, so a step made after it would be the one undone. + try s.write(addr, "1"); + try s.write(data, "abd\n"); + const version = (try s.stat(body)).version; + // Each as a shell does it: `: > data` after an empty address, `echo + // abd > data` over the line it holds, and Edit's `s/d/d/`. + try s.write(addr, "#2,#2"); + const h = try s.hold(data, 1); + try s.truncate(h); + s.clunk(h); + try s.write(addr, "1"); + try s.write(data, "abd\n"); + try s.write(ctl, "Edit ,s/d/d/\n"); + try s.expect(body, "abd\ndef\n"); + try std.testing.expectEqual(version, (try s.stat(body)).version); + try s.write(ctl, "Undo\n"); + try s.expect(body, "abc\ndef\n"); +} + +test "flags: an open's writes in a row go in as one edit, seen by the next request" { + var s = try Session.initFile(std.testing.allocator, "one\n"); + defer s.deinit(); + const n = try s.focused(); + const body = s.at("/pane/{d}/body", .{n}); + const version = (try s.stat(body)).version; + const h = try s.hold(body, 1); + for (0..100) |_| try std.testing.expectEqual(@as(u16, 0), try s.writeFid(h, "0123456789\n")); + try std.testing.expectEqual(@as(usize, 4 + 1100), (try s.read(body)).len); + try std.testing.expectEqual(version + 1, (try s.stat(body)).version); + s.clunk(h); + try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n"); + try s.expect(body, "one\n"); + // data inserts at its address go on the same way. + try s.write(s.at("/pane/{d}/addr", .{n}), "#0"); + const d = try s.hold(s.at("/pane/{d}/data", .{n}), 1); + _ = try s.writeFid(d, "a"); + _ = try s.writeFid(d, "b"); + s.clunk(d); + try s.expect(body, "abone\n"); +} diff --git a/test/behaviour/look_and_exec.zig b/test/behaviour/look_and_exec.zig index 98cab342..0777d385 100644 --- a/test/behaviour/look_and_exec.zig +++ b/test/behaviour/look_and_exec.zig @@ -323,3 +323,13 @@ test "look-and-exec: a +PdfSections row's first number is the page: a section no _ = try s.refused(look, "design.pdf:1:999\n"); try expectEqual(@as(usize, 2), try errs(&s)); } + +test "look-and-exec: a pane's tagexec runs a word as a click in its tag, and reads back what it touched" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + const tagexec = s.at("/pane/{d}/tagexec", .{n}); + try s.write(tagexec, "Msg from the tag\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "from the tag") != null); + try s.expect(tagexec, s.at("{d}\n", .{n})); +} diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig index a46f8432..553b1ca9 100644 --- a/test/behaviour/panes.zig +++ b/test/behaviour/panes.zig @@ -363,3 +363,188 @@ 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: 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); +} diff --git a/test/behaviour/tags.zig b/test/behaviour/tags.zig index 6c1c6fcd..f4a5a469 100644 --- a/test/behaviour/tags.zig +++ b/test/behaviour/tags.zig @@ -397,3 +397,56 @@ test "tags: a name typed into the tag's path survives Esc, pending, and the next try s.expect(name, "/ztest.txt\n"); try std.testing.expect(std.mem.startsWith(u8, try s.read(s.at("/pane/{d}/tag", .{n})), "/ztest.txt ")); } + +test "tags: a pane's tag reads whole, its path first, and a write appends to its own text up to 4096 bytes" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + const path = s.at("/pane/{d}/tag", .{n}); + const whole = try tag(&s, n); + try std.testing.expect(std.mem.startsWith(u8, whole, "/test.txt")); + try std.testing.expect(std.mem.indexOf(u8, whole, "Del") != null); + try std.testing.expectEqual(@as(u64, whole.len), (try s.stat(path)).size); + try s.write(path, " Mine"); + try std.testing.expectEqualStrings(s.at("{s} Mine", .{whole}), try tag(&s, n)); + // A tag is a text like the body: a newline written to it stays one. + try s.write(path, "\nNext Line"); + try std.testing.expectEqualStrings(s.at("{s} Mine\nNext Line", .{whole}), try tag(&s, n)); + // Full, it takes not a byte more. + try s.replace(path, &@as([4096]u8, @splat('x'))); + try std.testing.expectEqual(@as(u16, 28), try s.refused(path, "x")); +} + +test "tags: truncating a tag clears its own text, u in the tag undoes it, and > drops the one newline ending it" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + const path = s.at("/pane/{d}/tag", .{n}); + try s.write(path, " Mine"); + // Done at the first read or write after it (or the close). + const h = try s.hold(path, 1); + try s.truncate(h); + try std.testing.expect(std.mem.indexOf(u8, try tag(&s, n), "Mine") == null); + s.clunk(h); + try std.testing.expectEqualStrings("/test.txt", std.mem.trimEnd(u8, try tag(&s, n), " ")); + // The tag keeps its undo: undoing in it brings back what was cleared. + try s.keys(":u:"); + try std.testing.expect(std.mem.endsWith(u8, try tag(&s, n), " Mine")); + // Written back without a leading space, the text still stands apart + // from the path, and a click on its first letter lands on it. + try s.replace(path, "Get"); + try std.testing.expectEqualStrings("/test.txt Get", try tag(&s, n)); + try s.click("Get", 0, 1); + try std.testing.expectEqual(@as(usize, "/test.txt ".len), try tagCol(&s, "/test.txt")); + // `echo Make > tag`: the newline ending the truncating write's text is + // dropped, one only; a newline between its writes stays. + const m = try s.hold(path, 1); + try s.truncate(m); + _ = try s.writeFid(m, "Make\n"); + _ = try s.writeFid(m, "More\n\n"); + s.clunk(m); + try std.testing.expectEqualStrings("/test.txt Make\nMore\n", try tag(&s, n)); + // `>>` keeps what it is given. + try s.write(path, " Tail\n"); + try std.testing.expectEqualStrings("/test.txt Make\nMore\n Tail\n", try tag(&s, n)); +} |
