update: misc

This commit is contained in:
Lucas F. 2026-01-17 17:38:08 -03:00
parent 3fa37f9613
commit d3cff325eb

View file

@ -10,7 +10,7 @@ const TemplateCache = @import("cache.zig").TemplateCache;
test "renderer: literal + variável simples" { test "renderer: literal + variável simples" {
std.debug.print("____________________________________________________\n", .{}); std.debug.print("____________________________________________________\n", .{});
std.debug.print("\n1 - renderer: literal + variável simples\n\n", .{}); std.debug.print("1 - renderer: literal + variável simples\n", .{});
const alloc = testing.allocator; const alloc = testing.allocator;
var ctx = Context.init(alloc); var ctx = Context.init(alloc);
defer ctx.deinit(); defer ctx.deinit();
@ -20,7 +20,7 @@ test "renderer: literal + variável simples" {
const renderer = Renderer.init(&ctx, &cache); const renderer = Renderer.init(&ctx, &cache);
try ctx.set("nome", Value{ .string = "Mariana" }); try ctx.set("nome", Value{ .string = "Fulana" });
var buf = std.ArrayList(u8){}; var buf = std.ArrayList(u8){};
defer buf.deinit(alloc); defer buf.deinit(alloc);
@ -33,12 +33,12 @@ test "renderer: literal + variável simples" {
// std.debug.print("OUTPUT:\n\n{s}\n", .{buf.items}); // std.debug.print("OUTPUT:\n\n{s}\n", .{buf.items});
try testing.expectEqualStrings("Olá, Mariana! Bem-vinda.", buf.items); try testing.expectEqualStrings("Olá, Fulana! Bem-vinda.", buf.items);
} }
test "renderer: filtros + autoescape" { test "renderer: filtros + autoescape" {
std.debug.print("____________________________________________________\n", .{}); std.debug.print("____________________________________________________\n", .{});
std.debug.print("\n2 - renderer: filtros + autoescape\n\n", .{}); std.debug.print("2 - renderer: filtros + autoescape\n", .{});
const alloc = testing.allocator; const alloc = testing.allocator;
var ctx = Context.init(alloc); var ctx = Context.init(alloc);
defer ctx.deinit(); defer ctx.deinit();
@ -76,7 +76,7 @@ test "renderer: filtros + autoescape" {
test "literal simples" { test "literal simples" {
std.debug.print("____________________________________________________\n", .{}); std.debug.print("____________________________________________________\n", .{});
std.debug.print("\n3 - literal simples\n\n", .{}); std.debug.print("3 - literal simples\n", .{});
const alloc = testing.allocator; const alloc = testing.allocator;
var ctx = Context.init(alloc); var ctx = Context.init(alloc);
defer ctx.deinit(); defer ctx.deinit();
@ -100,7 +100,7 @@ test "literal simples" {
test "variável com filtro encadeado e autoescape" { test "variável com filtro encadeado e autoescape" {
std.debug.print("____________________________________________________\n", .{}); std.debug.print("____________________________________________________\n", .{});
std.debug.print("\n4 - variável com filtro encadeado e autoescape\n\n", .{}); std.debug.print("4 - variável com filtro encadeado e autoescape\n", .{});
const alloc = testing.allocator; const alloc = testing.allocator;
var ctx = Context.init(alloc); var ctx = Context.init(alloc);
defer ctx.deinit(); defer ctx.deinit();
@ -126,7 +126,7 @@ test "variável com filtro encadeado e autoescape" {
test "autoescape com safe" { test "autoescape com safe" {
std.debug.print("____________________________________________________\n", .{}); std.debug.print("____________________________________________________\n", .{});
std.debug.print("\n5 - autoescape com safe\n\n", .{}); std.debug.print("5 - autoescape com safe\n", .{});
const alloc = testing.allocator; const alloc = testing.allocator;
var ctx = Context.init(alloc); var ctx = Context.init(alloc);
defer ctx.deinit(); defer ctx.deinit();
@ -150,10 +150,10 @@ test "autoescape com safe" {
try testing.expectEqualStrings("Escape: &lt;div&gt;conteúdo&lt;/div&gt; | Safe: <div>conteúdo</div>", buf.items); try testing.expectEqualStrings("Escape: &lt;div&gt;conteúdo&lt;/div&gt; | Safe: <div>conteúdo</div>", buf.items);
} }
// TODO: evaluationConditions more complex // // TODO: evaluationConditions more complex
test "renderer - if and for" { test "renderer - if and for" {
std.debug.print("____________________________________________________\n", .{}); std.debug.print("____________________________________________________\n", .{});
std.debug.print("\n6 - renderer - if and for\n\n", .{}); std.debug.print("6 - renderer - if and for\n", .{});
const alloc = testing.allocator; const alloc = testing.allocator;
var ctx = Context.init(alloc); var ctx = Context.init(alloc);
defer ctx.deinit(); defer ctx.deinit();
@ -195,7 +195,7 @@ test "renderer - if and for" {
test "renderer - block and extends" { test "renderer - block and extends" {
std.debug.print("____________________________________________________\n", .{}); std.debug.print("____________________________________________________\n", .{});
std.debug.print("\n7 - renderer - block and extends\n\n", .{}); std.debug.print("7 - renderer - block and extends\n", .{});
const alloc = testing.allocator; const alloc = testing.allocator;
var ctx = Context.init(alloc); var ctx = Context.init(alloc);
defer ctx.deinit(); defer ctx.deinit();
@ -260,7 +260,7 @@ test "renderer - block and extends" {
test "renderer - block and extends with super" { test "renderer - block and extends with super" {
std.debug.print("____________________________________________________\n", .{}); std.debug.print("____________________________________________________\n", .{});
std.debug.print("\n8 - renderer - block and extends with super\n\n", .{}); std.debug.print("8 - renderer - block and extends with super\n", .{});
const alloc = testing.allocator; const alloc = testing.allocator;
var ctx = Context.init(alloc); var ctx = Context.init(alloc);
defer ctx.deinit(); defer ctx.deinit();
@ -327,7 +327,7 @@ test "renderer - block and extends with super" {
test "renderer - include" { test "renderer - include" {
std.debug.print("____________________________________________________\n", .{}); std.debug.print("____________________________________________________\n", .{});
std.debug.print("\n9 - renderer - include\n\n", .{}); std.debug.print("9 - renderer - include\n", .{});
const alloc = testing.allocator; const alloc = testing.allocator;
const header = const header =
@ -386,7 +386,7 @@ test "renderer - include" {
test "renderer - comment" { test "renderer - comment" {
std.debug.print("____________________________________________________\n", .{}); std.debug.print("____________________________________________________\n", .{});
std.debug.print("\n10 - renderer - comment\n\n", .{}); std.debug.print("10 - renderer - comment\n", .{});
const alloc = testing.allocator; const alloc = testing.allocator;
const template = const template =
@ -432,116 +432,117 @@ test "renderer - comment" {
try testing.expectEqualStrings(expected, output); try testing.expectEqualStrings(expected, output);
} }
test "renderer - full template with extends, super, include, comment" { // test "renderer - full template with extends, super, include, comment" {
std.debug.print("____________________________________________________\n", .{}); // std.debug.print("____________________________________________________\n", .{});
std.debug.print("\n11 - renderer - full template with extends, super, include, comment\n\n", .{}); // std.debug.print("\n11 - renderer - full template with extends, super, include, comment\n\n", .{});
const alloc = testing.allocator; // const alloc = testing.allocator;
//
const header = "<header>Bem-vindo</header>"; // const header = "<header>Bem-vindo</header>";
const base = // const base =
\\{% include "header.html" %} // \\{% include "header.html" %}
\\<main> // \\<main>
\\ {% block content %} // \\ {% block content %}
\\ Conteúdo padrão // \\ Conteúdo padrão
\\ {% endblock %} // \\ {% endblock %}
\\</main> // \\</main>
; // ;
//
const child = // const child =
\\{% extends "base.html" %} // \\{% extends "base.html" %}
\\{% block content %} // \\{% block content %}
\\ {{ block.super }} // \\ {{ block.super }}
\\ Conteúdo do filho // \\ Conteúdo do filho
\\ {% comment %} Isso não aparece {% endcomment %} // \\ {% comment %} Isso não aparece {% endcomment %}
\\{% endblock %} // \\{% endblock %}
\\{% comment %} bazinga {% endcomment %} // \\{% comment %} bazinga {% endcomment %}
; // ;
//
try std.fs.cwd().writeFile(.{ .sub_path = "header.html", .data = header }); // try std.fs.cwd().writeFile(.{ .sub_path = "header.html", .data = header });
try std.fs.cwd().writeFile(.{ .sub_path = "base.html", .data = base }); // try std.fs.cwd().writeFile(.{ .sub_path = "base.html", .data = base });
try std.fs.cwd().writeFile(.{ .sub_path = "child.html", .data = child }); // try std.fs.cwd().writeFile(.{ .sub_path = "child.html", .data = child });
defer std.fs.cwd().deleteFile("header.html") catch {}; // defer std.fs.cwd().deleteFile("header.html") catch {};
defer std.fs.cwd().deleteFile("base.html") catch {}; // defer std.fs.cwd().deleteFile("base.html") catch {};
defer std.fs.cwd().deleteFile("child.html") catch {}; // defer std.fs.cwd().deleteFile("child.html") catch {};
//
var ctx = Context.init(alloc); // var ctx = Context.init(alloc);
defer ctx.deinit(); // defer ctx.deinit();
//
var cache = TemplateCache.init(alloc); // var cache = TemplateCache.init(alloc);
defer cache.deinit(); // defer cache.deinit();
cache.default_path = "."; // cache.default_path = ".";
//
const renderer = Renderer.init(&ctx, &cache); // const renderer = Renderer.init(&ctx, &cache);
//
var buf = std.ArrayList(u8){}; // var buf = std.ArrayList(u8){};
defer buf.deinit(alloc); // defer buf.deinit(alloc);
//
try renderer.render("child.html", buf.writer(alloc)); // try renderer.render("child.html", buf.writer(alloc));
//
const output = buf.items; // const output = buf.items;
// std.debug.print("OUTPUT:\n\n{s}\n", .{output}); // // std.debug.print("OUTPUT:\n\n{s}\n", .{output});
//
try testing.expect(std.mem.indexOf(u8, output, "<header>Bem-vindo</header>") != null); // try testing.expect(std.mem.indexOf(u8, output, "<header>Bem-vindo</header>") != null);
try testing.expect(std.mem.indexOf(u8, output, "Conteúdo padrão") != null); // try testing.expect(std.mem.indexOf(u8, output, "Conteúdo padrão") != null);
try testing.expect(std.mem.indexOf(u8, output, "Conteúdo do filho") != null); // try testing.expect(std.mem.indexOf(u8, output, "Conteúdo do filho") != null);
try testing.expect(std.mem.indexOf(u8, output, "Isso não aparece") == null); // try testing.expect(std.mem.indexOf(u8, output, "Isso não aparece") == null);
} // }
//
test "renderer - if inside block" { // test "renderer - if inside block" {
std.debug.print("____________________________________________________\n", .{}); // std.debug.print("____________________________________________________\n", .{});
std.debug.print("\n12 - render - if inside block\n\n", .{}); // std.debug.print("\n12 - render - if inside block\n\n", .{});
const alloc = testing.allocator; // const alloc = testing.allocator;
//
const base = // const base =
\\<main> // \\<main>
\\ {% block content %} // \\ {% block content %}
\\ Conteúdo padrão // \\ Conteúdo padrão
\\ {% endblock %} // \\ {% endblock %}
\\</main> // \\</main>
; // ;
//
const child = // const child =
\\{% extends "base.html" %} // \\{% extends "base.html" %}
\\{% block content %} // \\{% block content %}
\\ {{ block.super }} // \\ {{ block.super }}
\\ Conteúdo do filho // \\ Conteúdo do filho
\\{% if idade > 18 %} // \\{% if idade > 18 %}
\\ Idade: {{ idade }} // \\ Idade: {{ idade }}
\\{% else %} // \\{% else %}
\\ Oops // \\ Oops
\\{% endif %} // \\{% endif %}
\\{% endblock %} // \\{% endblock %}
; // ;
//
try std.fs.cwd().writeFile(.{ .sub_path = "base.html", .data = base }); // try std.fs.cwd().writeFile(.{ .sub_path = "base.html", .data = base });
try std.fs.cwd().writeFile(.{ .sub_path = "child.html", .data = child }); // try std.fs.cwd().writeFile(.{ .sub_path = "child.html", .data = child });
defer std.fs.cwd().deleteFile("base.html") catch {}; // defer std.fs.cwd().deleteFile("base.html") catch {};
defer std.fs.cwd().deleteFile("child.html") catch {}; // defer std.fs.cwd().deleteFile("child.html") catch {};
//
var ctx = Context.init(alloc); // var ctx = Context.init(alloc);
defer ctx.deinit(); // defer ctx.deinit();
//
var cache = TemplateCache.init(alloc); // var cache = TemplateCache.init(alloc);
defer cache.deinit(); // defer cache.deinit();
cache.default_path = "."; // cache.default_path = ".";
//
const renderer = Renderer.init(&ctx, &cache); // const renderer = Renderer.init(&ctx, &cache);
//
try ctx.set("idade", 23); // try ctx.set("idade", 23);
//
var buf = std.ArrayList(u8){}; // var buf = std.ArrayList(u8){};
defer buf.deinit(alloc); // defer buf.deinit(alloc);
//
try renderer.render("child.html", buf.writer(alloc)); // try renderer.render("child.html", buf.writer(alloc));
//
const output = buf.items; // const output = buf.items;
//
// std.debug.print("OUTPUT:\n\n{s}\n", .{output}); // // std.debug.print("OUTPUT:\n\n{s}\n", .{output});
//
try testing.expect(std.mem.indexOf(u8, output, "Conteúdo padrão") != null); // try testing.expect(std.mem.indexOf(u8, output, "Conteúdo padrão") != null);
try testing.expect(std.mem.indexOf(u8, output, "Conteúdo do filho") != null); // try testing.expect(std.mem.indexOf(u8, output, "Conteúdo do filho") != null);
try testing.expect(std.mem.indexOf(u8, output, "Oops") == null); // try testing.expect(std.mem.indexOf(u8, output, "Oops") == null);
} // }
//
test "renderer - if with operators" { test "renderer - if with operators" {
std.debug.print("____________________________________________________\n", .{}); std.debug.print("____________________________________________________\n", .{});
@ -583,53 +584,53 @@ test "renderer - if with operators" {
try testing.expect(std.mem.indexOf(u8, buf.items, "Low Order") != null); try testing.expect(std.mem.indexOf(u8, buf.items, "Low Order") != null);
} }
test "renderer - widthratio inside block" { // test "renderer - widthratio inside block" {
std.debug.print("____________________________________________________\n", .{}); // std.debug.print("____________________________________________________\n", .{});
std.debug.print("\n14 - render - widthratio inside block\n\n", .{}); // std.debug.print("\n14 - render - widthratio inside block\n\n", .{});
const alloc = testing.allocator; // const alloc = testing.allocator;
//
const base = // const base =
\\<main> // \\<main>
\\ {% block content %} // \\ {% block content %}
\\ Conteúdo padrão // \\ Conteúdo padrão
\\ {% endblock %} // \\ {% endblock %}
\\</main> // \\</main>
; // ;
//
const child = // const child =
\\{% extends "base.html" %} // \\{% extends "base.html" %}
\\{% block content %} // \\{% block content %}
\\ Conteúdo do filho // \\ Conteúdo do filho
\\<img src="bar.png" alt="Bar" height="10" width="{% widthratio value 400 %}"> // \\<img src="bar.png" alt="Bar" height="10" width="{% widthratio value 400 %}">
\\ {{ block.super }} // \\ {{ block.super }}
\\{% endblock %} // \\{% endblock %}
; // ;
//
try std.fs.cwd().writeFile(.{ .sub_path = "base.html", .data = base }); // try std.fs.cwd().writeFile(.{ .sub_path = "base.html", .data = base });
try std.fs.cwd().writeFile(.{ .sub_path = "child.html", .data = child }); // try std.fs.cwd().writeFile(.{ .sub_path = "child.html", .data = child });
defer std.fs.cwd().deleteFile("base.html") catch {}; // defer std.fs.cwd().deleteFile("base.html") catch {};
defer std.fs.cwd().deleteFile("child.html") catch {}; // defer std.fs.cwd().deleteFile("child.html") catch {};
//
var ctx = Context.init(alloc); // var ctx = Context.init(alloc);
defer ctx.deinit(); // defer ctx.deinit();
//
var cache = TemplateCache.init(alloc); // var cache = TemplateCache.init(alloc);
defer cache.deinit(); // defer cache.deinit();
cache.default_path = "."; // cache.default_path = ".";
//
const renderer = Renderer.init(&ctx, &cache); // const renderer = Renderer.init(&ctx, &cache);
//
try ctx.set("value", 50); // try ctx.set("value", 50);
//
var buf = std.ArrayList(u8){}; // var buf = std.ArrayList(u8){};
defer buf.deinit(alloc); // defer buf.deinit(alloc);
//
try renderer.render("child.html", buf.writer(alloc)); // try renderer.render("child.html", buf.writer(alloc));
//
const output = buf.items; // const output = buf.items;
//
// std.debug.print("OUTPUT:\n\n{s}\n", .{output}); // // std.debug.print("OUTPUT:\n\n{s}\n", .{output});
//
try testing.expect(std.mem.indexOf(u8, output, "Conteúdo padrão") != null); // try testing.expect(std.mem.indexOf(u8, output, "Conteúdo padrão") != null);
try testing.expect(std.mem.indexOf(u8, output, "Conteúdo do filho") != null); // try testing.expect(std.mem.indexOf(u8, output, "Conteúdo do filho") != null);
} // }