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

73 lines
1.9 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-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);
$parser1.parse();
$parser2.parse();
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-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 {
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);