70 lines
3.2 KiB
Python
70 lines
3.2 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""Словарь-исключения: чтение, комментарии, матч целого фрагмента, гомоглифы."""
|
||
import pytest
|
||
|
||
import app
|
||
|
||
|
||
@pytest.fixture
|
||
def dict_paths(tmp_path, monkeypatch):
|
||
d1 = tmp_path / "exceptions.txt"
|
||
d2 = tmp_path / "toponyms.txt"
|
||
monkeypatch.setattr(app, "EXCEPTIONS_FILES", [str(d1), str(d2)])
|
||
return d1, d2
|
||
|
||
|
||
def test_load_comments_and_missing_files(dict_paths):
|
||
d1, d2 = dict_paths
|
||
d1.write_text(
|
||
"# комментарий-секция\n"
|
||
"РОЧС\n"
|
||
" мбар \n"
|
||
"последнее слово", encoding="utf-8") # без завершающего \n — тоже грузится
|
||
words = app.load_exceptions()
|
||
assert words == {"рочс", "мбар", "последнее слово"}
|
||
d1.unlink()
|
||
if d2.exists():
|
||
d2.unlink()
|
||
assert app.load_exceptions() == set() # файлов нет — пустой словарь, не ошибка
|
||
|
||
|
||
def test_apply_exceptions_whole_fragment_only(dict_paths):
|
||
d1, _ = dict_paths
|
||
d1.write_text("машына\n", encoding="utf-8")
|
||
wl = app.load_exceptions()
|
||
items = [
|
||
{"fragment": " Машына "}, # целое совпадение, пробелы/регистр
|
||
{"fragment": "машынов"}, # частичное — НЕ отбрасывается
|
||
{"fragment": "в машыне"}, # фраза целиком не в словаре
|
||
{"fragment": ""}, # пустой — остаётся
|
||
{"fragment": "МАШЫНА"}, # регистр не важен
|
||
]
|
||
out, dropped = app.apply_exceptions(items, wl)
|
||
assert dropped == 2
|
||
assert [f["fragment"] for f in out] == ["машынов", "в машыне", ""]
|
||
|
||
|
||
def test_apply_exceptions_empty_whitelist():
|
||
items = [{"fragment": "что-то"}]
|
||
out, dropped = app.apply_exceptions(items, set())
|
||
assert out == items and dropped == 0
|
||
|
||
|
||
def test_homoglyph_latin_d_not_matched(dict_paths):
|
||
"""Гомоглиф-урок: латинская d в словаре не должна глушить кириллическое «дароги»."""
|
||
d1, _ = dict_paths
|
||
d1.write_text("dароги\n", encoding="utf-8") # латинская d
|
||
wl = app.load_exceptions()
|
||
out, _ = app.apply_exceptions([{"fragment": "дароги"}], wl) # кириллическая д
|
||
assert len(out) == 1 # кириллический вариант НЕ глушится
|
||
out, _ = app.apply_exceptions([{"fragment": "dароги"}], wl)
|
||
assert out == [] # точный гомоглиф глушится
|
||
|
||
|
||
def test_loader_three_paths_priority(tmp_path, monkeypatch):
|
||
"""Пути: рядом с app.py → cwd → родитель комплекта; все читаются вместе."""
|
||
d_root = tmp_path / "a.txt"
|
||
d_root.write_text("слово-корень\n", encoding="utf-8")
|
||
monkeypatch.setattr(app, "EXCEPTIONS_FILES", [str(d_root)])
|
||
assert "слово-корень" in app.load_exceptions()
|