comptime templates in zig
$ zig build test
All 12 tests passed.
$ zig build # results in zig-cache/lib/libtemplate.zig.a
from tests.zig
var print_buf: [1000]u8 = undefined;
test "template variables" {
const Tmpl = @import("template.zig").Template;
const tmpl = Tmpl(
"Hello {{.world}}",
.{ .eval_branch_quota = 1000 }, // default value. same as .{}
);
// bufPrint
const message = try tmpl.bufPrint(&print_buf, .{ .world = "friends" });
std.testing.expectEqualStrings("Hello friends", message);
// allocPrint
const message2 = try tmpl.allocPrint(std.testing.allocator, .{ .world = "again friends" });
defer std.testing.allocator.free(message2);
std.testing.expectEqualStrings("Hello again friends", message2);
}
test "range over interval" {
const Tmpl = @import("template.zig").Template;
const tmpl = Tmpl(
"5 times: {{ range $index := 0..4 }}{{ $index }}{{ end }}",
.{ .eval_branch_quota = 4000 },
);
// bufPrint
const message = try tmpl.bufPrint(&print_buf, .{});
std.testing.expectEqualStrings("5 times: 01234", message);
// allocPrint
const message2 = try tmpl.allocPrint(std.testing.allocator, .{});
defer std.testing.allocator.free(message2);
std.testing.expectEqualStrings("5 times: 01234", message2);
}
test "range over collection with index" {
const Tmpl = @import("template.zig").Template;
const tmpl = Tmpl(
"5 times: {{ range $index, $item := .items }}{{$item}}{{ $index }},{{ end }}",
.{ .eval_branch_quota = 6000 },
);
// bufPrint
const items = [_]u8{ 0, 1, 2, 3, 4 };
const message = try tmpl.bufPrint(&print_buf, .{ .items = items });
std.testing.expectEqualStrings("5 times: 00,11,22,33,44,", message);
// allocPrint
const message2 = try tmpl.allocPrint(std.testing.allocator, .{ .items = items });
defer std.testing.allocator.free(message2);
std.testing.expectEqualStrings("5 times: 00,11,22,33,44,", message2);
}
test "if - else if - else" {
const Tmpl = @import("template.zig").Template;
const tmpl = Tmpl("{{if .cond}}a{{else if .cond2}}b{{else}}c{{end}}", .{ .eval_branch_quota = 2000 });
std.testing.expectEqualStrings("a", try tmpl.bufPrint(&print_buf, .{ .cond = true }));
std.testing.expectEqualStrings("b", try tmpl.bufPrint(&print_buf, .{ .cond2 = 1 }));
std.testing.expectEqualStrings("c", try tmpl.bufPrint(&print_buf, .{}));
}