| 63 | |
| 64 | |
| 65 | def test_conversion(): |
| 66 | import opencc |
| 67 | import re |
| 68 | from collections import defaultdict |
| 69 | |
| 70 | with open(_testcases_path, 'r', encoding='utf-8') as f: |
| 71 | content = f.read() |
| 72 | |
| 73 | # Strip comments and trailing commas |
| 74 | pattern = re.compile(r'"(?:[^"\\]|\\.)*"|(/\*[\s\S]*?\*/|//.*)|(,\s*(?=[\]}]))') |
| 75 | clean_content = pattern.sub(lambda m: "" if (m.group(1) or m.group(2)) else m.group(0), content) |
| 76 | parsed = json.loads(clean_content) |
| 77 | |
| 78 | # Group test cases by configuration |
| 79 | config_cases = defaultdict(list) |
| 80 | for case in parsed.get('cases', []): |
| 81 | input_text = case.get('input') |
| 82 | expected = case.get('expected', {}) |
| 83 | if not input_text or not isinstance(expected, dict): |
| 84 | continue |
| 85 | for cfg, ans in expected.items(): |
| 86 | config_cases[cfg].append((input_text, ans)) |
| 87 | |
| 88 | # Run conversions config-by-config, reusing the converter instance |
| 89 | for cfg, cases in config_cases.items(): |
| 90 | converter = opencc.OpenCC(f'{cfg}.json') |
| 91 | for input_text, ans in cases: |
| 92 | assert converter.convert(input_text) == ans, \ |
| 93 | 'Failed to convert {} for {} -> {}'.format(cfg, input_text, ans) |
| 94 | |
| 95 | |
| 96 | def test_include_tofu_risk_dictionaries_option(): |