-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cases.py
More file actions
54 lines (45 loc) · 1.5 KB
/
Copy pathtest_cases.py
File metadata and controls
54 lines (45 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from pattern import Parser
import pytest
def test_empty_string_matching():
"""Empty patterns and empty strings"""
p = Parser("")
p.compile()
assert p.matcher("").match() == True
assert p.matcher("a").match() == False
def test_zero_width_assertions():
"""Anchors should not consume characters"""
p = Parser("^abc$")
p.compile()
assert p.matcher("abc").match() == True
assert p.matcher("xabc").match() == False
def test_catastrophic_backtracking():
"""Evil regex patterns that cause exponential time"""
# This should complete but may be slow
p = Parser("(a+)+b")
p.compile()
matcher = p.matcher("aaaaaaaaac") # No 'b' at end
result = matcher.match() # Should eventually return False
assert result == False
def test_nested_quantifiers():
"""Quantifiers on quantified expressions"""
p = Parser("(a*)*")
p.compile()
assert p.matcher("aaa").match() == True
def test_character_class_edge_cases():
"""Tricky character class patterns"""
# Empty class
with pytest.raises(SyntaxError):
Parser("[]").parse()
# ] as first character
p = Parser("[]abc]")
p.compile()
assert p.matcher("]").match() == True
def test_greedy_vs_lazy():
"""Verify greedy/lazy behavior"""
greedy = Parser("a*a")
greedy.compile()
lazy = Parser("a*?a")
lazy.compile()
# Both should match, but with different strategies
assert greedy.matcher("aaa").match() == True
assert lazy.matcher("aaa").match() == True