|
| 1 | +import pytest |
| 2 | + |
| 3 | +from stdlib.re import CompiledRegex |
| 4 | + |
| 5 | + |
1 | 6 | def test_match_simple_pattern(regex_matcher): |
2 | 7 | """Test matching a simple pattern.""" |
3 | 8 | assert regex_matcher(r"^\d{3}-\d{2}-\d{4}$", "123-45-6789") == True |
@@ -52,3 +57,58 @@ def test_match_with_anchors(regex_matcher): |
52 | 57 | assert ( |
53 | 58 | regex_matcher(r"exact", "not exact") == False |
54 | 59 | ) # "not exact" is not the full string |
| 60 | + |
| 61 | + |
| 62 | +# Test valid regex compilation and matching |
| 63 | +def test_valid_regex(): |
| 64 | + regex = CompiledRegex(r"\d+") # Compile a regex pattern for digits |
| 65 | + assert regex.match("123") is True # Should match |
| 66 | + assert regex.match("abc") is False # Should not match |
| 67 | + |
| 68 | + |
| 69 | +# Test invalid regex pattern |
| 70 | +def test_invalid_regex(): |
| 71 | + with pytest.raises(ValueError, match="Invalid regex pattern"): |
| 72 | + CompiledRegex(r"*invalid*") # Invalid regex pattern |
| 73 | + |
| 74 | + |
| 75 | +# Test matching with a compiled regex |
| 76 | +def test_match_compiled(): |
| 77 | + regex = CompiledRegex(r"[A-Za-z]+") # Compile a regex pattern for letters |
| 78 | + assert regex.match("Hello") is True # Should match |
| 79 | + assert regex.match("123") is False # Should not match |
| 80 | + |
| 81 | + |
| 82 | +# Test multiple regex instances |
| 83 | +def test_multiple_regex_instances(): |
| 84 | + regex1 = CompiledRegex(r"\d{3}") # Compile a regex pattern for exactly 3 digits |
| 85 | + regex2 = CompiledRegex(r"[a-z]+") # Compile a regex pattern for lowercase letters |
| 86 | + |
| 87 | + assert regex1.match("123") is True # Should match |
| 88 | + assert regex1.match("12") is False # Should not match |
| 89 | + |
| 90 | + assert regex2.match("abc") is True # Should match |
| 91 | + assert regex2.match("ABC") is False # Should not match |
| 92 | + |
| 93 | + |
| 94 | +# Test releasing compiled regex |
| 95 | +def test_release_compiled(): |
| 96 | + regex = CompiledRegex(r"\w+") # Compile a regex pattern for word characters |
| 97 | + assert regex.match("word") is True # Should match |
| 98 | + del regex # Release the compiled regex |
| 99 | + # No direct way to test if the C++ object was released, but this ensures no crashes |
| 100 | + |
| 101 | + |
| 102 | +# Test edge cases |
| 103 | +def test_edge_cases(): |
| 104 | + # Empty pattern |
| 105 | + regex = CompiledRegex(r"") |
| 106 | + assert regex.match("") is True # Should match empty string |
| 107 | + assert regex.match("a") is True # Should match any string |
| 108 | + |
| 109 | + # Complex pattern |
| 110 | + regex = CompiledRegex( |
| 111 | + r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}$" |
| 112 | + ) # Email regex |
| 113 | + assert regex.match("test@example.com") is True # Should match |
| 114 | + assert regex.match("invalid-email") is False # Should not match |
0 commit comments