Add test-parser.rakutest

This commit is contained in:
Aoran Zeng 2025-07-15 21:10:50 +08:00
parent 094acc1e85
commit efd98676b8
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98

View File

@ -0,0 +1,61 @@
#!/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-15>
# ---------------------------------------------------------------
use lib '../lib';
use Parser;
sub MAIN() {
my $test-file = './fixture/test-hierarchy.md'.IO;
unless $test-file.e {
die " test-hierarchy.md ";
}
my $parser = Parser::Parser.new($test-file);
$parser.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-hierarchy(@sections, $indent-level) {
my $indent = " " x $indent-level;
for @sections -> $section {
say "$indent- {$section.title}";
# 深度遍历
if $section.has-children {
print-sections-hierarchy($section.children, $indent-level + 1);
}
}
}