File: test_symbol.py

package info (click to toggle)
python-pathvalidate 3.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 692 kB
  • sloc: python: 3,987; makefile: 262; sh: 6
file content (142 lines) | stat: -rw-r--r-- 4,342 bytes parent folder | download | duplicates (2)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""

import itertools

import pytest

from pathvalidate import (
    ascii_symbols,
    replace_symbol,
    unprintable_ascii_chars,
    validate_symbol,
    validate_unprintable_char,
)
from pathvalidate.error import ErrorReason, ValidationError

from ._common import alphanum_chars


class Test_validate_symbol:
    VALID_CHARS = alphanum_chars
    INVALID_CHARS = ascii_symbols

    @pytest.mark.parametrize(
        ["value"], [["abc" + valid_char + "hoge123"] for valid_char in VALID_CHARS]
    )
    def test_normal(self, value):
        validate_symbol(value)

    @pytest.mark.parametrize(["value"], [["あいうえお"], ["シート"]])
    def test_normal_multibyte(self, value):
        pytest.skip("TODO")

        validate_symbol(value)

    @pytest.mark.parametrize(
        ["value"],
        [
            ["abc" + invalid_char + "hoge123"]
            for invalid_char in INVALID_CHARS + unprintable_ascii_chars
        ],
    )
    def test_exception_invalid_char(self, value):
        with pytest.raises(ValidationError) as e:
            validate_symbol(value)
        assert e.value.reason == ErrorReason.INVALID_CHARACTER


class Test_replace_symbol:
    TARGET_CHARS = ascii_symbols
    NOT_TARGET_CHARS = alphanum_chars
    REPLACE_TEXT_LIST = ["", "_"]

    @pytest.mark.parametrize(
        ["value", "replace_text", "expected"],
        [
            ["A" + c + "B", rep, "A" + rep + "B"]
            for c, rep in itertools.product(TARGET_CHARS, REPLACE_TEXT_LIST)
        ]
        + [
            ["A" + c + "B", rep, "A" + c + "B"]
            for c, rep in itertools.product(NOT_TARGET_CHARS, REPLACE_TEXT_LIST)
        ]
        + [["", "", ""]],
    )
    def test_normal(self, value, replace_text, expected):
        assert replace_symbol(value, replace_text) == expected

    @pytest.mark.parametrize(
        ["value", "exclude_symbols", "expected"],
        [
            ["/tmp/h!o|g$e.txt", ["/", "."], "/tmp/hoge.txt"],
            ["/tmp/h!o|g$e.txt", [], "tmphogetxt"],
            ["/tmp/h!o|g$e.txt", ["n", "o", "p"], "tmphogetxt"],
        ],
    )
    def test_normal_exclude_symbols(self, value, exclude_symbols, expected):
        assert replace_symbol(value, exclude_symbols=exclude_symbols) == expected

    @pytest.mark.parametrize(
        ["value", "replace_text", "is_replace_consecutive_chars", "is_strip", "expected"],
        [
            ["!a##b$$$c((((d]]]])", "_", True, True, "a_b_c_d"],
            ["!a##b$$$c((((d]]]])", "_", True, False, "_a_b_c_d_"],
            ["!a##b$$$c((((d]]]])", "_", False, True, "a__b___c____d"],
            ["!a##b$$$c((((d]]]])", "_", False, False, "_a__b___c____d_____"],
        ],
    )
    def test_normal_consecutive(
        self, value, replace_text, is_replace_consecutive_chars, is_strip, expected
    ):
        assert (
            replace_symbol(
                value,
                replace_text,
                is_replace_consecutive_chars=is_replace_consecutive_chars,
                is_strip=is_strip,
            )
            == expected
        )

    @pytest.mark.parametrize(
        ["value", "expected"],
        [
            [None, TypeError],
            [1, TypeError],
            [True, TypeError],
        ],
    )
    def test_abnormal(self, value, expected):
        with pytest.raises(expected):
            replace_symbol(value)


class Test_validate_unprintable_char:
    VALID_CHARS = alphanum_chars
    INVALID_CHARS = unprintable_ascii_chars

    @pytest.mark.parametrize(
        ["value"], [["abc" + valid_char + "hoge123"] for valid_char in VALID_CHARS]
    )
    def test_normal(self, value):
        validate_unprintable_char(value)

    @pytest.mark.parametrize(["value"], [["あいうえお"], ["シート"]])
    def test_normal_multibyte(self, value):
        pytest.skip("TODO")

        validate_unprintable_char(value)

    @pytest.mark.parametrize(
        ["value"],
        [
            ["abc" + invalid_char + "hoge123"]
            for invalid_char in INVALID_CHARS + unprintable_ascii_chars
        ],
    )
    def test_exception_invalid_char(self, value):
        with pytest.raises(ValidationError) as e:
            validate_unprintable_char(value)
        assert e.value.reason == ErrorReason.INVALID_CHARACTER