60 lines
2.1 KiB
Python
60 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Verification test for wordfreq_counter.py
|
|
|
|
Both the module under test and its input fixture are resolved from this
|
|
file's own directory. The test therefore grades the copy that sits beside
|
|
it and runs on a host that has never seen an artifact directory, and it
|
|
prints the resolved module path so a shadowed import is visible instead of
|
|
silent.
|
|
"""
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
HERE = Path(__file__).resolve().parent
|
|
FIXTURE = HERE / "test_input.txt"
|
|
|
|
# Import the module that sits beside this file.
|
|
sys.path.insert(0, str(HERE))
|
|
import wordfreq_counter as _mod
|
|
from wordfreq_counter import count_words, process_file
|
|
|
|
_IMPORTED = Path(_mod.__file__).resolve()
|
|
assert _IMPORTED.parent == HERE, (
|
|
"graded %r, not the copy beside this test (%r)" % (str(_IMPORTED), str(HERE))
|
|
)
|
|
assert FIXTURE.is_file(), "fixture missing: %r" % (str(FIXTURE),)
|
|
|
|
|
|
def test_count_words():
|
|
"""Test basic word counting."""
|
|
result = count_words("hello hello world")
|
|
assert result == {"hello": 2, "world": 1}, f"Expected hello:2, world:1, got {result}"
|
|
print("test_count_words: PASS")
|
|
|
|
|
|
def test_process_file():
|
|
"""Test file processing."""
|
|
result = process_file(str(FIXTURE))
|
|
assert result['total_words'] == 24, f"Expected 24 words, got {result['total_words']}"
|
|
assert result['unique_words'] == 18, f"Expected 18 unique words, got {result['unique_words']}"
|
|
assert result['word_counts']['the'] == 4, f"Expected 'the': 4, got {result['word_counts'].get('the')}"
|
|
assert result['file'] == str(FIXTURE), f"Reported file {result['file']!r}, not the fixture"
|
|
print("test_process_file: PASS")
|
|
|
|
|
|
def test_output_format():
|
|
"""Test JSON output format."""
|
|
result = process_file(str(FIXTURE))
|
|
required_keys = {'file', 'total_words', 'unique_words', 'word_counts'}
|
|
assert required_keys.issubset(result.keys()), f"Missing keys: {required_keys - result.keys()}"
|
|
print("test_output_format: PASS")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("module: %s" % str(_IMPORTED))
|
|
print("fixture: %s" % str(FIXTURE))
|
|
test_count_words()
|
|
test_process_file()
|
|
test_output_format()
|
|
print("\nAll tests passed!")
|