aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authornatsuoto <[email protected]>2026-07-15 13:55:58 +0530
committernatsuoto <[email protected]>2026-07-15 13:55:58 +0530
commit2f69c608e319edb6b5af0133b4fddb2654aec96c (patch)
treefcde675b6970bcd12c1656d5503e72942f139a91 /tests
parent34694646345ce2a43c12df6fcb214371f3ee06c0 (diff)
downloadedify-2f69c608e319edb6b5af0133b4fddb2654aec96c.tar.xz
edify-2f69c608e319edb6b5af0133b4fddb2654aec96c.zip
feat(engine): unlock variable-width lookbehind under engine=regex; annotate the re-engine failure with an actionable error
Diffstat (limited to 'tests')
-rw-r--r--tests/builder/lookbehind.test.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/builder/lookbehind.test.py b/tests/builder/lookbehind.test.py
new file mode 100644
index 0000000..f16a6cc
--- /dev/null
+++ b/tests/builder/lookbehind.test.py
@@ -0,0 +1,59 @@
+"""Tests for lookbehind width behavior across the ``re`` and ``regex`` engines."""
+
+import pytest
+
+from edify import RegexBuilder
+from edify.errors.backend import VariableWidthLookbehindNotSupportedError
+
+
+def _variable_width_lookbehind_builder():
+ return RegexBuilder().assert_behind().between(1, 3).string("foo").end().string("bar")
+
+
+def _fixed_width_lookbehind_builder():
+ return RegexBuilder().assert_behind().string("foo").end().string("bar")
+
+
+def test_variable_width_lookbehind_under_re_raises_annotated_error():
+ with pytest.raises(VariableWidthLookbehindNotSupportedError) as excinfo:
+ _variable_width_lookbehind_builder().to_regex(engine="re")
+ text = str(excinfo.value)
+ assert "assert_behind" in text
+ assert "engine='regex'" in text
+ assert "= note:" in text
+ assert "help:" in text
+
+
+def test_variable_width_lookbehind_under_regex_compiles_and_matches():
+ compiled = _variable_width_lookbehind_builder().to_regex(engine="regex")
+ assert compiled.engine == "regex"
+ assert compiled.search("foobar") is not None
+ assert compiled.search("foofoobar") is not None
+ assert compiled.search("foofoofoobar") is not None
+ assert compiled.search("bar") is None
+
+
+def test_fixed_width_lookbehind_still_works_under_re():
+ compiled = _fixed_width_lookbehind_builder().to_regex(engine="re")
+ assert compiled.search("foobar") is not None
+ assert compiled.search("bar") is None
+
+
+def test_variable_width_lookbehind_error_chains_the_underlying_pattern_error():
+ with pytest.raises(VariableWidthLookbehindNotSupportedError) as excinfo:
+ _variable_width_lookbehind_builder().to_regex(engine="re")
+ import re
+
+ assert isinstance(excinfo.value.__cause__, re.error)
+
+
+def test_re_engine_still_surfaces_other_pattern_errors_unchanged(monkeypatch):
+ import re
+
+ def raise_other_error(_pattern, flags=0):
+ raise re.error("some other syntax error")
+
+ monkeypatch.setattr(re, "compile", raise_other_error)
+ with pytest.raises(re.error) as excinfo:
+ RegexBuilder().digit().to_regex(engine="re")
+ assert "some other syntax error" in str(excinfo.value)