#!/usr/bin/env python3
"""Test the url_title_extractor module."""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
from url_title_extractor import extract_title, TitleParser
import json
from pathlib import Path
def test_title_parser():
"""Test basic HTML title extraction."""
parser = TitleParser()
parser.feed("
Python TestingContent")
assert parser.title == "Python Testing", f"Expected 'Python Testing', got '{parser.title}'"
return True
def test_extract_title():
"""Test URL title extraction."""
result = extract_title("https://example.com", use_cache=False)
return "title" in result or "error" in result
def test_cache_file_creation():
"""Test that cache file location is writable."""
cache_dir = Path(__file__).resolve().parent
cache_dir.mkdir(parents=True, exist_ok=True)
return cache_dir.exists() and cache_dir.is_dir()
if __name__ == "__main__":
tests = [
("TitleParser", test_title_parser),
("Cache directory", test_cache_file_creation),
]
results = []
for name, test_fn in tests:
try:
passed = test_fn()
results.append({"test": name, "passed": passed, "error": None})
print(f"PASSED: {name}")
except Exception as e:
results.append({"test": name, "passed": False, "error": str(e)})
print(f"FAILED: {name} - {e}")
# Print summary
passed_count = sum(1 for r in results if r["passed"])
print(f"\nSummary: {passed_count}/{len(results)} tests passed")
sys.exit(0 if passed_count == len(results) else 1)