update: renderer initial idea
This commit is contained in:
parent
d1c63cddda
commit
0e74ddb278
2 changed files with 225 additions and 0 deletions
120
src/renderer_test.zig
Normal file
120
src/renderer_test.zig
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
|
||||
const Renderer = @import("renderer.zig").Renderer;
|
||||
const RenderError = @import("renderer.zig").RenderError;
|
||||
|
||||
const Context = @import("context.zig").Context;
|
||||
const Value = @import("context.zig").Value; // ajuste o caminho se estiver diferente
|
||||
|
||||
test "renderer: literal + variável simples" {
|
||||
const alloc = testing.allocator;
|
||||
var ctx = Context.init(alloc);
|
||||
defer ctx.deinit();
|
||||
|
||||
try ctx.set("nome", Value{ .string = "Mariana" });
|
||||
|
||||
const renderer = Renderer.init(&ctx);
|
||||
|
||||
var buf = std.ArrayList(u8){};
|
||||
defer buf.deinit(alloc);
|
||||
|
||||
const template =
|
||||
\\Olá, {{ nome }}! Bem-vinda.
|
||||
;
|
||||
|
||||
try renderer.renderString(template, buf.writer(alloc));
|
||||
|
||||
try testing.expectEqualStrings("Olá, Mariana! Bem-vinda.", buf.items);
|
||||
}
|
||||
|
||||
test "renderer: filtros + autoescape" {
|
||||
const alloc = testing.allocator;
|
||||
var ctx = Context.init(alloc);
|
||||
|
||||
defer ctx.deinit();
|
||||
|
||||
try ctx.set("html", Value{ .string = "<script>alert()</script>" });
|
||||
try ctx.set("texto", Value{ .string = "maiusculo e slug" });
|
||||
|
||||
const renderer = Renderer.init(&ctx);
|
||||
|
||||
var buf = std.ArrayList(u8){};
|
||||
defer buf.deinit(alloc);
|
||||
|
||||
const template =
|
||||
\\Normal: {{ html }}
|
||||
\\Safe: {{ html|safe }}
|
||||
\\Filtrado: {{ texto|upper|slugify }}
|
||||
;
|
||||
|
||||
try renderer.renderString(template, buf.writer(alloc));
|
||||
|
||||
const expected =
|
||||
\\Normal: <script>alert()</script>
|
||||
\\Safe: <script>alert()</script>
|
||||
\\Filtrado: maiusculo-e-slug
|
||||
;
|
||||
|
||||
try testing.expectEqualStrings(expected, buf.items);
|
||||
}
|
||||
|
||||
test "literal simples" {
|
||||
const alloc = testing.allocator;
|
||||
var ctx = Context.init(alloc);
|
||||
|
||||
defer ctx.deinit();
|
||||
|
||||
const renderer = Renderer.init(&ctx);
|
||||
|
||||
var buf = std.ArrayList(u8){};
|
||||
defer buf.deinit(alloc);
|
||||
|
||||
const template = "Texto literal com acentos: Olá, mundo!";
|
||||
|
||||
try renderer.renderString(template, buf.writer(alloc));
|
||||
|
||||
try testing.expectEqualStrings("Texto literal com acentos: Olá, mundo!", buf.items);
|
||||
}
|
||||
|
||||
test "variável com filtro encadeado e autoescape" {
|
||||
const alloc = testing.allocator;
|
||||
var ctx = Context.init(alloc);
|
||||
|
||||
defer ctx.deinit();
|
||||
|
||||
const renderer = Renderer.init(&ctx);
|
||||
|
||||
try ctx.set("texto", Value{ .string = "Exemplo de Texto" });
|
||||
|
||||
|
||||
var buf = std.ArrayList(u8){};
|
||||
defer buf.deinit(alloc);
|
||||
|
||||
const template = "Resultado: {{ texto|lower|upper }}";
|
||||
|
||||
try renderer.renderString(template, buf.writer(alloc));
|
||||
|
||||
try testing.expectEqualStrings("Resultado: EXEMPLO DE TEXTO", buf.items); // assume lower then upper
|
||||
}
|
||||
|
||||
test "autoescape com safe" {
|
||||
const alloc = testing.allocator;
|
||||
var ctx = Context.init(alloc);
|
||||
|
||||
defer ctx.deinit();
|
||||
|
||||
const renderer = Renderer.init(&ctx);
|
||||
|
||||
try ctx.set("html", Value{ .string = "<div>conteúdo</div>" });
|
||||
|
||||
|
||||
var buf = std.ArrayList(u8){};
|
||||
defer buf.deinit(alloc);
|
||||
|
||||
const template = "Escape: {{ html }} | Safe: {{ html|safe }}";
|
||||
|
||||
try renderer.renderString(template, buf.writer(alloc));
|
||||
|
||||
try testing.expectEqualStrings("Escape: <div>conteúdo</div> | Safe: <div>conteúdo</div>", buf.items);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue