File: test_escape.py

package info (click to toggle)
python-b2sdk 2.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,020 kB
  • sloc: python: 30,902; sh: 13; makefile: 8
file content (72 lines) | stat: -rw-r--r-- 2,251 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
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
######################################################################
#
# File: test/unit/utils/test_escape.py
#
# Copyright 2023 Backblaze Inc. All Rights Reserved.
#
# License https://www.backblaze.com/using_b2_code.html
#
######################################################################
import pytest

from b2sdk._internal.utils.escape import (
    escape_control_chars,
    substitute_control_chars,
    unprintable_to_hex,
)


@pytest.mark.parametrize(
    (
        'input_',
        'expected_unprintable_to_hex',
        'expected_escape_control_chars',
        'expected_substitute_control_chars',
    ),
    [
        ('', '', '', ('', False)),
        (' abc-z', ' abc-z', "' abc-z'", (' abc-z', False)),
        ('a\x7fb', 'a\\x7fb', "'a\\x7fb'", ('a�b', True)),
        ('a\x00b a\x9fb ', 'a\\x00b a\\x9fb ', "'a\\x00b a\\x9fb '", ('a�b a�b ', True)),
        ('a\x7fb\nc', 'a\\x7fb\nc', "'a\\x7fb\nc'", ('a�b\nc', True)),
        ('\x9bT\x9bEtest', '\\x9bT\\x9bEtest', "'\\x9bT\\x9bEtest'", ('�T�Etest', True)),
        (
            '\x1b[32mC\x1b[33mC\x1b[34mI',
            '\\x1b[32mC\\x1b[33mC\\x1b[34mI',
            "'\\x1b[32mC\\x1b[33mC\\x1b[34mI'",
            ('�[32mC�[33mC�[34mI', True),
        ),
    ],
)
def test_unprintable_to_hex(
    input_,
    expected_unprintable_to_hex,
    expected_escape_control_chars,
    expected_substitute_control_chars,
):
    assert unprintable_to_hex(input_) == expected_unprintable_to_hex
    assert escape_control_chars(input_) == expected_escape_control_chars
    assert substitute_control_chars(input_) == expected_substitute_control_chars


def test_unprintable_to_hex__none():
    """
    Test that unprintable_to_hex handles None.

    This was unintentionally supported and is only kept for compatibility.
    """
    assert unprintable_to_hex(None) is None  # type: ignore


def test_escape_control_chars__none():
    """
    Test that escape_control_chars handles None.

    This was unintentionally supported and is only kept for compatibility.
    """
    assert escape_control_chars(None) is None  # type: ignore


def test_substitute_control_chars__none():
    with pytest.raises(TypeError):
        substitute_control_chars(None)  # type: ignore