//! Template syntax (M1-TPL-005, 006, 007, 008, 012). //! //! A template is literal text, `{identifier}` placeholders, and `{{` or `}}` //! for literal braces. That is the whole language: M1-TPL-007 excludes //! conditions, defaults, formatting, filters and nesting, so anything between //! braces that is not a bare identifier is an error rather than a feature. //! //! Escaping is resolved during this parse, never during rendering, which is //! what makes M1-TPL-006's "inserted once, never recursively interpreted" //! structural: `{{amount}}` becomes literal text here, so no later stage has //! braces left to rescan. //! //! M1-TPL-012 is satisfied by absence. Accounts are stored as written by //! config/rules.zig and nothing calls this module on them, so a template //! cannot construct an account name. const std = @import("std"); const diag = @import("diag.zig"); const Lexer = @import("expr/lexer.zig").Lexer; pub const Error = error{ InvalidTemplate, OutOfMemory }; /// M1-TPL-004's three names, reserved against aliases by M1-TPL-006. pub const Builtin = enum { physical_row, rule_name, source_filename, pub fn name(self: Builtin) []const u8 { return switch (self) { .source_filename => "sourceFilename", .physical_row => "physicalRow", .rule_name => "ruleName", }; } pub fn parse(text: []const u8) ?Builtin { if (std.mem.eql(u8, text, "sourceFilename")) return .source_filename; if (std.mem.eql(u8, text, "physicalRow")) return .physical_row; if (std.mem.eql(u8, text, "ruleName")) return .rule_name; return null; } }; /// M1-TPL-006: a field alias may not take one of these names. pub fn isReserved(name: []const u8) bool { return Builtin.parse(name) != null; } pub const Segment = union(enum) { builtin: Builtin, /// A declared field alias. field: []const u8, /// Text to emit as-is, with brace escapes already resolved. literal: []const u8, }; pub const Template = struct { segments: []const Segment, }; /// Where the template came from, for diagnostics (M1-DIAG-002). pub const Context = struct { file: []const u8, key_path: []const u8, }; /// Parses a template. `known` is the set of declared field aliases; a /// placeholder naming anything else is rejected (M1-TPL-008). pub fn parse( arena: std.mem.Allocator, source: []const u8, known: []const []const u8, context: Context, diags: *diag.List, ) Error!Template { var segments: std.ArrayList(Segment) = .empty; var literal: std.ArrayList(u8) = .empty; var i: usize = 0; while (i < source.len) { switch (source[i]) { '{' => { if (i + 1 < source.len and source[i + 1] == '{') { try literal.append(arena, '{'); i += 2; continue; } const close = std.mem.findScalarPos(u8, source, i + 1, '}') orelse return fail(diags, context, "unmatched '{{' at byte {d}", .{i}); const name = source[i + 1 .. close]; // M1-TPL-005 and M1-TPL-007: a bare identifier, nothing else. // This is where "{ field }", "{a|upper}" and "{}" are refused. if (!Lexer.isIdentifier(name)) { return fail( diags, context, "'{s}' is not a valid placeholder name", .{name}, ); } try flush(arena, &segments, &literal); if (Builtin.parse(name)) |builtin| { try segments.append(arena, .{ .builtin = builtin }); } else if (contains(known, name)) { try segments.append(arena, .{ .field = name }); } else { return fail( diags, context, "'{s}' is not a declared field alias", .{name}, ); } i = close + 1; }, '}' => { if (i + 1 < source.len and source[i + 1] == '}') { try literal.append(arena, '}'); i += 2; continue; } return fail(diags, context, "unmatched '}}' at byte {d}", .{i}); }, else => { try literal.append(arena, source[i]); i += 1; }, } } try flush(arena, &segments, &literal); return .{ .segments = segments.items }; } fn flush( arena: std.mem.Allocator, segments: *std.ArrayList(Segment), literal: *std.ArrayList(u8), ) Error!void { if (literal.items.len == 0) return; try segments.append(arena, .{ .literal = literal.items }); // The old buffer stays valid: it belongs to the arena. literal.* = .empty; } fn contains(names: []const []const u8, name: []const u8) bool { for (names) |candidate| { if (std.mem.eql(u8, candidate, name)) return true; } return false; } fn fail( diags: *diag.List, context: Context, comptime fmt: []const u8, args: anytype, ) Error { diags.addConfig( .{ .file = context.file, .key_path = context.key_path }, fmt, args, ) catch return error.OutOfMemory; return error.InvalidTemplate; } // -- rendering --------------------------------------------------------------- pub const RenderError = error{ RenderFailed, OutOfMemory }; /// One decoded cell, addressed by the alias the profile gave its column. pub const Field = struct { alias: []const u8, value: []const u8, }; /// Everything a template can name for one row (M1-TPL-003, M1-TPL-004). pub const Values = struct { fields: []const Field, physical_row: usize, /// The exact name of the one rule that matched, which is why M1-TPL-009 /// renders only after classification has settled. rule_name: []const u8, /// The discovered lexical CSV path relative to inputPath, using '/' /// separators even when the CSV is a symlink. source_filename: []const u8, fn lookup(self: Values, alias: []const u8) ?[]const u8 { for (self.fields) |field| { if (std.mem.eql(u8, field.alias, alias)) return field.value; } return null; } }; /// Which hledger context the result lands in. It decides what M1-TPL-011 /// admits, and how M1-TPL-010 treats an empty result. pub const Kind = enum { comment, narration }; /// One rendering: the template, the row's values, the kind of text it /// produces, and the configuration key to blame if it fails. /// /// M1-TPL-009: construct one only after exactly one rule has matched. pub const Request = struct { key_path: []const u8, kind: Kind, template: Template, values: Values, /// The rendered text, or a failure with the reason in `diags`. An empty /// narration is an error; an empty comment is returned as the empty /// string, for the caller to omit the comment syntax entirely /// (M1-TPL-010). pub fn render( self: Request, arena: std.mem.Allocator, diags: *diag.List, ) RenderError![]const u8 { var out: std.ArrayList(u8) = .empty; for (self.template.segments) |segment| switch (segment) { .literal => |text| try out.appendSlice(arena, text), .builtin => |builtin| switch (builtin) { .source_filename => try out.appendSlice(arena, self.values.source_filename), .physical_row => { var buffer: [20]u8 = undefined; // zlinter-disable-next-line no_swallow_error - a usize is at most 20 digits, the buffer holds exactly that const text = std.mem.print(&buffer, "{d}", .{self.values.physical_row}) catch unreachable; try out.appendSlice(arena, text); }, .rule_name => try out.appendSlice(arena, self.values.rule_name), }, // M1-TPL-003: the exact decoded cell, with no trimming and no date, // amount or currency reformatting. .field => |alias| { const cell = self.values.lookup(alias) orelse return self.failRow( diags, "placeholder '{s}' has no column in this row", .{alias}, ); try out.appendSlice(arena, cell); }, }; const rendered = out.items; // M1-TPL-011: fail rather than substituting or discarding a character. if (self.offendingByte(rendered)) |offset| { return self.failRow( diags, "byte 0x{X:0>2} at offset {d} has no lossless representation in a {s}", .{ rendered[offset], offset, @tagName(self.kind) }, ); } // M1-TPL-010: an empty narration prevents generation; an empty comment is // simply not emitted, which is the caller's job. if (self.kind == .narration and rendered.len == 0) { return self.failRow(diags, "narration renders empty", .{}); } return rendered; } /// The conservative reject set from DESIGN_M1.md D4, applied to the whole /// rendered string: a semicolon written literally in a template would start a /// comment just as surely as one arriving from a cell. fn offendingByte(self: Request, text: []const u8) ?usize { // A narration that begins with one of these is read by hledger as a // status mark or a transaction code, not as the start of the description, // so the value would not survive the round trip. if (self.kind == .narration and text.len > 0) { switch (text[0]) { '*', '!', '(' => return 0, else => {}, } // hledger 1.52.3 strips whitespace from both ends of a description -- // verified against the binary, not assumed -- so a narration with any // has no lossless representation and M1-TPL-011 says to fail here. // Interior whitespace is kept, and is fine. if (isBlank(text[0])) return 0; if (isBlank(text[text.len - 1])) return text.len - 1; } for (text, 0..) |byte, offset| { const rejected = switch (self.kind) { // ';' opens a comment, '|' splits payee from note, and any C0 // control breaks the record. .narration => byte == ';' or byte == '|' or byte < 0x20, // A comment already runs to end of line, so only a line break // ends it early. Tabs stay: M1-TPL-010 forbids trimming. .comment => byte == '\n' or byte == '\r', }; if (rejected) return offset; } return null; } fn isBlank(byte: u8) bool { return byte == ' ' or byte == '\t'; } /// M1-TPL-009 wants the template, the source file and the physical row. fn failRow( self: Request, diags: *diag.List, comptime fmt: []const u8, args: anytype, ) RenderError { diags.addFmt(fmt, args, .{ .file = self.values.source_filename, .physical_row = self.values.physical_row, .key_path = self.key_path, }) catch return error.OutOfMemory; return error.RenderFailed; } }; // -- tests ------------------------------------------------------------------- const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; const expectEqualStrings = std.testing.expectEqualStrings; const expectError = std.testing.expectError; const aliases: []const []const u8 = &.{ "posting", "amount", "comment" }; fn parseWith(arena: *std.heap.ArenaAllocator, source: []const u8) Error!Template { const alloc = arena.allocator(); const diags = diag.List.create(alloc) catch return error.OutOfMemory; return parse(alloc, source, aliases, .{ .file = "project.yaml", .key_path = "profile.transaction.narrationTemplate", }, diags); } fn expectRejected(source: []const u8) !void { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); try expectError(error.InvalidTemplate, parseWith(&arena, source)); } test "M1-TPL-005: literal text is one segment" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const template = try parseWith(&arena, "just text"); try expectEqual(@as(usize, 1), template.segments.len); try expectEqualStrings("just text", template.segments[0].literal); } test "M1-TPL-005: a placeholder names a declared alias" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const template = try parseWith(&arena, "a {posting} b"); try expectEqual(@as(usize, 3), template.segments.len); try expectEqualStrings("a ", template.segments[0].literal); try expectEqualStrings("posting", template.segments[1].field); try expectEqualStrings(" b", template.segments[2].literal); } test "M1-TPL-005: doubled braces are literal braces" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const template = try parseWith(&arena, "{{literal}}"); try expectEqual(@as(usize, 1), template.segments.len); try expectEqualStrings("{literal}", template.segments[0].literal); } test "M1-TPL-005: an unmatched brace is an error" { try expectRejected("a { b"); try expectRejected("a } b"); try expectRejected("{posting"); try expectRejected("posting}"); try expectRejected("{"); try expectRejected("}"); } test "M1-TPL-005: an unmatched brace diagnostic names the brace and the byte" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); // The format string escapes the very brace it reports, and nothing else // asserts that the escape survives. A diagnostic that loses the character // the reader has to go and find is worse than none. try expectEqualStrings("unmatched '{' at byte 2", try messageFor(&arena, "a { b")); try expectEqualStrings("unmatched '}' at byte 2", try messageFor(&arena, "a } b")); } test "M1-TPL-005: a placeholder identifier admits no whitespace" { try expectRejected("{ posting }"); try expectRejected("{posting }"); try expectRejected("{ posting}"); try expectRejected("{post ing}"); } test "M1-TPL-005: an empty or malformed identifier is an error" { try expectRejected("{}"); try expectRejected("{1posting}"); try expectRejected("{post-ing}"); try expectRejected("{post.ing}"); } test "M1-TPL-007: templates offer no filters, defaults or expressions" { try expectRejected("{posting|upper}"); try expectRejected("{posting:-none}"); try expectRejected("{posting?}"); try expectRejected("{posting[0]}"); } test "M1-TPL-006: the three built-in names are available" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const template = try parseWith(&arena, "{sourceFilename}:{physicalRow} {ruleName}"); try expectEqual(Builtin.source_filename, template.segments[0].builtin); try expectEqualStrings(":", template.segments[1].literal); try expectEqual(Builtin.physical_row, template.segments[2].builtin); try expectEqualStrings(" ", template.segments[3].literal); try expectEqual(Builtin.rule_name, template.segments[4].builtin); } test "M1-TPL-006: the built-in names are reserved for aliases" { try expect(isReserved("sourceFilename")); try expect(isReserved("physicalRow")); try expect(isReserved("ruleName")); try expect(!isReserved("posting")); try expect(!isReserved("sourcefilename")); } test "M1-AC-015: an unknown placeholder is rejected with a diagnostic" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); const diags = try diag.List.create(alloc); try expectError(error.InvalidTemplate, parse(alloc, "{unknown}", aliases, .{ .file = "project.yaml", .key_path = "profile.transaction.narrationTemplate", }, diags)); try expect(!diags.isEmpty()); try expectEqualStrings("project.yaml", diags.items.items[0].file.?); try expectEqualStrings( "profile.transaction.narrationTemplate", diags.items.items[0].key_path.?, ); } test "M1-AC-016: inserted and escaped text is not interpreted again" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); // The escape produces literal braces; nothing rescans them, so the // template holds no placeholder for "amount" here. const template = try parseWith(&arena, "{posting} {{amount}}"); try expectEqual(@as(usize, 2), template.segments.len); try expectEqualStrings("posting", template.segments[0].field); try expectEqualStrings(" {amount}", template.segments[1].literal); } test "M1-AC-013: the worked example parses into its parts" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const template = try parseWith( &arena, "{sourceFilename}:{physicalRow} {ruleName} {{source}} {posting}", ); try expectEqual(Builtin.source_filename, template.segments[0].builtin); try expectEqual(Builtin.physical_row, template.segments[2].builtin); try expectEqual(Builtin.rule_name, template.segments[4].builtin); try expectEqualStrings(" {source} ", template.segments[5].literal); try expectEqualStrings("posting", template.segments[6].field); } fn messageFor(arena: *std.heap.ArenaAllocator, source: []const u8) ![]const u8 { const alloc = arena.allocator(); const diags = try diag.List.create(alloc); // zlinter-disable-next-line no_swallow_error - test-only: the diagnostic is what this asserts on _ = parse(alloc, source, aliases, .{ .file = "project.yaml", .key_path = "profile.transaction.narrationTemplate", }, diags) catch {}; return diags.items.items[0].message; } test "M1-TPL-008: a malformed name and an unknown name are reported differently" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); // Both are errors, but they are different mistakes and the diagnostic has // to say which. Whitespace inside the braces is a syntax problem; it // should not send the reader looking for a missing alias. const malformed = try messageFor(&arena, "{ posting }"); try expect(std.mem.find(u8, malformed, "placeholder name") != null); const unknown = try messageFor(&arena, "{unknown}"); try expect(std.mem.find(u8, unknown, "field alias") != null); } test "an empty template is valid and has no segments" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const template = try parseWith(&arena, ""); try expectEqual(@as(usize, 0), template.segments.len); } test "M1-TPL-005: adjacent placeholders need no separator" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const template = try parseWith(&arena, "{posting}{amount}"); try expectEqual(@as(usize, 2), template.segments.len); try expectEqualStrings("posting", template.segments[0].field); try expectEqualStrings("amount", template.segments[1].field); } // -- rendering tests --------------------------------------------------------- const sample: Values = .{ .fields = &.{ .{ .alias = "posting", .value = "market" }, .{ .alias = "amount", .value = "1,50" }, .{ .alias = "comment", .value = "" }, }, .source_filename = "bank/2026.csv", .physical_row = 12, .rule_name = "groceries", }; fn renderWith( arena: *std.heap.ArenaAllocator, source: []const u8, kind: Kind, values: Values, ) RenderError![]const u8 { const alloc = arena.allocator(); const diags = diag.List.create(alloc) catch return error.OutOfMemory; const parsed = parse(alloc, source, &.{ "posting", "amount", "comment" }, .{ .file = "project.yaml", .key_path = "profile.transaction.narrationTemplate", }, diags) catch return error.RenderFailed; const request: Request = .{ .template = parsed, .values = values, .kind = kind, .key_path = "profile.transaction.narrationTemplate", }; return request.render(alloc, diags); } fn narrationOf(arena: *std.heap.ArenaAllocator, source: []const u8) RenderError![]const u8 { return renderWith(arena, source, .narration, sample); } fn commentOf(arena: *std.heap.ArenaAllocator, source: []const u8) RenderError![]const u8 { return renderWith(arena, source, .comment, sample); } test "M1-TPL-003: a field placeholder renders the exact decoded cell" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); try expectEqualStrings("market", try narrationOf(&arena, "{posting}")); // No reformatting: the amount keeps the separator the cell used. try expectEqualStrings("1,50", try narrationOf(&arena, "{amount}")); } test "M1-TPL-003: an empty cell renders as an empty string" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); try expectEqualStrings("x", try narrationOf(&arena, "x{comment}")); } test "M1-TPL-003: surrounding whitespace in a cell is preserved" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const padded: Values = .{ .fields = &.{.{ .alias = "posting", .value = " spaced " }}, .source_filename = "a.csv", .physical_row = 0, .rule_name = "r", }; // Rendering never trims. A comment keeps what it was given (M1-TPL-010). try expectEqualStrings(" spaced ", try renderWith(&arena, "{posting}", .comment, padded)); // The same value in a narration is not trimmed either -- it is refused, // because hledger would trim it and M1-TPL-011 says to fail rather than // let a value change on the way out. try expectError( error.RenderFailed, renderWith(&arena, "{posting}", .narration, padded), ); } test "M1-TPL-004: the three built-ins render their values" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); try expectEqualStrings("bank/2026.csv", try narrationOf(&arena, "{sourceFilename}")); try expectEqualStrings("12", try narrationOf(&arena, "{physicalRow}")); try expectEqualStrings("groceries", try narrationOf(&arena, "{ruleName}")); } test "M1-TPL-004: physical row zero renders as 0" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const first: Values = .{ .fields = &.{}, .source_filename = "a.csv", .physical_row = 0, .rule_name = "r", }; try expectEqualStrings("0", try renderWith(&arena, "{physicalRow}", .narration, first)); } test "M1-AC-013: the worked interpolation example" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); try expectEqualStrings( "bank/2026.csv:12 groceries {source} market", try narrationOf(&arena, "{sourceFilename}:{physicalRow} {ruleName} {{source}} {posting}"), ); } test "M1-TPL-010: a zero-length narration prevents generation" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); try expectError(error.RenderFailed, narrationOf(&arena, "")); try expectError(error.RenderFailed, narrationOf(&arena, "{comment}")); } test "M1-TPL-010: a zero-length comment renders empty, for the caller to omit" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); try expectEqualStrings("", try commentOf(&arena, "")); try expectEqualStrings("", try commentOf(&arena, "{comment}")); } test "M1-AC-014: a whitespace-only comment is emitted untrimmed" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); // Not empty, and not trimmed: three spaces survive as three spaces. try expectEqualStrings(" ", try commentOf(&arena, " ")); try expectEqualStrings(" \t ", try commentOf(&arena, " \t ")); } test "M1-TPL-011: a narration cannot begin or end with whitespace" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); // hledger 1.52.3 strips both ends of a description, so neither survives. try expectError(error.RenderFailed, narrationOf(&arena, " leading")); try expectError(error.RenderFailed, narrationOf(&arena, "trailing ")); try expectError(error.RenderFailed, narrationOf(&arena, " both ")); // Interior spaces are ordinary text and are kept. Tabs anywhere were // already out as C0 controls, which is stricter than hledger needs. try expectEqualStrings("a b", try narrationOf(&arena, "a b")); } test "M1-TPL-011: a narration cannot begin with a status mark or code" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); // hledger would read these as a status or a transaction code rather than // as description text. try expectError(error.RenderFailed, narrationOf(&arena, "* starred")); try expectError(error.RenderFailed, narrationOf(&arena, "! pending")); try expectError(error.RenderFailed, narrationOf(&arena, "(123) coded")); // The same characters later in the narration are ordinary text. try expectEqualStrings("a * b", try narrationOf(&arena, "a * b")); try expectEqualStrings("a (b)", try narrationOf(&arena, "a (b)")); } test "M1-TPL-011: a narration cannot carry journal syntax" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); // A semicolon would start a comment, a pipe splits payee from note. try expectError(error.RenderFailed, narrationOf(&arena, "a;b")); try expectError(error.RenderFailed, narrationOf(&arena, "a|b")); try expectError(error.RenderFailed, narrationOf(&arena, "a\nb")); try expectError(error.RenderFailed, narrationOf(&arena, "a\x01b")); } test "M1-TPL-011: the check covers interpolated values, not just literals" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const hostile: Values = .{ .fields = &.{.{ .alias = "posting", .value = "cash; rebate" }}, .source_filename = "a.csv", .physical_row = 3, .rule_name = "r", }; try expectError(error.RenderFailed, renderWith(&arena, "{posting}", .narration, hostile)); } test "M1-TPL-011: a comment admits semicolons and pipes but not line breaks" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); // A comment already runs to end of line, so these are harmless there. try expectEqualStrings("a;b", try commentOf(&arena, "a;b")); try expectEqualStrings("a|b", try commentOf(&arena, "a|b")); try expectEqualStrings("a\tb", try commentOf(&arena, "a\tb")); try expectError(error.RenderFailed, commentOf(&arena, "a\nb")); try expectError(error.RenderFailed, commentOf(&arena, "a\rb")); } test "M1-TPL-009: an unavailable column fails with the template, file and row" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); const diags = try diag.List.create(alloc); const parsed = try parse(alloc, "{posting}", &.{"posting"}, .{ .file = "project.yaml", .key_path = "profile.transaction.narrationTemplate", }, diags); // Reachable only defensively: csv/fields.zig already guarantees every // aliased column exists in every data row it yields. const missing: Values = .{ .fields = &.{}, .source_filename = "bank/2026.csv", .physical_row = 4, .rule_name = "r", }; const request: Request = .{ .template = parsed, .values = missing, .kind = .narration, .key_path = "profile.transaction.narrationTemplate", }; try expectError(error.RenderFailed, request.render(alloc, diags)); const last = diags.items.items[diags.items.items.len - 1]; try expectEqualStrings("bank/2026.csv", last.file.?); try expectEqual(@as(usize, 4), last.physical_row.?); try expectEqualStrings("profile.transaction.narrationTemplate", last.key_path.?); } test "M1-SAFE-001: rendering the same inputs twice gives the same string" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const first = try narrationOf(&arena, "{ruleName}:{posting}"); const second = try narrationOf(&arena, "{ruleName}:{posting}"); try expectEqualStrings(first, second); } test "M1-SAFE-004: fail reports exhaustion rather than the invalid template" { // Recording the diagnostic is the only allocation `fail` makes, and it // makes it from the list's own arena. var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 0 }); var diags = diag.List.init(failing.allocator()); defer diags.deinit(); const outcome: Error = fail(&diags, .{ .file = "project.yaml", .key_path = "profile" }, "is invalid", .{}); try expectEqual(Error.OutOfMemory, outcome); }