-
Notifications
You must be signed in to change notification settings - Fork 0
/
either_test.go
83 lines (68 loc) · 2.81 KB
/
either_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
package control
import (
"errors"
"fmt"
"gotest.tools/v3/assert"
"strconv"
"testing"
)
var (
defaultEitherError = errors.New("default Either error")
_ Either[error, int] = Right[error, int]{10}
_ Either[error, int] = Left[error, int]{defaultEitherError}
right = RightOf[error, int](10)
left = LeftOf[error, int](defaultEitherError)
)
func TestIsRight(t *testing.T) {
assert.Assert(t, right.IsRight(), "should be a Right not Left")
assert.Assert(t, !left.IsRight(), "should be a Left not Right")
}
func TestIsLeft(t *testing.T) {
assert.Assert(t, !right.IsLeft(), "should be a Right not Left")
assert.Assert(t, left.IsLeft(), "should be a Left not Right")
}
func TestSwap(t *testing.T) {
assert.Assert(t, right.Swap().IsLeft(), "should be a Left not Right")
assert.Assert(t, left.Swap().IsRight(), "should be a Right not Left")
}
func TestEitherOrElse(t *testing.T) {
assert.Assert(t, right.OrElse(left).IsRight(), "should be a Right not Left")
assert.Assert(t, left.OrElse(right).IsRight(), "should be a Right not Left")
}
func TestEitherGetOrElse(t *testing.T) {
assert.Equal(t, right.GetOrElse(20), 10, "value should be 10")
assert.Equal(t, left.GetOrElse(20), 20, "value should be 20")
}
func TestEitherFilterOrElse(t *testing.T) {
transform := func(value int) error {
return fmt.Errorf("doesn't pass the EvenPredicate")
}
assert.Assert(t, right.FilterOrElse(EvenPredicate, transform).IsRight(), "should not be a Left")
assert.Assert(t, left.FilterOrElse(EvenPredicate, transform).IsLeft(), "should not be a Right")
odd := RightOf[error, int](11)
assert.Assert(t, odd.FilterOrElse(EvenPredicate, transform).IsLeft(), "should not be a Right")
}
func TestEitherFilter(t *testing.T) {
assert.Assert(t, !right.Filter(EvenPredicate).IsEmpty(), "should be a Some of Either")
assert.Assert(t, left.Filter(EvenPredicate).IsEmpty(), "should be a Empty of Either")
odd := RightOf[error, int](11)
assert.Assert(t, odd.Filter(EvenPredicate).IsEmpty(), "should be a Empty of Either")
}
func TestMapEither(t *testing.T) {
var mapper = func(value int) string {
return strconv.Itoa(value)
}
var mapRight = MapEither(right, mapper)
assert.Equal(t, mapRight.GetOrElse("good"), "10", "value should be 10")
var mapLeft = MapEither[error, int, string](left, mapper)
assert.Assert(t, mapLeft.IsLeft(), "should be an Left Either")
}
func TestFlatMapEither(t *testing.T) {
var mapper = func(value int) Either[error, string] {
return RightOf[error, string](strconv.Itoa(value))
}
var mapRight = FlatMapEither(right, mapper)
assert.Equal(t, mapRight.GetOrElse("good"), "10", "value should be 10")
var mapLeft = FlatMapEither[error, int, string](left, mapper)
assert.Assert(t, mapLeft.IsLeft(), "should be an Left Either")
}