aboutsummaryrefslogtreecommitdiff
path: root/tests/pattern/classes.test.py
blob: 8a823536c752efa25fd629b0bde07ef58a0688ee (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""Tests for the module-level character-class :class:`Pattern` constants."""

import pytest

from edify import (
    ALPHANUMERIC,
    ANY_CHAR,
    CARRIAGE_RETURN,
    DIGIT,
    LETTER,
    LOWERCASE,
    NEW_LINE,
    NON_DIGIT,
    NON_WHITESPACE,
    NON_WORD,
    NULL_BYTE,
    TAB,
    UPPERCASE,
    WHITESPACE,
    WORD,
    Pattern,
)


@pytest.mark.parametrize(
    ("constant", "expected"),
    [
        (ANY_CHAR, "."),
        (WHITESPACE, "\\s"),
        (NON_WHITESPACE, "\\S"),
        (DIGIT, "\\d"),
        (NON_DIGIT, "\\D"),
        (WORD, "\\w"),
        (NON_WORD, "\\W"),
        (NEW_LINE, "\\n"),
        (CARRIAGE_RETURN, "\\r"),
        (TAB, "\\t"),
        (NULL_BYTE, "\\0"),
        (LETTER, "[a-zA-Z]"),
        (UPPERCASE, "[A-Z]"),
        (LOWERCASE, "[a-z]"),
        (ALPHANUMERIC, "[a-zA-Z0-9]"),
    ],
)
def test_character_class_constant_compiles_to_expected_regex(constant, expected):
    assert constant.to_regex_string() == expected


@pytest.mark.parametrize(
    "constant",
    [
        ANY_CHAR,
        WHITESPACE,
        NON_WHITESPACE,
        DIGIT,
        NON_DIGIT,
        WORD,
        NON_WORD,
        NEW_LINE,
        CARRIAGE_RETURN,
        TAB,
        NULL_BYTE,
        LETTER,
        UPPERCASE,
        LOWERCASE,
        ALPHANUMERIC,
    ],
)
def test_character_class_constant_is_a_pattern(constant):
    assert isinstance(constant, Pattern)


@pytest.mark.parametrize(
    ("constant", "hit_input", "miss_input"),
    [
        (LETTER, "A", "4"),
        (LETTER, "z", " "),
        (UPPERCASE, "Q", "q"),
        (LOWERCASE, "q", "Q"),
        (ALPHANUMERIC, "4", " "),
        (ALPHANUMERIC, "A", "!"),
    ],
)
def test_convenience_char_class_constant_matches_expected_characters(
    constant, hit_input, miss_input
):
    assert constant.test(hit_input) is True
    assert constant.test(miss_input) is False