102 lines
3 KiB
Python
102 lines
3 KiB
Python
"""Traduction des expressions regex101 vers PostgreSQL."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from shortstats.regex_adapter import RegexTranslationError, to_postgres
|
|
|
|
from ..outils.collecte import BS, U_E9
|
|
|
|
U_1F600 = BS + "U" + "0001f600"
|
|
|
|
|
|
@pytest.mark.parametrize("pcre, pg", [
|
|
(r"apple ?pie", r"apple ?pie"),
|
|
(r"\bchat\b", r"\ychat\y"),
|
|
(r"\Bat", r"\Yat"),
|
|
(r"(?-i)TV", r"(?c)TV"),
|
|
(r"(?i)tv", r"tv"),
|
|
(r"(?s)a.b", r"a.b"),
|
|
(r"(?m)^bravo$", r"(?w)^bravo$"),
|
|
(r"(?x) a b # c", r"(?x) a b # c"),
|
|
(r"(?x)a # \m reste un commentaire", r"(?x)a # \m reste un commentaire"),
|
|
(r"(?-i)(?m)x", r"(?cw)x"),
|
|
(r"merci$", r"merci(?=\n?\Z)"),
|
|
(r"a|b$", r"a|b(?=\n?\Z)"),
|
|
(r"[$]\$", r"[$]\$"),
|
|
(r"\Afin\z", r"\Afin\Z"),
|
|
(r"fin\Z", r"fin(?=\n?\Z)"),
|
|
(r"\x{e9}t\xe9", U_E9 + "t" + U_E9),
|
|
(r"\x{1F600}", U_1F600),
|
|
(r"\h+", r"[[:blank:]]+"),
|
|
(r"[\h,]", r"[[:blank:],]"),
|
|
(r"\H", r"[^[:blank:]]"),
|
|
(r"\N+", r"[^\n]+"),
|
|
(r"\R", r"(?:\r\n|\n|\r)"),
|
|
(r"\p{L}+\pN\P{P}", r"[[:alpha:]]+[[:digit:]][^[:punct:]]"),
|
|
(r"[\p{L}\d]", r"[[:alpha:]\d]"),
|
|
(r"(?<nom>la) (?P<x>b) (?'y'c)", r"(la) (b) (c)"),
|
|
(r"(\w)\g1\g{1}", r"(\w)\1\1"),
|
|
(r"a{,3}", r"a{0,3}"),
|
|
(r"x{2}y{2,}z{2,5}?", r"x{2}y{2,}z{2,5}?"),
|
|
(r"{", r"\{"),
|
|
(r"a{x}", r"a\{x}"),
|
|
(r"\Qa.b*\E+", r"a\.b\*+"),
|
|
(r"\Q(fin", r"\(fin"),
|
|
(r"[\b\w\-\]]", r"[\b\w\-\]]"),
|
|
(r"[]a]", r"[]a]"),
|
|
(r"[^]a]", r"[^]a]"),
|
|
(r"[[:alpha:]]", r"[[:alpha:]]"),
|
|
(r"(?:ab)+?(?=c)(?!d)(?<=e)(?<!f)", r"(?:ab)+?(?=c)(?!d)(?<=e)(?<!f)"),
|
|
(r"(?#note)a", r"(?#note)a"),
|
|
(r"\.\/\#\ \-", r"\.\/\#\ \-"),
|
|
(r"\t\n\r\f\e\a\cA\d\D\s\S\w\W", r"\t\n\r\f\e\a\cA\d\D\s\S\w\W"),
|
|
])
|
|
def test_translation(pcre, pg):
|
|
assert to_postgres(pcre) == pg
|
|
|
|
|
|
@pytest.mark.parametrize("pcre, message", [
|
|
(r"\mchat\M", r"écrivez \b"),
|
|
(r"\ychat\y", r"écrivez \b"),
|
|
(r"\Y", r"écrivez \B"),
|
|
(U_E9, r"écrivez \x{e9}"),
|
|
(r"(?>a+)b", "groupe atomique"),
|
|
(r"a++", "possessif"),
|
|
(r"a*+", "possessif"),
|
|
(r"a?+", "possessif"),
|
|
(r"a{2}+", "possessif"),
|
|
(r"(?i:a)b", "seulement au tout début"),
|
|
(r"a(?i)b", "seulement au tout début"),
|
|
(r"(?-s)a.b", r"[^\n]"),
|
|
(r"(?U)a", "option (?U)"),
|
|
(r"(?R)", "récursion"),
|
|
(r"(a)(?1)", "récursion"),
|
|
(r"(?|a)", "(?|"),
|
|
(r"(?(1)a|b)", "condition"),
|
|
(r"(*UTF)a", "verbes"),
|
|
(r"(?C1)a", "(?C"),
|
|
(r"\K", r"\K"),
|
|
(r"\G", r"\G"),
|
|
(r"\X", r"\X"),
|
|
(r"\v", r"[\n\r]"),
|
|
(r"\k<n>", r"\1"),
|
|
(r"(?P=n)", "rappel nommé"),
|
|
(r"\g{n}", r"\g1"),
|
|
(r"\10", r"\9"),
|
|
(r"\p{Lu}", r"\p{L}"),
|
|
(r"[\P{L}]", "[ ]"),
|
|
(r"[\B]", "[ ]"),
|
|
(r"\x", "chiffres"),
|
|
(r"\x{zz}", r"\x{e9}"),
|
|
(r"\x{110000}", "Unicode"),
|
|
(r"\x00", "nul"),
|
|
(r"\q", "regex101"),
|
|
("a" + BS, "fin d'expression"),
|
|
(r"[abc", "non fermé"),
|
|
])
|
|
def test_refusals(pcre, message):
|
|
with pytest.raises(RegexTranslationError) as exc:
|
|
to_postgres(pcre)
|
|
assert message in str(exc.value), str(exc.value)
|