From f923d0342f7fbbc965867c19e03cb6c94fcb7fd3 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 06:08:57 -0300 Subject: hxlib: hxdiff's cases on the library, against helix's goldens and against the file pane (docs/next-editor.md, R6): test/hxlib.zig types each case into a `pardes-editor` value and prints its result as hxdiff prints a pane's, doing what an app does with the s, S, K and Alt-K it is handed back (the keys to its pattern, no answer) and printing a case that reaches any other action (a view, n and N, a macro, gw) as the caller's; hxdiff takes another driver's results (`--actual=`), and `zig build hxlib` (`-- --parity`) holds them to the goldens or the file pane; the key notation is test/hxkeys.zig, which both harnesses read; what the cases found, fixed: paneCursorLines gave the library's body no lines (mm, mi, ma, md, mr, insert-mode arrows), and the library's selectRegex keeps its pattern in `/` as pardes's prompt does (look.submitSearch); a page key in insert mode moves a view and is the caller's; test/lib's `zig build bench -- `: a key's cost, the pages it changes, a parse; hxlib 537 of 658 compared with 0 mismatches (5 waived as for pardes, 121 the caller's), --parity 617 of 738 with 0 mismatches and 0 waived; hxdiff 658 / parity 738 / hxgolf 124 with 0 mismatches, core-test 811 of 950, shell-test 990 of 993, replicas pass, lib-test pass --- test/hxdiff.zig | 109 ++++++++++++++++++-------------------------------------- 1 file changed, 34 insertions(+), 75 deletions(-) (limited to 'test/hxdiff.zig') diff --git a/test/hxdiff.zig b/test/hxdiff.zig index ae73863e..f18f498b 100644 --- a/test/hxdiff.zig +++ b/test/hxdiff.zig @@ -3,6 +3,7 @@ // --strict checks complete corpus coverage. Waivers pin both sides of a known difference. const std = @import("std"); const pardes = @import("pardes"); +const hxkeys = @import("hxkeys.zig"); // Its own root, so src/main.zig's logFn does not apply here: the core it // drives feeds every case's text through ghostty-vt, which narrates each @@ -36,6 +37,9 @@ pub fn main(init: std.process.Init) !void { var verbose = false; var strict = false; var pane_override: ?[]const u8 = null; + // Another driver's results, a line a case (test/hxlib.zig, the + // library's): held against the goldens, or with --parity the file pane. + var actual_path: ?[]const u8 = null; var goldens_path: ?[]const u8 = null; var waivers_path: ?[]const u8 = null; const paths = try arena.alloc([]const u8, @max(args.len - 1, 3)); // room for the defaults below @@ -56,6 +60,8 @@ pub fn main(init: std.process.Init) !void { const kind = a["--pane=".len..]; if (!std.mem.eql(u8, kind, "file") and !std.mem.eql(u8, kind, "tty")) return error.UnknownPane; pane_override = kind; + } else if (std.mem.startsWith(u8, a, "--actual=")) { + actual_path = a["--actual=".len..]; } else if (std.mem.eql(u8, a, "--waivers")) { if (waivers_path != null) return error.DuplicateWaiverPath; want_waivers = true; @@ -74,7 +80,8 @@ pub fn main(init: std.process.Init) !void { &.{ "test/hxcases/cases.jsonl", "test/hxcases/parity.jsonl" } else &.{ "test/hxcases/cases.jsonl", "test/hxcases/goldens.jsonl", "test/hxcases/waivers.jsonl" }; - if (parity) waivers_path = waivers_path orelse "test/hxcases/parity-waivers.jsonl"; + // the pty pane's differences from the file pane; the library has none + if (parity and actual_path == null) waivers_path = waivers_path orelse "test/hxcases/parity-waivers.jsonl"; @memcpy(paths[0..defaults.len], defaults); n_paths = defaults.len; } @@ -135,6 +142,17 @@ pub fn main(init: std.process.Init) !void { var n_cases: usize = 0; var n_bad: usize = 0; var n_waived: usize = 0; + var n_skipped: usize = 0; + var actuals: std.StringHashMap([]const u8) = .init(arena); + if (actual_path) |ap| { + const asrc = std.Io.Dir.cwd().readFileAlloc(init.io, ap, arena, .limited(256 * 1024 * 1024)) catch |e| fatal("read {s}: {s}", .{ ap, @errorName(e) }); + var alines = std.mem.splitScalar(u8, asrc, '\n'); + while (alines.next()) |line| { + if (line.len == 0) continue; + const named = std.json.parseFromSliceLeaky(struct { name: []const u8 }, arena, line, .{ .ignore_unknown_fields = true }) catch |e| fatal("{s}: bad result line: {s}", .{ ap, @errorName(e) }); + try actuals.put(named.name, line); + } + } var names: std.StringHashMap(void) = .init(arena); for (case_paths) |path| { const src = std.Io.Dir.cwd().readFileAlloc(init.io, path, arena, .limited(64 * 1024 * 1024)) catch |e| fatal("read {s}: {s}", .{ path, @errorName(e) }); @@ -155,7 +173,17 @@ pub fn main(init: std.process.Init) !void { goldens.get(case.name) orelse fatal("no golden for case {s}", .{case.name}) else null; - const actual = try runCase(arena, case, if (parity) "tty" else pane_override orelse case.pane); + const actual = if (actual_path) |ap| other: { + const result = actuals.get(case.name) orelse fatal("{s}: no result for case {s}", .{ ap, case.name }); + // a case the driver stopped in: where the app's part began + const skip = std.json.parseFromSliceLeaky(struct { skip: ?[]const u8 = null }, arena, result, .{ .ignore_unknown_fields = true }) catch fatal("{s}: bad result for {s}", .{ ap, case.name }); + if (skip.skip) |why| { + n_skipped += 1; + if (verbose) std.debug.print("SKIPPED {s}: {s}\n", .{ case.name, why }); + continue; + } + break :other std.json.parseFromSliceLeaky(Result, arena, result, .{}) catch fatal("{s}: bad result for {s}", .{ ap, case.name }); + } else try runCase(arena, case, if (parity) "tty" else pane_override orelse case.pane); try validateResult(actual); const want = reference orelse { const row = try std.json.Stringify.valueAlloc(arena, actual, .{ .emit_null_optional_fields = false }); @@ -165,7 +193,7 @@ pub fn main(init: std.process.Init) !void { }; try validateResult(want); const reference_name = if (parity) "file" else "helix"; - const actual_name = if (parity) "pty" else "pardes"; + const actual_name = if (actual_path != null) "lib" else if (parity) "pty" else "pardes"; const difference = try diffResult(arena, want, actual, reference_name, actual_name); if (difference.len == 0) { if (strict and waivers.contains(case.name)) fatal("stale waiver: {s}", .{case.name}); @@ -194,7 +222,8 @@ pub fn main(init: std.process.Init) !void { } } if (!parity and goldens_path == null) return; - std.debug.print("{s}: {d} cases, {d} mismatches, {d} waived\n", .{ if (parity) "hxparity" else "hxdiff", n_cases, n_bad, n_waived }); + std.debug.print("{s}{s}: {d} cases, {d} mismatches, {d} waived\n", .{ if (parity) "hxparity" else "hxdiff", if (actual_path != null) "-lib" else "", n_cases - n_skipped, n_bad, n_waived }); + if (actual_path != null) std.debug.print("{d} cases the library hands its caller (an action not the text's), not compared\n", .{n_skipped}); if (n_bad > 0) std.process.exit(1); } @@ -347,77 +376,7 @@ fn runCase(arena: std.mem.Allocator, case: Case, pane_kind: []const u8) !Result // keys: helix notation — printables verbatim, <...> specials var ki: usize = 0; - while (ki < case.keys.len) { - var key: pardes.Key = .{ .cp = 0 }; - if (case.keys[ki] == '<') { - const close = std.mem.indexOfScalarPos(u8, case.keys, ki, '>') orelse - fatal("case {s}: unterminated < in keys", .{case.name}); - var name = case.keys[ki + 1 .. close]; - ki = close + 1; - while (true) { - if (std.mem.startsWith(u8, name, "C-")) { - key.ctrl = true; - name = name[2..]; - } else if (std.mem.startsWith(u8, name, "A-")) { - key.alt = true; - name = name[2..]; - } else if (std.mem.startsWith(u8, name, "S-")) { - key.shift = true; - name = name[2..]; - } else break; - } - if (name.len == 1 or (name.len > 1 and name.len <= 4 and name[0] >= 0x80)) { - key.cp = std.unicode.utf8Decode(name) catch return error.InvalidKey; - if (!key.ctrl and !key.alt) key.text = name; - } else if (std.mem.eql(u8, name, "esc")) { - key.cp = pardes.Key.escape; - } else if (std.mem.eql(u8, name, "ret")) { - key.cp = pardes.Key.enter; - } else if (std.mem.eql(u8, name, "tab")) { - key.cp = pardes.Key.tab; - } else if (std.mem.eql(u8, name, "space")) { - key.cp = ' '; - if (!key.ctrl and !key.alt) key.text = " "; - } else if (std.mem.eql(u8, name, "backspace")) { - key.cp = pardes.Key.backspace; - } else if (std.mem.eql(u8, name, "del")) { - key.cp = pardes.Key.delete; - } else if (std.mem.eql(u8, name, "lt")) { - key.cp = '<'; - if (!key.ctrl and !key.alt) key.text = "<"; - } else if (std.mem.eql(u8, name, "gt")) { - key.cp = '>'; - if (!key.ctrl and !key.alt) key.text = ">"; - } else if (std.mem.eql(u8, name, "minus")) { - // helix spells Alt-'-' this way: a bare `-` after a modifier - // is a parse error in its own macro notation - key.cp = '-'; - if (!key.ctrl and !key.alt) key.text = "-"; - } else if (std.mem.eql(u8, name, "up")) { - key.cp = pardes.Key.up; - } else if (std.mem.eql(u8, name, "down")) { - key.cp = pardes.Key.down; - } else if (std.mem.eql(u8, name, "left")) { - key.cp = pardes.Key.left; - } else if (std.mem.eql(u8, name, "right")) { - key.cp = pardes.Key.right; - } else if (std.mem.eql(u8, name, "home")) { - key.cp = pardes.Key.home; - } else if (std.mem.eql(u8, name, "end")) { - key.cp = pardes.Key.end; - } else if (std.mem.eql(u8, name, "pageup")) { - key.cp = pardes.Key.page_up; - } else if (std.mem.eql(u8, name, "pagedown")) { - key.cp = pardes.Key.page_down; - } else fatal("case {s}: unknown key <{s}>", .{ case.name, name }); - } else { - const len = std.unicode.utf8ByteSequenceLength(case.keys[ki]) catch return error.InvalidKey; - if (len > case.keys.len - ki) return error.InvalidKey; - const text = case.keys[ki..][0..len]; - key.cp = std.unicode.utf8Decode(text) catch return error.InvalidKey; - key.text = text; - ki += len; - } + while (hxkeys.next(pardes.Key, case.keys, &ki) catch |err| fatal("case {s}: {s}", .{ case.name, @errorName(err) })) |key| { core.update(.{ .key = key }); pump(core); } -- cgit v1.3