diff options
Diffstat (limited to 'src/sam_edit.zig')
| -rw-r--r-- | src/sam_edit.zig | 122 |
1 files changed, 61 insertions, 61 deletions
diff --git a/src/sam_edit.zig b/src/sam_edit.zig index 3886f9eb..173a559b 100644 --- a/src/sam_edit.zig +++ b/src/sam_edit.zig @@ -1075,6 +1075,67 @@ pub fn moveDot(dot: Range, ops: []const Op) Range { return .{ .q0 = q0, .q1 = q1 }; } +/// Three files and a +Errors, the way the editor hands them over, and a +/// world whose disk holds /src/new.c and /src/data. +const Fixture = struct { + arena_state: std.heap.ArenaAllocator, + why: Why = .{}, + + const files = [_]File{ + .{ .name = "/src/a.c", .text = "int foo;\nint bar;\n", .id = 0 }, + .{ .name = "/src/b.h", .text = "foo bar foo\n", .id = 1, .dirty = true }, + .{ .name = "/src/c.c", .text = "bar\n", .id = 2 }, + .{ .name = "/src/+Errors", .text = "foo\n", .id = 3, .scratch = true }, + }; + + var opened: usize = 0; + + fn check(_: *anyopaque, path: []const u8, why: *Why) bool { + if (std.mem.eql(u8, path, "/src/new.c")) return true; + why.len = if (std.fmt.bufPrint(&why.buf, "B: cannot open {s}", .{path})) |said| said.len else |_| 0; + return false; + } + + /// The edited file, b.h, is asked about: refused. + fn refuseGet(_: *anyopaque, id: usize, why: *Why) bool { + if (id != 1) return false; + why.len = (std.fmt.bufPrint(&why.buf, "/src/b.h: Modified (e again to discard)", .{}) catch unreachable).len; + return true; + } + + fn open(_: *anyopaque, path: []const u8, why: *Why) ?File { + opened += 1; + if (std.mem.eql(u8, path, "/src/new.c")) return .{ .name = "/src/new.c", .text = "new foo\n", .id = 9 }; + why.len = if (std.fmt.bufPrint(&why.buf, "B: cannot open {s}", .{path})) |said| said.len else |_| 0; + return null; + } + + fn read(_: *anyopaque, arena: std.mem.Allocator, path: []const u8, why: *Why) ?[]const u8 { + if (std.mem.eql(u8, path, "/src/data")) return arena.dupe(u8, "DATA\n") catch null; + why.len = if (std.fmt.bufPrint(&why.buf, "can't open {s}: file does not exist", .{path})) |said| said.len else |_| 0; + return null; + } + + fn init() Fixture { + return .{ .arena_state = .init(std.testing.allocator) }; + } + + fn deinit(fx: *Fixture) void { + fx.arena_state.deinit(); + } + + fn edit(fx: *Fixture, from: usize, command: []const u8) !Result { + var ctx: u8 = 0; + const world: World = .{ .ctx = &ctx, .check = check, .open = open, .read = read, .refuseGet = refuseGet }; + return run(fx.arena_state.allocator(), &files, from, command, world, &fx.why); + } + + /// File `i`'s text after the Edit. + fn after(fx: *Fixture, res: Result, i: usize) ![]const u8 { + return apply(fx.arena_state.allocator(), res.files[i].text, res.files[i].ops.items); + } +}; + fn edited(text: []const u8, command: []const u8) ![]u8 { var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena_state.deinit(); @@ -1199,67 +1260,6 @@ test "x, y and s match whole runes: a byte pattern never splits one" { try expectEdit("\u{e9}", ",s/$/!/", "\u{e9}!"); } -/// Three files and a +Errors, the way the editor hands them over, and a -/// world whose disk holds /src/new.c and /src/data. -const Fixture = struct { - arena_state: std.heap.ArenaAllocator, - why: Why = .{}, - - const files = [_]File{ - .{ .name = "/src/a.c", .text = "int foo;\nint bar;\n", .id = 0 }, - .{ .name = "/src/b.h", .text = "foo bar foo\n", .id = 1, .dirty = true }, - .{ .name = "/src/c.c", .text = "bar\n", .id = 2 }, - .{ .name = "/src/+Errors", .text = "foo\n", .id = 3, .scratch = true }, - }; - - var opened: usize = 0; - - fn check(_: *anyopaque, path: []const u8, why: *Why) bool { - if (std.mem.eql(u8, path, "/src/new.c")) return true; - why.len = if (std.fmt.bufPrint(&why.buf, "B: cannot open {s}", .{path})) |said| said.len else |_| 0; - return false; - } - - /// The edited file, b.h, is asked about: refused. - fn refuseGet(_: *anyopaque, id: usize, why: *Why) bool { - if (id != 1) return false; - why.len = (std.fmt.bufPrint(&why.buf, "/src/b.h: Modified (e again to discard)", .{}) catch unreachable).len; - return true; - } - - fn open(_: *anyopaque, path: []const u8, why: *Why) ?File { - opened += 1; - if (std.mem.eql(u8, path, "/src/new.c")) return .{ .name = "/src/new.c", .text = "new foo\n", .id = 9 }; - why.len = if (std.fmt.bufPrint(&why.buf, "B: cannot open {s}", .{path})) |said| said.len else |_| 0; - return null; - } - - fn read(_: *anyopaque, arena: std.mem.Allocator, path: []const u8, why: *Why) ?[]const u8 { - if (std.mem.eql(u8, path, "/src/data")) return arena.dupe(u8, "DATA\n") catch null; - why.len = if (std.fmt.bufPrint(&why.buf, "can't open {s}: file does not exist", .{path})) |said| said.len else |_| 0; - return null; - } - - fn init() Fixture { - return .{ .arena_state = .init(std.testing.allocator) }; - } - - fn deinit(fx: *Fixture) void { - fx.arena_state.deinit(); - } - - fn edit(fx: *Fixture, from: usize, command: []const u8) !Result { - var ctx: u8 = 0; - const world: World = .{ .ctx = &ctx, .check = check, .open = open, .read = read, .refuseGet = refuseGet }; - return run(fx.arena_state.allocator(), &files, from, command, world, &fx.why); - } - - /// File `i`'s text after the Edit. - fn after(fx: *Fixture, res: Result, i: usize) ![]const u8 { - return apply(fx.arena_state.allocator(), res.files[i].text, res.files[i].ops.items); - } -}; - test "X and Y run a command in each open file whose line matches, or does not" { var fx: Fixture = .init(); defer fx.deinit(); |
