Files
text-analyzer/tests/test_core.py
2026-09-07 09:19:29 +03:00

191 lines
3.4 KiB
Python

"""Tests for text analyzer core."""
from pathlib import Path
import pytest
from text_analyzer.core import (
TextStatistics,
analyze_file,
analyze_text,
count_words,
extract_words,
)
@pytest.mark.parametrize(
("text", "expected"),
[
(
"Hello hello world",
["hello", "hello", "world"],
),
(
"One, two! THREE.",
["one", "two", "three"],
),
(
"Alpha-beta alpha-beta",
["alpha-beta", "alpha-beta"],
),
],
)
def test_extract_words(
text: str,
expected: list[str],
) -> None:
"""Test word extraction."""
assert extract_words(text) == expected
def test_extract_words_min_len() -> None:
"""Test minimum word length."""
text = "a bb ccc dddd"
assert extract_words(
text,
min_len=3,
) == ["ccc", "dddd"]
def test_count_words() -> None:
"""Test word frequency counting."""
result = count_words(
"apple apple banana",
)
assert result["apple"] == 2
assert result["banana"] == 1
def test_analyze_text() -> None:
"""Test complete text analysis."""
text = "Apple banana apple.\nBanana orange."
result = analyze_text(
text,
top=2,
)
assert result == TextStatistics(
lines=2,
words=5,
characters=len(text),
top_words=[
("apple", 2),
("banana", 2),
],
)
def test_empty_text() -> None:
"""Test empty text."""
result = analyze_text("")
assert result.lines == 0
assert result.words == 0
assert result.characters == 0
assert result.top_words == []
def test_empty_file(
tmp_path: Path,
) -> None:
"""Test empty file."""
path = tmp_path / "empty.txt"
path.write_text(
"",
encoding="utf-8",
)
result = analyze_file(path)
assert result.lines == 0
assert result.words == 0
assert result.characters == 0
def test_missing_file(
tmp_path: Path,
) -> None:
"""Test missing file."""
path = tmp_path / "missing.txt"
with pytest.raises(FileNotFoundError):
analyze_file(path)
def test_invalid_encoding(
tmp_path: Path,
) -> None:
"""Test invalid encoding."""
path = tmp_path / "invalid.txt"
path.write_bytes(b"\xff")
with pytest.raises(
UnicodeDecodeError,
):
analyze_file(
path,
encoding="utf-8",
)
@pytest.mark.parametrize(
("top", "expected"),
[
(
0,
[],
),
(
1,
[("apple", 3)],
),
(
2,
[
("apple", 3),
("banana", 2),
],
),
],
)
def test_top_parameter(
top: int,
expected: list[tuple[str, int]],
) -> None:
"""Test top-N parameter."""
text = "apple apple apple banana banana orange"
result = analyze_text(
text,
top=top,
)
assert result.top_words == expected
def test_invalid_min_len() -> None:
"""Test invalid min_len."""
with pytest.raises(
ValueError,
match="min_len",
):
analyze_text(
"hello",
min_len=0,
)
def test_invalid_top() -> None:
"""Test invalid top."""
with pytest.raises(
ValueError,
match="top",
):
analyze_text(
"hello",
top=-1,
)