65 lines
1.7 KiB
Python
65 lines
1.7 KiB
Python
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from text_analyzer.core import analyze_file
|
|
|
|
|
|
def test_basic_analysis(tmp_path: Path) -> None:
|
|
test_file = tmp_path / "test.txt"
|
|
test_file.write_text("hello world\nhello python")
|
|
|
|
res = analyze_file(test_file)
|
|
assert res["lines"] == 2
|
|
assert res["words"] == 4
|
|
assert res["top_words"] == [("hello", 2), ("world", 1), ("python", 1)]
|
|
|
|
|
|
def test_file_not_found() -> None:
|
|
with pytest.raises(FileNotFoundError):
|
|
analyze_file(Path("non_existent_file_123.txt"))
|
|
|
|
|
|
def test_empty_file(tmp_path: Path) -> None:
|
|
test_file = tmp_path / "empty.txt"
|
|
test_file.write_text("")
|
|
|
|
res = analyze_file(test_file)
|
|
assert res["lines"] == 0
|
|
assert res["words"] == 0
|
|
assert res["top_words"] == []
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"min_len,expected_words",
|
|
[
|
|
(1, 4),
|
|
(3, 2),
|
|
(4, 1),
|
|
],
|
|
)
|
|
def test_min_length_filter(tmp_path: Path, min_len: int, expected_words: int) -> None:
|
|
test_file = tmp_path / "filter.txt"
|
|
test_file.write_text("a ab abc abcd")
|
|
|
|
res = analyze_file(test_file, min_len=min_len)
|
|
assert res["words"] == expected_words
|
|
|
|
|
|
def test_top_n_param(tmp_path: Path) -> None:
|
|
test_file = tmp_path / "top.txt"
|
|
test_file.write_text("one two two three three three")
|
|
|
|
res = analyze_file(test_file, top_n=2)
|
|
top_words = res["top_words"]
|
|
assert isinstance(top_words, list)
|
|
assert len(top_words) == 2
|
|
|
|
|
|
def test_punctuation_stripping(tmp_path: Path) -> None:
|
|
test_file = tmp_path / "punct.txt"
|
|
test_file.write_text("hello, world! hello...")
|
|
|
|
res = analyze_file(test_file)
|
|
assert res["top_words"] == [("hello", 2), ("world", 1)]
|