// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! The oracle suites: every case transformed and compared against what a //! reference processor produced for it. //! //! There are two, because there had to be. `tests/cases` is recorded from //! libxslt, which implements XSLT 1.0 and nothing else, so nothing in it says //! anything about 2.0. `tests/cases-20` is recorded from Saxon-B 8.8 -- the //! last Saxon that implements XSLT 2.0 and nothing else, which is the //! property an oracle needs. `tools/oracle20.sh` says why at length. //! //! What makes this worth more than an ordinary test file is that nobody wrote //! the expected output. `tools/oracle.sh` runs `xsltproc` over each case and //! captures what it printed, so a case asserts what the reference //! implementation actually does rather than what the author believed it does //! -- which for a specification this size is a different thing often enough to //! matter. Nearly every bug this suite has caught was a place where the //! specification's words admitted a reading that turned out not to be the one //! everybody else implements. //! //! A case in `tests/cases-20` may also carry an `expected.saxon-he`, which //! means the modern Saxon disagreed with the one that recorded it: 8.8 is of //! 2006 and saxon-he 12.10 is an XSLT 3.0 processor, so each is wrong in ways //! the other is not. That file is a disagreement between two references //! rather than between this library and one, and it owes a `why.md` for the //! same reason -- a disagreement nobody wrote down is indistinguishable from //! a bug nobody noticed. //! //! A case may carry an `expected.zxsl` as well. That means this library //! deliberately produces something else, and the `why.md` beside it says why; //! the test then asserts the `expected.zxsl`, and the libxslt output stays in //! the directory as the record of what is being disagreed with. //! //! The cases are read from disk rather than embedded so that adding one is //! adding a directory, with nothing to register. const std = @import("std"); const Io = std.Io; const zxsl = @import("zxsl"); const options = @import("build_options"); const max_file_bytes = 8 * 1024 * 1024; /// Reads a case's `xsl:include`, `xsl:import` and `document()` from the case /// directory. const CaseLoader = struct { io: Io, dir: []const u8, fn loader(c: *CaseLoader) zxsl.stylesheet.Loader { return .{ .ctx = c, .loadFn = loadFn, .resolveFn = resolveFn }; } fn loadFn( ctx: ?*anyopaque, gpa: std.mem.Allocator, base: []const u8, href: []const u8, ) zxsl.Error![]u8 { const c: *CaseLoader = @ptrCast(@alignCast(ctx.?)); const path = try resolvePath(gpa, base, href); defer gpa.free(path); return readFile(gpa, c.io, path) catch error.DocumentNotFound; } fn resolveFn( _: ?*anyopaque, gpa: std.mem.Allocator, base: []const u8, href: []const u8, ) zxsl.Error![]u8 { return resolvePath(gpa, base, href); } fn resolvePath( gpa: std.mem.Allocator, base: []const u8, href: []const u8, ) std.mem.Allocator.Error![]u8 { const dir = std.fs.path.dirname(base) orelse return gpa.dupe(u8, href); return std.fs.path.join(gpa, &.{ dir, href }); } }; fn readFile(gpa: std.mem.Allocator, io: Io, path: []const u8) ![]u8 { const file = try Io.Dir.cwd().openFile(io, path, .{}); defer file.close(io); var buffer: [64 * 1024]u8 = undefined; var reader = file.reader(io, &buffer); return reader.interface.allocRemaining(gpa, .limited(max_file_bytes)); } /// Runs one case and returns what this library produced. fn runCase(gpa: std.mem.Allocator, io: Io, dir: []const u8) ![]u8 { const style_path = try std.fs.path.join(gpa, &.{ dir, "style.xsl" }); defer gpa.free(style_path); const input_path = try std.fs.path.join(gpa, &.{ dir, "input.xml" }); defer gpa.free(input_path); const style_src = try readFile(gpa, io, style_path); defer gpa.free(style_src); const input_src = try readFile(gpa, io, input_path); defer gpa.free(input_src); var case_loader: CaseLoader = .{ .io = io, .dir = dir }; const sheet = try zxsl.stylesheet.parse(gpa, style_src, .{ .loader = case_loader.loader(), .base_uri = style_path, }); defer sheet.destroy(); const source = try zxsl.tree.parse(gpa, input_src, .{ .base_uri = input_path }); defer source.destroy(); return zxsl.transform.transformToString(gpa, sheet, source, .{ .loader = case_loader.loader(), }); } /// Where the cases of one suite live, and which reference recorded them. const Suite = struct { dir: []const u8, oracle: []const u8, }; const suites = [_]Suite{ .{ .dir = options.cases_dir, .oracle = "libxslt" }, .{ .dir = options.cases_20_dir, .oracle = "Saxon-B 8.8" }, }; /// The name of every directory under a suite that holds a case. fn caseNames(gpa: std.mem.Allocator, io: Io, suite_dir: []const u8) ![][]u8 { var dir = try Io.Dir.cwd().openDir(io, suite_dir, .{ .iterate = true }); defer dir.close(io); var names: std.ArrayList([]u8) = .empty; errdefer { for (names.items) |n| gpa.free(n); names.deinit(gpa); } var it = dir.iterate(); while (try it.next(io)) |entry| { if (entry.kind != .directory) continue; try names.append(gpa, try gpa.dupe(u8, entry.name)); } // Alphabetical, so that a failure list reads the same way twice. std.mem.sort([]u8, names.items, {}, lessThanSlice); return names.toOwnedSlice(gpa); } fn lessThanSlice(_: void, a: []u8, b: []u8) bool { return std.mem.order(u8, a, b) == .lt; } test "every case agrees with its oracle, or says why it does not" { for (suites) |suite| try checkSuite(suite); } fn checkSuite(suite: Suite) !void { const gpa = std.testing.allocator; const io = std.testing.io; const names = try caseNames(gpa, io, suite.dir); defer { for (names) |n| gpa.free(n); gpa.free(names); } try std.testing.expect(names.len > 0); var failures: usize = 0; for (names) |name| { const dir = try std.fs.path.join(gpa, &.{ suite.dir, name }); defer gpa.free(dir); // A case with an `expected.zxsl` is one this library deliberately // answers differently; that file is then the assertion and `expected` // stays as the record of what libxslt said. const divergent_path = try std.fs.path.join(gpa, &.{ dir, "expected.zxsl" }); defer gpa.free(divergent_path); const oracle_path = try std.fs.path.join(gpa, &.{ dir, "expected" }); defer gpa.free(oracle_path); const want = readFile(gpa, io, divergent_path) catch readFile(gpa, io, oracle_path) catch |err| { std.debug.print("case {s}/{s}: no expected output ({t})\n", .{ suite.dir, name, err }); failures += 1; continue; }; defer gpa.free(want); const got = runCase(gpa, io, dir) catch |err| { std.debug.print("case {s}/{s}: {t}\n", .{ suite.dir, name, err }); failures += 1; continue; }; defer gpa.free(got); if (!std.mem.eql(u8, want, got)) { std.debug.print( "case {s}/{s} differs from {s}\n--- expected ---\n{s}\n--- got ---\n{s}\n", .{ suite.dir, name, suite.oracle, want, got }, ); failures += 1; } } if (failures != 0) { std.debug.print("{s}: {d} of {d} cases failed\n", .{ suite.dir, failures, names.len }); return error.ConformanceFailures; } } test "a case that diverges explains itself" { // A divergence is a claim about the specification, and a claim nobody // wrote down is indistinguishable from a bug that was papered over. // // Two kinds of file make the claim. `expected.zxsl` says this library // answers differently from its oracle; `expected.saxon-he` says the two // Saxons answer differently from each other. Both owe the same // explanation, so the rule is simply that every `expected.*` file beyond // the recording itself needs a `why.md`. for (suites) |suite| { for ([_][]const u8{ "expected.zxsl", "expected.saxon-he" }) |claim| { try checkExplained(suite, claim); } } } fn checkExplained(suite: Suite, claim: []const u8) !void { const gpa = std.testing.allocator; const io = std.testing.io; const names = try caseNames(gpa, io, suite.dir); defer { for (names) |n| gpa.free(n); gpa.free(names); } for (names) |name| { const dir = try std.fs.path.join(gpa, &.{ suite.dir, name }); defer gpa.free(dir); const divergent_path = try std.fs.path.join(gpa, &.{ dir, claim }); defer gpa.free(divergent_path); const divergent = readFile(gpa, io, divergent_path) catch continue; gpa.free(divergent); const why_path = try std.fs.path.join(gpa, &.{ dir, "why.md" }); defer gpa.free(why_path); const why = readFile(gpa, io, why_path) catch { std.debug.print("case {s}/{s} has an {s} but no why.md\n", .{ suite.dir, name, claim }); return error.UndocumentedDivergence; }; defer gpa.free(why); try std.testing.expect(why.len > 200); } }