| 1 |
"""Tests for cyclopts.docs.types module.""" |
| 2 |
|
| 3 |
from typing import get_args |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from cyclopts.docs.types import ( |
| 8 |
FORMAT_ALIASES, |
| 9 |
CanonicalDocFormat, |
| 10 |
DocFormat, |
| 11 |
normalize_format, |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
def test_format_aliases_sync_with_docformat(): |
| 16 |
"""Test that all FORMAT_ALIASES keys are in DocFormat type.""" |
| 17 |
|
| 18 |
doc_format_values = set(get_args(DocFormat)) |
| 19 |
|
| 20 |
|
| 21 |
format_alias_keys = set(FORMAT_ALIASES.keys()) |
| 22 |
|
| 23 |
|
| 24 |
assert format_alias_keys == doc_format_values, ( |
| 25 |
f"FORMAT_ALIASES keys {format_alias_keys} must match DocFormat values {doc_format_values}" |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
def test_format_aliases_values_are_canonical(): |
| 30 |
"""Test that all FORMAT_ALIASES values are valid canonical formats.""" |
| 31 |
canonical_values = set(get_args(CanonicalDocFormat)) |
| 32 |
|
| 33 |
for alias, canonical in FORMAT_ALIASES.items(): |
| 34 |
assert canonical in canonical_values, ( |
| 35 |
f"FORMAT_ALIASES['{alias}'] = '{canonical}' is not a valid CanonicalDocFormat" |
| 36 |
) |
| 37 |
|
| 38 |
|
| 39 |
def test_common_suffixes_have_format_aliases(): |
| 40 |
"""Test that common file suffixes have corresponding format aliases.""" |
| 41 |
|
| 42 |
common_suffixes = ["md", "markdown", "html", "htm", "rst", "rest"] |
| 43 |
|
| 44 |
for suffix in common_suffixes: |
| 45 |
assert suffix in FORMAT_ALIASES, f"Common suffix '{suffix}' should have a format alias" |
| 46 |
|
| 47 |
|
| 48 |
def test_canonical_formats_have_identity_mapping(): |
| 49 |
"""Test that canonical formats map to themselves in FORMAT_ALIASES.""" |
| 50 |
canonical_values = get_args(CanonicalDocFormat) |
| 51 |
|
| 52 |
for canonical in canonical_values: |
| 53 |
assert canonical in FORMAT_ALIASES, f"Canonical format '{canonical}' should be in FORMAT_ALIASES" |
| 54 |
assert FORMAT_ALIASES[canonical] == canonical, f"Canonical format '{canonical}' should map to itself" |
| 55 |
|
| 56 |
|
| 57 |
def test_file_suffix_handling(): |
| 58 |
"""Test that file suffixes work correctly with period stripping and format inference.""" |
| 59 |
|
| 60 |
test_cases = [ |
| 61 |
(".md", "md", "markdown"), |
| 62 |
(".markdown", "markdown", "markdown"), |
| 63 |
(".html", "html", "html"), |
| 64 |
(".htm", "htm", "html"), |
| 65 |
(".rst", "rst", "rst"), |
| 66 |
(".rest", "rest", "rst"), |
| 67 |
] |
| 68 |
|
| 69 |
for suffix_with_period, suffix_key, expected_canonical in test_cases: |
| 70 |
|
| 71 |
key = suffix_with_period.lstrip(".") |
| 72 |
assert key == suffix_key, f"Period stripping failed for '{suffix_with_period}'" |
| 73 |
|
| 74 |
|
| 75 |
assert key in FORMAT_ALIASES, f"Key '{key}' should be in FORMAT_ALIASES" |
| 76 |
|
| 77 |
|
| 78 |
assert FORMAT_ALIASES[key] == expected_canonical, ( |
| 79 |
f"FORMAT_ALIASES['{key}'] should be '{expected_canonical}', got '{FORMAT_ALIASES[key]}'" |
| 80 |
) |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
inferred_format = FORMAT_ALIASES.get(key) |
| 85 |
assert inferred_format == expected_canonical, ( |
| 86 |
f"Suffix '{suffix_with_period}' should infer format '{expected_canonical}', got '{inferred_format}'" |
| 87 |
) |
| 88 |
|
| 89 |
|
| 90 |
def test_normalize_format_with_all_aliases(): |
| 91 |
"""Test normalize_format works with all defined aliases.""" |
| 92 |
for alias, expected_canonical in FORMAT_ALIASES.items(): |
| 93 |
|
| 94 |
assert normalize_format(alias) == expected_canonical |
| 95 |
|
| 96 |
|
| 97 |
assert normalize_format(alias.upper()) == expected_canonical |
| 98 |
|
| 99 |
|
| 100 |
if len(alias) > 1: |
| 101 |
mixed_case = alias[0].upper() + alias[1:].lower() |
| 102 |
assert normalize_format(mixed_case) == expected_canonical |
| 103 |
|
| 104 |
|
| 105 |
def test_normalize_format_invalid(): |
| 106 |
"""Test normalize_format raises ValueError for invalid formats.""" |
| 107 |
with pytest.raises(ValueError, match='Unsupported format "invalid"'): |
| 108 |
normalize_format("invalid") |
| 109 |
|
| 110 |
with pytest.raises(ValueError, match='Unsupported format "pdf"'): |
| 111 |
normalize_format("pdf") |
| 112 |
|
| 113 |
|
| 114 |
def test_all_doc_format_values_have_normalization(): |
| 115 |
"""Test that all DocFormat literal values can be normalized.""" |
| 116 |
doc_format_values = get_args(DocFormat) |
| 117 |
|
| 118 |
for format_value in doc_format_values: |
| 119 |
|
| 120 |
result = normalize_format(format_value) |
| 121 |
|
| 122 |
assert result in get_args(CanonicalDocFormat), ( |
| 123 |
f"normalize_format('{format_value}') = '{result}' is not a CanonicalDocFormat" |
| 124 |
) |