diff options
| -rw-r--r-- | src/ninep/pane.zig | 200 | ||||
| -rw-r--r-- | test/behaviour.zig | 3 | ||||
| -rw-r--r-- | test/behaviour/addresses.zig | 65 | ||||
| -rw-r--r-- | test/behaviour/flags.zig | 42 | ||||
| -rw-r--r-- | test/behaviour/rules.zig | 20 | ||||
| -rw-r--r-- | test/behaviour/tree.zig | 44 |
6 files changed, 174 insertions, 200 deletions
diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig index 33f816fe..bf9dc381 100644 --- a/src/ninep/pane.zig +++ b/src/ninep/pane.zig @@ -1098,88 +1098,6 @@ const withFile = th.withFile; const serialOf = th.serialOf; const Status = tree.Status; -test "index lists serial kind dirty flag and name, and stats 0, a view generated by each read" { - const gpa = testing.allocator; - const p = try withFile(gpa, "hello\nthere\n"); - defer p.deinit(); - const pane = p.panes[0].?; - const index = @backingInt(tree.TopFile.index); - - const a = rd(p, index, 0, 4096); - try testing.expectEqual(Status.ok, a.reply.status); - var want: [64]u8 = undefined; - const clean_row = try std.fmt.bufPrint(&want, "{d} text 0 /test.txt {d}\n", .{ pane.serial, columnOf(p, 0) }); - try testing.expect(std.mem.indexOf(u8, a.bytes, clean_row) != null); - const at = std.mem.indexOf(u8, a.bytes, clean_row).?; - const stat = call(p, .{ .tag = 1, .op = .getattr, .node = index }); - try testing.expectEqual(@as(u64, 0), stat.reply.attr.size); - try testing.expectEqual(@as(u16, 0o444), stat.reply.attr.mode); - - pane.file.?.saved_revision = pane.file.?.revision -% 1; - var dirty_want: [64]u8 = undefined; - const dirty_row = try std.fmt.bufPrint(&dirty_want, "{d} text 1 /test.txt {d}\n", .{ pane.serial, columnOf(p, 0) }); - try testing.expect(std.mem.indexOf(u8, rd(p, index, 0, 4096).bytes, dirty_row) != null); - const kind = at + std.mem.indexOfScalar(u8, clean_row, ' ').? + 1; - try testing.expectEqualStrings("text", rd(p, index, kind, 4).bytes); - - const serial = try th.newPane(p); - const both = rd(p, index, 0, 4096); - var expected: [4200]u8 = undefined; - const made = p.panes[p.paneBySerial(serial).?].?; - try testing.expect(std.mem.endsWith(u8, both.bytes, try std.fmt.bufPrint(&expected, "\n{d} text 0 {s} {d}\n", .{ serial, nameOf(p, made), columnOf(p, p.paneBySerial(serial).?) }))); -} - -test "a terminal is listed as term with its directory as name" { - const p = try th.withTerm(testing.allocator); - defer p.deinit(); - const pane = p.panes[0].?; - p.setCwd(0, "/work/dir"); - var want: [64]u8 = undefined; - try testing.expect(std.mem.indexOf( - u8, - rd(p, @backingInt(tree.TopFile.index), 0, 4096).bytes, - try std.fmt.bufPrint(&want, "{d} term 0 /work/dir {d}\n", .{ pane.serial, columnOf(p, 0) }), - ) != null); - try testing.expectEqualStrings("/work/dir\n", rd(p, Node.of(pane.serial, .name), 0, 4096).bytes); - const refused = wr(p, Node.of(pane.serial, .name), "/elsewhere\n"); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expect(std.mem.startsWith(u8, refused.reply.ename, "invalid rename: a terminal is named")); -} - -test "body reads at any offset and writes append" { - const gpa = testing.allocator; - const p = try withFile(gpa, "one\ntwo\n"); - defer p.deinit(); - const serial = serialOf(p); - const body = Node.of(serial, .body); - - try testing.expectEqualStrings("one\ntwo\n", rd(p, body, 0, 100).bytes); - try testing.expectEqualStrings("two\n", rd(p, body, 4, 100).bytes); - try testing.expectEqualStrings("wo", rd(p, body, 5, 2).bytes); - try testing.expectEqualStrings("", rd(p, body, 999, 2).bytes); - try testing.expect(rd(p, body, 0, 100).bytes.ptr == p.panes[0].?.file.?.content.ptr); - - const w = call(p, .{ .tag = 5, .op = .write, .node = body, .off = 0, .data = "three\n" }); - try testing.expectEqual(@as(u32, 6), w.reply.written); - try testing.expectEqualStrings("one\ntwo\nthree\n", p.panes[0].?.file.?.content); - - const short = wr(p, body, "a\xC3"); - try testing.expectEqual(@as(u32, 1), short.reply.written); - try testing.expectEqualStrings("one\ntwo\nthree\na", p.panes[0].?.file.?.content); - - const stat = call(p, .{ .tag = 6, .op = .getattr, .node = body }); - try testing.expectEqual(@as(u64, "one\ntwo\nthree\na".len), stat.reply.attr.size); - try testing.expectEqual(@as(u16, 0o666), stat.reply.attr.mode); - try testing.expect(stat.reply.attr.mtime >= p.fs.started); - - // The qid version is the pane's revision, so a stat sees an edit land. - try testing.expectEqual(p.panes[0].?.file.?.revision, stat.reply.attr.version); - _ = wr(p, body, "more\n"); - const later = call(p, .{ .tag = 7, .op = .getattr, .node = body }); - try testing.expect(later.reply.attr.version != stat.reply.attr.version); - try testing.expectEqual(@as(u32, 0), call(p, .{ .tag = 8, .op = .getattr, .node = Node.of(serial, .tag) }).reply.attr.version); -} - test "a body write to a terminal pane types at its shell" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 10 }); @@ -1228,32 +1146,6 @@ test "tag reads the whole tag and writes append to the editable tail" { try testing.expectEqual(E.NOSPC, wr(p, node, "x").errno()); } -test "data and xdata read from addr, move it, and write through it" { - const gpa = testing.allocator; - const p = try withFile(gpa, "one\ntwo\n"); - defer p.deinit(); - const serial = serialOf(p); - const addr = Node.of(serial, .addr); - const data = Node.of(serial, .data); - const xdata = Node.of(serial, .xdata); - - _ = wr(p, addr, "#0"); - try testing.expectEqualStrings("one", rd(p, data, 0, 3).bytes); - try testing.expectEqual(@as(u32, 3), p.panes[0].?.fs.addr.q0); - try testing.expectEqual(@as(u32, 3), p.panes[0].?.fs.addr.q1); - - _ = wr(p, addr, "1"); - try testing.expectEqualStrings("one\n", rd(p, xdata, 0, 100).bytes); - _ = wr(p, addr, "1"); - try testing.expectEqualStrings("one\ntwo\n", rd(p, data, 0, 100).bytes); - - _ = wr(p, addr, "1"); - const w = wr(p, data, "ONE\n"); - try testing.expectEqual(@as(u32, 4), w.reply.written); - try testing.expectEqualStrings("ONE\ntwo\n", p.panes[0].?.file.?.content); - try testing.expectEqual(@as(u32, 4), p.panes[0].?.fs.addr.q0); -} - test "data never splits a rune, in either direction" { const gpa = testing.allocator; const p = try withFile(gpa, "\u{00e9}x\n"); @@ -1268,98 +1160,6 @@ test "data never splits a rune, in either direction" { try testing.expectEqual(@as(u32, 1), wr(p, Node.of(serial, .data), "a\xC3").reply.written); } -test "sel reads the selection and writing sel replaces it" { - const gpa = testing.allocator; - const p = try withFile(gpa, "one\ntwo\n"); - defer p.deinit(); - const serial = serialOf(p); - const addr_node = Node.of(serial, .addr); - const dot = Node.of(serial, .dot); - const sel = Node.of(serial, .sel); - - // Copying addr onto dot is the whole of acme's `dot=addr`, and back again - // of `addr=dot`; each range file reads exactly what the other takes. - _ = wr(p, addr_node, "#0,#3"); - try testing.expectEqual(Status.ok, wr(p, dot, " 0 3 ").reply.status); - try testing.expectEqualStrings(" 0 3 ", rd(p, dot, 0, 100).bytes); - try testing.expectEqual(range_len, call(p, .{ .tag = 1, .op = .getattr, .node = dot }).reply.attr.size); - try testing.expectEqualStrings("one", rd(p, sel, 0, 100).bytes); - try testing.expectEqual(@as(u64, 3), call(p, .{ .tag = 1, .op = .getattr, .node = sel }).reply.attr.size); - - _ = wr(p, addr_node, "#0"); - _ = wr(p, addr_node, " 0 3 "); - try testing.expectEqual(@as(u32, 0), p.panes[0].?.fs.addr.q0); - try testing.expectEqual(@as(u32, 3), p.panes[0].?.fs.addr.q1); - - // A truncation empties dot once its open goes, nothing written after - // it; an address expression addresses it. - const h = call(p, .{ .tag = 8, .op = .open, .node = dot, .omode = 1 }).reply.handle; - _ = call(p, .{ .tag = 9, .op = .setattr, .node = dot, .truncate = true }); - try testing.expectEqualStrings("one", rd(p, sel, 0, 100).bytes); - _ = call(p, .{ .tag = 10, .op = .release, .node = dot, .handle = h, .opened = true }); - try testing.expectEqualStrings("", rd(p, sel, 0, 100).bytes); - try testing.expectEqual(Status.ok, wr(p, dot, "#0,#3").reply.status); - - try testing.expectEqual(Status.ok, wr(p, sel, "ONE").reply.status); - try testing.expectEqualStrings("ONE\ntwo\n", p.panes[0].?.file.?.content); - // What was written is selected, as acme's wrsel leaves it. - try testing.expectEqualStrings("ONE", rd(p, sel, 0, 100).bytes); - // A write of another open replaces it; one open's writes run on. - _ = wr(p, sel, "!"); - try testing.expectEqualStrings("!\ntwo\n", p.panes[0].?.file.?.content); - const hs = call(p, .{ .tag = 3, .op = .open, .node = sel, .omode = 1 }).reply.handle; - _ = call(p, .{ .tag = 4, .op = .write, .node = sel, .handle = hs, .data = "a" }); - _ = call(p, .{ .tag = 5, .op = .write, .node = sel, .handle = hs, .data = "b" }); - _ = call(p, .{ .tag = 6, .op = .release, .node = sel, .handle = hs, .opened = true }); - try testing.expectEqualStrings("ab\ntwo\n", p.panes[0].?.file.?.content); - try testing.expectEqualStrings("ab", rd(p, sel, 0, 100).bytes); -} - -test "the flag files read and write the state acme kept behind ctl verbs" { - const gpa = testing.allocator; - const p = try withFile(gpa, "one\ntwo\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[0].?; - const pf = &p.panes[0].?.fs; - const dirty = Node.of(serial, .dirty); - const mark = Node.of(serial, .mark); - const scroll = Node.of(serial, .scroll); - - for ([_]u64{ dirty, mark, scroll }) |node| { - try testing.expectEqual(@as(u64, 2), call(p, .{ .tag = 1, .op = .getattr, .node = node }).reply.attr.size); - for ([_][]const u8{ "", "yes", "2", "0 1", "true" }) |bad| { - try testing.expectEqual(E.INVAL, wr(p, node, bad).errno()); - try testing.expectEqualStrings("bad flag: takes 1 or 0", wr(p, node, bad).reply.ename); - } - } - - try testing.expectEqualStrings("0\n", rd(p, dirty, 0, 8).bytes); - try testing.expectEqualStrings("1\n", rd(p, mark, 0, 8).bytes); - try testing.expectEqualStrings("1\n", rd(p, scroll, 0, 8).bytes); - - try testing.expectEqual(Status.ok, wr(p, dirty, "1\n").reply.status); - try testing.expect(dirtyOf(pane)); - try testing.expectEqualStrings("1\n", rd(p, dirty, 0, 8).bytes); - _ = wr(p, dirty, "0"); - try testing.expect(!dirtyOf(pane)); - - _ = wr(p, mark, "0"); - _ = wr(p, scroll, "0"); - try testing.expect(pf.nomark and pf.noscroll); - try testing.expectEqualStrings("0\n", rd(p, mark, 0, 8).bytes); - try testing.expectEqualStrings("0\n", rd(p, scroll, 0, 8).bytes); - _ = wr(p, mark, "1"); - _ = wr(p, scroll, "1"); - try testing.expect(!pf.nomark and !pf.noscroll); - - // A terminal has no file, so its dirty flag is readable but not writable. - const term = try th.withTerm(gpa); - defer term.deinit(); - try testing.expectEqualStrings("0\n", rd(term, Node.of(serialOf(term), .dirty), 0, 8).bytes); - try testing.expectEqual(E.INVAL, wr(term, Node.of(serialOf(term), .dirty), "1").errno()); -} - test "the writes of one open are one undo step, as bash's line-buffered printf makes them; the next open is another" { const p = try withFile(testing.allocator, "one\n"); defer p.deinit(); diff --git a/test/behaviour.zig b/test/behaviour.zig index 49245d82..a272d0f6 100644 --- a/test/behaviour.zig +++ b/test/behaviour.zig @@ -3,9 +3,11 @@ //! 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/addresses.zig"); _ = @import("behaviour/columns_and_tags.zig"); _ = @import("behaviour/edit.zig"); _ = @import("behaviour/event.zig"); + _ = @import("behaviour/flags.zig"); _ = @import("behaviour/log.zig"); _ = @import("behaviour/look_and_exec.zig"); _ = @import("behaviour/mounts.zig"); @@ -14,4 +16,5 @@ test { _ = @import("behaviour/root_ctl.zig"); _ = @import("behaviour/rules.zig"); _ = @import("behaviour/settings.zig"); + _ = @import("behaviour/tree.zig"); } diff --git a/test/behaviour/addresses.zig b/test/behaviour/addresses.zig new file mode 100644 index 00000000..a1474c3d --- /dev/null +++ b/test/behaviour/addresses.zig @@ -0,0 +1,65 @@ +//! docs/typ/reference.typ, Addresses and data <addresses>. +const std = @import("std"); +const Session = @import("session").Session; +const expectEqual = std.testing.expectEqual; + +test "addresses: data and xdata read from addr, data moving it, and a write goes through it" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const n = try s.focused(); + const addr = s.at("/pane/{d}/addr", .{n}); + const data = s.at("/pane/{d}/data", .{n}); + try s.write(addr, "#0"); + try std.testing.expectEqualStrings("one", try s.readAt(data, 0, 3)); + try expectEqual(.{ @as(u32, 3), @as(u32, 3) }, try s.range(addr)); + try s.write(addr, "1"); + try s.expect(s.at("/pane/{d}/xdata", .{n}), "one\n"); + try s.write(addr, "1"); + try s.expect(data, "one\ntwo\n"); + try s.write(addr, "1"); + try s.write(data, "ONE\n"); + try s.expect(s.at("/pane/{d}/body", .{n}), "ONE\ntwo\n"); + try expectEqual(@as(u32, 4), (try s.range(addr))[0]); +} + +test "addresses: sel reads the selection, and a write to sel replaces it and is selected" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const n = try s.focused(); + const addr = s.at("/pane/{d}/addr", .{n}); + const dot = s.at("/pane/{d}/dot", .{n}); + const sel = s.at("/pane/{d}/sel", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + // Copying addr onto dot is the whole of acme's `dot=addr`, and back again + // of `addr=dot`; each range file reads exactly what the other takes. + try s.write(addr, "#0,#3"); + try s.write(dot, " 0 3 "); + try s.expect(dot, " 0 3 "); + try expectEqual(@as(u64, 24), (try s.stat(dot)).size); + try s.expect(sel, "one"); + try expectEqual(@as(u64, 3), (try s.stat(sel)).size); + try s.write(addr, "#0"); + try s.write(addr, " 0 3 "); + try expectEqual(.{ @as(u32, 0), @as(u32, 3) }, try s.range(addr)); + // A truncation empties dot once its open goes, nothing written after + // it; an address expression addresses it. + const h = try s.hold(dot, 1); + try s.truncate(h); + try s.expect(sel, "one"); + s.clunk(h); + try s.expect(sel, ""); + try s.write(dot, "#0,#3"); + try s.write(sel, "ONE"); + try s.expect(body, "ONE\ntwo\n"); + // What was written is selected, as acme's wrsel leaves it. + try s.expect(sel, "ONE"); + // A write of another open replaces it; one open's writes run on. + try s.write(sel, "!"); + try s.expect(body, "!\ntwo\n"); + const w = try s.hold(sel, 1); + try expectEqual(@as(u16, 0), try s.writeFid(w, "a")); + try expectEqual(@as(u16, 0), try s.writeFid(w, "b")); + s.clunk(w); + try s.expect(body, "ab\ntwo\n"); + try s.expect(sel, "ab"); +} diff --git a/test/behaviour/flags.zig b/test/behaviour/flags.zig new file mode 100644 index 00000000..656d219f --- /dev/null +++ b/test/behaviour/flags.zig @@ -0,0 +1,42 @@ +//! docs/typ/reference.typ, Flags and undo <flags>. +const std = @import("std"); +const Session = @import("session").Session; + +test "flags: dirty, mark and scroll read and write the state acme kept behind ctl verbs, 1 or 0" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const n = try s.focused(); + const dirty = s.at("/pane/{d}/dirty", .{n}); + const mark = s.at("/pane/{d}/mark", .{n}); + const scroll = s.at("/pane/{d}/scroll", .{n}); + for ([_][]const u8{ dirty, mark, scroll }) |flag| { + try std.testing.expectEqual(@as(u64, 2), (try s.stat(flag)).size); + for ([_][]const u8{ "", "yes", "2", "0 1", "true" }) |bad| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(flag, bad)); + try std.testing.expectEqualStrings("bad flag: takes 1 or 0", s.said); + } + } + try s.expect(dirty, "0\n"); + try s.expect(mark, "1\n"); + try s.expect(scroll, "1\n"); + // dirty as /index shows it. + try s.write(dirty, "1\n"); + try s.expect(dirty, "1\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), s.at("{d} text 1 ", .{n})) != null); + try s.write(dirty, "0"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), s.at("{d} text 0 ", .{n})) != null); + try s.write(mark, "0"); + try s.write(scroll, "0"); + try s.expect(mark, "0\n"); + try s.expect(scroll, "0\n"); + try s.write(mark, "1"); + try s.write(scroll, "1"); + try s.expect(mark, "1\n"); + try s.expect(scroll, "1\n"); + // A terminal has no file, so its dirty flag is readable but not writable. + var t = try Session.initTerm(std.testing.allocator); + defer t.deinit(); + const term_dirty = t.at("/pane/{d}/dirty", .{try t.focused()}); + try t.expect(term_dirty, "0\n"); + try std.testing.expectEqual(@as(u16, 22), try t.refused(term_dirty, "1")); +} diff --git a/test/behaviour/rules.zig b/test/behaviour/rules.zig index a29db6f0..4b853a40 100644 --- a/test/behaviour/rules.zig +++ b/test/behaviour/rules.zig @@ -17,3 +17,23 @@ test "rules: a control character in a write to any ctl refuses the whole write, try std.testing.expect(std.mem.indexOf(u8, s.said, "\"bogus\"") != null); try std.testing.expect(std.mem.indexOfScalar(u8, s.said, '\t') == null); } + +test "rules: body reads at any offset and writes append; a stat says its length, and its version moves with each edit" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const body = s.at("/pane/{d}/body", .{try s.focused()}); + try std.testing.expectEqualStrings("two\n", try s.readAt(body, 4, 100)); + try std.testing.expectEqualStrings("wo", try s.readAt(body, 5, 2)); + try std.testing.expectEqualStrings("", try s.readAt(body, 999, 2)); + try s.write(body, "three\n"); + try s.expect(body, "one\ntwo\nthree\n"); + // A rune cut at the write's end is not taken. + try s.write(body, "a\xC3"); + try s.expect(body, "one\ntwo\nthree\na"); + const st = try s.stat(body); + try std.testing.expectEqual(@as(u64, "one\ntwo\nthree\na".len), st.size); + try std.testing.expectEqual(@as(u16, 0o666), st.mode); + try s.write(body, "more\n"); + try std.testing.expect((try s.stat(body)).version != st.version); + try std.testing.expectEqual(@as(u32, 0), (try s.stat(s.at("/pane/{d}/tag", .{try s.focused()}))).version); +} diff --git a/test/behaviour/tree.zig b/test/behaviour/tree.zig new file mode 100644 index 00000000..7f9b37a4 --- /dev/null +++ b/test/behaviour/tree.zig @@ -0,0 +1,44 @@ +//! docs/typ/reference.typ, The tree <tree>. +const std = @import("std"); +const Session = @import("session").Session; + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +/// The first column's serial, /layout's first word. +fn firstColumn(s: *Session) ![]const u8 { + return std.mem.sliceTo(try s.read("/layout"), ' '); +} + +test "tree: index lists serial kind dirty name column, and stats 0, a view generated by each read" { + var s = try Session.initFile(std.testing.allocator, "hello\nthere\n"); + defer s.deinit(); + const n = try s.focused(); + const col = try firstColumn(&s); + const clean = s.at("{d} text 0 /test.txt {s}\n", .{ n, col }); + const index = try s.read("/index"); + const at = std.mem.indexOf(u8, index, clean) orelse return error.NotListed; + const st = try s.stat("/index"); + try std.testing.expectEqual(@as(u64, 0), st.size); + try std.testing.expectEqual(@as(u16, 0o444), st.mode); + try s.write(s.at("/pane/{d}/body", .{n}), "more\n"); + try std.testing.expect(has(try s.read("/index"), s.at("{d} text 1 /test.txt {s}\n", .{ n, col }))); + // Read from an offset, as any file. + const kind = at + std.mem.indexOfScalar(u8, clean, ' ').? + 1; + try std.testing.expectEqualStrings("text", try s.readAt("/index", kind, 4)); + const made = try s.newPane(); + const name = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{made})), "\n"); + try std.testing.expect(std.mem.endsWith(u8, try s.read("/index"), s.at("\n{d} text 0 {s} {s}\n", .{ made, name, col }))); +} + +test "tree: a terminal is listed as term with its directory as name, which a write to name cannot change" { + var s = try Session.initTerm(std.testing.allocator); + defer s.deinit(); + const n = try s.focused(); + const dir = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n"); + try std.testing.expect(std.mem.startsWith(u8, dir, "/")); + try std.testing.expect(has(try s.read("/index"), s.at("{d} term 0 {s} {s}\n", .{ n, dir, try firstColumn(&s) }))); + try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/name", .{n}), "/elsewhere\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "invalid rename: a terminal is named")); +} |
