155 lines
5.2 KiB
Python
155 lines
5.2 KiB
Python
"""Tests for the flavors module."""
|
|
|
|
from regex_humanizer.flavors import (
|
|
get_flavor,
|
|
get_supported_flavors,
|
|
validate_flavor,
|
|
detect_flavor,
|
|
check_feature_support,
|
|
get_compatibility_warnings,
|
|
)
|
|
|
|
|
|
class TestFlavorRegistry:
|
|
"""Tests for the FlavorRegistry class."""
|
|
|
|
def test_list_flavors(self):
|
|
"""Test listing all supported flavors."""
|
|
flavors = get_supported_flavors()
|
|
assert "pcre" in flavors
|
|
assert "javascript" in flavors
|
|
assert "python" in flavors
|
|
assert "go" in flavors
|
|
|
|
def test_get_flavor(self):
|
|
"""Test getting a flavor by name."""
|
|
flavor = get_flavor("pcre")
|
|
assert flavor is not None
|
|
assert flavor.name == "pcre"
|
|
|
|
def test_get_invalid_flavor(self):
|
|
"""Test getting an invalid flavor returns None."""
|
|
flavor = get_flavor("invalid")
|
|
assert flavor is None
|
|
|
|
def test_validate_flavor_valid(self):
|
|
"""Test validating a valid flavor."""
|
|
assert validate_flavor("pcre") is True
|
|
assert validate_flavor("javascript") is True
|
|
|
|
def test_validate_flavor_invalid(self):
|
|
"""Test validating an invalid flavor."""
|
|
assert validate_flavor("invalid") is False
|
|
|
|
def test_flavor_has_features(self):
|
|
"""Test that flavors have feature support information."""
|
|
flavor = get_flavor("pcre")
|
|
assert flavor is not None
|
|
assert len(flavor.supported_features) > 0
|
|
|
|
|
|
class TestDetectFlavor:
|
|
"""Tests for the detect_flavor function."""
|
|
|
|
def test_detect_pcre_features(self):
|
|
"""Test detecting PCRE-specific features."""
|
|
flavor = detect_flavor(r"(?P<name>pattern)\k<name>")
|
|
assert flavor == "pcre"
|
|
|
|
def test_detect_js_lookahead(self):
|
|
"""Test detecting JavaScript patterns."""
|
|
flavor = detect_flavor(r"(?=pattern)")
|
|
assert flavor in ("javascript", "pcre")
|
|
|
|
def test_detect_go_backslash_k(self):
|
|
"""Test detecting Go patterns."""
|
|
flavor = detect_flavor(r"\k<name>")
|
|
assert flavor in ("go", "python", "pcre")
|
|
|
|
def test_detect_possessive_quantifiers(self):
|
|
"""Test detecting possessive quantifiers."""
|
|
flavor = detect_flavor(r"a++")
|
|
assert flavor == "pcre"
|
|
|
|
|
|
class TestFeatureSupport:
|
|
"""Tests for checking feature support."""
|
|
|
|
def test_check_js_lookbehind(self):
|
|
"""Test that JavaScript doesn't support lookbehind."""
|
|
pattern = r"(?<=pattern)"
|
|
unsupported = check_feature_support(pattern, "javascript")
|
|
assert "lookbehind" in unsupported
|
|
|
|
def test_check_go_lookbehind(self):
|
|
"""Test that Go doesn't support lookbehind."""
|
|
pattern = r"(?<=pattern)"
|
|
unsupported = check_feature_support(pattern, "go")
|
|
assert "lookbehind" in unsupported
|
|
|
|
def test_check_js_possessive(self):
|
|
"""Test that JavaScript doesn't support possessive quantifiers."""
|
|
pattern = r"a++"
|
|
unsupported = check_feature_support(pattern, "javascript")
|
|
assert "possessive_quantifiers" in unsupported
|
|
|
|
def test_pcre_supports_lookbehind(self):
|
|
"""Test that PCRE supports lookbehind."""
|
|
pattern = r"(?<=pattern)"
|
|
unsupported = check_feature_support(pattern, "pcre")
|
|
assert "lookbehind" not in unsupported
|
|
|
|
|
|
class TestCompatibilityWarnings:
|
|
"""Tests for generating compatibility warnings."""
|
|
|
|
def test_js_lookbehind_warning(self):
|
|
"""Test warning for JavaScript lookbehind."""
|
|
pattern = r"(?<=pattern)"
|
|
warnings = get_compatibility_warnings(pattern, "javascript")
|
|
assert len(warnings) > 0
|
|
warning_types = [w.feature for w in warnings]
|
|
assert "lookbehind" in warning_types
|
|
|
|
def test_go_backreference_warning(self):
|
|
"""Test warning for Go named backreferences."""
|
|
pattern = r"\k<name>"
|
|
warnings = get_compatibility_warnings(pattern, "go")
|
|
warning_types = [w.feature for w in warnings]
|
|
assert "named_groups" in warning_types or "backreferences_general" in warning_types or "named_backreferences" in warning_types
|
|
|
|
def test_pcre_no_warnings(self):
|
|
"""Test that PCRE has no warnings for basic patterns."""
|
|
pattern = r"\w+"
|
|
warnings = get_compatibility_warnings(pattern, "pcre")
|
|
assert len(warnings) == 0
|
|
|
|
def test_warning_severity(self):
|
|
"""Test that warnings have proper severity levels."""
|
|
pattern = r"(?<=pattern)"
|
|
warnings = get_compatibility_warnings(pattern, "javascript")
|
|
assert len(warnings) > 0
|
|
for w in warnings:
|
|
assert w.severity in ("warning", "error")
|
|
|
|
|
|
class TestFlavorAttributes:
|
|
"""Tests for flavor attributes."""
|
|
|
|
def test_flavor_display_name(self):
|
|
"""Test that flavors have display names."""
|
|
flavor = get_flavor("pcre")
|
|
assert flavor.display_name == "PCRE"
|
|
flavor = get_flavor("javascript")
|
|
assert flavor.display_name == "JavaScript"
|
|
|
|
def test_flavor_description(self):
|
|
"""Test that flavors have descriptions."""
|
|
flavor = get_flavor("python")
|
|
assert len(flavor.description) > 0
|
|
|
|
def test_flavor_quirks(self):
|
|
"""Test that flavors have quirk information."""
|
|
flavor = get_flavor("go")
|
|
assert len(flavor.quirks) > 0
|