-
Notifications
You must be signed in to change notification settings - Fork 0
/
gomads_test.go
77 lines (62 loc) · 1.7 KB
/
gomads_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
package gomads_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/tjarratt/gomads"
)
var _ = Describe("Monads", func() {
Describe("Maybe", func() {
It("can sometimes contain a value", func() {
m := Maybe(func() interface{} {
return nil
})
Expect(m.Value()).To(BeNil())
})
It("can be tricked into returning something if nothing was provided", func() {
m := Maybe(func() interface{} {
return nil
}).OrSome("fooled you!")
Expect(m.Value()).To(Equal("fooled you!"))
})
It("can chain maybe-values together", func() {
m := Maybe(func() interface{} {
return nil
}).OrSome(Maybe(func() interface{} {
return "chained"
}))
Expect(m.Value()).To(Equal("chained"))
})
It("treats funcs that panic as nil", func() {
m := Maybe(func() interface{} {
panic("woah there")
})
Expect(m.Value()).To(BeNil())
})
})
Describe("chained errors", func() {
It("can be used to chain operations that may fail", func() {
e := Error(func(thunk interface{}) interface{} {
return 5
}).Compose(Error(func(thunk interface{}) interface{} {
return 1
}))
Expect(e.Value()).To(Equal(1))
})
It("returns nil if any of the operations panics", func() {
e := Error(func(_ interface{}) interface{} {
return 5
}).Compose(Error(func(_ interface{}) interface{} {
panic("FFFFFFFFFFFFFFFFUUUUUUUUUUUUUUU")
}))
Expect(e.Value()).To(BeNil())
})
It("chains the result of the computation along through each func", func() {
e := Error(func(_ interface{}) interface{} {
return 1
}).Compose(Error(func(thunk interface{}) interface{} {
return thunk.(int) + 1
}))
Expect(e.Value()).To(Equal(2))
})
})
})