mymarkdown

My markdown
git clone https://git.grace.moe/mymarkdown
Log | Files | Refs

commit dd9e881dbd3f92d3f2274e3cdc5616146463a393
parent 2b57651ee184c93a9c69d5d4a680115fd637ee34
Author: gracefu <81774659+gracefuu@users.noreply.github.com>
Date:   Wed, 21 May 2025 03:02:11 +0800

Reenable the remaining tests

Diffstat:
Msrc/test/test.zig | 142+++++++++++++++++++++++++++++++++++++++++++------------------------------------
1 file changed, 78 insertions(+), 64 deletions(-)

diff --git a/src/test/test.zig b/src/test/test.zig @@ -7,32 +7,27 @@ const Ast = @import("../Ast.zig"); const GeneralPurposeAllocator = std.heap.GeneralPurposeAllocator(.{}); const ArenaAllocator = std.heap.ArenaAllocator; -fn testParse(input: []const u8, expected: []const u8) !void { +// This is in a function so the result is memoized. +fn padInput(comptime input: []const u8) []const u8 { + return input ++ "\n" ** 128; +} + +fn testParse(comptime input: []const u8, expected: []const u8) !void { + try testParseWithFn(input, expected, parse); + try testParseWithFn(input, expected, parse3); +} + +fn testParseWithFn(comptime input: []const u8, expected: []const u8, parseFn: anytype) !void { var arena: ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); - const safe_input = try arena.allocator().alloc(u8, input.len + 128); - @memcpy(safe_input[0..input.len], input); - @memset(safe_input[input.len..], '\n'); + const safe_input = padInput(input); - const ast = try parse(std.testing.allocator, arena.allocator(), safe_input); + const ast = try parseFn(std.testing.allocator, arena.allocator(), safe_input); var ast_render: std.ArrayListUnmanaged(u8) = .empty; defer ast_render.deinit(std.testing.allocator); try ast.renderAst(ast_render.writer(std.testing.allocator), safe_input); try std.testing.expectEqualStrings(expected, ast_render.items); - - // AstGen2 is too broken - // const ast2 = try parse2(std.testing.allocator, arena.allocator(), safe_input); - // var ast2_render: std.ArrayListUnmanaged(u8) = .empty; - // defer ast2_render.deinit(std.testing.allocator); - // try ast2.renderAst(ast2_render.writer(std.testing.allocator), safe_input); - // try std.testing.expectEqualStrings(expected, ast2_render.items); - - const ast3 = try parse3(std.testing.allocator, arena.allocator(), safe_input); - var ast3_render: std.ArrayListUnmanaged(u8) = .empty; - defer ast3_render.deinit(std.testing.allocator); - try ast3.renderAst(ast3_render.writer(std.testing.allocator), safe_input); - try std.testing.expectEqualStrings(expected, ast3_render.items); } test "Empty" { @@ -332,25 +327,44 @@ test "Thematic break" { ); } -// test "Mixed indentation" { -// try testParse("" ++ -// "+ aaa\n" ++ -// "\n" ++ -// "\tbbbbb\n", -// \\.document -// \\ .elaboration -// \\ .error .inconsistent_indentation at 3:1 -// \\ .marker -// \\ "+" -// \\ .paragraph -// \\ .text -// \\ "aaa" -// \\ .paragraph -// \\ .text -// \\ "bbbbb" -// \\ -// ); -// } +test "Mixed indentation" { + // AstGen1 used to try very hard to recover when it sees a tab + try testParseWithFn("" ++ + "+ aaa\n" ++ + "\n" ++ + "\tbbbbb\n", + \\.document + \\ .elaboration + \\ .error .inconsistent_indentation at 3:1 + \\ .marker + \\ "+" + \\ .paragraph + \\ .text + \\ "aaa" + \\ .paragraph + \\ .text + \\ "bbbbb" + \\ + , parse); + + try testParseWithFn("" ++ + "+ aaa\n" ++ + "\n" ++ + "\tbbbbb\n", + \\.document + \\ .elaboration + \\ .marker + \\ "+" + \\ .paragraph + \\ .text + \\ "aaa" + \\ .paragraph + \\ .error .inconsistent_indentation at 3:1 + \\ .text + \\ "bbbbb" + \\ + , parse3); +} test "Tabs in text" { try testParse("" ++ @@ -414,31 +428,31 @@ test "Super long line" { }), taggedAst); } -// test "Many short lines" { -// const input = try std.testing.allocator.create([(1 << 25) - 4]u8); -// defer std.testing.allocator.destroy(input); -// @memset(@as(*[(1 << 24) - 2][2]u8, @ptrCast(input)), "a\n"[0..2].*); +test "Many short lines" { + const input = try std.testing.allocator.create([(1 << 25) - 4]u8); + defer std.testing.allocator.destroy(input); + @memset(@as(*[(1 << 24) - 2][2]u8, @ptrCast(input)), "a\n"[0..2].*); -// var arena: ArenaAllocator = .init(std.testing.allocator); -// defer arena.deinit(); -// const ast = try parse(std.testing.allocator, arena.allocator(), input); -// try std.testing.expectEqual(1 << 24, ast.nodes.len); -// try std.testing.expectEqual( -// @as(Ast.Node.Tagged, .{ .document = .{ .num_children = 1 } }), -// ast.nodes[0].toTagged(), -// ); -// try std.testing.expectEqual( -// @as(Ast.Node.Tagged, .{ .paragraph = .{ .off = 0, .num_children = (1 << 24) - 2 } }), -// ast.nodes[1].toTagged(), -// ); -// try std.testing.expectEqual( -// @as(Ast.Node.Tagged, .{ .text = .{ .off = 0, .len = 1 } }), -// ast.nodes[2].toTagged(), -// ); -// for (1..(1 << 24) - 2) |i| { -// try std.testing.expectEqual( -// @as(Ast.Node.Tagged, .{ .space_text = .{ .off = @intCast(i * 2), .len = 1 } }), -// ast.nodes[i + 2].toTagged(), -// ); -// } -// } + var arena: ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const ast = try parse(std.testing.allocator, arena.allocator(), input); + try std.testing.expectEqual(1 << 24, ast.nodes.len); + try std.testing.expectEqual( + @as(Ast.Node.Tagged, .{ .document = .{ .num_children = 1 } }), + ast.nodes[0].toTagged(), + ); + try std.testing.expectEqual( + @as(Ast.Node.Tagged, .{ .paragraph = .{ .off = 0, .num_children = (1 << 24) - 2 } }), + ast.nodes[1].toTagged(), + ); + try std.testing.expectEqual( + @as(Ast.Node.Tagged, .{ .text = .{ .off = 0, .len = 1 } }), + ast.nodes[2].toTagged(), + ); + for (1..(1 << 24) - 2) |i| { + try std.testing.expectEqual( + @as(Ast.Node.Tagged, .{ .space_text = .{ .off = @intCast(i * 2), .len = 1 } }), + ast.nodes[i + 2].toTagged(), + ); + } +}