fix: add scoped error logging for lexer/parser errors
- Add std.log.scoped(.pugz) to template.zig and view_engine.zig - Log detailed error info (code, line, column, message) when parsing fails - Log template path context in ViewEngine on parse errors - Remove debug print from lexer, use proper scoped logging instead - Move benchmarks, docs, examples, playground, tests out of src/ to project root - Update build.zig and documentation paths accordingly - Bump version to 0.3.1
This commit is contained in:
76
tests/helper.zig
Normal file
76
tests/helper.zig
Normal file
@@ -0,0 +1,76 @@
|
||||
//! Test helper for Pugz engine
|
||||
//! Provides common utilities for template testing
|
||||
|
||||
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 == '<') {
|
||||
// Strip trailing whitespace before tags
|
||||
while (result.items.len > 0 and (result.items[result.items.len - 1] == ' ' or result.items[result.items.len - 1] == '\t')) {
|
||||
_ = result.pop();
|
||||
}
|
||||
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' or c == ' ' or c == '\t') {
|
||||
// Treat all whitespace (including newlines) uniformly
|
||||
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();
|
||||
|
||||
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");
|
||||
|
||||
// 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);
|
||||
}
|
||||
Reference in New Issue
Block a user