-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpipe_test.go
More file actions
87 lines (68 loc) · 1.49 KB
/
Copy pathpipe_test.go
File metadata and controls
87 lines (68 loc) · 1.49 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package pipe_test
import (
"errors"
"math/rand"
"testing"
. "github.com/aslrousta/pipe"
. "github.com/stretchr/testify/assert"
)
func TestPipe(t *testing.T) {
t.Run("Empty", func(t *testing.T) {
pipe := Pipe()
NotNil(t, pipe)
NoError(t, pipe())
})
t.Run("One", func(t *testing.T) {
executed := false
pipe := Pipe(func() { executed = true })
NotNil(t, pipe)
NoError(t, pipe())
Equal(t, true, executed)
})
t.Run("More", func(t *testing.T) {
var result int
pipe := Pipe(
func(n int) int { return n + 12 },
func(n int) { result = n / 2 },
)
NotNil(t, pipe)
NoError(t, pipe(10))
Equal(t, 11, result)
})
t.Run("Error", func(t *testing.T) {
var result int
pipe := Pipe(
func(n int) (int, error) {
if n > 0 {
return n * n, nil
} else {
return 0, errors.New("n must be positive")
}
},
func(n int) { result = n / 2 },
)
NotNil(t, pipe)
NoError(t, pipe(6))
Equal(t, 18, result)
err := pipe(-1)
EqualError(t, err, "1st func failed: n must be positive")
EqualError(t, errors.Unwrap(err), "n must be positive")
})
}
var result int
func BenchmarkPipe(b *testing.B) {
sqr := func(x int) int { return x * x }
inc := func(x int) int { return x + 1 }
x := rand.Intn(1000)
b.Run("SqrPlusOneDirect", func(b *testing.B) {
for n := 0; n < b.N; n++ {
result = inc(sqr(x))
}
})
b.Run("SqrPlusOnePipe", func(b *testing.B) {
pipe := Pipe(sqr, inc, func(x int) { result = x })
for n := 0; n < b.N; n++ {
pipe(x)
}
})
}