update: add extends, block, super, filter_block, autoescape and spaceless
This commit is contained in:
parent
261c02f59b
commit
0192ad0b64
3 changed files with 822 additions and 375 deletions
|
|
@ -2,329 +2,329 @@ const std = @import("std");
|
|||
const testing = std.testing;
|
||||
const parser = @import("parser.zig");
|
||||
|
||||
// test "parse texto simples" {
|
||||
// const allocator = testing.allocator;
|
||||
// const template = "Olá mundo!";
|
||||
// const nodes = try parser.parse(allocator, template);
|
||||
// defer {
|
||||
// for (nodes) |node| {
|
||||
// node.deinit(allocator);
|
||||
// }
|
||||
// allocator.free(nodes);
|
||||
// }
|
||||
//
|
||||
// try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
// try testing.expect(nodes[0].type == .text);
|
||||
// try testing.expectEqualStrings("Olá mundo!", nodes[0].text.?.content);
|
||||
// }
|
||||
//
|
||||
// test "parse variável simples" {
|
||||
// const allocator = testing.allocator;
|
||||
// const template = "Olá {{ nome }}!";
|
||||
// const nodes = try parser.parse(allocator, template);
|
||||
// defer {
|
||||
// for (nodes) |node| {
|
||||
// node.deinit(allocator);
|
||||
// }
|
||||
// allocator.free(nodes);
|
||||
// }
|
||||
//
|
||||
// try testing.expectEqual(@as(usize, 3), nodes.len);
|
||||
//
|
||||
// try testing.expect(nodes[0].type == .text);
|
||||
// try testing.expectEqualStrings("Olá ", nodes[0].text.?.content);
|
||||
//
|
||||
// try testing.expect(nodes[1].type == .variable);
|
||||
// try testing.expectEqualStrings("nome", nodes[1].variable.?.content);
|
||||
//
|
||||
// try testing.expect(nodes[2].type == .text);
|
||||
// try testing.expectEqualStrings("!", nodes[2].text.?.content);
|
||||
// }
|
||||
//
|
||||
// test "parse variável com espaços" {
|
||||
// const allocator = testing.allocator;
|
||||
// const template = "{{ espacos }}";
|
||||
// const nodes = try parser.parse(allocator, template);
|
||||
// defer {
|
||||
// for (nodes) |node| {
|
||||
// node.deinit(allocator);
|
||||
// }
|
||||
// allocator.free(nodes);
|
||||
// }
|
||||
//
|
||||
// try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
// try testing.expect(nodes[0].type == .variable);
|
||||
// try testing.expectEqualStrings("espacos", nodes[0].variable.?.content);
|
||||
// }
|
||||
//
|
||||
// test "parse tag simples" {
|
||||
// const allocator = testing.allocator;
|
||||
// const template = "Antes {% minha_tag %} Depois";
|
||||
// const nodes = try parser.parse(allocator, template);
|
||||
// defer {
|
||||
// for (nodes) |node| {
|
||||
// node.deinit(allocator);
|
||||
// }
|
||||
// allocator.free(nodes);
|
||||
// }
|
||||
//
|
||||
// try testing.expectEqual(@as(usize, 3), nodes.len);
|
||||
//
|
||||
// try testing.expect(nodes[0].type == .text);
|
||||
// try testing.expectEqualStrings("Antes ", nodes[0].text.?.content);
|
||||
//
|
||||
// try testing.expect(nodes[1].type == .tag);
|
||||
// try testing.expectEqualStrings("minha_tag", nodes[1].tag.?.name);
|
||||
// try testing.expectEqualStrings("", nodes[1].tag.?.args);
|
||||
//
|
||||
// try testing.expect(nodes[2].type == .text);
|
||||
// try testing.expectEqualStrings(" Depois", nodes[2].text.?.content);
|
||||
// }
|
||||
//
|
||||
// test "parse if block básico" {
|
||||
// const allocator = testing.allocator;
|
||||
// const template = "{% if usuario.logado %}Bem-vindo!{% endif %}";
|
||||
// const nodes = try parser.parse(allocator, template);
|
||||
// defer {
|
||||
// for (nodes) |node| {
|
||||
// node.deinit(allocator);
|
||||
// }
|
||||
// allocator.free(nodes);
|
||||
// }
|
||||
//
|
||||
// try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
// try testing.expect(nodes[0].type == .if_block);
|
||||
//
|
||||
// const ib = nodes[0].@"if".?;
|
||||
// try testing.expectEqualStrings("usuario.logado", ib.condition);
|
||||
// try testing.expectEqual(@as(usize, 1), ib.true_body.len);
|
||||
// try testing.expect(nodes[0].@"if".?.true_body[0].type == .text);
|
||||
// try testing.expectEqualStrings("Bem-vindo!", ib.true_body[0].text.?.content);
|
||||
// try testing.expectEqual(@as(usize, 0), ib.false_body.len);
|
||||
// }
|
||||
//
|
||||
// test "parse if block sem else" {
|
||||
// const allocator = testing.allocator;
|
||||
// const template = "{% if cond %}Verdadeiro{% endif %}";
|
||||
// const nodes = try parser.parse(allocator, template);
|
||||
// defer {
|
||||
// for (nodes) |node| {
|
||||
// node.deinit(allocator);
|
||||
// }
|
||||
// allocator.free(nodes);
|
||||
// }
|
||||
//
|
||||
// try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
// try testing.expect(nodes[0].type == .if_block);
|
||||
// const ib = nodes[0].@"if".?;
|
||||
// try testing.expectEqualStrings("cond", ib.condition);
|
||||
// try testing.expectEqual(@as(usize, 1), ib.true_body.len);
|
||||
// try testing.expectEqualStrings("Verdadeiro", ib.true_body[0].text.?.content);
|
||||
// try testing.expectEqual(@as(usize, 0), ib.false_body.len);
|
||||
// }
|
||||
//
|
||||
// test "parse if block com else" {
|
||||
// const allocator = testing.allocator;
|
||||
// const template = "{% if cond %}Verdadeiro{% else %}Falso{% endif %}";
|
||||
// const nodes = try parser.parse(allocator, template);
|
||||
// defer {
|
||||
// for (nodes) |node| {
|
||||
// node.deinit(allocator);
|
||||
// }
|
||||
// allocator.free(nodes);
|
||||
// }
|
||||
//
|
||||
// try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
// try testing.expect(nodes[0].type == .if_block);
|
||||
// const ib = nodes[0].@"if".?;
|
||||
// try testing.expectEqualStrings("cond", ib.condition);
|
||||
// try testing.expectEqual(@as(usize, 1), ib.true_body.len);
|
||||
// try testing.expectEqualStrings("Verdadeiro", ib.true_body[0].text.?.content);
|
||||
// try testing.expectEqual(@as(usize, 1), ib.false_body.len);
|
||||
// try testing.expectEqualStrings("Falso", ib.false_body[0].text.?.content);
|
||||
// }
|
||||
//
|
||||
// test "parse for block sem empty" {
|
||||
// const allocator = testing.allocator;
|
||||
// const template = "{% for item in lista %}Item: {{ item }}{% endfor %}";
|
||||
// const nodes = try parser.parse(allocator, template);
|
||||
// defer {
|
||||
// for (nodes) |node| node.deinit(allocator);
|
||||
// allocator.free(nodes);
|
||||
// }
|
||||
//
|
||||
// try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
// try testing.expect(nodes[0].type == .for_block);
|
||||
// const fb = nodes[0].@"for".?;
|
||||
// try testing.expectEqualStrings("item", fb.loop_var);
|
||||
// try testing.expectEqualStrings("lista", fb.iterable);
|
||||
// try testing.expectEqual(@as(usize, 2), fb.body.len); // <--- corrigido: 2 nós
|
||||
// try testing.expectEqual(@as(usize, 0), fb.empty_body.len);
|
||||
//
|
||||
// try testing.expect(fb.body[0].type == .text);
|
||||
// try testing.expectEqualStrings("Item: ", fb.body[0].text.?.content);
|
||||
// try testing.expect(fb.body[1].type == .variable);
|
||||
// try testing.expectEqualStrings("item", fb.body[1].variable.?.content);
|
||||
// }
|
||||
//
|
||||
// test "parse for block com empty" {
|
||||
// const allocator = testing.allocator;
|
||||
// const template = "{% for item in lista %}Tem{% empty %}Vazio{% endfor %}";
|
||||
// const nodes = try parser.parse(allocator, template);
|
||||
// defer {
|
||||
// for (nodes) |node| node.deinit(allocator);
|
||||
// allocator.free(nodes);
|
||||
// }
|
||||
//
|
||||
// try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
// try testing.expect(nodes[0].type == .for_block);
|
||||
// const fb = nodes[0].@"for".?;
|
||||
// try testing.expectEqual(@as(usize, 1), fb.body.len);
|
||||
// try testing.expectEqualStrings("Tem", fb.body[0].text.?.content);
|
||||
// try testing.expectEqual(@as(usize, 1), fb.empty_body.len);
|
||||
// try testing.expectEqualStrings("Vazio", fb.empty_body[0].text.?.content);
|
||||
// }
|
||||
//
|
||||
// test "parse comment" {
|
||||
// const allocator = testing.allocator;
|
||||
// const template = "Bazinga! {% comment %}{% for item in lista %}Tem{% empty %}Vazio{% endfor %}{% endcomment %}";
|
||||
// const nodes = try parser.parse(allocator, template);
|
||||
// defer {
|
||||
// for (nodes) |node| node.deinit(allocator);
|
||||
// allocator.free(nodes);
|
||||
// }
|
||||
//
|
||||
// try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
// try testing.expect(nodes[0].type == .text);
|
||||
// try testing.expectEqualStrings("Bazinga! ", nodes[0].text.?.content);
|
||||
// }
|
||||
//
|
||||
// test "parse include simples" {
|
||||
// const allocator = testing.allocator;
|
||||
// const template = "Cabeçalho {% include \"header.zdt\" %} Conteúdo {% include \"footer.zdt\" %}";
|
||||
// const nodes = try parser.parse(allocator, template);
|
||||
// defer {
|
||||
// for (nodes) |node| node.deinit(allocator);
|
||||
// allocator.free(nodes);
|
||||
// }
|
||||
//
|
||||
// try testing.expectEqual(@as(usize, 4), nodes.len);
|
||||
//
|
||||
// try testing.expect(nodes[0].type == .text);
|
||||
// try testing.expectEqualStrings("Cabeçalho ", nodes[0].text.?.content);
|
||||
//
|
||||
// try testing.expect(nodes[1].type == .include);
|
||||
// try testing.expectEqualStrings("header.zdt", nodes[1].include.?.template_name);
|
||||
//
|
||||
// try testing.expect(nodes[2].type == .text);
|
||||
// try testing.expectEqualStrings(" Conteúdo ", nodes[2].text.?.content);
|
||||
//
|
||||
// try testing.expect(nodes[3].type == .include);
|
||||
// try testing.expectEqualStrings("footer.zdt", nodes[3].include.?.template_name);
|
||||
//
|
||||
// }
|
||||
//
|
||||
// test "parse include sem aspas (erro esperado no futuro, mas por enquanto aceita)" {
|
||||
// const allocator = testing.allocator;
|
||||
// const template = "{% include header.zdt %}";
|
||||
// const nodes = try parser.parse(allocator, template);
|
||||
// defer {
|
||||
// for (nodes) |node| node.deinit(allocator);
|
||||
// allocator.free(nodes);
|
||||
// }
|
||||
//
|
||||
// try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
// try testing.expect(nodes[0].type == .include);
|
||||
// try testing.expectEqualStrings("header.zdt", nodes[0].include.?.template_name);
|
||||
// }
|
||||
//
|
||||
// test "parse with simples" {
|
||||
// const allocator = testing.allocator;
|
||||
// const template = "{% with nome=\"Lucas\" idade=30 %}Olá {{ nome }}, você tem {{ idade }} anos.{% endwith %}";
|
||||
// const nodes = try parser.parse(allocator, template);
|
||||
// defer {
|
||||
// for (nodes) |node| node.deinit(allocator);
|
||||
// allocator.free(nodes);
|
||||
// }
|
||||
//
|
||||
// try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
// try testing.expect(nodes[0].type == .with_block);
|
||||
// const w = nodes[0].with.?;
|
||||
// try testing.expectEqual(@as(usize, 2), w.assignments.len);
|
||||
// try testing.expectEqual(@as(usize, 5), w.body.len);
|
||||
// try testing.expect(w.body[0].type == .text);
|
||||
// }
|
||||
//
|
||||
// test "parse now simples" {
|
||||
// const allocator = testing.allocator;
|
||||
// const template = "Data atual: {% now \"Y-m-d\" %} às {% now \"H:i\" %}";
|
||||
// const nodes = try parser.parse(allocator, template);
|
||||
// defer {
|
||||
// for (nodes) |node| node.deinit(allocator);
|
||||
// allocator.free(nodes);
|
||||
// }
|
||||
//
|
||||
// try testing.expectEqual(@as(usize, 4), nodes.len);
|
||||
//
|
||||
// try testing.expect(nodes[0].type == .text);
|
||||
// try testing.expectEqualStrings("Data atual: ", nodes[0].text.?.content);
|
||||
//
|
||||
// try testing.expect(nodes[1].type == .now);
|
||||
// try testing.expectEqualStrings("Y-m-d", nodes[1].now.?.format);
|
||||
//
|
||||
// try testing.expect(nodes[2].type == .text);
|
||||
// try testing.expectEqualStrings(" às ", nodes[2].text.?.content);
|
||||
//
|
||||
// try testing.expect(nodes[3].type == .now);
|
||||
// try testing.expectEqualStrings("H:i", nodes[3].now.?.format);
|
||||
//
|
||||
// }
|
||||
//
|
||||
// test "parse now sem aspas" {
|
||||
// const allocator = testing.allocator;
|
||||
// const template = "{% now Y-m-d %}";
|
||||
// const nodes = try parser.parse(allocator, template);
|
||||
// defer {
|
||||
// for (nodes) |node| node.deinit(allocator);
|
||||
// allocator.free(nodes);
|
||||
// }
|
||||
//
|
||||
// try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
// try testing.expect(nodes[0].type == .now);
|
||||
// try testing.expectEqualStrings("Y-m-d", nodes[0].now.?.format);
|
||||
// }
|
||||
//
|
||||
// test "parse extends" {
|
||||
// const allocator = testing.allocator;
|
||||
// const template = "{% extends \"base.zdt\" %}";
|
||||
// const nodes = try parser.parse(allocator, template);
|
||||
// defer {
|
||||
// for (nodes) |node| node.deinit(allocator);
|
||||
// allocator.free(nodes);
|
||||
// }
|
||||
//
|
||||
// try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
// try testing.expect(nodes[0].type == .extends);
|
||||
// try testing.expectEqualStrings("base.zdt", nodes[0].extends.?.parent_name);
|
||||
// }
|
||||
//
|
||||
// test "parse block simples" {
|
||||
// const allocator = testing.allocator;
|
||||
// const template = "{% block titulo %}Meu Título{% endblock %}";
|
||||
// const nodes = try parser.parse(allocator, template);
|
||||
// defer {
|
||||
// for (nodes) |node| node.deinit(allocator);
|
||||
// allocator.free(nodes);
|
||||
// }
|
||||
//
|
||||
// try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
// try testing.expect(nodes[0].type == .block);
|
||||
// const b = nodes[0].block.?;
|
||||
// try testing.expectEqualStrings("titulo", b.name);
|
||||
// try testing.expectEqual(@as(usize, 1), b.body.len);
|
||||
// try testing.expectEqualStrings("Meu Título", b.body[0].text.?.content);
|
||||
// }
|
||||
//
|
||||
test "parse texto simples" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "Olá mundo!";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| {
|
||||
node.deinit(allocator);
|
||||
}
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .text);
|
||||
try testing.expectEqualStrings("Olá mundo!", nodes[0].text.?.content);
|
||||
}
|
||||
|
||||
test "parse variável simples" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "Olá {{ nome }}!";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| {
|
||||
node.deinit(allocator);
|
||||
}
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 3), nodes.len);
|
||||
|
||||
try testing.expect(nodes[0].type == .text);
|
||||
try testing.expectEqualStrings("Olá ", nodes[0].text.?.content);
|
||||
|
||||
try testing.expect(nodes[1].type == .variable);
|
||||
try testing.expectEqualStrings("nome", nodes[1].variable.?.expr);
|
||||
|
||||
try testing.expect(nodes[2].type == .text);
|
||||
try testing.expectEqualStrings("!", nodes[2].text.?.content);
|
||||
}
|
||||
|
||||
test "parse variável com espaços" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "{{ espacos }}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| {
|
||||
node.deinit(allocator);
|
||||
}
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .variable);
|
||||
try testing.expectEqualStrings("espacos", nodes[0].variable.?.expr);
|
||||
}
|
||||
|
||||
test "parse tag simples" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "Antes {% minha_tag %} Depois";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| {
|
||||
node.deinit(allocator);
|
||||
}
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 3), nodes.len);
|
||||
|
||||
try testing.expect(nodes[0].type == .text);
|
||||
try testing.expectEqualStrings("Antes ", nodes[0].text.?.content);
|
||||
|
||||
try testing.expect(nodes[1].type == .tag);
|
||||
try testing.expectEqualStrings("minha_tag", nodes[1].tag.?.name);
|
||||
try testing.expectEqualStrings("", nodes[1].tag.?.args);
|
||||
|
||||
try testing.expect(nodes[2].type == .text);
|
||||
try testing.expectEqualStrings(" Depois", nodes[2].text.?.content);
|
||||
}
|
||||
|
||||
test "parse if block básico" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "{% if usuario.logado %}Bem-vindo!{% endif %}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| {
|
||||
node.deinit(allocator);
|
||||
}
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .if_block);
|
||||
|
||||
const ib = nodes[0].@"if".?;
|
||||
try testing.expectEqualStrings("usuario.logado", ib.condition);
|
||||
try testing.expectEqual(@as(usize, 1), ib.true_body.len);
|
||||
try testing.expect(nodes[0].@"if".?.true_body[0].type == .text);
|
||||
try testing.expectEqualStrings("Bem-vindo!", ib.true_body[0].text.?.content);
|
||||
try testing.expectEqual(@as(usize, 0), ib.false_body.len);
|
||||
}
|
||||
|
||||
test "parse if block sem else" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "{% if cond %}Verdadeiro{% endif %}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| {
|
||||
node.deinit(allocator);
|
||||
}
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .if_block);
|
||||
const ib = nodes[0].@"if".?;
|
||||
try testing.expectEqualStrings("cond", ib.condition);
|
||||
try testing.expectEqual(@as(usize, 1), ib.true_body.len);
|
||||
try testing.expectEqualStrings("Verdadeiro", ib.true_body[0].text.?.content);
|
||||
try testing.expectEqual(@as(usize, 0), ib.false_body.len);
|
||||
}
|
||||
|
||||
test "parse if block com else" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "{% if cond %}Verdadeiro{% else %}Falso{% endif %}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| {
|
||||
node.deinit(allocator);
|
||||
}
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .if_block);
|
||||
const ib = nodes[0].@"if".?;
|
||||
try testing.expectEqualStrings("cond", ib.condition);
|
||||
try testing.expectEqual(@as(usize, 1), ib.true_body.len);
|
||||
try testing.expectEqualStrings("Verdadeiro", ib.true_body[0].text.?.content);
|
||||
try testing.expectEqual(@as(usize, 1), ib.false_body.len);
|
||||
try testing.expectEqualStrings("Falso", ib.false_body[0].text.?.content);
|
||||
}
|
||||
|
||||
test "parse for block sem empty" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "{% for item in lista %}Item: {{ item }}{% endfor %}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| node.deinit(allocator);
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .for_block);
|
||||
const fb = nodes[0].@"for".?;
|
||||
try testing.expectEqualStrings("item", fb.loop_var);
|
||||
try testing.expectEqualStrings("lista", fb.iterable);
|
||||
try testing.expectEqual(@as(usize, 2), fb.body.len); // <--- corrigido: 2 nós
|
||||
try testing.expectEqual(@as(usize, 0), fb.empty_body.len);
|
||||
|
||||
try testing.expect(fb.body[0].type == .text);
|
||||
try testing.expectEqualStrings("Item: ", fb.body[0].text.?.content);
|
||||
try testing.expect(fb.body[1].type == .variable);
|
||||
try testing.expectEqualStrings("item", fb.body[1].variable.?.expr);
|
||||
}
|
||||
|
||||
test "parse for block com empty" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "{% for item in lista %}Tem{% empty %}Vazio{% endfor %}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| node.deinit(allocator);
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .for_block);
|
||||
const fb = nodes[0].@"for".?;
|
||||
try testing.expectEqual(@as(usize, 1), fb.body.len);
|
||||
try testing.expectEqualStrings("Tem", fb.body[0].text.?.content);
|
||||
try testing.expectEqual(@as(usize, 1), fb.empty_body.len);
|
||||
try testing.expectEqualStrings("Vazio", fb.empty_body[0].text.?.content);
|
||||
}
|
||||
|
||||
test "parse comment" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "Bazinga! {% comment %}{% for item in lista %}Tem{% empty %}Vazio{% endfor %}{% endcomment %}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| node.deinit(allocator);
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .text);
|
||||
try testing.expectEqualStrings("Bazinga! ", nodes[0].text.?.content);
|
||||
}
|
||||
|
||||
test "parse include simples" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "Cabeçalho {% include \"header.zdt\" %} Conteúdo {% include \"footer.zdt\" %}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| node.deinit(allocator);
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 4), nodes.len);
|
||||
|
||||
try testing.expect(nodes[0].type == .text);
|
||||
try testing.expectEqualStrings("Cabeçalho ", nodes[0].text.?.content);
|
||||
|
||||
try testing.expect(nodes[1].type == .include);
|
||||
try testing.expectEqualStrings("header.zdt", nodes[1].include.?.template_name);
|
||||
|
||||
try testing.expect(nodes[2].type == .text);
|
||||
try testing.expectEqualStrings(" Conteúdo ", nodes[2].text.?.content);
|
||||
|
||||
try testing.expect(nodes[3].type == .include);
|
||||
try testing.expectEqualStrings("footer.zdt", nodes[3].include.?.template_name);
|
||||
|
||||
}
|
||||
|
||||
test "parse include sem aspas (erro esperado no futuro, mas por enquanto aceita)" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "{% include header.zdt %}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| node.deinit(allocator);
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .include);
|
||||
try testing.expectEqualStrings("header.zdt", nodes[0].include.?.template_name);
|
||||
}
|
||||
|
||||
test "parse with simples" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "{% with nome=\"Lucas\" idade=30 %}Olá {{ nome }}, você tem {{ idade }} anos.{% endwith %}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| node.deinit(allocator);
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .with_block);
|
||||
const w = nodes[0].with.?;
|
||||
try testing.expectEqual(@as(usize, 2), w.assignments.len);
|
||||
try testing.expectEqual(@as(usize, 5), w.body.len);
|
||||
try testing.expect(w.body[0].type == .text);
|
||||
}
|
||||
|
||||
test "parse now simples" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "Data atual: {% now \"Y-m-d\" %} às {% now \"H:i\" %}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| node.deinit(allocator);
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 4), nodes.len);
|
||||
|
||||
try testing.expect(nodes[0].type == .text);
|
||||
try testing.expectEqualStrings("Data atual: ", nodes[0].text.?.content);
|
||||
|
||||
try testing.expect(nodes[1].type == .now);
|
||||
try testing.expectEqualStrings("Y-m-d", nodes[1].now.?.format);
|
||||
|
||||
try testing.expect(nodes[2].type == .text);
|
||||
try testing.expectEqualStrings(" às ", nodes[2].text.?.content);
|
||||
|
||||
try testing.expect(nodes[3].type == .now);
|
||||
try testing.expectEqualStrings("H:i", nodes[3].now.?.format);
|
||||
|
||||
}
|
||||
|
||||
test "parse now sem aspas" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "{% now Y-m-d %}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| node.deinit(allocator);
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .now);
|
||||
try testing.expectEqualStrings("Y-m-d", nodes[0].now.?.format);
|
||||
}
|
||||
|
||||
test "parse extends" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "{% extends \"base.zdt\" %}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| node.deinit(allocator);
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .extends);
|
||||
try testing.expectEqualStrings("base.zdt", nodes[0].extends.?.parent_name);
|
||||
}
|
||||
|
||||
test "parse block simples" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "{% block titulo %}Meu Título{% endblock %}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| node.deinit(allocator);
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .block);
|
||||
const b = nodes[0].block.?;
|
||||
try testing.expectEqualStrings("titulo", b.name);
|
||||
try testing.expectEqual(@as(usize, 1), b.body.len);
|
||||
try testing.expectEqualStrings("Meu Título", b.body[0].text.?.content);
|
||||
}
|
||||
|
||||
test "parse block com super" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "{% block conteudo %}{{ block.super }} Conteúdo filho{% endblock %}";
|
||||
|
|
@ -342,3 +342,132 @@ test "parse block com super" {
|
|||
// try testing.expectEqualStrings("conteudo", b.name);
|
||||
try testing.expectEqualStrings(" Conteúdo filho", b.body[1].text.?.content);
|
||||
}
|
||||
|
||||
|
||||
test "parse filter block simples" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "{% filter upper %}olá mundo{% endfilter %}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| node.deinit(allocator);
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .filter_block);
|
||||
const fb = nodes[0].filter_block.?;
|
||||
try testing.expectEqualStrings("upper", fb.filters); // correto
|
||||
try testing.expectEqual(@as(usize, 1), fb.body.len);
|
||||
try testing.expectEqualStrings("olá mundo", fb.body[0].text.?.content);
|
||||
}
|
||||
|
||||
test "parse filter block com múltiplos filtros" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "{% filter upper|escape %}Conteúdo <b>negrito</b>{% endfilter %}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| node.deinit(allocator);
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .filter_block);
|
||||
const fb = nodes[0].filter_block.?;
|
||||
try testing.expectEqualStrings("upper|escape", fb.filters);
|
||||
try testing.expectEqual(@as(usize, 1), fb.body.len);
|
||||
try testing.expectEqualStrings("Conteúdo <b>negrito</b>", fb.body[0].text.?.content);
|
||||
}
|
||||
|
||||
test "parse variable com filtros" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "{{ nome|upper|default:\"Visitante\" }}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| node.deinit(allocator);
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .variable);
|
||||
const v = nodes[0].variable.?;
|
||||
try testing.expectEqualStrings("nome", v.expr);
|
||||
try testing.expectEqual(@as(usize, 2), v.filters.len);
|
||||
try testing.expectEqualStrings("upper", v.filters[0].name);
|
||||
try testing.expect(v.filters[0].arg == null);
|
||||
try testing.expectEqualStrings("default", v.filters[1].name);
|
||||
try testing.expectEqualStrings("Visitante", v.filters[1].arg.?);
|
||||
}
|
||||
|
||||
test "parse autoescape on" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "{% autoescape on %}Texto <b>negrito</b>{% endautoescape %}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| node.deinit(allocator);
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .autoescape);
|
||||
const ae = nodes[0].autoescape.?;
|
||||
try testing.expect(ae.enabled == true);
|
||||
try testing.expectEqual(@as(usize, 1), ae.body.len);
|
||||
try testing.expectEqualStrings("Texto <b>negrito</b>", ae.body[0].text.?.content);
|
||||
}
|
||||
|
||||
test "parse autoescape off" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "{% autoescape off %}Texto <b>negrito</b>{% endautoescape %}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| node.deinit(allocator);
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .autoescape);
|
||||
const ae = nodes[0].autoescape.?;
|
||||
try testing.expect(ae.enabled == false);
|
||||
try testing.expectEqual(@as(usize, 1), ae.body.len);
|
||||
}
|
||||
|
||||
test "parse spaceless simples" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "{% spaceless %}<p> Texto com espaços </p>{% endspaceless %}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| node.deinit(allocator);
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .spaceless);
|
||||
const sl = nodes[0].spaceless.?;
|
||||
try testing.expectEqual(@as(usize, 1), sl.body.len);
|
||||
try testing.expectEqualStrings("<p> Texto com espaços </p>", sl.body[0].text.?.content);
|
||||
}
|
||||
|
||||
test "parse spaceless aninhado" {
|
||||
const allocator = testing.allocator;
|
||||
const template = "{% spaceless %}Outer {% spaceless %}Inner{% endspaceless %} Outer{% endspaceless %}";
|
||||
const nodes = try parser.parse(allocator, template);
|
||||
defer {
|
||||
for (nodes) |node| node.deinit(allocator);
|
||||
allocator.free(nodes);
|
||||
}
|
||||
|
||||
try testing.expectEqual(@as(usize, 1), nodes.len);
|
||||
try testing.expect(nodes[0].type == .spaceless);
|
||||
const sl = nodes[0].spaceless.?;
|
||||
for (sl.body) |b| {
|
||||
std.debug.print("type: {s}\n", .{@tagName(b.type)});
|
||||
if (b.type == .spaceless) {
|
||||
std.debug.print(" - tag -> spaceless\n", .{});
|
||||
}
|
||||
if(b.type == .text) {
|
||||
std.debug.print(" - text -> {s}\n", .{b.text.?.content});
|
||||
}
|
||||
std.debug.print("----------\n", .{});
|
||||
}
|
||||
try testing.expectEqual(@as(usize, 3), sl.body.len);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue