initial
This commit is contained in:
commit
da8c1563c6
7 changed files with 935 additions and 0 deletions
186
src/parser_test.zig
Normal file
186
src/parser_test.zig
Normal file
|
|
@ -0,0 +1,186 @@
|
|||
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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue