File: test_match.py

package info (click to toggle)
python-pcre2 0.4.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 284 kB
  • sloc: python: 288; makefile: 31
file content (40 lines) | stat: -rw-r--r-- 1,646 bytes parent folder | download
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
import pytest
import pcre2
from pcre2.exceptions import CompileError, MatchError, LibraryError


# All tests should match successfully.
test_data_match_bounds = [
    (b".*", "aba•ba••ba•••b".encode(), 0, 0, 0, 0, 26),
    (".*", "aba•ba••ba•••b", 0, 0, 0, 0, 14),
]
@pytest.mark.parametrize("pattern,subject,options,offset,group,start,end", test_data_match_bounds)
def test_match_bounds(pattern, subject, options, offset, group, start, end):
    p = pcre2.compile(pattern, options=options)
    m = p.match(subject, offset=offset)
    assert (m.start(group), m.end(group)) == (start, end)


test_data_match_substring = [
    (b".*", "aba•ba••ba•••b".encode(), 0, 0, "aba•ba••ba•••b".encode()),
    (".*", "aba•ba••ba•••b", 0, 0, "aba•ba••ba•••b"),
]
@pytest.mark.parametrize("pattern,subject,options,offset,substring", test_data_match_substring)
def test_match_substring(pattern, subject, options, offset, substring):
    p = pcre2.compile(pattern, options=options)
    m = p.match(subject, offset=offset)
    assert m.substring() == substring


test_data_match_expand = [
    (b"[abc]*", b"", b"dabacbaccbacccb", 0, 0, b"dabacbaccbacccb"),
    ("[abc]*", "", "dabacbaccbacccb", 0, 0, "dabacbaccbacccb"),
    ("[abc]*", "", "dabacbaccbacccb", 0, 1, "d"),
]
@pytest.mark.parametrize(
    "pattern,replacement,subject,options,offset,result", test_data_match_expand
)
def test_match_expand(pattern, replacement, subject, options, offset, result):
    p = pcre2.compile(pattern, options=options)
    m = p.match(subject, offset=offset)
    assert m.expand(replacement) == result