-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry_test.go
80 lines (66 loc) · 3.01 KB
/
try_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
package control
import (
"errors"
"gotest.tools/v3/assert"
"strconv"
"testing"
)
var (
defaultTryError = errors.New("default Try error")
nilCauseError = errors.New("error cause should not be nil")
nilPredicateError = errors.New("predicate should not be nil")
_ Try[int] = Success[int]{10}
_ Try[int] = Failure[int]{cause: defaultTryError}
success = SuccessOf[int](10)
failure = FailureOf[int](defaultTryError)
EvenPredicate = func(value int) bool {
return value%2 == 0
}
)
func TestIsFailure(t *testing.T) {
assert.Assert(t, !success.IsFailure(), "success should not be a failure")
assert.Assert(t, failure.IsFailure(), "failure should not be a success")
}
func TestTryOf(t *testing.T) {
assert.Assert(t, !TryOf(func() (int, error) { return 10, nil }).IsFailure(), "should not be a failure")
assert.Assert(t, TryOf(func() (int, error) { return 0, defaultTryError }).IsFailure(), "should not be a success")
}
func TestOrElse(t *testing.T) {
assert.Equal(t, success.OrElse(20), 10, "value should be 10")
assert.Equal(t, failure.OrElse(20), 20, "value should be 20")
}
func TestOrElseCause(t *testing.T) {
value, err := success.OrElseCause()
assert.NilError(t, err, err)
assert.Equal(t, value, 10, err)
_, err = failure.OrElseCause()
assert.Error(t, err, "default Try error", "should return default cause")
}
func TestTryFilterCheckParam(t *testing.T) {
_, err := success.Filter(nil, defaultTryError).OrElseCause()
assert.Error(t, err, nilPredicateError.Error(), "should return a Try with nil predicate cause")
_, err = success.Filter(EvenPredicate, nil).OrElseCause()
assert.Error(t, err, nilCauseError.Error(), "should return a Try with nil cause error")
_, err = failure.Filter(nil, defaultTryError).OrElseCause()
assert.Error(t, err, nilPredicateError.Error(), "should return a Try with nil predicate cause")
_, err = failure.Filter(EvenPredicate, nil).OrElseCause()
assert.Error(t, err, nilCauseError.Error(), "should return a Try with nil cause error")
}
func TestTryFilter(t *testing.T) {
assert.Assert(t, !success.Filter(EvenPredicate, defaultTryError).IsFailure(), "should not be a failure")
assert.Assert(t, failure.Filter(EvenPredicate, defaultTryError).IsFailure(), "should not be a success")
}
func TestTryMap(t *testing.T) {
var mapper = func(value int) string {
return strconv.Itoa(value)
}
assert.Assert(t, MapTry[int, string](failure, mapper).IsFailure(), "result of MapTry function should be a failure")
assert.Assert(t, !MapTry[int, string](success, mapper).IsFailure(), "result of MapTry function should be a success")
}
func TestTryFlatMap(t *testing.T) {
var mapper = func(value int) Try[string] {
return SuccessOf[string](strconv.Itoa(value))
}
assert.Assert(t, FlatMapTry[int, string](failure, mapper).IsFailure(), "result of MapTry function should be a failure")
assert.Assert(t, !FlatMapTry[int, string](success, mapper).IsFailure(), "result of MapTry function should be a success")
}