forked from JulienPalard/Pipe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pipe.py
More file actions
69 lines (45 loc) · 1.51 KB
/
test_pipe.py
File metadata and controls
69 lines (45 loc) · 1.51 KB
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
import pipe
def test_uniq():
assert list(() | pipe.uniq) == []
def test_take_zero():
assert list([1, 2, 3] | pipe.take(0)) == []
def test_take_one():
assert list([1, 2, 3] | pipe.take(1)) == [1]
def test_empty_iterable():
assert list([] | pipe.take(999)) == []
def test_aliasing():
is_even = pipe.where(lambda x: not x % 2)
assert list(range(10) | is_even) == [0, 2, 4, 6, 8]
def test_netcat():
data = [
b"HEAD / HTTP/1.0\r\n",
b"Host: python.org\r\n",
b"\r\n",
]
response = ""
for packet in data | pipe.netcat("python.org", 80):
response += packet.decode("UTF-8")
assert "HTTP" in response
def test_enumerate():
data = [4, "abc", {"key": "value"}]
expected = [(5, 4), (6, "abc"), (7, {"key": "value"})]
assert list(data | pipe.enumerate(start=5)) == expected
def test_class_support():
class Factory:
n = 10
@pipe.Pipe
def mul(self, iterable):
return (x * self.n for x in iterable)
assert list([1, 2, 3] | Factory().mul) == [10, 20, 30]
def test_pipe_repr():
@pipe.Pipe
def sample_pipe(iterable):
return (x * 2 for x in iterable)
assert repr(sample_pipe) == "piped::<sample_pipe>(*(), **{})"
@pipe.Pipe
def sample_pipe_with_args(iterable, factor):
return (x * factor for x in iterable)
pipe_instance = sample_pipe_with_args(3)
real_repr = repr(pipe_instance)
assert "piped::<sample_pipe_with_args>(" in real_repr
assert "3" in real_repr