chsrc/tool/rawstr4c/test/test-parser.rakutest
2025-07-15 21:12:07 +08:00

62 lines
1.8 KiB
Raku
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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);
}
}
}