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