File: test_ltsv.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 (101 lines) | stat: -rw-r--r-- 2,500 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
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""

import itertools

import pytest

from pathvalidate import sanitize_ltsv_label, validate_ltsv_label
from pathvalidate.error import ErrorReason, ValidationError

from ._common import INVALID_WIN_FILENAME_CHARS, alphanum_chars


VALID_LABEL_CHARS = alphanum_chars + ("_", ".", "-")
INVALID_LABEL_CHARS = INVALID_WIN_FILENAME_CHARS + (
    "!",
    "#",
    "$",
    "&",
    "'",
    "=",
    "~",
    "^",
    "@",
    "`",
    "[",
    "]",
    "+",
    ";",
    "{",
    "}",
    ",",
    "(",
    ")",
    "%",
    " ",
    "\t",
    "\n",
    "\r",
    "\f",
    "\v",
)


class Test_validate_ltsv_label:
    VALID_CHARS = alphanum_chars
    INVALID_CHARS = INVALID_LABEL_CHARS

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

    @pytest.mark.parametrize(
        ["value"],
        [["abc" + invalid_char + "hoge123"] for invalid_char in INVALID_CHARS]
        + [["あいうえお"], ["ラベル"]],
    )
    def test_exception_invalid_char(self, value):
        with pytest.raises(ValidationError) as e:
            validate_ltsv_label(value)
        assert e.value.reason == ErrorReason.INVALID_CHARACTER


class Test_sanitize_ltsv_label:
    TARGET_CHARS = INVALID_LABEL_CHARS
    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 sanitize_ltsv_label(value, replace_text) == expected

    @pytest.mark.parametrize(["value", "expected"], [["aあいbうえcお", "abc"]])
    def test_normal_multibyte(self, value, expected):
        sanitize_ltsv_label(value)

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