-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvals_test.go
108 lines (93 loc) · 2.37 KB
/
vals_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package result_test
import (
"errors"
"testing"
"github.com/bmheenan/result"
"github.com/stretchr/testify/assert"
)
func TestTryVals(t *testing.T) {
a, b := result.TryVals(func() (string, string, error) {
return "hello", "world", nil
}()).OrPanic("Couldn't get strings")
assert.Equal(t, "hello world", a+" "+b)
c, d := result.TryVals(func() (string, string, error) {
return "", "", errors.New("expected error")
}()).OrUse("hello", "world")
assert.Equal(t, "hello world", c+" "+d)
}
func TestValsErrorf(t *testing.T) {
assert.EqualError(
t,
result.ValsErrorf[int, int]("Expected error %v %v", 10, "hi"),
"Expected error 10 hi",
)
}
func TestValsOrError(t *testing.T) {
defer result.HandleReturn()
a, b := func() (res result.Vals[int, int]) {
v := result.NewVals(1, 2)
v.
OrError("Unexpected error")
return v
}().OrPanic("Got an error")
assert.Equal(t, 1, a)
assert.Equal(t, 2, b)
_, _ = func() (res result.Vals[int, int]) {
defer result.Handle(&res)
result.ValsErrorf[int, int]("Expected error").
OrError("OrError triggered")
return result.NewVals(0, 0)
}().OrDoAndReturn(func(err error) {
assert.EqualError(
t,
err,
"OrError triggered: Expected error",
)
})
t.Errorf("This line should not execute")
}
func TestValsOrDoAndReturn(t *testing.T) {
defer result.HandleReturn()
a, b := result.NewVals("hello", "world").
OrDoAndReturn(func(err error) {
t.Errorf("This line should not execute")
})
assert.Equal(t, "hello", a)
assert.Equal(t, "world", b)
_, _ = result.ValsErrorf[int, int]("Expected error").
OrDoAndReturn(func(err error) {
assert.EqualError(
t,
err,
"Expected error",
)
})
t.Errorf("This line should not execute")
}
func TestValsOrPanic(t *testing.T) {
a, b := result.NewVals(1.1, "1.2").
OrPanic("NewVals was an error Vals")
assert.Equal(t, 1.1, a)
assert.Equal(t, "1.2", b)
assert.PanicsWithErrorf(
t,
"Panic: Expected error",
func() {
_, _ = result.ValsErrorf[int, string]("Expected error").
OrPanic("Panic")
},
"Expected panic from error Vals",
)
}
func TestValsOrUse(t *testing.T) {
a, b := result.NewVals(true, -50).
OrUse(false, 100)
assert.Equal(t, true, a)
assert.Equal(t, -50, b)
c, d := result.ValsErrorf[map[int]string, int]("Expected error").
OrUse(map[int]string{
0: "hello",
}, 100)
assert.Equal(t, map[int]string{0: "hello"}, c)
assert.Equal(t, 100, d)
}