//! The hledger compatibility corpus (M1-JRN-008). //! //! M1-JRN-008 asks for automated tests covering "the complete range of //! narration, posting-comment, account, amount, and currency text the //! implementation claims it can emit". The claim is DESIGN_M1.md §4.5 (D4) //! plus the decimal renderer and the currency enum, so this file walks the //! boundary of each: values just inside the reject set, the widest amounts an //! i64 coefficient reaches, and both currencies there are. //! //! The cases are built as journal transactions and put through the real //! emitter, not through a copy of its rules. A case the emitter refuses fails //! the Zig test before hledger ever sees it, so the corpus cannot quietly //! drift away from what the program will actually produce. //! //! What the hledger side asserts is that hledger 1.52.3 *parses* every //! journal. It deliberately does not assert that hledger agrees with us about //! decoded comment payloads: §11.1 settled that our own parser is the model of //! record there, and hledger trims comment whitespace where we do not. const std = @import("std"); const row = @import("../row.zig"); const Amount = @import("../journal/Amount.zig"); const Transaction = @import("../journal/Transaction.zig"); const Journal = @import("../Journal.zig"); pub const Case = struct { /// Whether hledger's own re-print of this journal must read back, through /// our parser, as the journal we emitted. /// /// True everywhere except comments. §11.1 settled that our parser is the /// model of record for comment payloads, and hledger trims whitespace /// there where M1-TPL-010 forbids us to. For that case hledger must still /// *parse* what we wrote (M1-JRN-007); it simply need not agree about the /// payload. Naming the exception here keeps it visible instead of buried /// in a shell script. hledger_roundtrip: bool = true, /// Used as the file name, so it says which journal failed. name: []const u8, transactions: []const Transaction, /// The journal this case describes, rendered by whoever asks with `{f}` /// through the same path the program itself emits by. pub fn journal(self: Case) Journal { return .{ .transactions = self.transactions }; } }; fn amount(coef: i64, scale: u8, currency: row.Currency) Amount { return .{ .quantity = .{ .coef = coef, .scale = scale }, .currency = currency }; } /// The shape every case shares: one transaction, two postings, the second /// leaving its amount for hledger to infer (M1-JRN-005). fn one(spec: struct { narration: []const u8, quantity: Amount, source_account: []const u8, source_comment: ?[]const u8 = null, target_account: []const u8, target_comment: ?[]const u8 = null, }) Transaction { return .{ .date = .{ .year = 2026, .month = 9, .day = 11 }, .narration = spec.narration, .postings = .{ .{ .account = spec.source_account, .amount = spec.quantity, .comment = spec.source_comment }, .{ .account = spec.target_account, .amount = null, .comment = spec.target_comment }, }, }; } const eur = amount(-150, 2, .eur); const bank = "assets:bank"; const food = "expenses:food"; fn plain(narration: []const u8) Transaction { return one(.{ .narration = narration, .source_account = bank, .target_account = food, .quantity = eur }); } fn withAccounts(source_account: []const u8, target_account: []const u8) Transaction { return one(.{ .narration = "account case", .source_account = source_account, .target_account = target_account, .quantity = eur }); } fn withComment(comment: []const u8) Transaction { return one(.{ .narration = "comment case", .source_account = bank, .target_account = food, .quantity = eur, .source_comment = comment }); } fn withAmount(quantity: Amount) Transaction { return one(.{ .narration = "amount case", .source_account = bank, .target_account = food, .quantity = quantity }); } /// D4 rejects `\n`, `\r`, `;`, `|`, C0 controls, a leading `*`, `!` or `(`, /// and leading or trailing whitespace. Everything else is claimed emittable, /// so everything else has to parse. const narrations = [_]Transaction{ plain("a"), // Punctuation hledger gives meaning to elsewhere, or that looks like // syntax: none of it is special inside a description. plain("a*b"), plain("a!b"), plain("a(b)c"), plain("a[b]c"), plain("a#b"), plain("a%b"), plain("a=b"), plain("a~b"), plain("a@b"), plain("a@@b"), plain("a$b"), plain("a\"b\""), plain("a'b'"), plain("a\\b"), plain("a:b"), plain("a,b"), plain("a--b"), plain("a b"), // Text that could be read as another kind of token. plain("2026-01-01"), plain("123"), plain("-1.50 EUR"), plain("assets:bank"), plain("include other.journal"), plain("P 2026-01-01 EUR 1.00"), // Beyond ASCII. M1-CSV-001 decodes UTF-8 and normalizes nothing, so // whatever the bank wrote arrives here intact. plain("Zahlung für Bücher"), plain("日本語の説明"), plain("emoji 🧾 receipt"), plain("combining a\u{0301}cute"), plain("right-to-left שלום"), }; /// D4 rejects `;`, two consecutive spaces, leading or trailing whitespace, and /// a leading `(` or `[`. const accounts = [_]Transaction{ withAccounts("a", "b"), withAccounts("assets:bank:checking:2026", "expenses:food:groceries:weekly"), withAccounts("a b c", "d e f"), withAccounts("assets-bank", "expenses_food"), withAccounts("Aktiva:Bank", "Ausgaben:Lebensmittel"), withAccounts("資産:銀行", "費用:食品"), withAccounts("a(b)", "c[d]"), withAccounts("a*b", "c!d"), withAccounts("a.b", "c-d"), }; /// D4 rejects only `\n` and `\r` in a comment. Tabs and surrounding spaces /// stay, because M1-TPL-010 forbids trimming (§11.1). const comments = [_]Transaction{ withComment("plain"), withComment("with; a semicolon"), withComment("with two spaces"), withComment("with\ta tab"), withComment(" leading space"), withComment("trailing space "), withComment(" "), withComment("Bücher 日本語 🧾"), withComment("2026-01-01 looks like a date"), withComment("assets:bank -1.00 EUR"), one(.{ .narration = "both comments", .source_account = bank, .target_account = food, .quantity = eur, .source_comment = "source side", .target_comment = "destination side" }), }; /// The full reach of an i64 coefficient with a u8 scale, both signs, and the /// scales at each end. /// /// The widest magnitude is `maxInt(i64)`, not `minInt(i64)`. Values only ever /// enter through `Decimal.parse`, which builds the magnitude before applying /// the sign and so refuses one more than `maxInt(i64)` as an overflow. A /// `Decimal` holding `minInt(i64)` is therefore unreachable, and emitting one /// would produce a journal our own parser could not read back. const amounts = [_]Transaction{ withAmount(amount(0, 0, .eur)), withAmount(amount(0, 2, .eur)), withAmount(amount(1, 0, .eur)), withAmount(amount(-1, 0, .eur)), withAmount(amount(150, 2, .eur)), withAmount(amount(-150, 2, .eur)), withAmount(amount(std.math.maxInt(i64), 0, .eur)), withAmount(amount(-std.math.maxInt(i64), 0, .eur)), withAmount(amount(std.math.maxInt(i64), 18, .eur)), withAmount(amount(-std.math.maxInt(i64), 18, .eur)), // A scale wider than the coefficient has digits: all leading zeroes. withAmount(amount(1, 30, .eur)), withAmount(amount(-1, 30, .eur)), }; /// M1-CSV-020 admits exactly these, so this is the complete range. const currencies = [_]Transaction{ withAmount(amount(-150, 2, .eur)), withAmount(amount(-150, 2, .usd)), }; /// Several transactions in one file, which is the ordinary shape. const multiple = [_]Transaction{ plain("first"), plain("second"), plain("third"), }; pub const cases = [_]Case{ .{ .name = "narration", .transactions = &narrations }, .{ .name = "account", .transactions = &accounts }, .{ .name = "comment", .transactions = &comments, .hledger_roundtrip = false }, .{ .name = "amount", .transactions = &amounts }, .{ .name = "currency", .transactions = ¤cies }, .{ .name = "multiple", .transactions = &multiple }, }; // -- tests ------------------------------------------------------------------- const diag = @import("../diag.zig"); const parse = @import("../journal/parse.zig"); const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; test "M1-JRN-008: every case in the corpus is one the emitter accepts" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); // A corpus that quietly dropped the hard cases would prove nothing, so // each one goes through the emitter the program itself uses. for (cases) |case| { const text = alloc.print("{f}", .{case.journal()}) catch |err| { std.debug.print("case '{s}' was refused: {any}\n", .{ case.name, err }); return err; }; try expect(text.len > 0); } } test "M1-JRN-008: the corpus covers every context the requirement names" { // narration, posting-comment, account, amount and currency text. for ([_][]const u8{ "narration", "comment", "account", "amount", "currency" }) |context| { var found = false; for (cases) |case| { if (std.mem.eql(u8, case.name, context)) found = true; } try expect(found); } } test "M1-JRN-008: what the corpus emits reads back as what it was" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); // hledger parsing the output is one half. The other is that our own // parser recovers the same journal, which is what M1-HIST-004 compares // approved journals against. for (cases) |case| { var diags = diag.List.init(alloc); const text = try alloc.print("{f}", .{case.journal()}); const reparsed = parse.parse(alloc, case.name, text, &diags) catch |err| { std.debug.print("case '{s}' did not parse back: {any}\n", .{ case.name, err }); return err; }; try expectEqual(case.transactions.len, reparsed.transactions.len); reparsed.compare(.{ .transactions = case.transactions }, .{ .diags = &diags, .location = .{ .csv = case.name, .journal = case.name }, }) catch { std.debug.print("case '{s}' changed on the round trip\n", .{case.name}); for (diags.items.items) |d| std.debug.print(" {s}\n", .{d.message}); return error.RoundTripChanged; }; } } test "M1-PKG-007: nothing in a run reaches the compatibility corpus" { // hledger may be a compatibility-test dependency and must not be a runtime // one. This module is test data plus a dump program wired into its own // build step; the pipeline that a command runs never imports it. The // packaging half of this -- that only the application is installed, and // that it does not so much as name hledger -- is asserted in // nix/package.nix, where it can be seen. const cli = @import("../cli.zig"); inline for (@typeInfo(cli).@"struct".decl_names) |name| { try expect(!std.mem.eql(u8, name, "compat")); } // And nothing here runs a program: a case renders in memory, through the // same Journal the pipeline emits. Were it ever to hand back text fetched // from somewhere else, this is what would notice. try expectEqual(Journal, @TypeOf(cases[0].journal())); }