mirror of
https://github.com/RubyMetric/chsrc
synced 2025-07-20 23:26:47 +08:00
62 lines
1.8 KiB
Raku
62 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-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);
|
||
}
|
||
}
|
||
}
|