follow PugJs

This commit is contained in:
2026-01-24 23:53:19 +05:30
parent 621f8def47
commit 27c4898706
893 changed files with 44597 additions and 10484 deletions

View File

@@ -4,21 +4,72 @@
const std = @import("std");
const pugz = @import("pugz");
/// Normalizes HTML by removing indentation/formatting whitespace.
/// This allows comparing pretty vs non-pretty output.
fn normalizeHtml(allocator: std.mem.Allocator, html: []const u8) ![]const u8 {
var result = std.ArrayListUnmanaged(u8){};
var i: usize = 0;
var in_tag = false;
var last_was_space = false;
while (i < html.len) {
const c = html[i];
if (c == '<') {
in_tag = true;
last_was_space = false;
try result.append(allocator, c);
} else if (c == '>') {
in_tag = false;
last_was_space = false;
try result.append(allocator, c);
} else if (c == '\n' or c == '\r') {
// Skip newlines
i += 1;
continue;
} else if (c == ' ' or c == '\t') {
if (in_tag) {
// Preserve single space in tags for attribute separation
if (!last_was_space) {
try result.append(allocator, ' ');
last_was_space = true;
}
} else {
// Outside tags: skip leading whitespace after >
if (result.items.len > 0 and result.items[result.items.len - 1] != '>') {
if (!last_was_space) {
try result.append(allocator, ' ');
last_was_space = true;
}
}
}
i += 1;
continue;
} else {
last_was_space = false;
try result.append(allocator, c);
}
i += 1;
}
return result.toOwnedSlice(allocator);
}
/// Expects the template to produce the expected output when rendered with the given data.
/// Uses arena allocator for automatic cleanup.
/// Normalizes whitespace/formatting so pretty-print differences don't cause failures.
pub fn expectOutput(template: []const u8, data: anytype, expected: []const u8) !void {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
var lexer = pugz.Lexer.init(allocator, template);
const tokens = try lexer.tokenize();
var parser = pugz.Parser.init(allocator, tokens);
const doc = try parser.parse();
const raw_result = try pugz.render(allocator, doc, data);
const raw_result = try pugz.renderTemplate(allocator, template, data);
const result = std.mem.trimRight(u8, raw_result, "\n");
const expected_trimmed = std.mem.trimRight(u8, expected, "\n");
try std.testing.expectEqualStrings(expected, result);
// Normalize both for comparison (ignores pretty-print differences)
const norm_result = try normalizeHtml(allocator, result);
const norm_expected = try normalizeHtml(allocator, expected_trimmed);
try std.testing.expectEqualStrings(norm_expected, norm_result);
}