File: test__compat.py

package info (click to toggle)
python-cheroot 10.0.1%2Bds1-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,048 kB
  • sloc: python: 6,222; makefile: 15
file content (61 lines) | stat: -rw-r--r-- 1,661 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
"""Test suite for cross-python compatibility helpers."""

import pytest

from cheroot._compat import extract_bytes, ntob, ntou, bton


@pytest.mark.parametrize(
    ('func', 'inp', 'out'),
    (
        (ntob, 'bar', b'bar'),
        (ntou, 'bar', u'bar'),
        (bton, b'bar', 'bar'),
    ),
)
def test_compat_functions_positive(func, inp, out):
    """Check that compatibility functions work with correct input."""
    assert func(inp, encoding='utf-8') == out


@pytest.mark.parametrize(
    'func',
    (
        ntob,
        ntou,
    ),
)
def test_compat_functions_negative_nonnative(func):
    """Check that compatibility functions fail loudly for incorrect input."""
    non_native_test_str = b'bar'
    with pytest.raises(TypeError):
        func(non_native_test_str, encoding='utf-8')


def test_ntou_escape():
    """Check that ``ntou`` supports escape-encoding under Python 2."""
    expected = u'hišřії'
    actual = ntou('hi\u0161\u0159\u0456\u0457', encoding='escape')
    assert actual == expected


@pytest.mark.parametrize(
    ('input_argument', 'expected_result'),
    (
        (b'qwerty', b'qwerty'),
        (memoryview(b'asdfgh'), b'asdfgh'),
    ),
)
def test_extract_bytes(input_argument, expected_result):
    """Check that legitimate inputs produce bytes."""
    assert extract_bytes(input_argument) == expected_result


def test_extract_bytes_invalid():
    """Ensure that invalid input causes exception to be raised."""
    with pytest.raises(
            ValueError,
            match=r'^extract_bytes\(\) only accepts bytes '
            'and memoryview/buffer$',
    ):
        extract_bytes(u'some юнікод їїї')