If the zon file is available at compile time you can
@importthe file.
I tried this approach, but it errors with expected type '[:0]const u8'. Could you give an example of this?
The closest thing I could find was this post, and reduced it to the below which works:
test "parse ZON file" {
const allocator = std.testing.allocator;
const Foo = struct {
bar: []const u8,
};
const file = try std.fs.cwd().readFileAllocOptions(allocator, "foo.zon", 1024, null, std.mem.Alignment.@"1", 0);
defer allocator.free(file);
const parsed_struct = try std.zon.parse.fromSlice(Foo, allocator, file, null, .{});
defer std.zon.parse.free(allocator, parsed_struct);
try std.testing.expectEqualStrings("AAA", parsed_struct.bar);
}
where foo.zon is:
.{
.bar = "AAA",
}
but wanted to know if there’s a better way ?
Edit: Sorry, its as simple as:
test "parse zon file" {
const data = @import("foo.zon");
try std.testing.expectEqualStrings("AAA", data.bar);
}