"""拼音校验模块测试。""" from pl_japanese.cleaner.pinyin_checker import ( check_line, check_segment, is_valid_syllable, _split_into_syllables, ) class TestSyllableValidity: def test_valid_syllables(self): for py in ['xue', 'jue', 'que', 'lv', 'nv', 'yun', 'zhi', 'shi', 'bao', 'shang', 'hai', 'xian', 'er', 'n', 'ng']: assert is_valid_syllable(py), f'{py} 应为合法音节' def test_invalid_syllables(self): for py in ['bap', 'ijao', 'qina', 'hue', 'xyz', 'blah']: assert not is_valid_syllable(py), f'{py} 应为非法音节' class TestSegmentCheck: def test_illegal_syllable_flagged(self): # 保 标成 bap(非法音节) problem = check_segment('保', 'bap') assert problem is not None assert 'bap' in problem assert 'bao' in problem # 给出正确建议 def test_wrong_reading_flagged(self): # 保 标成 xian(合法音节但非该字读音) problem = check_segment('保', 'xian') assert problem is not None assert 'bao' in problem def test_correct_reading_passes(self): assert check_segment('保', 'bao') is None assert check_segment('中', 'zhong') is None def test_non_hanzi_skipped(self): # 假名/字母分段拼音为空,跳过 assert check_segment('める', '') is None assert check_segment('Q', '') is None class TestLineCheck: def test_bad_line_flagged(self): line = '上|海|保|険:シャン|ハイ|ほ|けん:shang|hai|bap|xian' problems = check_line(line) assert len(problems) == 1 assert 'bap' in problems[0] def test_good_lines_pass(self): for good in [ '学|生:がく|せい:xue|sheng', '中|国:ちゅう|ごく:zhong|guo', '決|める:き|める:jue|', '血|液:けつ|えき:xue|ye', ]: assert check_line(good) == [], f'{good} 不应有告警' class TestSyllableSplit: def test_connected_pinyin_splits(self): assert _split_into_syllables('jinri') == ['jin', 'ri'] assert _split_into_syllables('nvjiang') == ['nv', 'jiang'] def test_unsplittable_returns_none(self): assert _split_into_syllables('bapxian') is None