From f8ec682474c38f6c761c24ae75b193f8f70fb4a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=88=AA=E5=AE=87?= <3364451258@qq.com> Date: Fri, 10 Jul 2026 15:29:10 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=8F=90=E5=8F=96=20MarkdownSplitter?= =?UTF-8?q?=20=E8=BE=B9=E7=95=8C=E6=B5=8B=E8=AF=95=E5=88=B0=E7=8B=AC?= =?UTF-8?q?=E7=AB=8B=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_splitters_markdown.py | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/test_splitters_markdown.py diff --git a/tests/test_splitters_markdown.py b/tests/test_splitters_markdown.py new file mode 100644 index 0000000..513f462 --- /dev/null +++ b/tests/test_splitters_markdown.py @@ -0,0 +1,43 @@ +"""MarkdownSplitter 边界测试.""" +import pytest +from src.core.splitters import MarkdownSplitter + + +class TestMarkdownSplitterEdgeCases: + """Markdown 分块边界情况.""" + + @pytest.fixture + def splitter(self): + return MarkdownSplitter(max_size=1000, overlap=100) + + def test_no_headings_document(self, splitter): + """无标题文档正常分块.""" + md = "这是一段没有标题的纯文本。\n\n第二段内容。" + chunks = splitter.split(md, source_file="nohead.md") + assert len(chunks) >= 1 + + def test_deep_headings(self, splitter): + """h4-h6 深层标题.""" + md = "# 一级\n## 二级\n### 三级\n#### 四级\n内容在这里。\n##### 五级\n更多内容。\n###### 六级\n最深的内容。" + chunks = splitter.split(md, source_file="deep.md") + assert len(chunks) >= 1 + + def test_hash_in_code_block_not_heading(self, splitter): + """代码块中的 # 号不被误识别为标题.""" + md = "# 真实标题\n这是内容。\n```python\n# 这不是标题,是注释\nx = 1\n## 这也不是标题\n```\n更多内容。" + chunks = splitter.split(md, source_file="codehash.md") + section_titles = [c.get("section_title", "") for c in chunks] + for title in section_titles: + assert "不是标题" not in title + + def test_adjacent_headings_empty_content(self, splitter): + """标题后紧接标题(空内容).""" + md = "# 标题 A\n# 标题 B\n内容 B。" + chunks = splitter.split(md, source_file="adjacent.md") + assert len(chunks) >= 1 + + def test_only_headings_no_content(self, splitter): + """仅有标题无正文.""" + md = "# 只有标题\n## 没有内容" + chunks = splitter.split(md, source_file="headingsonly.md") + assert isinstance(chunks, list)