|
| 1 | +import pytest |
| 2 | +from regex_enumerator import RegexEnumerator |
| 3 | +from regex_enumerator.regex_parser import RegexError |
| 4 | + |
| 5 | + |
| 6 | +def test_grammar_group(): |
| 7 | + with pytest.raises(RegexError, match='Invalid group'): |
| 8 | + RegexEnumerator(r'(?)') |
| 9 | + |
| 10 | + with pytest.raises(RegexError, match='Invalid group'): |
| 11 | + RegexEnumerator(r'(?') |
| 12 | + |
| 13 | + with pytest.raises(RegexError, match='Invalid named group'): |
| 14 | + RegexEnumerator(r'(?<>)') |
| 15 | + |
| 16 | + with pytest.raises(RegexError, match='Invalid named group'): |
| 17 | + RegexEnumerator(r'(?<') |
| 18 | + |
| 19 | + with pytest.raises(RegexError, match='Duplicate named group'): |
| 20 | + RegexEnumerator(r'(?<name>a)(?<name>b)') |
| 21 | + |
| 22 | + with pytest.raises(RegexError, match='Invalid group'): |
| 23 | + RegexEnumerator(r'(?a)') |
| 24 | + |
| 25 | + with pytest.raises(RegexError, match='Unmatched closing parenthesis'): |
| 26 | + RegexEnumerator(r'a)') |
| 27 | + |
| 28 | + with pytest.raises(RegexError, match='Unmatched opening parenthesis'): |
| 29 | + RegexEnumerator(r'(a') |
| 30 | + |
| 31 | + |
| 32 | +def test_grammar_backreference(): |
| 33 | + with pytest.raises(RegexError, match='Named back reference not found'): |
| 34 | + RegexEnumerator(r'\k<name>') |
| 35 | + |
| 36 | + with pytest.raises(RegexError, match='Positional back reference not found'): |
| 37 | + RegexEnumerator(r'\1') |
| 38 | + |
| 39 | + with pytest.raises(RegexError, match='Incomplete escape sequence'): |
| 40 | + RegexEnumerator('\\') |
| 41 | + |
| 42 | + with pytest.raises(RegexError, match='Invalid named back reference'): |
| 43 | + RegexEnumerator(r'\k') |
| 44 | + |
| 45 | + with pytest.raises(RegexError, match='Invalid named back reference'): |
| 46 | + RegexEnumerator(r'\k<') |
| 47 | + |
| 48 | + with pytest.raises(RegexError, match='Invalid named back reference'): |
| 49 | + RegexEnumerator(r'\ka') |
| 50 | + |
| 51 | + with pytest.raises(RegexError, match='Invalid named back reference'): |
| 52 | + RegexEnumerator(r'\k<>') |
| 53 | + |
| 54 | + |
| 55 | +def test_grammar_escape_character(): |
| 56 | + with pytest.raises(RegexError, match='Incomplete escape sequence'): |
| 57 | + RegexEnumerator('[\\') |
| 58 | + |
| 59 | + with pytest.raises(RegexError, match='Invalid ASCII escape character'): |
| 60 | + RegexEnumerator(r'\x') |
| 61 | + |
| 62 | + with pytest.raises(RegexError, match='Invalid ASCII escape character'): |
| 63 | + RegexEnumerator(r'\xh') |
| 64 | + |
| 65 | + with pytest.raises(RegexError, match='Invalid ASCII escape character 0'): |
| 66 | + RegexEnumerator(r'[\x0]') |
| 67 | + |
| 68 | + with pytest.raises(RegexError, match='Invalid unicode escape character'): |
| 69 | + RegexEnumerator(r'\u0') |
| 70 | + |
| 71 | + with pytest.raises(RegexError, match='Invalid unicode escape character'): |
| 72 | + RegexEnumerator(r'\un') |
| 73 | + |
| 74 | + with pytest.raises(RegexError, match='Unicode property not supported'): |
| 75 | + RegexEnumerator(r'\p{L}') |
| 76 | + |
| 77 | + |
| 78 | +def test_grammar_charclass(): |
| 79 | + with pytest.raises(RegexError, match='Unclosed character class'): |
| 80 | + RegexEnumerator(r'[') |
| 81 | + |
| 82 | + with pytest.raises(RegexError, match='Unclosed character class'): |
| 83 | + RegexEnumerator(r'[a') |
| 84 | + |
| 85 | + |
| 86 | +def test_grammar_quantifiers(): |
| 87 | + with pytest.raises(RegexError, match='Invalid quantifier'): |
| 88 | + RegexEnumerator(r'a{') |
| 89 | + |
| 90 | + with pytest.raises(RegexError, match='Invalid quantifier'): |
| 91 | + RegexEnumerator(r'a{a}') |
| 92 | + |
| 93 | + with pytest.raises(RegexError, match='Invalid quantifier'): |
| 94 | + RegexEnumerator(r'a{1') |
| 95 | + |
| 96 | + with pytest.raises(RegexError, match='Invalid quantifier'): |
| 97 | + RegexEnumerator(r'a{1 d') |
| 98 | + |
| 99 | + with pytest.raises(RegexError, match='Invalid quantifier'): |
| 100 | + RegexEnumerator(r'a{1, f') |
| 101 | + |
| 102 | + with pytest.raises(RegexError, match='Max length cannot be less than min length in quantifier'): |
| 103 | + RegexEnumerator(r'a{2,1}') |
| 104 | + |
| 105 | + with pytest.raises(RegexError, match='Invalid quantifier'): |
| 106 | + RegexEnumerator(r'a{1,2 d') |
0 commit comments