File: support_test_app_sre.py

package info (click to toggle)
pypy3 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,848 kB
  • sloc: python: 1,291,746; ansic: 74,281; asm: 5,187; cpp: 3,017; sh: 2,533; makefile: 544; xml: 243; lisp: 45; csh: 21; awk: 4
file content (39 lines) | stat: -rw-r--r-- 1,366 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
"""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

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):
    for arg, expected in tests:
        assert ord(expected) == _sre.getlower(ord(arg), flags)