MCPcopy Create free account
hub / github.com/BYVoid/OpenCC / test_conversion

Function test_conversion

python/tests/test_opencc.py:65–93  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

63
64
65def 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
96def test_include_tofu_risk_dictionaries_option():

Callers

nothing calls this directly

Calls 6

convertMethod · 0.95
isinstanceFunction · 0.85
readMethod · 0.45
getMethod · 0.45
appendMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected