From b8a2686d09835c8bcbd67d9d606206eb5ffdb027 Mon Sep 17 00:00:00 2001 From: 24_OskinEA <24_OskinEA@iux.local> Date: Mon, 7 Sep 2026 09:19:29 +0300 Subject: [PATCH] Add text analyzer tests --- tests/test_core.py | 190 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 tests/test_core.py diff --git a/tests/test_core.py b/tests/test_core.py new file mode 100644 index 0000000..a14913c --- /dev/null +++ b/tests/test_core.py @@ -0,0 +1,190 @@ +"""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, + )