From 0116e84c09d9a4a5f4ad58a6da533d2423cf8030 Mon Sep 17 00:00:00 2001 From: "Lucas F." Date: Wed, 14 Jan 2026 14:08:49 -0300 Subject: [PATCH] update: full template test --- src/renderer_test.zig | 298 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 245 insertions(+), 53 deletions(-) diff --git a/src/renderer_test.zig b/src/renderer_test.zig index 74a1b38..4b36c79 100644 --- a/src/renderer_test.zig +++ b/src/renderer_test.zig @@ -9,6 +9,8 @@ const Value = @import("context.zig").Value; const TemplateCache = @import("cache.zig").TemplateCache; test "renderer: literal + variável simples" { + std.debug.print("____________________________________________________\n", .{}); + std.debug.print("\n1 - renderer: literal + variável simples\n\n", .{}); const alloc = testing.allocator; var ctx = Context.init(alloc); defer ctx.deinit(); @@ -29,10 +31,14 @@ test "renderer: literal + variável simples" { try renderer.renderString(template, buf.writer(alloc)); + // std.debug.print("OUTPUT:\n\n{s}\n", .{buf.items}); + try testing.expectEqualStrings("Olá, Mariana! Bem-vinda.", buf.items); } test "renderer: filtros + autoescape" { + std.debug.print("____________________________________________________\n", .{}); + std.debug.print("\n2 - renderer: filtros + autoescape\n\n", .{}); const alloc = testing.allocator; var ctx = Context.init(alloc); defer ctx.deinit(); @@ -62,10 +68,15 @@ test "renderer: filtros + autoescape" { \\Filtrado: maiusculo-e-slug ; + + // std.debug.print("OUTPUT:\n\n{s}\n", .{buf.items}); + try testing.expectEqualStrings(expected, buf.items); } test "literal simples" { + std.debug.print("____________________________________________________\n", .{}); + std.debug.print("\n3 - literal simples\n\n", .{}); const alloc = testing.allocator; var ctx = Context.init(alloc); defer ctx.deinit(); @@ -82,10 +93,14 @@ test "literal simples" { try renderer.renderString(template, buf.writer(alloc)); + // std.debug.print("OUTPUT:\n\n{s}\n", .{buf.items}); + try testing.expectEqualStrings("Texto literal com acentos: Olá, mundo!", buf.items); } test "variável com filtro encadeado e autoescape" { + std.debug.print("____________________________________________________\n", .{}); + std.debug.print("\n4 - variável com filtro encadeado e autoescape\n\n", .{}); const alloc = testing.allocator; var ctx = Context.init(alloc); defer ctx.deinit(); @@ -104,10 +119,14 @@ test "variável com filtro encadeado e autoescape" { try renderer.renderString(template, buf.writer(alloc)); + // std.debug.print("OUTPUT:\n\n{s}\n", .{buf.items}); + try testing.expectEqualStrings("Resultado: EXEMPLO DE TEXTO", buf.items); // assume lower then upper } test "autoescape com safe" { + std.debug.print("____________________________________________________\n", .{}); + std.debug.print("\n5 - autoescape com safe\n\n", .{}); const alloc = testing.allocator; var ctx = Context.init(alloc); defer ctx.deinit(); @@ -126,10 +145,15 @@ test "autoescape com safe" { try renderer.renderString(template, buf.writer(alloc)); + // std.debug.print("OUTPUT:\n\n{s}\n", .{buf.items}); + try testing.expectEqualStrings("Escape: <div>conteúdo</div> | Safe:
conteúdo
", buf.items); } +// TODO: evaluationConditions more complex test "renderer - if and for" { + std.debug.print("____________________________________________________\n", .{}); + std.debug.print("\n6 - renderer - if and for\n\n", .{}); const alloc = testing.allocator; var ctx = Context.init(alloc); defer ctx.deinit(); @@ -160,6 +184,8 @@ test "renderer - if and for" { try renderer.renderString(template, buf.writer(alloc)); + // std.debug.print("OUTPUT:\n\n{s}\n", .{buf.items}); + try testing.expect(std.mem.indexOf(u8, buf.items, "Sim!") != null); try testing.expect(std.mem.indexOf(u8, buf.items, "Não") == null); try testing.expect(std.mem.indexOf(u8, buf.items, "- Ana") != null); @@ -168,12 +194,15 @@ test "renderer - if and for" { } test "renderer - block and extends" { + std.debug.print("____________________________________________________\n", .{}); + std.debug.print("\n7 - renderer - block and extends\n\n", .{}); const alloc = testing.allocator; var ctx = Context.init(alloc); defer ctx.deinit(); var cache = TemplateCache.init(alloc); defer cache.deinit(); + cache.default_path = "."; const renderer = Renderer.init(&ctx, &cache); @@ -220,7 +249,7 @@ test "renderer - block and extends" { const output = buf.items; - std.debug.print("OUTPUT:\n{s}\n", .{output}); + // std.debug.print("OUTPUT:\n\n{s}\n", .{output}); try testing.expect(std.mem.indexOf(u8, output, "") != null); try testing.expect(std.mem.indexOf(u8, output, "Meu Título") != null); @@ -230,12 +259,15 @@ test "renderer - block and extends" { } test "renderer - block and extends with super" { + std.debug.print("____________________________________________________\n", .{}); + std.debug.print("\n8 - renderer - block and extends with super\n\n", .{}); const alloc = testing.allocator; var ctx = Context.init(alloc); defer ctx.deinit(); var cache = TemplateCache.init(alloc); defer cache.deinit(); + cache.default_path = "."; const renderer = Renderer.init(&ctx, &cache); @@ -284,7 +316,7 @@ test "renderer - block and extends with super" { const output = buf.items; - std.debug.print("{s}\n", .{output}); + // std.debug.print("OUTPUT:\n\n{s}\n", .{buf.items}); try testing.expect(std.mem.indexOf(u8, output, "") != null); try testing.expect(std.mem.indexOf(u8, output, "Meu Título") != null); @@ -294,6 +326,8 @@ test "renderer - block and extends with super" { } test "renderer - include" { + std.debug.print("____________________________________________________\n", .{}); + std.debug.print("\n9 - renderer - include\n\n", .{}); const alloc = testing.allocator; const header = @@ -330,6 +364,7 @@ test "renderer - include" { var cache = TemplateCache.init(alloc); defer cache.deinit(); + cache.default_path = "."; const renderer = Renderer.init(&ctx, &cache); @@ -342,12 +377,16 @@ test "renderer - include" { const output = buf.items; + // std.debug.print("OUTPUT:\n\n{s}\n", .{buf.items}); + try testing.expect(std.mem.indexOf(u8, output, "

Bem-vindo

") != null); try testing.expect(std.mem.indexOf(u8, output, "Olá Lucas!") != null); try testing.expectEqualStrings(expected, output); } test "renderer - comment" { + std.debug.print("____________________________________________________\n", .{}); + std.debug.print("\n10 - renderer - comment\n\n", .{}); const alloc = testing.allocator; const template = @@ -370,6 +409,7 @@ test "renderer - comment" { var cache = TemplateCache.init(alloc); defer cache.deinit(); + cache.default_path = "."; const renderer = Renderer.init(&ctx, &cache); @@ -382,6 +422,8 @@ test "renderer - comment" { const output = buf.items; + // std.debug.print("OUTPUT:\n\n{s}\n", .{buf.items}); + try testing.expect(std.mem.indexOf(u8, output, "Olá Lucas") != null); try testing.expect(std.mem.indexOf(u8, output, "Fim: Lucas") != null); try testing.expect(std.mem.indexOf(u8, output, "Isso é um comentário") == null); @@ -390,54 +432,204 @@ test "renderer - comment" { try testing.expectEqualStrings(expected, output); } -// FIX: comment inside block -// -// test "renderer - full template with extends, super, include, comment" { -// const alloc = testing.allocator; -// -// const header = "
Bem-vindo
"; -// const base = -// \\{% include "header.html" %} -// \\
-// \\ {% block content %} -// \\ Conteúdo padrão -// \\ {% endblock %} -// \\
-// ; -// -// const child = -// \\{% extends "base.html" %} -// \\{% block content %} -// \\ {{ block.super }} -// \\ Conteúdo do filho -// \\ {% comment %} Isso não aparece {% endcomment %} -// \\{% endblock %} -// ; -// -// 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 = "child.html", .data = child }); -// defer std.fs.cwd().deleteFile("header.html") catch {}; -// 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); -// -// var buf = std.ArrayList(u8){}; -// defer buf.deinit(alloc); -// -// try renderer.render("child.html", buf.writer(alloc)); -// -// const output = buf.items; -// -// try testing.expect(std.mem.indexOf(u8, output, "
Bem-vindo
") != 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, "Isso não aparece") == null); -// } +test "renderer - full template with extends, super, include, comment" { + std.debug.print("____________________________________________________\n", .{}); + std.debug.print("\n11 - renderer - full template with extends, super, include, comment\n\n", .{}); + const alloc = testing.allocator; + + const header = "
Bem-vindo
"; + const base = + \\{% include "header.html" %} + \\
+ \\ {% block content %} + \\ Conteúdo padrão + \\ {% endblock %} + \\
+ ; + + const child = + \\{% extends "base.html" %} + \\{% block content %} + \\ {{ block.super }} + \\ Conteúdo do filho + \\ {% comment %} Isso não aparece {% endcomment %} + \\{% endblock %} + \\{% comment %} bazinga {% endcomment %} + ; + + 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 = "child.html", .data = child }); + defer std.fs.cwd().deleteFile("header.html") catch {}; + 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(); + cache.default_path = "."; + + const renderer = Renderer.init(&ctx, &cache); + + var buf = std.ArrayList(u8){}; + defer buf.deinit(alloc); + + try renderer.render("child.html", buf.writer(alloc)); + + const output = buf.items; + // std.debug.print("OUTPUT:\n\n{s}\n", .{output}); + + try testing.expect(std.mem.indexOf(u8, output, "
Bem-vindo
") != 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, "Isso não aparece") == null); +} + +test "renderer - if inside block" { + std.debug.print("____________________________________________________\n", .{}); + std.debug.print("\n12 - render - if inside block\n\n", .{}); + const alloc = testing.allocator; + + const base = + \\
+ \\ {% block content %} + \\ Conteúdo padrão + \\ {% endblock %} + \\
+ ; + + const child = + \\{% extends "base.html" %} + \\{% block content %} + \\ {{ block.super }} + \\ Conteúdo do filho + \\{% if idade > 18 %} + \\ Idade: {{ idade }} + \\{% else %} + \\ Oops + \\{% endif %} + \\{% 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(); + cache.default_path = "."; + + const renderer = Renderer.init(&ctx, &cache); + + try ctx.set("idade", 23); + + var buf = std.ArrayList(u8){}; + defer buf.deinit(alloc); + + try renderer.render("child.html", buf.writer(alloc)); + + const output = buf.items; + + // 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 do filho") != null); + try testing.expect(std.mem.indexOf(u8, output, "Oops") == null); +} + +test "renderer - if with operators" { + std.debug.print("____________________________________________________\n", .{}); + std.debug.print("\n13 - render - if inside block\n\n", .{}); + + const alloc = testing.allocator; + 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("idade", Value{ .int = 20 }); + try ctx.set("nome", Value{ .string = "Lucas" }); + try ctx.set("ativo", Value{ .bool = true }); + try ctx.set("order", 1); + + const template = + \\{% if idade > 18 %}Maior{% endif %} + \\{% if idade < 18 %}Menor{% endif %} + \\{% if nome == "Lucas" %}Olá Lucas{% endif %} + \\{% if ativo %}Ativo{% endif %} + \\{% if order >= 2 %}High Order{% else %}Low Order{% endif %} + ; + + var buf = std.ArrayList(u8){}; + defer buf.deinit(alloc); + + try renderer.renderString(template, buf.writer(alloc)); + + // std.debug.print("OUTPUT:\n\n{s}\n", .{buf.items}); + + try testing.expect(std.mem.indexOf(u8, buf.items, "Maior") != null); + try testing.expect(std.mem.indexOf(u8, buf.items, "Olá Lucas") != null); + try testing.expect(std.mem.indexOf(u8, buf.items, "Ativo") != null); + try testing.expect(std.mem.indexOf(u8, buf.items, "Menor") == null); + try testing.expect(std.mem.indexOf(u8, buf.items, "Low Order") != null); +} + +test "renderer - widthratio inside block" { + std.debug.print("____________________________________________________\n", .{}); + std.debug.print("\n14 - render - widthratio inside block\n\n", .{}); + const alloc = testing.allocator; + + const base = + \\
+ \\ {% block content %} + \\ Conteúdo padrão + \\ {% endblock %} + \\
+ ; + + const child = + \\{% extends "base.html" %} + \\{% block content %} + \\ Conteúdo do filho + \\Bar + \\ {{ block.super }} + \\{% 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(); + cache.default_path = "."; + + const renderer = Renderer.init(&ctx, &cache); + + try ctx.set("value", 50); + + var buf = std.ArrayList(u8){}; + defer buf.deinit(alloc); + + try renderer.render("child.html", buf.writer(alloc)); + + const output = buf.items; + + // 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 do filho") != null); +}