File: support_test_app_sre.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (45 lines) | stat: -rw-r--r-- 1,616 bytes parent folder | download | duplicates (4)
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
"""Support functions for app-level _sre tests."""
import locale, _sre
from sre_constants import OPCODES as _OPCODES
from sre_constants import ATCODES as _ATCODES
from sre_constants import CHCODES as _CHCODES
from sre_constants import MAXREPEAT, SRE_FLAG_UNICODE, SRE_FLAG_LOCALE

OPCODES = {_opcode.name.lower(): int(_opcode) for _opcode in _OPCODES}
ATCODES = {_atcode.name.lower(): int(_atcode) for _atcode in _ATCODES}
CHCODES = {_chcode.name.lower(): int(_chcode) for _chcode in _CHCODES}

def encode_literal(string):
    opcodes = []
    for character in string:
        opcodes.extend([OPCODES["literal"], ord(character)])
    return opcodes

def assert_match(opcodes, strings):
    assert_something_about_match(lambda x: x, opcodes, strings)

def assert_no_match(opcodes, strings):
    assert_something_about_match(lambda x: not x, opcodes, strings)

def assert_something_about_match(assert_modifier, opcodes, strings):
    if isinstance(strings, str):
        strings = [strings]
    for string in strings:
        assert assert_modifier(search(opcodes, string))

def search(opcodes, string):
    pattern = _sre.compile("ignore", 0, opcodes, 0, {}, None)
    return pattern.search(string)

def void_locale():
    locale.setlocale(locale.LC_ALL, (None, None))

def assert_lower_equal(tests, flags):
    if flags == 0:
        checkerfn = _sre.ascii_tolower
    elif flags == SRE_FLAG_UNICODE:
        checkerfn = _sre.unicode_tolower
    else:
        assert False # SRE_FLAG_LOCALE: not supported, and not needed, since 3.7
    for arg, expected in tests:
        assert ord(expected) == checkerfn(ord(arg))