-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtest_c_file.py
141 lines (121 loc) · 4.12 KB
/
test_c_file.py
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
import io
import sys
import pathlib
import typing
import pytest
from cPyExtPatt import cFile
@pytest.mark.parametrize(
'arg, expected',
(
('~/foo/bar.txt', '~/foo/bar.txt',),
(pathlib.Path('~/foo/bar.txt'), '~/foo/bar.txt',),
)
)
def test_parse_filesystem_argument(arg, expected):
assert cFile.parse_filesystem_argument(arg) == expected
@pytest.mark.parametrize(
'arg, expected',
(
('~/foo/bar.txt', str,),
)
)
def test_parse_filesystem_argument_return_type(arg, expected):
assert type(cFile.parse_filesystem_argument(arg)) == expected
@pytest.mark.skipif(not (sys.version_info.minor >= 7), reason='Python 3.7+')
@pytest.mark.parametrize(
'arg, expected',
(
# Number of arguments.
(None, "function missing required argument 'path' (pos 1)"),
([1, 2.9], 'expected str, bytes or os.PathLike object, not list'),
)
)
def test_parse_filesystem_argument_raises(arg, expected):
with pytest.raises(TypeError) as err:
if arg is None:
cFile.parse_filesystem_argument()
else:
cFile.parse_filesystem_argument(arg)
assert err.value.args[0] == expected
@pytest.mark.skipif(not (sys.version_info.minor < 7), reason='Python < 3.7')
@pytest.mark.parametrize(
'arg, expected',
(
# Number of arguments.
(None, "Required argument 'path' (pos 1) not found"),
([1, 2.9], 'expected str, bytes or os.PathLike object, not list'),
)
)
def test_parse_filesystem_argument_raises_pre_37(arg, expected):
with pytest.raises(TypeError) as err:
if arg is None:
cFile.parse_filesystem_argument()
else:
cFile.parse_filesystem_argument(arg)
assert err.value.args[0] == expected
@pytest.mark.parametrize(
'file_object, size, expected',
(
(io.BytesIO(b'Some bytes.'), 4, b'Some'),
(io.BytesIO(b'Some bytes.'), None, b'Some bytes.'),
)
)
def test_read_python_file_to_c(file_object, size, expected):
if size is None:
result = cFile.read_python_file_to_c(file_object)
else:
result = cFile.read_python_file_to_c(file_object, size)
assert result == expected
@pytest.mark.parametrize(
'bytes_to_write, expected',
(
(b'Some bytes.', len(b'Some bytes.')),
(b'Some\0bytes.', len(b'Some\0bytes.')),
)
)
def test_write_bytes_to_python_string_file(bytes_to_write, expected):
file = io.StringIO()
result = cFile.write_bytes_to_python_file(bytes_to_write, file)
assert result == expected
@pytest.mark.parametrize(
'bytes_to_write, expected',
(
('Some string.', "a bytes-like object is required, not 'str'"),
)
)
def test_write_bytes_to_python_string_file_raises(bytes_to_write, expected):
file = io.StringIO()
with pytest.raises(TypeError) as err:
cFile.write_bytes_to_python_file(bytes_to_write, file)
assert err.value.args[0] == expected
@pytest.mark.parametrize(
'bytes_to_write, expected',
(
('Some bytes.', "a bytes-like object is required, not 'str'"),
(b'Some bytes.', "a bytes-like object is required, not 'str'"),
)
)
def test_write_bytes_to_python_bytes_file_raises(bytes_to_write, expected):
file = io.BytesIO()
with pytest.raises(TypeError) as err:
cFile.write_bytes_to_python_file(bytes_to_write, file)
assert err.value.args[0] == expected
# TODO: Fix this. Why is position 420 when it is a 25 character write? String termination?
@pytest.mark.skip(
reason=(
"Fails on Python 3.9 and 3.11 with"
" \"UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 420: ordinal not in range(128)\""
),
)
def test_wrap_python_file():
file = io.BytesIO()
result = cFile.wrap_python_file(file)
print()
print(' Result '.center(75, '-'))
print(result.decode('ascii'))
print(' Result DONE '.center(75, '-'))
print(' file.getvalue() '.center(75, '-'))
get_value = file.getvalue()
print(get_value)
print(' file.getvalue() DONE '.center(75, '-'))
assert get_value == b'Test write to python file'