Skip to content

Commit b8243fd

Browse files
committed
Updated Tests for modified tester
1 parent a08cbbf commit b8243fd

4 files changed

Lines changed: 57 additions & 7 deletions

File tree

src/rexplain/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.2.3"
1+
__version__ = "0.2.4"
22

33
from .core.explainer import RegexExplainer
44
from .core.generator import ExampleGenerator

src/rexplain/core/parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ class Quantifier(RegexAST):
5151

5252
@dataclass
5353
class Anchor(RegexAST):
54-
"""
54+
r"""
5555
Represents anchors like ^, $, \b, etc.
5656
"""
5757
value: str
5858

5959
@dataclass
6060
class Escape(RegexAST):
61-
"""
61+
r"""
6262
Represents escape sequences like \d, \w, etc.
6363
"""
6464
value: str

src/rexplain/core/tester.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,14 @@ def test(self, pattern: str, test_string: str, flags: int = 0) -> MatchResult:
9292
import re as _re
9393
esc = node.value
9494
esc_re = _re.compile(esc)
95+
# Show the escape as written in the pattern (single backslash)
96+
display_esc = esc.encode('utf-8').decode('unicode_escape') if esc.startswith('\\') else esc
9597
if esc_re.fullmatch(c):
96-
details.append(f"{c!r} matches escape {esc} at position {j}")
98+
details.append(f"{c!r} matches escape {display_esc} at position {j}")
9799
i += 1
98100
j += 1
99101
else:
100-
reason = (f"Failed at position {j}: expected {esc}, got '{c}'")
102+
reason = (f"Failed at position {j}: expected {display_esc}, got '{c}'")
101103
return MatchResult(
102104
matches=False,
103105
reason=reason,

tests/test_tester.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def test_no_match():
1919
assert result.matches is False
2020
assert result.failed_at == 0
2121
assert result.partial_matches == []
22+
# Should mention expected literal and got
23+
assert 'expected literal' in result.reason and 'got' in result.reason
2224
print('test_no_match passed')
2325

2426
def test_partial_match():
@@ -29,7 +31,8 @@ def test_partial_match():
2931
assert result.matches is False
3032
assert result.failed_at == 2
3133
assert result.partial_matches == ['ab']
32-
assert 'unexpected character' in result.reason or 'failed at position' in result.reason
34+
# Should mention expected literal and got
35+
assert 'expected literal' in result.reason and 'got' in result.reason
3336
print('test_partial_match passed')
3437

3538
def test_too_short():
@@ -38,9 +41,52 @@ def test_too_short():
3841
assert result.matches is False
3942
assert result.failed_at == 2
4043
assert result.partial_matches == ['ab']
41-
assert 'too short' in result.reason or 'failed at position' in result.reason
44+
# Should mention string too short or expected more input
45+
assert 'too short' in result.reason or 'expected more input' in result.reason
4246
print('test_too_short passed')
4347

48+
def test_charclass_fail():
49+
tester = RegexTester()
50+
result = tester.test(r'[0-9][a-z][A-Z]', '1a!')
51+
assert result.matches is False
52+
assert result.failed_at == 2
53+
assert 'expected character in [A-Z]' in result.reason and 'got' in result.reason
54+
print('test_charclass_fail passed')
55+
56+
def test_escape_fail():
57+
tester = RegexTester()
58+
# \d
59+
result = tester.test(r'\d', 'a')
60+
assert result.matches is False
61+
assert result.failed_at == 0
62+
assert r'expected \d' in result.reason and 'got' in result.reason
63+
# \w
64+
result = tester.test(r'\w', '!')
65+
assert result.matches is False
66+
assert result.failed_at == 0
67+
assert r'expected \w' in result.reason and 'got' in result.reason
68+
# \s
69+
result = tester.test(r'\s', 'A')
70+
assert result.matches is False
71+
assert result.failed_at == 0
72+
assert r'expected \s' in result.reason and 'got' in result.reason
73+
# \D
74+
result = tester.test(r'\D', '5')
75+
assert result.matches is False
76+
assert result.failed_at == 0
77+
assert r'expected \D' in result.reason and 'got' in result.reason
78+
# \S
79+
result = tester.test(r'\S', ' ')
80+
assert result.matches is False
81+
assert result.failed_at == 0
82+
assert r'expected \S' in result.reason and 'got' in result.reason
83+
# \W
84+
result = tester.test(r'\W', 'a')
85+
assert result.matches is False
86+
assert result.failed_at == 0
87+
assert r'expected \W' in result.reason and 'got' in result.reason
88+
print('test_escape_fail passed')
89+
4490
def test_regex_features():
4591
tester = RegexTester()
4692
result = tester.test(r'\d{2,4}', '123')
@@ -67,6 +113,8 @@ def main():
67113
test_no_match()
68114
test_partial_match()
69115
test_too_short()
116+
test_charclass_fail()
117+
test_escape_fail()
70118
test_regex_features()
71119
test_flag_sensitive_match()
72120
print('All tester tests passed!')

0 commit comments

Comments
 (0)