blob: 27b5ddfb2bdf3026d62aee92503710e4b5d9dcca (
plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
"""Tests for the :mod:`edify.errors.quantifier` exception classes."""
import pytest
from edify import Pattern, RegexBuilder
from edify.errors.quantifier import DanglingQuantifierError, StackedQuantifierError
def test_to_regex_string_raises_when_a_bare_quantifier_has_no_operand():
with pytest.raises(DanglingQuantifierError):
RegexBuilder().exactly(3).to_regex_string()
def test_to_regex_string_raises_for_optional_with_no_operand():
with pytest.raises(DanglingQuantifierError):
RegexBuilder().optional().to_regex_string()
def test_to_regex_string_raises_for_zero_or_more_with_no_operand():
with pytest.raises(DanglingQuantifierError):
RegexBuilder().zero_or_more().to_regex_string()
def test_to_regex_string_raises_for_one_or_more_with_no_operand():
with pytest.raises(DanglingQuantifierError):
RegexBuilder().one_or_more().to_regex_string()
def test_to_regex_string_raises_for_between_with_no_operand():
with pytest.raises(DanglingQuantifierError):
RegexBuilder().between(2, 5).to_regex_string()
def test_pattern_to_regex_string_raises_when_a_bare_quantifier_has_no_operand():
with pytest.raises(DanglingQuantifierError):
Pattern().exactly(3).to_regex_string()
def test_to_regex_raises_when_a_bare_quantifier_has_no_operand():
with pytest.raises(DanglingQuantifierError):
RegexBuilder().at_least(2).to_regex()
def test_dangling_message_hints_at_appending_an_operand():
with pytest.raises(DanglingQuantifierError, match="Append an element"):
RegexBuilder().exactly(3).to_regex_string()
def test_dangling_quantifier_error_message_contains_expected_text():
error = DanglingQuantifierError()
assert "Dangling quantifier" in str(error)
assert "no operand" in str(error)
def test_stacking_one_or_more_over_exactly_raises():
with pytest.raises(StackedQuantifierError):
RegexBuilder().one_or_more().exactly(3).digit()
def test_stacking_optional_over_at_least_raises():
with pytest.raises(StackedQuantifierError):
RegexBuilder().optional().at_least(2).digit()
def test_stacking_between_over_zero_or_more_raises():
with pytest.raises(StackedQuantifierError):
RegexBuilder().zero_or_more().between(1, 3).digit()
def test_stacking_lazy_variants_also_raises():
with pytest.raises(StackedQuantifierError):
RegexBuilder().one_or_more_lazy().exactly(2).digit()
def test_stacking_on_pattern_raises():
with pytest.raises(StackedQuantifierError):
Pattern().one_or_more().exactly(3).digit()
def test_a_valid_quantifier_element_quantifier_element_chain_works():
expr = RegexBuilder().one_or_more().digit().exactly(3).word()
assert expr.to_regex_string() == "\\d+\\w{3}"
def test_stacked_quantifier_error_message_contains_expected_text():
error = StackedQuantifierError()
assert "stack" in str(error)
assert "pending" in str(error)
|