chsrc/tool/rawstr4c/test/test-parser.rakutest
2025-07-16 00:23:09 +08:00

72 lines
1.8 KiB
Raku

#!/usr/bin/env raku
# ---------------------------------------------------------------
# SPDX-License-Identifier: GPL-3.0-or-later
# ---------------------------------------------------------------
# Test File : test-parser.rakutest
# Test Authors : Aoran Zeng <ccmywish@qq.com>
# Created On : <2025-07-15>
# Last Modified : <2025-07-16>
# ---------------------------------------------------------------
use lib '../lib';
use Parser;
my $test-file1 = './fixture/test-hierarchy-with-root.md'.IO;
my $test-file2 = './fixture/test-hierarchy-without-root.md'.IO;
my $parser1 = Parser::Parser.new($test-file1);
my $parser2 = Parser::Parser.new($test-file2);
$parser1.parse();
$parser2.parse();
sub print-sections-flatly($parser)
{
say "====== sections ======";
for $parser.sections.kv -> $i, $section {
my $title = $section.title || "(Root)";
my $has-config = $section.config.keys ?? "" !! "";
my $has-code = $section.codeblock ?? "" !! "";
say " [$i] Level {$section.level}: $title - $has-config, $has-code";
}
}
sub print-sections-hierarchyly($parser) {
say "====== hierarchy ======";
my $indent = 0;
sub format-section($section, $level) {
my $prefix = ' ' x $level;
my $title = $section.title // '(Root)';
return "{$prefix}- {$title} (level {$section.level})";
}
sub print-section-tree($section, $level) {
say format-section($section, $level);
if $section.has-children {
for $section.children -> $child {
print-section-tree($child, $level + 1);
}
}
}
my $root = $parser.root-section();
print-section-tree($root, $indent);
}
sub print-parser-summary($parser) {
print-sections-flatly($parser);
print-sections-hierarchyly($parser);
}
# 测试两个文件
print-parser-summary($parser1);
say "\n\n";
print-parser-summary($parser2);