Test parser hierarchy

This commit is contained in:
Aoran Zeng
2025-07-16 00:10:45 +08:00
parent efd98676b8
commit 676089ee7a
4 changed files with 187 additions and 104 deletions

View File

@@ -5,57 +5,68 @@
# Test File : test-parser.rakutest
# Test Authors : Aoran Zeng <ccmywish@qq.com>
# Created On : <2025-07-15>
# Last Modified : <2025-07-15>
# Last Modified : <2025-07-16>
# ---------------------------------------------------------------
use lib '../lib';
use Parser;
sub MAIN() {
my $test-file = './fixture/test-hierarchy.md'.IO;
unless $test-file.e {
die " test-hierarchy.md ";
}
my $test-file1 = './fixture/test-hierarchy-with-global.md'.IO;
my $test-file2 = './fixture/test-hierarchy-without-global.md'.IO;
my $parser1 = Parser::Parser.new($test-file1);
my $parser2 = Parser::Parser.new($test-file2);
my $parser = Parser::Parser.new($test-file);
$parser.parse;
$parser1.parse();
$parser2.parse();
say "====== ======";
for $parser.global-config.keys.sort -> $key {
my $value = $parser.global-config.get($key);
say "$key = {$value.as-string} (type: {$value.type})";
}
say "";
say "====== Section TOC ======";
print-sections-hierarchy($parser.root-sections, 0);
say "";
say "====== Section ======";
my @flat-sections = $parser.sections;
say " {+@flat-sections} sections:";
for @flat-sections -> $section {
my $hier-path = $section.get-hierarchical-path;
my $has-code = $section.raw-string ?? True !! False;
say " - {$section.title}";
say " : {$section.level}) $hier-path";
say " : $has-code";
# 显示section的配置
if $section.config.keys {
say " : " ~ $section.config.keys.join(", ");
}
say "";
sub print-sections-flatly($parser)
{
say "====== sections ======";
for $parser.sections.kv -> $i, $section {
my $title = $section.title || "(Global)";
my $has-config = $section.config.keys ?? "" !! "";
my $has-code = $section.raw-string ?? "" !! "";
say " [$i] Level {$section.level}: $title - $has-config, $has-code";
}
}
sub print-sections-hierarchy(@sections, $indent-level) {
my $indent = " " x $indent-level;
for @sections -> $section {
say "$indent- {$section.title}";
# 深度遍历
sub print-sections-hierarchyly($parser) {
say "====== hierarchy ======";
my $indent = 0;
sub format-section($section, $level) {
my $prefix = ' ' x $level;
my $title = $section.title // '(Global)';
return "{$prefix}- {$title} (level {$section.level})";
}
sub print-section-tree($section, $level) {
say format-section($section, $level);
if $section.has-children {
print-sections-hierarchy($section.children, $indent-level + 1);
for $section.children -> $child {
print-section-tree($child, $level + 1);
}
}
}
# 从parser的global section开始打印
my $global = $parser.global-section();
print-section-tree($global, $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);