From 08299b37dfda86e56e4f2b442f68ccd2da7a82e3 Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Fri, 24 Oct 2025 23:15:35 +0800 Subject: feat: Enhance Processor, RuleExtractor, and Renderers with type hints and improved documentation - Added type hints to Processor methods for better clarity and type safety. - Improved documentation for Processor methods, including detailed descriptions of parameters and return types. - Refactored RuleExtractor to support optional configuration file loading and added error handling for file operations. - Enhanced MarkdownRenderer to handle both list and dictionary inputs, with improved rendering logic. - Created comprehensive examples and tests for all components, ensuring robust functionality and error handling. - Added example rules for D&D 5E and structured output files for various formats (JSON, HTML, Markdown). - Established a testing framework with clear instructions and coverage reporting. --- tests/test_processor.py | 114 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 tests/test_processor.py (limited to 'tests/test_processor.py') diff --git a/tests/test_processor.py b/tests/test_processor.py new file mode 100644 index 0000000..c08fc52 --- /dev/null +++ b/tests/test_processor.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python3 +""" +Processor 模块单元测试 +""" + +import unittest +from conventionalrp.core.processor import Processor + + +class TestProcessor(unittest.TestCase): + """Processor 类的单元测试""" + + def setUp(self): + """设置测试环境""" + self.processor = Processor() + self.sample_tokens = [ + { + "type": "metadata", + "timestamp": "2025-10-24 14:30:01", + "speaker": "艾莉娅", + "content": [ + {"type": "dialogue", "content": "「测试对话」"} + ] + }, + { + "type": "metadata", + "timestamp": "2025-10-24 14:30:05", + "speaker": "DM", + "content": [ + {"type": "text", "content": "测试文本"} + ] + } + ] + + def test_init_without_rules(self): + """测试无规则初始化""" + processor = Processor() + self.assertEqual(processor.rules, {}) + + def test_init_with_rules(self): + """测试带规则初始化""" + rules = {"test_rule": "value"} + processor = Processor(rules) + self.assertEqual(processor.rules, rules) + + def test_process_tokens(self): + """测试处理 token 列表""" + result = self.processor.process_tokens(self.sample_tokens) + + self.assertIsInstance(result, list) + self.assertEqual(len(result), len(self.sample_tokens)) + + # 检查处理标记 + for token in result: + if "timestamp" in token: + self.assertTrue(token.get("processed")) + + def test_apply_rules(self): + """测试应用规则到单个 token""" + token = self.sample_tokens[0] + result = self.processor.apply_rules(token) + + self.assertIsInstance(result, dict) + self.assertIn("timestamp", result) + self.assertTrue(result.get("processed")) + + def test_generate_json_output(self): + """测试生成 JSON 输出""" + output = self.processor.generate_json_output(self.sample_tokens) + + self.assertIsInstance(output, str) + self.assertIn("timestamp", output) + self.assertIn("speaker", output) + + def test_generate_html_output(self): + """测试生成 HTML 输出""" + output = self.processor.generate_html_output(self.sample_tokens) + + self.assertIsInstance(output, str) + self.assertIn("", output) + self.assertIn("", output) + + def test_generate_markdown_output(self): + """测试生成 Markdown 输出""" + output = self.processor.generate_markdown_output(self.sample_tokens) + + self.assertIsInstance(output, str) + self.assertIn("-", output) + + def test_generate_output_json(self): + """测试生成输出 - JSON 格式""" + output = self.processor.generate_output(self.sample_tokens, "json") + self.assertIsInstance(output, str) + + def test_generate_output_html(self): + """测试生成输出 - HTML 格式""" + output = self.processor.generate_output(self.sample_tokens, "html") + self.assertIsInstance(output, str) + + def test_generate_output_markdown(self): + """测试生成输出 - Markdown 格式""" + output = self.processor.generate_output(self.sample_tokens, "markdown") + self.assertIsInstance(output, str) + + def test_generate_output_unsupported_format(self): + """测试生成输出 - 不支持的格式""" + with self.assertRaises(ValueError) as context: + self.processor.generate_output(self.sample_tokens, "pdf") + + self.assertIn("Unsupported format type", str(context.exception)) + + +if __name__ == "__main__": + unittest.main() -- cgit v1.2.3-70-g09d2