mirror of
https://github.com/RubyMetric/chsrc
synced 2025-07-20 23:26:47 +08:00
Test parser hierarchy
This commit is contained in:
parent
efd98676b8
commit
676089ee7a
74
tool/rawstr4c/test/fixture/test-hierarchy-with-global.md
Normal file
74
tool/rawstr4c/test/fixture/test-hierarchy-with-global.md
Normal file
@ -0,0 +1,74 @@
|
||||
- global-prefix = `GLOBAL_`
|
||||
- global-output = `:terminal`
|
||||
|
||||
```c
|
||||
int global_code_block = 1;
|
||||
```
|
||||
|
||||
some comments
|
||||
|
||||
|
||||
|
||||
# Java
|
||||
|
||||
- java-prefix = `JAVA_`
|
||||
|
||||
## Maven Config
|
||||
|
||||
- maven-name = `maven`
|
||||
|
||||
```xml
|
||||
<settings>
|
||||
<mirrors>
|
||||
</mirrors>
|
||||
</settings>
|
||||
```
|
||||
|
||||
## Gradle Config
|
||||
|
||||
```groovy
|
||||
repositories {
|
||||
maven { url 'https://example.com' }
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
# Python
|
||||
|
||||
- python-prefix = `PYTHON_`
|
||||
|
||||
## pip config
|
||||
|
||||
```bash
|
||||
pip config set global.index-url https://example.com
|
||||
```
|
||||
|
||||
## conda config
|
||||
|
||||
- language = `yaml`
|
||||
|
||||
```
|
||||
channels:
|
||||
- https://example.com/conda
|
||||
```
|
||||
|
||||
|
||||
|
||||
# Docker
|
||||
|
||||
## Dockerfile
|
||||
|
||||
```dockerfile
|
||||
FROM ubuntu:20.04
|
||||
RUN echo "Hello World"
|
||||
```
|
||||
|
||||
### Multi-stage Build
|
||||
|
||||
```dockerfile
|
||||
FROM node:16 AS builder
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
RUN npm install
|
||||
```
|
63
tool/rawstr4c/test/fixture/test-hierarchy-without-global.md
Normal file
63
tool/rawstr4c/test/fixture/test-hierarchy-without-global.md
Normal file
@ -0,0 +1,63 @@
|
||||
# Java
|
||||
|
||||
- java-prefix = `JAVA_`
|
||||
|
||||
## Maven Config
|
||||
|
||||
- maven-name = `maven`
|
||||
|
||||
```xml
|
||||
<settings>
|
||||
<mirrors>
|
||||
</mirrors>
|
||||
</settings>
|
||||
```
|
||||
|
||||
## Gradle Config
|
||||
|
||||
```groovy
|
||||
repositories {
|
||||
maven { url 'https://example.com' }
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
# Python
|
||||
|
||||
- python-prefix = `PYTHON_`
|
||||
|
||||
## pip config
|
||||
|
||||
```bash
|
||||
pip config set global.index-url https://example.com
|
||||
```
|
||||
|
||||
## conda config
|
||||
|
||||
- language = `yaml`
|
||||
|
||||
```
|
||||
channels:
|
||||
- https://example.com/conda
|
||||
```
|
||||
|
||||
|
||||
|
||||
# Docker
|
||||
|
||||
## Dockerfile
|
||||
|
||||
```dockerfile
|
||||
FROM ubuntu:20.04
|
||||
RUN echo "Hello World"
|
||||
```
|
||||
|
||||
### Multi-stage Build
|
||||
|
||||
```dockerfile
|
||||
FROM node:16 AS builder
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
RUN npm install
|
||||
```
|
@ -1,65 +0,0 @@
|
||||
# Global Config
|
||||
|
||||
- prefix = `TEST_`
|
||||
- output = `:terminal`
|
||||
- translate = `:escape`
|
||||
|
||||
## Java
|
||||
|
||||
### Maven Config
|
||||
|
||||
- name = `maven_settings`
|
||||
|
||||
```xml
|
||||
<mirror>
|
||||
<id>test</id>
|
||||
<name>Test Mirror</name>
|
||||
<url>https://example.com</url>
|
||||
</mirror>
|
||||
```
|
||||
|
||||
### Gradle Config
|
||||
|
||||
```groovy
|
||||
repositories {
|
||||
maven { url 'https://example.com' }
|
||||
}
|
||||
```
|
||||
|
||||
## Python
|
||||
|
||||
- prefix = `PY_`
|
||||
|
||||
### pip config
|
||||
|
||||
```ini
|
||||
[global]
|
||||
index-url = https://example.com/simple
|
||||
```
|
||||
|
||||
### conda config
|
||||
|
||||
- language = `yaml`
|
||||
|
||||
```
|
||||
channels:
|
||||
- https://example.com/conda
|
||||
```
|
||||
|
||||
## Docker
|
||||
|
||||
### Dockerfile
|
||||
|
||||
```dockerfile
|
||||
FROM ubuntu:20.04
|
||||
RUN echo "Hello World"
|
||||
```
|
||||
|
||||
#### Multi-stage Build
|
||||
|
||||
```dockerfile
|
||||
FROM node:16 AS builder
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
RUN npm install
|
||||
```
|
@ -5,57 +5,68 @@
|
||||
# Test File : test-parser.rakutest
|
||||
# Test Authors : Aoran Zeng <ccmywish@qq.com>
|
||||
# Created On : <2025-07-15>
|
||||
# Last Modified : <2025-07-15>
|
||||
# Last Modified : <2025-07-16>
|
||||
# ---------------------------------------------------------------
|
||||
|
||||
use lib '../lib';
|
||||
use Parser;
|
||||
|
||||
sub MAIN() {
|
||||
my $test-file = './fixture/test-hierarchy.md'.IO;
|
||||
unless $test-file.e {
|
||||
die "测试文件 test-hierarchy.md 不存在!";
|
||||
}
|
||||
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);
|
||||
|
||||
my $parser = Parser::Parser.new($test-file);
|
||||
$parser.parse;
|
||||
$parser1.parse();
|
||||
$parser2.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-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-hierarchy(@sections, $indent-level) {
|
||||
my $indent = " " x $indent-level;
|
||||
for @sections -> $section {
|
||||
say "$indent- {$section.title}";
|
||||
# 深度遍历
|
||||
|
||||
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 {
|
||||
print-sections-hierarchy($section.children, $indent-level + 1);
|
||||
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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user