4
4
class TestPipeline (unittest .TestCase ):
5
5
6
6
7
+ def test_it_runs_a_pipeline_with_one_callable (self ):
8
+ pipeline = Pipeline ()
9
+ result = pipeline .send ('hello ' ) \
10
+ .through ([str .strip ]) \
11
+ .then_return ()
12
+ self .assertEqual ('hello' , result )
13
+
7
14
def test_it_runs_a_pipeline_with_callables (self ):
8
15
pipeline = Pipeline ()
9
- result = pipeline .send ('a sample string that is passed through to all pipes. ' ) \
16
+ result = pipeline .send ('hello world ' ) \
10
17
.through ([str .title , str .strip ]) \
11
18
.then_return ()
12
- self .assertEqual ('A Sample String That Is Passed Through To All Pipes. ' , result )
19
+ self .assertEqual ('Hello World ' , result )
13
20
14
21
def test_it_runs_a_pipeline_with_callables_and_executes_the_destination (self ):
15
22
pipeline = Pipeline ()
16
- result = pipeline .send ('a sample string that is passed through to all pipes. ' ) \
23
+ result = pipeline .send ('hello world ' ) \
17
24
.through ([str .title , str .strip ]) \
18
- .then (lambda x : x .replace ('A Sample ' , 'A Nice Long ' ))
19
- self .assertEqual ('A Nice Long String That Is Passed Through To All Pipes. ' , result )
25
+ .then (lambda x : x .replace ('Hello ' , 'Goodbye ' ))
26
+ self .assertEqual ('Goodbye World ' , result )
20
27
21
28
def test_it_runs_a_pipeline_with_callables_and_closures (self ):
22
29
pipeline = Pipeline ()
23
- result = pipeline .send ('a sample string that is passed through to all pipes. ' ) \
30
+ result = pipeline .send ('hello world ' ) \
24
31
.through ([
25
- lambda x , next : next (x .replace ('all ' , 'all the ' )),
32
+ lambda x , next_pipe : next_pipe (x .replace ('hello ' , 'goodbye ' )),
26
33
str .title ,
27
34
str .strip
28
35
]) \
29
36
.then_return ()
30
- self .assertEqual ('A Sample String That Is Passed Through To All The Pipes. ' , result )
37
+ self .assertEqual ('Goodbye World ' , result )
31
38
32
39
def test_it_runs_a_pipeline_with_closures (self ):
33
40
pipeline = Pipeline ()
34
- result = pipeline .send ('a sample string that is passed through to all pipes.' ) \
41
+ result = pipeline .send ('hello world' ) \
42
+ .through ([
43
+ lambda x , next_pipe : next_pipe (x .title ()),
44
+ lambda x , next_pipe : next_pipe (x .replace ('Hello' , 'Goodbye' ))
45
+ ]) \
46
+ .then_return ()
47
+ self .assertEqual ('Goodbye World' , result )
48
+
49
+ def test_it_runs_a_pipeline_with_custom_pipes (self ):
50
+ def custom_pipe (passable , next_pipe ):
51
+ passable = passable .replace ('Hello' , 'Goodbye' )
52
+ return next_pipe (passable )
53
+
54
+ pipeline = Pipeline ()
55
+ result = pipeline .send (' hello world ' ) \
56
+ .through ([
57
+ lambda x , next_pipe : next_pipe (x .title ()),
58
+ str .strip ,
59
+ custom_pipe
60
+ ]) \
61
+ .then_return ()
62
+ self .assertEqual ('Goodbye World' , result )
63
+
64
+ def test_it_runs_a_pipeline_with_classes (self ):
65
+ class TitlePipe :
66
+ def handle (self , passable , next_pipe ):
67
+ return next_pipe (passable .title ())
68
+
69
+ pipeline = Pipeline ()
70
+ result = pipeline .send (' hello world ' ) \
71
+ .through ([
72
+ TitlePipe (),
73
+ str .strip
74
+ ]) \
75
+ .then_return ()
76
+ self .assertEqual ('Hello World' , result )
77
+
78
+ def test_it_runs_a_pipeline_with_classes_and_custom_handler (self ):
79
+ class TitlePipe :
80
+ def execute (self , passable , next_pipe ):
81
+ return next_pipe (passable .title ())
82
+
83
+ pipeline = Pipeline ()
84
+ result = pipeline .send (' hello world ' ) \
85
+ .via ('execute' ) \
35
86
.through ([
36
- lambda x , next : next ( x . title () ),
37
- lambda x , next : next ( x . replace ( 'All' , 'All The' ))
87
+ TitlePipe ( ),
88
+ str . strip
38
89
]) \
39
90
.then_return ()
40
- self .assertEqual ('A Sample String That Is Passed Through To All The Pipes. ' , result )
91
+ self .assertEqual ('Hello World ' , result )
41
92
42
93
def test_it_runs_a_pipeline_by_sending_late (self ):
43
94
pipeline = Pipeline ()
44
95
pipeline .through ([str .title , str .strip ])
45
- result = pipeline .send ('a sample string that is passed through to all pipes. ' ) \
96
+ result = pipeline .send ('hello ' ) \
46
97
.then_return ()
47
- self .assertEqual ('A Sample String That Is Passed Through To All Pipes. ' , result )
98
+ self .assertEqual ('Hello ' , result )
48
99
49
100
def test_it_runs_a_pipeline_setup_via_pipe (self ):
50
101
pipeline = Pipeline ()
51
102
pipeline .pipe ([str .title , str .strip ])
52
- result = pipeline .send ('a sample string that is passed through to all pipes. ' ) \
103
+ result = pipeline .send ('hello ' ) \
53
104
.then_return ()
54
- self .assertEqual ('A Sample String That Is Passed Through To All Pipes. ' , result )
105
+ self .assertEqual ('Hello ' , result )
55
106
56
107
def test_it_bails_early (self ):
57
108
pipeline = Pipeline ()
58
109
result = pipeline .send ('bork' ) \
59
110
.through ([
60
- lambda x , next : False ,
111
+ lambda x , next_pipe : False ,
61
112
str .strip
62
113
]) \
63
114
.then ()
@@ -68,7 +119,7 @@ def test_it_bails_in_the_middle(self):
68
119
result = pipeline .send ('bork ' ) \
69
120
.through ([
70
121
str .strip ,
71
- lambda x , next : x ,
122
+ lambda x , next_pipe : x ,
72
123
str .title
73
124
]) \
74
125
.then ()
0 commit comments