update: misc

This commit is contained in:
Lucas F. 2026-01-12 09:08:01 -03:00
parent c7efa6baae
commit 175c30b199

View file

@ -1,27 +1,58 @@
const std = @import("std");
const zdt_prov = @import("zdt_prov");
const Context = @import("context.zig").Context;
const Renderer = @import("renderer.zig").Renderer;
const parser = @import("parser.zig");
const TemplateCache = @import("cache.zig").TemplateCache;
pub fn main() !void {
// Prints to stderr, ignoring potential errors.
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
try zdt_prov.bufferedPrint();
}
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const alloc = arena.allocator();
test "simple test" {
const gpa = std.testing.allocator;
var list: std.ArrayList(i32) = .empty;
defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak!
try list.append(gpa, 42);
try std.testing.expectEqual(@as(i32, 42), list.pop());
}
const base =
\\<html>
\\<head><title>{% block title %}Título Padrão{% endblock %}</title></head>
\\<body>
\\{% block content %}Conteúdo padrão{% endblock %}
\\</body>
\\</html>
;
test "fuzz example" {
const Context = struct {
fn testOne(context: @This(), input: []const u8) anyerror!void {
_ = context;
// Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input));
}
};
try std.testing.fuzz(Context{}, Context.testOne, .{});
const child =
\\{% extends "base.html" %}
\\{% block title %}Meu Título{% endblock %}
\\{% block content %}
\\Olá {{ nome }}!
\\{% endblock %}
;
try std.fs.cwd().writeFile(.{
.sub_path = "base.html",
.data = base,
});
try std.fs.cwd().writeFile(.{
.sub_path = "child.html",
.data = child,
});
defer std.fs.cwd().deleteFile("base.html") catch {};
defer std.fs.cwd().deleteFile("child.html") catch {};
var ctx = Context.init(alloc);
defer ctx.deinit();
var cache = TemplateCache.init(alloc);
defer cache.deinit();
const renderer = Renderer.init(&ctx, &cache);
try ctx.set("nome", "Lucas");
var buffer = std.ArrayList(u8){};
defer buffer.deinit(ctx.allocator());
try renderer.render("child.html", buffer.writer(ctx.allocator()));
const output = buffer.items;
std.debug.print("{s}\n", .{output});
}