-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpipeline_test.go
113 lines (82 loc) · 2.12 KB
/
pipeline_test.go
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
package pipeline
import (
"strings"
"testing"
)
//
// StringPipes
//
type UpperCasePipe struct{}
func (u *UpperCasePipe) Handle(value PipeValue, next PipeNext) PipeValue {
// get value
text := value.(string)
capitalized := strings.ToUpper(text)
return next(capitalized)
}
type TrimSpacePipe struct{}
func (t *TrimSpacePipe) Handle(value PipeValue, next PipeNext) PipeValue {
// get value
text := value.(string)
trimmed := strings.Trim(text, " ")
return next(trimmed)
}
type ReplaceTextPipe struct{}
func (r *ReplaceTextPipe) Handle(value PipeValue, next PipeNext) PipeValue {
// get value
text := value.(string)
replaced := strings.ReplaceAll(text, "BURAKDEMIRTAS.ORG", "BUKI.DEV")
return next(replaced)
}
func TestStringPipelines(t *testing.T) {
pipes := []PipeInterface{new(UpperCasePipe), new(TrimSpacePipe)}
data := Send(" burakdemirtas.org ").Through(pipes).ThenReturn()
if data != "BURAKDEMIRTAS.ORG" {
t.Errorf("got %s, want BURAKDEMIRTAS.ORG", data)
}
pipes = append(pipes, new(ReplaceTextPipe))
data = Send(" burakdemirtas.org ").Through(pipes).ThenReturn()
if data != "BUKI.DEV" {
t.Errorf("got %s, want BUKI.DEV", data)
}
data = Send(" burakdemirtas.org ").Through(pipes).Then(func(value PipeValue) PipeValue {
return strings.ReplaceAll(value.(string), ".DEV", "")
})
if data != "BUKI" {
t.Errorf("got %s, want BUKI", data)
}
}
//
// ArrayPipes
//
type DoublePipe struct{}
func (s *DoublePipe) Handle(value PipeValue, next PipeNext) PipeValue {
// get value
numbers := value.([]int)
result := make([]int, len(numbers))
for i, v := range numbers {
result[i] = v * v
}
return next(result)
}
type SumPipe struct{}
func (s *SumPipe) Handle(value PipeValue, next PipeNext) PipeValue {
// get value
numbers := value.([]int)
sum := 0
for _, v := range numbers {
sum += v
}
return next(sum)
}
func TestArrayPipelines(t *testing.T) {
pipes := []PipeInterface{new(DoublePipe), new(SumPipe)}
result := Send([]int{1, 2, 3}).Through(pipes).ThenReturn()
if result != 14 {
t.Errorf("got %d, want 14", result)
}
}
//
// MapPipes
//
func TestMapPipelines(t *testing.T) {
}