From 676089ee7a5ee8e67526d57537e9b32370605b01 Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Wed, 16 Jul 2025 00:10:45 +0800 Subject: [PATCH] Test parser hierarchy --- .../fixture/test-hierarchy-with-global.md | 74 +++++++++++++++ .../fixture/test-hierarchy-without-global.md | 63 +++++++++++++ tool/rawstr4c/test/fixture/test-hierarchy.md | 65 -------------- tool/rawstr4c/test/test-parser.rakutest | 89 +++++++++++-------- 4 files changed, 187 insertions(+), 104 deletions(-) create mode 100644 tool/rawstr4c/test/fixture/test-hierarchy-with-global.md create mode 100644 tool/rawstr4c/test/fixture/test-hierarchy-without-global.md delete mode 100644 tool/rawstr4c/test/fixture/test-hierarchy.md diff --git a/tool/rawstr4c/test/fixture/test-hierarchy-with-global.md b/tool/rawstr4c/test/fixture/test-hierarchy-with-global.md new file mode 100644 index 0000000..4c9e741 --- /dev/null +++ b/tool/rawstr4c/test/fixture/test-hierarchy-with-global.md @@ -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 + + + + +``` + +## 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 +``` diff --git a/tool/rawstr4c/test/fixture/test-hierarchy-without-global.md b/tool/rawstr4c/test/fixture/test-hierarchy-without-global.md new file mode 100644 index 0000000..153bffc --- /dev/null +++ b/tool/rawstr4c/test/fixture/test-hierarchy-without-global.md @@ -0,0 +1,63 @@ +# Java + +- java-prefix = `JAVA_` + +## Maven Config + +- maven-name = `maven` + +```xml + + + + +``` + +## 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 +``` diff --git a/tool/rawstr4c/test/fixture/test-hierarchy.md b/tool/rawstr4c/test/fixture/test-hierarchy.md deleted file mode 100644 index d17469a..0000000 --- a/tool/rawstr4c/test/fixture/test-hierarchy.md +++ /dev/null @@ -1,65 +0,0 @@ -# Global Config - -- prefix = `TEST_` -- output = `:terminal` -- translate = `:escape` - -## Java - -### Maven Config - -- name = `maven_settings` - -```xml - - test - Test Mirror - https://example.com - -``` - -### 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 -``` diff --git a/tool/rawstr4c/test/test-parser.rakutest b/tool/rawstr4c/test/test-parser.rakutest index 6694796..e9030d7 100644 --- a/tool/rawstr4c/test/test-parser.rakutest +++ b/tool/rawstr4c/test/test-parser.rakutest @@ -5,57 +5,68 @@ # Test File : test-parser.rakutest # Test Authors : Aoran Zeng # 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);