mirror of
https://github.com/RubyMetric/chsrc
synced 2025-07-20 06:49:34 +08:00
Add test-parser.rakutest
This commit is contained in:
parent
094acc1e85
commit
efd98676b8
61
tool/rawstr4c/test/test-parser.rakutest
Normal file
61
tool/rawstr4c/test/test-parser.rakutest
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user