From cf347f91ee838759dc44ed34ec6ff320186e90ce Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 01:44:36 -0300 Subject: One verb table: the flags, the console's, the script's and ctl's words are rows Six hand-kept lists said one vocabulary: root.zig's parser, its usage and its header; the console's completion list, its help strings and the execute chain; Host.zig's list of lines that do not move State; the script's header comment and its step chain; control.zig's names and its magic node numbers. Now each is a table, and the parser, the help, the completion and the dispatch read it. - root.zig: `Flag`, an enum, and `flags`, a row each (arg, doc): the loop looks the word up, takes its argument when the row names one, and a switch sets what it means; `usage` is made from the rows at compile time (the same text, and a line for --help). Errors say the flag and what it needs. - Console.zig: `Verb` and `verbs`, a row each (args, doc, moves_state): execute is a switch on the verb, a line whose first word is none goes to the sketch as before; Tab completes from the rows (a verb, and with each lowercase word its first argument may be: `seek start`); `help` prints the rows wrapped to the log's width, `help VERB` its line. - Host.zig: movesState asks the row; a sketch's command moves State. - script.zig: `Verb` and `verbs` (args, doc, frame, input): step is a switch, a row with `frame` is followed by one; the input verbs are `input`, which a 9P ctl will call live. - control.zig: `File` and `files` (mode, doc), a node being its number (the same numbers); `Verb` and `verbs` for ctl's own two words. Behaviour is unchanged but for help's text, completion's list (the same words, and `timing`, `play`, `seek`... alone) and errors' words. The diff adds more than it deletes (+630 -521): the rows carry a line of doc for every verb, which nothing had before. Tests: test-control, test-timing, test-setup, test-same, test-kill, test-past, test-import, test-paths: 70/70 steps, 29/29 tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/host/script.zig | 474 +++++++++++++++++++++++++++------------------------- 1 file changed, 244 insertions(+), 230 deletions(-) (limited to 'src/host/script.zig') diff --git a/src/host/script.zig b/src/host/script.zig index a559d76..e3976e0 100644 --- a/src/host/script.zig +++ b/src/host/script.zig @@ -1,55 +1,9 @@ //! End-to-end scripts: plain text that plays a sketch like a person would, -//! through the same routing and frames, on a fixed 60 Hz clock. -//! -//! # comment -//! click 710 320 move there, press and release the left button -//! move|down|up X Y one pointer event, then a frame -//! wheel|wheel-x X Y DELTA -//! cancel what losing focus sends -//! key NAME press and release a key (`sketch.Key` names) -//! type TEXT text input, as typed into a focused field -//! console LINE run a console command (`console get` -//! takes no frame: it only reads) -//! frames N advance N frames without input -//! expect FIELD VALUE compare a state field (enums by name or number); -//! FIELD may index arrays and vector lanes, -//! `boids.buffer[3].pos[0]` -//! expect FIELD VALUE ~T numbers within T of VALUE -//! await FIELD VALUE [~T] run frames until it holds (a minute at most) -//! print FIELD write a field's value to stderr (a vector, -//! array or struct: every value in it), no frame -//! expect-snapshot NAME live state equals the newest mark NAME, leaf -//! by leaf (padding ignored, and leaves a layout -//! change reset), exactly -//! expect-key WHY FIELD VALUE -//! the newest keyframe of this session taken for -//! WHY (`Sessions.Why`: before_reload, mark...) -//! holds VALUE, read in its own layout -//! expect-log TEXT a recent console line contains TEXT -//! expect-no-log TEXT no recent console line does -//! expect-audio KEY stream KEY has received samples, or voice -//! KEY sounded since the last check of it -//! expect-no-audio KEY voice KEY did not sound since the last check -//! screenshot FILE save the next frame as PNG -//! expect-same-screenshot A B [MAX [X Y W H]] -//! two saved PNGs match, every channel within MAX -//! (default 0), in the rect if given (pixels) -//! expect-different-screenshot A B [X Y W H] -//! two saved PNGs differ somewhere (in the rect) -//! expect-pixel FILE X Y R G B A [TOL] -//! a saved PNG's pixel, every channel within TOL -//! load LIBRARY swap in another build of the sketch -//! reload load the watched library again -//! await-reload SECONDS run frames until a build replaces the library -//! kill die by SIGKILL, as a crash would -//! -//! `{name}` is replaced by `--define name=value` from the command line. -//! -//! Each line but `frames`, `print`, `console get`, the expectations and -//! `await-reload` is followed by one frame; `update` gets one call per -//! frame, live or replayed, so -//! `console play` then `frames N` replays N recorded frames at the -//! script's 60 Hz. +//! through the same routing and frames, on a fixed 60 Hz clock. A line is +//! a verb of `verbs` and its words; `# comment`; `{name}` is replaced by +//! `--define name=value` from the command line. `update` gets one call per +//! frame, live or replayed, so `console play` then `frames N` replays N +//! recorded frames at the script's 60 Hz. const std = @import("std"); const c = @import("c"); const abi = @import("abi"); @@ -63,6 +17,44 @@ pub const frame_ns: u64 = std.time.ns_per_s / 60; /// The most frames `await` plays: a minute. const await_limit = 60 * 60; +/// The script's verbs, a row each. A verb with `frame` is followed by one +/// frame; the `input` verbs are also what a 9P `ctl` takes live (`input`). +pub const Verb = enum { click, move, down, up, wheel, @"wheel-x", cancel, key, hold, release, type, console, frames, expect, await, print, @"expect-snapshot", @"expect-key", @"expect-log", @"expect-no-log", @"expect-audio", @"expect-no-audio", screenshot, @"expect-same-screenshot", @"expect-different-screenshot", @"expect-pixel", load, reload, @"await-reload", kill, quit }; +pub const Row = struct { args: []const u8 = "", doc: []const u8, frame: bool = false, input: bool = false }; +pub const verbs = std.enums.EnumArray(Verb, Row).init(.{ + .click = .{ .args = "X Y", .doc = "move there, press and release the left button", .frame = true, .input = true }, + .move = .{ .args = "X Y", .doc = "a pointer move", .frame = true, .input = true }, + .down = .{ .args = "X Y", .doc = "the left button down there", .frame = true, .input = true }, + .up = .{ .args = "X Y", .doc = "the left button up there", .frame = true, .input = true }, + .wheel = .{ .args = "X Y DELTA", .doc = "a wheel turn", .frame = true, .input = true }, + .@"wheel-x" = .{ .args = "X Y DELTA", .doc = "a sideways wheel turn", .frame = true, .input = true }, + .cancel = .{ .doc = "what losing focus sends", .frame = true, .input = true }, + .key = .{ .args = "NAME", .doc = "press and release a key (`sketch.Key` names)", .frame = true, .input = true }, + .hold = .{ .args = "NAME", .doc = "a key down until released, for modifiers", .frame = true, .input = true }, + .release = .{ .args = "NAME", .doc = "a held key up", .frame = true, .input = true }, + .type = .{ .args = "TEXT", .doc = "text input, as typed into a focused field", .frame = true, .input = true }, + .console = .{ .args = "LINE", .doc = "a console line (`console get` takes no frame: it only reads); one that fails fails the script", .frame = true }, + .frames = .{ .args = "N", .doc = "N frames without input" }, + .expect = .{ .args = "FIELD VALUE [~T]", .doc = "a State field holds VALUE (enums by name or number; numbers within T); FIELD may index arrays and vector lanes, `boids.buffer[3].pos[0]`" }, + .await = .{ .args = "FIELD VALUE [~T]", .doc = "frames until expect holds, a minute at most" }, + .print = .{ .args = "FIELD", .doc = "a field's value to stderr (a vector, array or struct: every value in it)" }, + .@"expect-snapshot" = .{ .args = "NAME", .doc = "live State equals the newest mark NAME, leaf by leaf (padding, and leaves a layout change reset, ignored)" }, + .@"expect-key" = .{ .args = "WHY FIELD VALUE", .doc = "the newest keyframe of this session taken for WHY (`Sessions.Why`: before_reload, mark...) holds VALUE, read in its own layout" }, + .@"expect-log" = .{ .args = "TEXT", .doc = "a recent console line contains TEXT" }, + .@"expect-no-log" = .{ .args = "TEXT", .doc = "no recent console line does" }, + .@"expect-audio" = .{ .args = "KEY", .doc = "stream KEY has received samples, or voice KEY sounded since the last check of it" }, + .@"expect-no-audio" = .{ .args = "KEY", .doc = "voice KEY did not sound since the last check" }, + .screenshot = .{ .args = "FILE", .doc = "save the next frame as PNG", .frame = true }, + .@"expect-same-screenshot" = .{ .args = "A B [MAX [X Y W H]]", .doc = "two saved PNGs match, every channel within MAX (0), in the rect if given (pixels)" }, + .@"expect-different-screenshot" = .{ .args = "A B [X Y W H]", .doc = "two saved PNGs differ somewhere (in the rect)" }, + .@"expect-pixel" = .{ .args = "FILE X Y R G B A [TOL]", .doc = "a saved PNG's pixel, every channel within TOL" }, + .load = .{ .args = "LIBRARY", .doc = "swap in another build of the sketch", .frame = true }, + .reload = .{ .doc = "load the watched library again", .frame = true }, + .@"await-reload" = .{ .args = "SECONDS", .doc = "frames until a build replaces the library" }, + .kill = .{ .doc = "die by SIGKILL, as a crash would" }, + .quit = .{ .doc = "end the run" }, +}); + pub fn run(host: *Host, source: []const u8, defines: []const [2][]const u8) !void { var arena: std.heap.ArenaAllocator = .init(host.gpa); defer arena.deinit(); @@ -75,9 +67,9 @@ pub fn run(host: *Host, source: []const u8, defines: []const [2][]const u8) !voi const line = try substitute(arena.allocator(), std.mem.trim(u8, raw, " \t\r"), defines); if (line.len == 0 or line[0] == '#') continue; var words = std.mem.tokenizeAny(u8, line, " \t"); - const command = words.next().?; + const verb = std.meta.stringToEnum(Verb, words.next().?); const rest = std.mem.trim(u8, words.rest(), " \t"); - step(host, arena.allocator(), command, rest, &words, &now) catch |err| { + (if (verb) |v| step(host, arena.allocator(), v, rest, &words, &now) else error.UnknownScriptCommand) catch |err| { failures += 1; std.debug.print("script:{d}: {s}: {t}\n", .{ number, line, err }); if (err != error.ExpectationFailed) break; @@ -87,188 +79,210 @@ pub fn run(host: *Host, source: []const u8, defines: []const [2][]const u8) !voi if (failures > 0) return error.ScriptFailed; } -fn step(host: *Host, arena: std.mem.Allocator, command: []const u8, rest: []const u8, words: anytype, now: *u64) !void { - const eq = std.mem.eql; - if (eq(u8, command, "click")) { - const x, const y = try point(words); - try host.route(.{ .kind = .mouse_move, .time = now.*, .x = x, .y = y, .flags = held(host) }); - try host.route(.{ .kind = .mouse_down, .time = now.*, .x = x, .y = y, .code = 1, .flags = held(host) }); - try host.route(.{ .kind = .mouse_up, .time = now.*, .x = x, .y = y, .code = 1, .flags = held(host) }); - } else if (eq(u8, command, "move") or eq(u8, command, "down") or eq(u8, command, "up")) { - const x, const y = try point(words); - const kind: abi.Event.Kind = if (eq(u8, command, "move")) .mouse_move else if (eq(u8, command, "down")) .mouse_down else .mouse_up; - try host.route(.{ .kind = kind, .time = now.*, .x = x, .y = y, .code = if (kind == .mouse_move) 0 else 1, .flags = held(host) }); - } else if (eq(u8, command, "wheel") or eq(u8, command, "wheel-x")) { - const x, const y = try point(words); - const delta = try std.fmt.parseFloat(f32, words.next() orelse return error.ExpectedDelta); - try host.route(.{ .kind = if (eq(u8, command, "wheel-x")) .wheel_horizontal else .wheel, .time = now.*, .x = x, .y = y, .value = @bitCast(delta), .flags = held(host) }); - } else if (eq(u8, command, "cancel")) { - try host.route(.{ .kind = .cancel, .time = now.* }); - } else if (eq(u8, command, "key")) { - const key = std.meta.stringToEnum(abi.Key, rest) orelse return error.UnknownKey; - try host.route(.{ .kind = .key_down, .time = now.*, .code = @backingInt(key) }); - try host.route(.{ .kind = .key_up, .time = now.*, .code = @backingInt(key) }); - } else if (eq(u8, command, "hold") or eq(u8, command, "release")) { - const key = std.meta.stringToEnum(abi.Key, rest) orelse return error.UnknownKey; - try host.route(.{ .kind = if (eq(u8, command, "hold")) .key_down else .key_up, .time = now.*, .code = @backingInt(key) }); - } else if (eq(u8, command, "type")) { - for (rest) |byte| try host.route(.{ .kind = .text, .time = now.*, .value = byte }); - } else if (eq(u8, command, "console")) { - // A failing console line fails the script, like a failed expect. - host.runLine(rest, now.*) catch |err| { - std.debug.print(" console {s}: {t}\n", .{ rest, err }); - return err; - }; - if (std.mem.startsWith(u8, rest, "get ")) return; - } else if (eq(u8, command, "print")) { - const path = words.next() orelse return error.ExpectedField; - var buf: [4096]u8 = undefined; - var w: std.Io.Writer = .fixed(&buf); - fields.formatPath(host.state.layout, host.live(), path, &w) catch |err| switch (err) { - error.UnknownField => return error.UnknownField, - error.WriteFailed => {}, - }; - std.debug.print("{s} = {s}\n", .{ path, w.buffered() }); - return; - } else if (eq(u8, command, "frames")) { - const count = try std.fmt.parseInt(usize, rest, 10); - for (0..count) |_| try advance(host, now); - return; - } else if (eq(u8, command, "expect") or eq(u8, command, "await")) { - const path = words.next() orelse return error.ExpectedField; - const want, const tolerance = try expected(std.mem.trim(u8, words.rest(), " \t")); - // Looked up again after each frame: a reload may move the field. - _ = host.state.layout.find(path) orelse return error.UnknownField; - // `await` plays frames until the field holds the value, for up to a - // minute: for waits whose length is the sketch's business. - if (eq(u8, command, "await")) { - var waited: usize = 0; - while (!holds(host, path, want, tolerance) and waited < await_limit) : (waited += 1) try advance(host, now); - } - if (!holds(host, path, want, tolerance)) { - var buf: [128]u8 = undefined; +/// Routes an input verb's events at `now`, no frame after: a script's +/// line, or a 9P `ctl` line live. +pub fn input(host: *Host, verb: Verb, words: *std.mem.TokenIterator(u8, .any), now: u64) !void { + const rest = std.mem.trim(u8, words.rest(), " \t"); + switch (verb) { + .click => { + const x, const y = try point(words); + try host.route(.{ .kind = .mouse_move, .time = now, .x = x, .y = y, .flags = held(host) }); + try host.route(.{ .kind = .mouse_down, .time = now, .x = x, .y = y, .code = 1, .flags = held(host) }); + try host.route(.{ .kind = .mouse_up, .time = now, .x = x, .y = y, .code = 1, .flags = held(host) }); + }, + .move, .down, .up => { + const x, const y = try point(words); + const kind: abi.Event.Kind = switch (verb) { + .move => .mouse_move, + .down => .mouse_down, + else => .mouse_up, + }; + try host.route(.{ .kind = kind, .time = now, .x = x, .y = y, .code = if (kind == .mouse_move) 0 else 1, .flags = held(host) }); + }, + .wheel, .@"wheel-x" => { + const x, const y = try point(words); + const delta = try std.fmt.parseFloat(f32, words.next() orelse return error.ExpectedDelta); + try host.route(.{ .kind = if (verb == .@"wheel-x") .wheel_horizontal else .wheel, .time = now, .x = x, .y = y, .value = @bitCast(delta), .flags = held(host) }); + }, + .cancel => try host.route(.{ .kind = .cancel, .time = now }), + .key => { + const key = std.meta.stringToEnum(abi.Key, rest) orelse return error.UnknownKey; + try host.route(.{ .kind = .key_down, .time = now, .code = @backingInt(key) }); + try host.route(.{ .kind = .key_up, .time = now, .code = @backingInt(key) }); + }, + .hold, .release => { + const key = std.meta.stringToEnum(abi.Key, rest) orelse return error.UnknownKey; + try host.route(.{ .kind = if (verb == .hold) .key_down else .key_up, .time = now, .code = @backingInt(key) }); + }, + .type => for (rest) |byte| try host.route(.{ .kind = .text, .time = now, .value = byte }), + else => return error.NotInput, + } +} + +fn step(host: *Host, arena: std.mem.Allocator, verb: Verb, rest: []const u8, words: *std.mem.TokenIterator(u8, .any), now: *u64) !void { + switch (verb) { + .click, .move, .down, .up, .wheel, .@"wheel-x", .cancel, .key, .hold, .release, .type => try input(host, verb, words, now.*), + .console => { + // A failing console line fails the script, like a failed expect. + host.runLine(rest, now.*) catch |err| { + std.debug.print(" console {s}: {t}\n", .{ rest, err }); + return err; + }; + if (std.mem.startsWith(u8, rest, "get ")) return; + }, + .print => { + const path = words.next() orelse return error.ExpectedField; + var buf: [4096]u8 = undefined; var w: std.Io.Writer = .fixed(&buf); fields.formatPath(host.state.layout, host.live(), path, &w) catch |err| switch (err) { - error.UnknownField => w.writeAll("no such field") catch {}, + error.UnknownField => return error.UnknownField, error.WriteFailed => {}, }; - if (tolerance) |t| { - std.debug.print(" {s}: expected {s} ~{d}, found {s}\n", .{ path, want, t, w.buffered() }); - } else std.debug.print(" {s}: expected {s}, found {s}\n", .{ path, want, w.buffered() }); - return error.ExpectationFailed; - } - return; - } else if (eq(u8, command, "expect-snapshot")) { - const saved = try Console.markState(host, arena, rest); - if (!same(host, saved.layout, saved.bytes, rest)) return error.ExpectationFailed; - return; - } else if (eq(u8, command, "expect-key")) { - const why = std.meta.stringToEnum(Sessions.Why, words.next() orelse "") orelse return error.UnknownWhy; - const path = words.next() orelse return error.ExpectedField; - const want = std.mem.trim(u8, words.rest(), " \t"); - const s = &host.sessions; - try s.commit(.none); - var k = s.keys; - while (k > 0) { - k -= 1; - const key = try s.load(s.session, k); - defer key.free(host.gpa); - if (key.why != why) continue; - const leaf = (try memory.layoutOf(arena, key.layout)).find(path) orelse return error.UnknownField; - if (fields.matches(leaf, key.state, want)) return; - var buf: [128]u8 = undefined; - var w: std.Io.Writer = .fixed(&buf); - fields.format(leaf, key.state, &w) catch {}; - std.debug.print(" {s} keyframe {d}: {s} expected {s}, found {s}\n", .{ @tagName(why), k, path, want, w.buffered() }); + std.debug.print("{s} = {s}\n", .{ path, w.buffered() }); + }, + .frames => { + const count = try std.fmt.parseInt(usize, rest, 10); + for (0..count) |_| try advance(host, now); + }, + .expect, .await => { + const path = words.next() orelse return error.ExpectedField; + const want, const tolerance = try expected(std.mem.trim(u8, words.rest(), " \t")); + // Looked up again after each frame: a reload may move the field. + _ = host.state.layout.find(path) orelse return error.UnknownField; + // `await` plays frames until the field holds the value, for up to a + // minute: for waits whose length is the sketch's business. + if (verb == .await) { + var waited: usize = 0; + while (!holds(host, path, want, tolerance) and waited < await_limit) : (waited += 1) try advance(host, now); + } + if (!holds(host, path, want, tolerance)) { + var buf: [128]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + fields.formatPath(host.state.layout, host.live(), path, &w) catch |err| switch (err) { + error.UnknownField => w.writeAll("no such field") catch {}, + error.WriteFailed => {}, + }; + if (tolerance) |t| { + std.debug.print(" {s}: expected {s} ~{d}, found {s}\n", .{ path, want, t, w.buffered() }); + } else std.debug.print(" {s}: expected {s}, found {s}\n", .{ path, want, w.buffered() }); + return error.ExpectationFailed; + } + }, + .@"expect-snapshot" => { + const saved = try Console.markState(host, arena, rest); + if (!same(host, saved.layout, saved.bytes, rest)) return error.ExpectationFailed; + }, + .@"expect-key" => { + const why = std.meta.stringToEnum(Sessions.Why, words.next() orelse "") orelse return error.UnknownWhy; + const path = words.next() orelse return error.ExpectedField; + const want = std.mem.trim(u8, words.rest(), " \t"); + const s = &host.sessions; + try s.commit(.none); + var k = s.keys; + while (k > 0) { + k -= 1; + const key = try s.load(s.session, k); + defer key.free(host.gpa); + if (key.why != why) continue; + const leaf = (try memory.layoutOf(arena, key.layout)).find(path) orelse return error.UnknownField; + if (fields.matches(leaf, key.state, want)) return; + var buf: [128]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + fields.format(leaf, key.state, &w) catch {}; + std.debug.print(" {s} keyframe {d}: {s} expected {s}, found {s}\n", .{ @tagName(why), k, path, want, w.buffered() }); + return error.ExpectationFailed; + } + std.debug.print(" no {s} keyframe in this session\n", .{@tagName(why)}); return error.ExpectationFailed; - } - std.debug.print(" no {s} keyframe in this session\n", .{@tagName(why)}); - return error.ExpectationFailed; - } else if (eq(u8, command, "expect-log") or eq(u8, command, "expect-no-log")) { - var found = false; - for (&host.console.lines) |*l| { - if (std.mem.indexOf(u8, l.bytes[0..l.len], rest) != null) found = true; - } - if (found != eq(u8, command, "expect-log")) { - std.debug.print(" console log:\n", .{}); - for (&host.console.lines) |*l| std.debug.print(" {s}\n", .{l.bytes[0..l.len]}); - return error.ExpectationFailed; - } - return; - } else if (eq(u8, command, "expect-same-screenshot") or eq(u8, command, "expect-different-screenshot")) { - const a = words.next() orelse return error.ExpectedFile; - const b = words.next() orelse return error.ExpectedFile; - const want_same = eq(u8, command, "expect-same-screenshot"); - const max: u8 = if (!want_same) 0 else if (words.next()) |m| std.fmt.parseInt(u8, m, 10) catch return error.InvalidValue else 0; - var rect: ?[4]usize = null; - if (words.next()) |x| { - var r: [4]usize = undefined; - r[0] = std.fmt.parseInt(usize, x, 10) catch return error.InvalidRect; - for (r[1..]) |*v| v.* = std.fmt.parseInt(usize, words.next() orelse return error.InvalidRect, 10) catch return error.InvalidRect; - rect = r; - } - const diff = try comparePng(arena, a, b, max, rect); - if ((diff.count == 0) != want_same) { - if (want_same) { - std.debug.print(" {s} vs {s}: {d} pixels differ by more than {d}, first at {d},{d}\n", .{ a, b, diff.count, max, diff.first[0], diff.first[1] }); - } else std.debug.print(" {s} and {s} are the same\n", .{ a, b }); - return error.ExpectationFailed; - } - return; - } else if (eq(u8, command, "expect-pixel")) { - const file = words.next() orelse return error.ExpectedFile; - var n: [7]u8 = .{ 0, 0, 0, 0, 0, 0, 0 }; - var x: usize = 0; - var y: usize = 0; - for (0..7) |i| { - const word = words.next() orelse if (i == 6) break else return error.ExpectedPixel; - if (i < 2) { - (if (i == 0) &x else &y).* = std.fmt.parseInt(usize, word, 10) catch return error.ExpectedPixel; - } else n[i - 2] = std.fmt.parseInt(u8, word, 0) catch return error.ExpectedPixel; - } - const tol = n[4]; - const surface = try loadPng(arena, file); - defer c.SDL_DestroySurface(surface); - if (x >= surface.*.w or y >= surface.*.h) return error.OutsideScreenshot; - const p = (@as([*]const u8, @ptrCast(surface.*.pixels.?)) + y * @as(usize, @intCast(surface.*.pitch)) + x * 4)[0..4]; - for (p, n[0..4]) |got, want| if (@max(got, want) - @min(got, want) > tol) { - std.debug.print(" {s} at {d},{d}: expected {d} {d} {d} {d}, found {d} {d} {d} {d}\n", .{ file, x, y, n[0], n[1], n[2], n[3], p[0], p[1], p[2], p[3] }); - return error.ExpectationFailed; - }; - return; - } else if (eq(u8, command, "expect-audio")) { - const key = try std.fmt.parseInt(u32, rest, 10); - if (host.input.stream(key).pushed == 0 and !host.audio.sounded(key)) return error.ExpectationFailed; - return; - } else if (eq(u8, command, "expect-no-audio")) { - if (host.audio.sounded(try std.fmt.parseInt(u32, rest, 10))) return error.ExpectationFailed; - return; - } else if (eq(u8, command, "screenshot")) { - if (std.fs.path.dirname(rest)) |dir| try std.Io.Dir.cwd().createDirPath(host.io, dir); - host.screenshot = try arena.dupeSentinel(u8, rest, 0); - } else if (eq(u8, command, "load")) { - try host.load(rest); - } else if (eq(u8, command, "reload")) { - try host.reload(); - } else if (eq(u8, command, "await-reload")) { - const seconds = try std.fmt.parseInt(u64, rest, 10); - const start = host.reloads; - const deadline = std.Io.Timestamp.now(host.io, .awake).nanoseconds + @as(i96, seconds) * std.time.ns_per_s; - while (host.reloads == start) { - if (std.Io.Timestamp.now(host.io, .awake).nanoseconds > deadline) return error.NoReload; - try advance(host, now); - try std.Io.sleep(host.io, .fromMilliseconds(10), .awake); - } - return; - } else if (eq(u8, command, "kill")) { - // A crash, for tests of what survives one: SIGKILL, no cleanup. - _ = std.os.linux.kill(std.os.linux.getpid(), .KILL); - return; - } else if (eq(u8, command, "quit")) { - host.quit = true; - return; - } else return error.UnknownScriptCommand; - try advance(host, now); + }, + .@"expect-log", .@"expect-no-log" => { + var found = false; + for (&host.console.lines) |*l| { + if (std.mem.indexOf(u8, l.bytes[0..l.len], rest) != null) found = true; + } + if (found != (verb == .@"expect-log")) { + std.debug.print(" console log:\n", .{}); + for (&host.console.lines) |*l| std.debug.print(" {s}\n", .{l.bytes[0..l.len]}); + return error.ExpectationFailed; + } + }, + .@"expect-same-screenshot", .@"expect-different-screenshot" => { + const a = words.next() orelse return error.ExpectedFile; + const b = words.next() orelse return error.ExpectedFile; + const want_same = verb == .@"expect-same-screenshot"; + const max: u8 = if (!want_same) 0 else if (words.next()) |m| std.fmt.parseInt(u8, m, 10) catch return error.InvalidValue else 0; + var rect: ?[4]usize = null; + if (words.next()) |x| { + var r: [4]usize = undefined; + r[0] = std.fmt.parseInt(usize, x, 10) catch return error.InvalidRect; + for (r[1..]) |*v| v.* = std.fmt.parseInt(usize, words.next() orelse return error.InvalidRect, 10) catch return error.InvalidRect; + rect = r; + } + const diff = try comparePng(arena, a, b, max, rect); + if ((diff.count == 0) != want_same) { + if (want_same) { + std.debug.print(" {s} vs {s}: {d} pixels differ by more than {d}, first at {d},{d}\n", .{ a, b, diff.count, max, diff.first[0], diff.first[1] }); + } else std.debug.print(" {s} and {s} are the same\n", .{ a, b }); + return error.ExpectationFailed; + } + }, + .@"expect-pixel" => { + const file = words.next() orelse return error.ExpectedFile; + var n: [7]u8 = .{ 0, 0, 0, 0, 0, 0, 0 }; + var x: usize = 0; + var y: usize = 0; + for (0..7) |i| { + const word = words.next() orelse if (i == 6) break else return error.ExpectedPixel; + if (i < 2) { + (if (i == 0) &x else &y).* = std.fmt.parseInt(usize, word, 10) catch return error.ExpectedPixel; + } else n[i - 2] = std.fmt.parseInt(u8, word, 0) catch return error.ExpectedPixel; + } + const tol = n[4]; + const surface = try loadPng(arena, file); + defer c.SDL_DestroySurface(surface); + if (x >= surface.*.w or y >= surface.*.h) return error.OutsideScreenshot; + const p = (@as([*]const u8, @ptrCast(surface.*.pixels.?)) + y * @as(usize, @intCast(surface.*.pitch)) + x * 4)[0..4]; + for (p, n[0..4]) |got, want| if (@max(got, want) - @min(got, want) > tol) { + std.debug.print(" {s} at {d},{d}: expected {d} {d} {d} {d}, found {d} {d} {d} {d}\n", .{ file, x, y, n[0], n[1], n[2], n[3], p[0], p[1], p[2], p[3] }); + return error.ExpectationFailed; + }; + }, + .@"expect-audio" => { + const key = try std.fmt.parseInt(u32, rest, 10); + if (host.input.stream(key).pushed == 0 and !host.audio.sounded(key)) return error.ExpectationFailed; + }, + .@"expect-no-audio" => { + if (host.audio.sounded(try std.fmt.parseInt(u32, rest, 10))) return error.ExpectationFailed; + }, + .screenshot => { + if (std.fs.path.dirname(rest)) |dir| try std.Io.Dir.cwd().createDirPath(host.io, dir); + host.screenshot = try arena.dupeSentinel(u8, rest, 0); + }, + .load => { + try host.load(rest); + }, + .reload => { + try host.reload(); + }, + .@"await-reload" => { + const seconds = try std.fmt.parseInt(u64, rest, 10); + const start = host.reloads; + const deadline = std.Io.Timestamp.now(host.io, .awake).nanoseconds + @as(i96, seconds) * std.time.ns_per_s; + while (host.reloads == start) { + if (std.Io.Timestamp.now(host.io, .awake).nanoseconds > deadline) return error.NoReload; + try advance(host, now); + try std.Io.sleep(host.io, .fromMilliseconds(10), .awake); + } + }, + .kill => { + // A crash, for tests of what survives one: SIGKILL, no cleanup. + _ = std.os.linux.kill(std.os.linux.getpid(), .KILL); + }, + .quit => { + host.quit = true; + }, + } + if (verbs.get(verb).frame) try advance(host, now); } /// The modifiers `hold` has down, for a pointer event's `flags`. -- cgit v1.3