-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
198 lines (160 loc) · 5.95 KB
/
test.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
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
from copy import deepcopy
from random import randint
import pytest
from arko.wrapper import ArkoWrapper
try:
# noinspection PyPackageRequirements
from rich.console import Console
console = Console(color_system='truecolor')
# noinspection PyShadowingBuiltins
print = console.print
except ImportError: # pragma: no cover
import builtins
Console = None
# noinspection PyShadowingBuiltins
print = builtins.print
data = range(1, 10)
data_a = range(1, 5)
data_b = range(5, 10)
wrapper = ArkoWrapper(data)
@pytest.mark.skip # pragma: no cover # NOSONAR
class TestWrapper(ArkoWrapper): # pragma: no cover
__test__ = False
class TestType: # pragma: no cover
def test_type_hits(self):
new_wrapper: TestWrapper[int] = TestWrapper(range(1, 10))
assert new_wrapper.all()
class TestOperator: # pragma: no cover
def test(self):
from sys import maxsize
assert ArkoWrapper().__root__ == []
v = randint(0, maxsize)
assert ArkoWrapper(v).__root__ == [v]
with pytest.raises(ValueError):
ArkoWrapper(max_operate_times=maxsize + 1)
with pytest.raises(ValueError):
ArkoWrapper(max_operate_times=-1)
with pytest.raises(ValueError):
wrapper.max_operate_time = -5
print(wrapper.root)
print(wrapper.max_operate_time)
test_wrapper = ArkoWrapper(max_operate_times=50)
test_wrapper.max_operate_time = 10
assert test_wrapper.max_operate_time == 10
def test_string(self):
print()
print(str(wrapper))
print(repr(wrapper))
def test_max_gen(self):
# noinspection PyTypeChecker
assert len(list(
ArkoWrapper(range(100), max_operate_times=20)._max_gen())
) == 20
assert len(list(wrapper._max_gen())) == len(data)
def test_index(self):
value = randint(0, 100)
assert int(ArkoWrapper(range(value))) == value
def test_len(self):
from itertools import tee
assert wrapper.length == len(data)
assert ArkoWrapper(
tee(range(100))[0], max_operate_times=10
).length == 10
def test_add(self):
wrapper_a = ArkoWrapper(data_a)
wrapper_b = ArkoWrapper(data_b)
assert list(wrapper_a + wrapper_b) == list(data)
assert list(data_a + wrapper_b) == list(data)
assert list(wrapper_a + data_b) == list(data)
assert list(0 + wrapper) == list(range(10))
assert list(wrapper + 10) == list(range(1, 11))
new_wrapper = deepcopy(wrapper)
assert new_wrapper.append(10) == range(1, 11)
@pytest.mark.flaky(reruns=5)
def test_equal(self):
from itertools import tee
print()
assert wrapper == ArkoWrapper(data)
assert wrapper == data
assert wrapper == list(data)
assert ArkoWrapper(0) == 0
assert wrapper + 10 == range(1, 11)
assert wrapper + 11 != range(1, 11)
assert wrapper != range(1, 11)
list_a, list_b = tee(data)
assert ArkoWrapper(list_a) == list_b
@pytest.mark.flaky(reruns=3)
def test_copy(self):
from copy import (
copy,
deepcopy,
)
wrapper_a = copy(wrapper)
wrapper_b = deepcopy(wrapper)
assert wrapper_a == wrapper_b
def test_slice(self):
s = range(30)
assert ArkoWrapper(s).slice(10) == range(10)
assert ArkoWrapper(s).slice(10, 20) == range(10, 20)
assert ArkoWrapper(s).slice(10, 20, 2) == list(range(30))[10:20:2]
def test_reverse(self):
assert wrapper.reverse() == data.__reversed__()
assert reversed(wrapper) == reversed(data)
assert - wrapper == data.__reversed__()
assert reversed(ArkoWrapper([1, 2, 3])) == [3, 2, 1]
def test_getitem(self):
assert wrapper[5] == data[5]
assert wrapper @ 5 == data[5]
assert wrapper[-3] == data[-3]
assert wrapper[7:] == data[7:]
assert wrapper[:5] == data[:5]
assert wrapper[:-2] == data[:-2]
assert ArkoWrapper(wrapper)[:5] == data[:5]
with pytest.raises(ValueError):
print(wrapper[20])
with pytest.raises(IndexError):
print(wrapper['a'])
def test_mul(self):
assert wrapper * 2 == list(data) + list(data)
with pytest.raises(ValueError):
print(wrapper * -1)
with pytest.raises(TypeError):
print(wrapper * 'a')
def test_collect(self):
assert wrapper.collect() == list(data)
assert wrapper >> tuple() == tuple(data)
assert wrapper >> tuple([0]) == tuple([0, *data])
with pytest.raises(TypeError):
# noinspection PyStatementEffect,PyTypeChecker
wrapper >> wrapper.root
def test_accumulate(self):
import operator
from itertools import accumulate
assert wrapper.accumulate() == accumulate(data)
op = operator.mul
assert wrapper.accumulate(op) == accumulate(data, op)
def test_bool(self):
assert not ArkoWrapper([1, 2, 3, 0, 5]).all()
assert ArkoWrapper([0, 0, 0, 0, 1, 0]).any()
assert not ArkoWrapper([0, 0, 0, 0, 0, 0]).any()
def test_chain(self):
assert wrapper.chain(range(10, 15), range(15, 20)) == range(1, 20)
def test_combinations(self):
...
def test_father(self):
class NewWrapper(ArkoWrapper):
def test(self):
return self.root
new_wrapper = NewWrapper(wrapper)
new_wrapper.filter_false(lambda _: True).filter(lambda _: False)
def test_map(self):
assert wrapper.map(lambda x: x * 2, 0) == map(lambda x: x * 2, wrapper)
assert (
wrapper.map(lambda x: x * 2, 1)
==
map(lambda x: x * 2, wrapper[1:])
)
def test_extend(self):
assert wrapper.extend(range(10, 15)) == range(1, 15)
if __name__ == '__main__': # pragma: no cover
pytest.main([__file__])