#!/usr/bin/env wolframscript
(* Regenerate the WXF kernel-fixture file consumed by tests/wxf_kernel_fixtures.rs.
Run from this directory:
wolframscript -file generate.wls
For each (name, wl_expression, options) entry, BinarySerialize the WL
expression and emit a `pub const NAME: &[u8] = &[...]` line into
wxf_kernel_fixtures.rs. The fixture is the canonical output of the
reference Wolfram kernel — committing it lets tests cross-check our
WXF parser against the kernel without a runtime kernel dependency.
Re-run this script whenever a test case is added or the kernel's WXF
output changes (rare). *)
cases = {
{"INTEGER_42", "42", {}},
{"INTEGER_NEG_LARGE", "-1234567890", {}},
{"REAL_3_5", "3.5", {}},
{"STRING_HELLO", "\"hello\"", {}},
{"SYMBOL_PLUS", "Plus", {}},
{"SYMBOL_MYPKG_X", "MyPkg`x", {}},
{"LIST_INTS", "{1, 2, 3}", {}},
{"LIST_EMPTY", "{}", {}},
{"LIST_MIXED", "{\"a\", 1, 2.5}", {}},
{"FUNCTION_MYPKG", "MyPkg`myFunc[1, 2, 3]", {}},
{"ASSOCIATION_PLAIN", "<|\"a\" -> 1, \"b\" -> 2|>", {}},
{"ASSOCIATION_DELAYED", "<|\"a\" -> 1, \"b\" :> 2|>", {}},
{"BYTE_ARRAY", "ByteArray[{0, 1, 2, 255, 128}]", {}},
{"NUMERIC_ARRAY_INT32", "NumericArray[{10, 20, 30}, \"Integer32\"]", {}},
{"COMPRESSED_RANGE_100", "Range[100]", {PerformanceGoal -> "Size"}}
};
formatBytes[bytes_ByteArray] := With[{lst = Normal[bytes]},
StringJoin["&[", StringRiffle[("0x" <> IntegerString[#, 16, 2]) & /@ lst, ", "], "]"]
];
renderEntry[{name_, wl_, opts_}] := Module[{expr, bytes, hex, header},
expr = ToExpression[wl, InputForm, Hold];
bytes = BinarySerialize[expr /. Hold[x_] :> Unevaluated[x], opts];
hex = formatBytes[bytes];
StringJoin[
"/// `", wl, "`",
If[opts === {}, "", ", " <> ToString[opts, InputForm]],
"\n#[rustfmt::skip]\npub const ", name, ": &[u8] = ", hex, ";\n"
]
];
header = "//! Auto-generated by tests/fixtures/generate.wls -- do NOT edit by hand.
//!
//! Each constant is the canonical WXF byte sequence produced by the Wolfram
//! kernel's `BinarySerialize` for the input shown in its docstring. Tests
//! consume these fixtures to verify our WXF parser against kernel output
//! without needing a runtime wolframscript invocation.
//!
//! Regenerate with:
//! `wolframscript -file tests/fixtures/generate.wls`
";
WriteString["wxf_kernel_fixtures.rs", header];
Scan[WriteString["wxf_kernel_fixtures.rs", renderEntry[#]] &, cases];
WriteString["wxf_kernel_fixtures.rs", "\n"];
Print["wrote wxf_kernel_fixtures.rs (", Length[cases], " entries)"];