-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompose_test.go
More file actions
76 lines (63 loc) · 1.74 KB
/
compose_test.go
File metadata and controls
76 lines (63 loc) · 1.74 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
package godux_test
import (
. "github.com/zpencerq/godux"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Compose", func() {
It("Composes from right to left", func() {
double := func(x int) int {
return 2 * x
}
square := func(x int) int {
return x * x
}
Expect(Compose(square)(5)).To(Equal(25))
Expect(Compose(square, double)(5)).To(Equal(100))
Expect(Compose(double, square, double)(5)).To(Equal(200))
})
It("Composes functions from right to left", func() {
a := func(next func(string) string) func(string) string {
return func(x string) string {
return next(x + "a")
}
}
b := func(next func(string) string) func(string) string {
return func(x string) string {
return next(x + "b")
}
}
c := func(next func(string) string) func(string) string {
return func(x string) string {
return next(x + "c")
}
}
final := func(s string) string {
return s
}
Expect(Compose(a, b, c)(final).(func(string) string)("")).To(Equal("abc"))
Expect(Compose(b, c, a)(final).(func(string) string)("")).To(Equal("bca"))
Expect(Compose(c, a, b)(final).(func(string) string)("")).To(Equal("cab"))
})
It("Can be seeded with multiple arguments", func() {
square := func(x int) int {
return x * x
}
add := func(x, y int) int {
return x + y
}
Expect(Compose(square, add)(1, 2)).To(Equal(9))
})
It("Returns the first given argument if given no functions", func() {
Expect(Compose()(1, 2)).To(Equal(1))
Expect(Compose()(3)).To(Equal(3))
Expect(Compose()(nil)).To(BeNil())
Expect(func() {
Compose()()
}).To(Panic())
})
It("Returns the first function if given only one", func() {
fn := func() int { return 3 }
Expect(Compose(fn)()).To(Equal(fn()))
})
})