udate: add include, with_block and now into parser
This commit is contained in:
parent
da8c1563c6
commit
3019009325
2 changed files with 473 additions and 1 deletions
|
|
@ -184,3 +184,112 @@ test "parse for block com empty" {
|
|||
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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue