add: тесты для unbreakables.py

This commit is contained in:
2025-07-22 13:12:07 +03:00
parent 7ff18dc2e8
commit acc592426f
6 changed files with 104 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
""" """
Typography - библиотека для экранной типографики текста с поддержкой HTML. etpgrf - библиотека для экранной типографики текста с поддержкой HTML.
Основные возможности: Основные возможности:
- Автоматическая расстановка переносов - Автоматическая расстановка переносов

View File

@@ -49,7 +49,7 @@ _EN_UNBREAKABLE_WORDS = frozenset([
'a', 'an', 'as', 'at', 'by', 'in', 'is', 'it', 'of', 'on', 'or', 'so', 'to', 'if', 'a', 'an', 'as', 'at', 'by', 'in', 'is', 'it', 'of', 'on', 'or', 'so', 'to', 'if',
# 3-4 letter words # 3-4 letter words
'for', 'from', 'into', 'that', 'then', 'they', 'this', 'was', 'were', 'what', 'when', 'with', 'for', 'from', 'into', 'that', 'then', 'they', 'this', 'was', 'were', 'what', 'when', 'with',
'not', 'but', 'which', 'not', 'but', 'which', 'the'
]) ])
# --- Настройки логирования --- # --- Настройки логирования ---

View File

@@ -28,7 +28,7 @@ if __name__ == '__main__':
print(result, "\n\n") print(result, "\n\n")
# Определяем пользовательские правила типографа # Определяем пользовательские правила типографа
typo_ru = etpgrf.Typographer(langs='ru', mode='mixed', hyphenation=hyphen_settings) typo_ru = etpgrf.Typographer(langs='ru', mode='unicode', hyphenation=hyphen_settings)
result = typo_ru.process(text="Какой-то длинный текст для проверки переносов. Перпердикюляция!") result = typo_ru.process(text="Какой-то длинный текст для проверки переносов. Перпердикюляция!")
print(result, "\n\n") print(result, "\n\n")
@@ -80,15 +80,15 @@ if __name__ == '__main__':
"Later, over coffee, Anna joked, “I told the tailor, Make it so I never want to take it off. " "Later, over coffee, Anna joked, “I told the tailor, Make it so I never want to take it off. "
"Looks like they succeeded!") "Looks like they succeeded!")
etpgrf.defaults.etpgrf_settings.hyphenation.MAX_UNHYPHENATED_LEN = 6 etpgrf.defaults.etpgrf_settings.hyphenation.MAX_UNHYPHENATED_LEN = 6
typo_en = etpgrf.Typographer(langs='en', mode='mixed', hyphenation=True) typo_en = etpgrf.Typographer(langs='en', hyphenation=True)
result = typo_en.process(text=txt) result = typo_en.process(text=txt)
print(result, "\n\n--------------\n\n") print(result, "\n\n--------------\n\n")
# Проверяем если есть HTML-тегов # Проверяем если есть HTML-тегов
txt = ("<p>As they walked down the street, Anna noticed how the coats tailored cut moved gracefully with her." txt = ("<p>As they walked down the street, Anna noticed how the coats tailored cut moved gracefully with her."
" The consideration of every detail - from the <i>choice of fabric</i> to the delicate embroidery - made it" " The consideration of every detail&nbsp;- from the <i>choice of fabric</i> to the delicate embroidery - made it"
" clear that this was no ordinary coat.</p><style>body { font-family: Arial; }</style>") " clear that this was no ordinary coat.</p><style>body { font-family: Arial; }</style>")
typo_en = etpgrf.Typographer(langs='en', mode='mixed', process_html=True, hyphenation=True) typo_en = etpgrf.Typographer(langs='en', process_html=True, hyphenation=True)
result = typo_en.process(text=txt) result = typo_en.process(text=txt)
print(result, "\n\n--------------\n\n") print(result, "\n\n--------------\n\n")

View File

@@ -4,3 +4,10 @@ beautifulsoup4==4.13.4
soupsieve==2.7 soupsieve==2.7
typing_extensions==4.14.1 typing_extensions==4.14.1
pytest==8.4.1
exceptiongroup==1.3.0
iniconfig==2.1.0
packaging==25.0
pluggy==1.6.0
Pygments==2.19.2
tomli==2.2.1

1
tests/__init__.py Normal file
View File

@@ -0,0 +1 @@
# Тесты etpgrf - библиотеки для экранной типографики текста с поддержкой HTML.

