|
| 1 | +import re |
| 2 | +import sys |
| 3 | + |
| 4 | +import pytest |
| 5 | +from inline_snapshot import snapshot |
| 6 | + |
| 7 | +import pydantic_monty |
| 8 | + |
| 9 | + |
| 10 | +def test_re_module(): |
| 11 | + m = pydantic_monty.Monty('import re') |
| 12 | + output = m.run() |
| 13 | + assert output is None |
| 14 | + |
| 15 | + |
| 16 | +def test_re_compile(): |
| 17 | + code = """ |
| 18 | +import re |
| 19 | +pattern = re.compile(r'\\d+') |
| 20 | +matches = pattern.findall('There are 24 hours in a day and 365 days in a year.') |
| 21 | +""" |
| 22 | + m = pydantic_monty.Monty(code) |
| 23 | + output = m.run() |
| 24 | + assert output is None |
| 25 | + |
| 26 | + |
| 27 | +supported_flags = [ |
| 28 | + (['re.I', 're.IGNORECASE'], re.IGNORECASE), |
| 29 | + (['re.M', 're.MULTILINE'], re.MULTILINE), |
| 30 | + (['re.S', 're.DOTALL'], re.DOTALL), |
| 31 | +] |
| 32 | +if sys.version_info >= (3, 11): |
| 33 | + supported_flags.append((['re.NOFLAG'], re.NOFLAG)) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize( |
| 37 | + 'flags,target', |
| 38 | + supported_flags, |
| 39 | + ids=str, |
| 40 | +) |
| 41 | +def test_re_constant(flags: list[str], target: int): |
| 42 | + code = f'import re; ({",".join(flags)},)' |
| 43 | + m = pydantic_monty.Monty(code) |
| 44 | + output = m.run() |
| 45 | + assert all(map(lambda orig: orig == target, output)) |
| 46 | + |
| 47 | + |
| 48 | +def test_re_compile_repr(): |
| 49 | + code = r""" |
| 50 | +import re |
| 51 | +pattern = re.compile(r'\d+', re.IGNORECASE | re.DOTALL) |
| 52 | +pattern |
| 53 | +""" |
| 54 | + m = pydantic_monty.Monty(code) |
| 55 | + output = m.run() |
| 56 | + assert output == r"re.compile('\\d+', re.IGNORECASE|re.DOTALL)" |
| 57 | + |
| 58 | + |
| 59 | +def test_re_match_repr(): |
| 60 | + code = """ |
| 61 | +import re |
| 62 | +pattern = re.compile(r'\\d+') |
| 63 | +pattern.match('123abc') |
| 64 | +""" |
| 65 | + m = pydantic_monty.Monty(code) |
| 66 | + output = m.run() |
| 67 | + assert output == "<re.Match object; span=(0, 3), match='123'>" |
| 68 | + |
| 69 | + |
| 70 | +def test_re_match_groups(): |
| 71 | + code = """ |
| 72 | +import re |
| 73 | +pattern = re.compile(r'(\\d+)-(\\w+)') |
| 74 | +match = pattern.match('123-abc') |
| 75 | +match.groups() |
| 76 | +""" |
| 77 | + m = pydantic_monty.Monty(code) |
| 78 | + output = m.run() |
| 79 | + assert output == ('123', 'abc') |
| 80 | + |
| 81 | + |
| 82 | +def test_re_substitution(): |
| 83 | + code = """ |
| 84 | +import re |
| 85 | +pattern = re.compile(r'\\s+') |
| 86 | +result = pattern.sub('-', 'This is a test.') |
| 87 | +result |
| 88 | +""" |
| 89 | + m = pydantic_monty.Monty(code) |
| 90 | + output = m.run() |
| 91 | + assert output == 'This-is-a-test.' |
| 92 | + |
| 93 | + |
| 94 | +def test_re_error_handling(): |
| 95 | + code = """ |
| 96 | +import re |
| 97 | +try: |
| 98 | + pattern = re.compile(r'[') |
| 99 | +except Exception as e: |
| 100 | + error_message = str(e) |
| 101 | +error_message |
| 102 | +""" |
| 103 | + m = pydantic_monty.Monty(code) |
| 104 | + output = m.run() |
| 105 | + error = 'Parsing error at position 1: Invalid character class' |
| 106 | + assert error in output |
| 107 | + |
| 108 | + |
| 109 | +def test_re_resume(): |
| 110 | + code = """ |
| 111 | +import re |
| 112 | +pattern = re.compile(func()) |
| 113 | +matches = pattern.findall('Sample 123 text 456') |
| 114 | +dump(matches) |
| 115 | +""" |
| 116 | + m = pydantic_monty.Monty(code) |
| 117 | + progress = m.start() |
| 118 | + assert isinstance(progress, pydantic_monty.FunctionSnapshot) |
| 119 | + |
| 120 | + assert progress.function_name == snapshot('func') |
| 121 | + assert progress.args == snapshot(()) |
| 122 | + assert progress.kwargs == snapshot({}) |
| 123 | + |
| 124 | + progress2 = progress.resume(return_value='\\d+') |
| 125 | + assert isinstance(progress2, pydantic_monty.FunctionSnapshot) |
| 126 | + |
| 127 | + result = progress2.resume(return_value=['123', '456']) |
| 128 | + assert isinstance(result, pydantic_monty.MontyComplete) |
| 129 | + assert result.output == snapshot(['123', '456']) |
| 130 | + |
| 131 | + |
| 132 | +def test_re_persistence(): |
| 133 | + code = """ |
| 134 | +import re |
| 135 | +pattern = re.compile(r'\\w+') |
| 136 | +dump() |
| 137 | +matches = pattern.findall('Test 123!') |
| 138 | +matches |
| 139 | +""" |
| 140 | + m = pydantic_monty.Monty(code) |
| 141 | + progress = m.start() |
| 142 | + assert isinstance(progress, pydantic_monty.FunctionSnapshot) |
| 143 | + |
| 144 | + data = progress.dump() |
| 145 | + |
| 146 | + progress2 = pydantic_monty.FunctionSnapshot.load(data) |
| 147 | + |
| 148 | + result = progress2.resume(return_value=None) |
| 149 | + assert isinstance(result, pydantic_monty.MontyComplete) |
| 150 | + assert result.output == snapshot(['Test', '123']) |
| 151 | + |
| 152 | + |
| 153 | +def test_re_error_upcast(): |
| 154 | + code = """ |
| 155 | +import re |
| 156 | +re.compile(r'[') |
| 157 | +""" |
| 158 | + m = pydantic_monty.Monty(code) |
| 159 | + try: |
| 160 | + m.run() |
| 161 | + assert False, 'Expected an exception to be raised' |
| 162 | + except pydantic_monty.MontyRuntimeError as e: |
| 163 | + error_message = str(e) |
| 164 | + assert True, 'Expected an exception to be raised' |
| 165 | + if sys.version_info >= (3, 13): |
| 166 | + assert type(e.exception()) is re.PatternError |
| 167 | + else: |
| 168 | + assert type(e.exception()) is re.error |
| 169 | + assert 'Parsing error at position 1: Invalid character class' in error_message |
0 commit comments