update: test for with, extends, super, include and commment

This commit is contained in:
Lucas F. 2026-01-17 20:41:55 -03:00
parent 745b2b29ca
commit 36cc1caac0

View file

@ -68,7 +68,6 @@ test "renderer: filtros + autoescape" {
\\Filtrado: maiusculo-e-slug
;
// std.debug.print("OUTPUT:\n\n{s}\n", .{buf.items});
try testing.expectEqualStrings(expected, buf.items);
@ -432,121 +431,123 @@ test "renderer - comment" {
try testing.expectEqualStrings(expected, output);
}
// 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 = "<header>Bem-vindo</header>";
// const base =
// \\{% include "header.html" %}
// \\<main>
// \\ {% block content %}
// \\ Conteúdo padrão
// \\ {% endblock %}
// \\</main>
// ;
//
// 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, "<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 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 =
// \\<main>
// \\ {% block content %}
// \\ Conteúdo padrão
// \\ {% endblock %}
// \\</main>
// ;
//
// 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 - full template with extends, super, include, comment" {
std.debug.print("____________________________________________________\n", .{});
std.debug.print("11 - renderer - full template with extends, super, include, comment\n", .{});
const alloc = testing.allocator;
const header = "<header>Bem-vindo</header>";
const base =
\\{% include "header.html" %}
\\<main>
\\ {% block content %}
\\ Conteúdo padrão
\\ {% endblock %}
\\</main>
;
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, "<header>Bem-vindo</header>") != null);
try testing.expect(std.mem.indexOf(u8, output, "<main>") != 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);
try testing.expect(std.mem.indexOf(u8, output, "bazinga") == null);
}
test "renderer - if inside block" {
std.debug.print("____________________________________________________\n", .{});
std.debug.print("12 - render - if inside block\n", .{});
const alloc = testing.allocator;
const base =
\\<main>
\\ {% block content %}
\\ Conteúdo padrão
\\ {% endblock %}
\\</main>
;
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);
try testing.expect(std.mem.indexOf(u8, output, "Idade: 23") != null);
}
test "renderer - if with operators" {
std.debug.print("____________________________________________________\n", .{});
std.debug.print("\n13 - render - if inside block\n\n", .{});
std.debug.print("13 - render - if inside block\n", .{});
const alloc = testing.allocator;
var ctx = Context.init(alloc);
@ -584,53 +585,53 @@ test "renderer - if with operators" {
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 =
// \\<main>
// \\ {% block content %}
// \\ Conteúdo padrão
// \\ {% endblock %}
// \\</main>
// ;
//
// const child =
// \\{% extends "base.html" %}
// \\{% block content %}
// \\ Conteúdo do filho
// \\<img src="bar.png" alt="Bar" height="10" width="{% widthratio value 400 %}">
// \\ {{ 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);
// }
test "renderer - widthratio inside block" {
std.debug.print("____________________________________________________\n", .{});
std.debug.print("14 - render - widthratio inside block\n", .{});
const alloc = testing.allocator;
const base =
\\<main>
\\ {% block content %}
\\ Conteúdo padrão
\\ {% endblock %}
\\</main>
;
const child =
\\{% extends "base.html" %}
\\{% block content %}
\\ Conteúdo do filho
\\<img src="bar.png" alt="Bar" height="10" width="{% widthratio value 400 %}">
\\ {{ 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);
}