View File

@@ -0,0 +1,90 @@
# tests/test_unbreakables.py
import pytest
from etpgrf import Unbreakables
# Список русских слов, которые должны "приклеиваться" к следующему слову.
RUSSIAN_PREPOSITIONS_TO_TEST = [
# Предлоги (только короткие... длинные, типа `ввиду`, `ввиду` и т.п., могут быть "висячими")
'в', 'без', 'до', 'из', 'к', 'на', 'по', 'о', 'от', 'перед', 'при', 'через', 'с', 'у', 'за', 'над',
'об', 'под', 'про', 'для', 'ко', 'со', 'без', 'то', 'во', 'из-за', 'из-под', 'как'
# Союзы (без сложных, тип `как будто`, `как если бы`, `за то` и т.п.)
'и', 'а', 'но', 'да', 'как',
# Частицы
'не', 'ни',
# Местоимения
'я', 'ты', 'он', 'мы', 'вы', 'им', 'их', 'ей', 'ею',
# Устаревшие или специфичные
'сей', 'сия', 'сие',
]
@pytest.mark.parametrize("word", RUSSIAN_PREPOSITIONS_TO_TEST)
def test_russian_prepositions_are_unbreakable(word):
"""
Проверяет ПОВЕДЕНИЕ: короткие слова "приклеиваются" к следующему. Для русского языка. Параметризованный тест
"""
# Arrange (подготовка)
unbreakables_ru = Unbreakables(langs='ru')
input_text = f"Проверка {word} тестирование."
expected_text = f"Проверка {word}\u00A0тестирование."
# Act (действие, которое выполняем)
actual_text = unbreakables_ru.process(input_text)
# Assert (утверждение, что результат соответствует ожиданиям)
assert actual_text == expected_text
# Список английских слов, которые должны "приклеиваться" к следующему слову.
ENGLISH_PREPOSITIONS_TO_TEST = [
'a', 'an', 'as', 'at', 'by', 'in', 'is', 'it', 'of', 'on', 'or', 'so', 'to', 'if',
'for', 'from', 'into', 'that', 'then', 'they', 'this', 'was', 'were', 'what', 'when', 'with',
'not', 'but', 'which', 'the'
]
@pytest.mark.parametrize("word", ENGLISH_PREPOSITIONS_TO_TEST)
def test_english_prepositions_are_unbreakable(word):
"""
Проверяет ПОВЕДЕНИЕ: короткие слова "приклеиваются" к следующему. Для английского языка. Параметризованный тест
"""
# Arrange (подготовка)
unbreakables_en = Unbreakables(langs='en')
input_text = f"Training {word} test."
expected_text = f"Training {word}\u00A0test."
# Act (действие, которое выполняем)
actual_text = unbreakables_en.process(input_text)
# Assert (утверждение, что результат соответствует ожиданиям)
assert actual_text == expected_text
# Смешанный тест для русского и английского языков
def test_mix_prepositions_are_unbreakable():
"""
Проверяет ПОВЕДЕНИЕ: короткие слова "приклеиваются" к следующему. Для смешанного русско-английского текста.
"""
# Arrange (подготовка)
unbreakables_mix = Unbreakables(langs='ru+en')
input_text = f"Для the Guardian он написал блестящую статью."
expected_text = f"Для\u00A0the\u00A0Guardian он\u00A0написал блестящую статью."
# Act (действие, которое выполняем)
actual_text = unbreakables_mix.process(input_text)
# Assert (утверждение, что результат соответствует ожиданиям)
assert actual_text == expected_text
# Список русских постпозитивных частиц, которые должны "приклеиваться" к предыдущему слову.
RUSSIAN_POSTPOSITIVE_PARTICLES_TO_TEST = [
'ли', 'ль', 'же', 'ж', 'бы', 'б'
]
@pytest.mark.parametrize("word", RUSSIAN_POSTPOSITIVE_PARTICLES_TO_TEST)
def test_russian_postpositive_particle(word):
"""
Проверяет ПОВЕДЕНИЕ: русские постпозитивные частицы "приклеиваются" к предыдущему слову.
"""
# Arrange
unbreakables_ru = Unbreakables(langs='ru')
input_text = f"Отчего {word} не поспать?"
expected_text = f"Отчего\u00A0{word} не\u00A0поспать?"
# Act
actual_text = unbreakables_ru.process(input_text)
# Assert
assert actual_text == expected_text