File: test_text.py

package info (click to toggle)
python-pyspnego 0.10.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,648 kB
  • sloc: python: 16,191; sh: 182; makefile: 11
file content (201 lines) | stat: -rw-r--r-- 4,627 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# -*- coding: utf-8 -*-
# Copyright: (c) 2020, Jordan Borean (@jborean93) <jborean93@gmail.com>
# MIT License (see LICENSE or https://opensource.org/licenses/MIT)
import typing

import pytest

import spnego._text as text


class Obj:
    def __init__(self, str: typing.Optional[str] = None, repr: typing.Optional[str] = None) -> None:
        self.str = str
        self.repr = repr

    def __eq__(self, other: typing.Any) -> bool:
        return isinstance(other, Obj) and self.str == other.str and self.repr == other.repr

    def __str__(self) -> str:
        return self.str  # type: ignore[return-value] # Testing this scenario

    def __repr__(self) -> str:
        return self.repr  # type: ignore[return-value] # Testing this scenario


class ObjRepr:
    def __init__(self, repr: typing.Optional[str] = None) -> None:
        self.repr = repr

    def __repr__(self) -> str:
        return self.repr  # type: ignore[return-value] # Testing this scenario


class ObjUnicodeError:
    def __str__(self) -> str:
        return "café".encode("ascii")  # type: ignore[return-value] # Testing this scenario

    def __repr__(self) -> str:
        return "café".encode("ascii")  # type: ignore[return-value] # Testing this scenario


def test_to_bytes_from_bytes():
    actual = text.to_bytes(b"\xFF\x00\x7F\x80")

    assert actual == b"\xFF\x00\x7F\x80"


@pytest.mark.parametrize(
    "value, expected",
    [
        ("cafe", b"cafe"),
        ("café", b"caf\xc3\xa9"),
        ("ÜseӜ", b"\x55\xCC\x88\x73\x65\xD3\x9C"),
    ],
)
def test_to_bytes_from_text(value, expected):
    actual = text.to_bytes(value)

    assert actual == expected


def test_to_bytes_encoding():
    actual = text.to_bytes("café", encoding="windows-1252")

    assert actual == b"caf\xe9"


def test_to_bytes_errors():
    with pytest.raises(UnicodeEncodeError, match="codec can't encode character"):
        text.to_bytes("café", encoding="ascii")

    actual = text.to_bytes("café", encoding="ascii", errors="replace")

    assert actual == b"caf?"


def test_to_bytes_nonstr():
    actual = text.to_bytes(Obj(str="café"))

    assert actual == b"caf\xc3\xa9"


def test_to_bytes_nonstr_default():
    actual = text.to_bytes(Obj())

    assert actual == b""


def test_to_bytes_nonstr_repr():
    actual = text.to_bytes(ObjRepr(repr="café"))

    assert actual == b"caf\xc3\xa9"


def test_to_bytes_nonstr_unicode_error():
    actual = text.to_bytes(ObjUnicodeError())

    assert actual == b""


def test_to_bytes_nonstr_passthru():
    actual = text.to_bytes(Obj(), nonstring="passthru")

    assert actual == Obj()


def test_to_bytes_nonstr_empty():
    actual = text.to_bytes(Obj(), nonstring="empty")

    assert actual == b""


def test_to_bytes_nonstr_invalid():
    with pytest.raises(ValueError, match="Invalid nonstring value"):
        text.to_bytes(Obj(), nonstring="invalid")


def test_to_text_from_text():
    actual = text.to_text("café")

    assert actual == "café"


@pytest.mark.parametrize(
    "value, expected",
    [
        (b"cafe", "cafe"),
        (b"caf\xc3\xa9", "café"),
        (b"\x55\xCC\x88\x73\x65\xD3\x9C", "ÜseӜ"),
    ],
)
def test_to_text_from_bytes(value, expected):
    actual = text.to_text(value)

    assert actual == expected


def test_to_text_encoding():
    actual = text.to_text(b"caf\xe9", encoding="windows-1252")

    assert actual == "café"


def test_to_text_errors():
    with pytest.raises(UnicodeError, match="codec can't decode byte 0xff"):
        text.to_text(b"caf\xFF")

    actual = text.to_text(b"caf\xFF", errors="replace")

    assert actual == "caf�"


def test_to_text_nonstr():
    actual = text.to_text(Obj(str="café"))

    assert actual == "café"


def test_to_text_nonstr_unicode():
    class ObjUnicode:
        def __unicode__(self):
            return "café"

    actual = text.to_text(ObjUnicode())

    assert actual == "café"


def test_to_text_nonstr_default():
    actual = text.to_text(Obj())

    assert actual == ""


def test_to_text_nonstr_repr():
    actual = text.to_text(ObjRepr(repr="café"))

    assert actual == "café"


def test_to_text_nonstr_unicode_error():
    actual = text.to_text(ObjUnicodeError())

    assert actual == ""


def test_to_text_nonstr_passthru():
    actual = text.to_text(Obj(), nonstring="passthru")

    assert actual == Obj()


def test_to_text_nonstr_empty():
    actual = text.to_text(Obj(), nonstring="empty")

    assert actual == ""


def test_to_text_nonstr_invalid():
    with pytest.raises(ValueError, match="Invalid nonstring value"):
        text.to_text(Obj(), nonstring="invalid")