-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils_test.go
More file actions
108 lines (102 loc) · 2.56 KB
/
utils_test.go
File metadata and controls
108 lines (102 loc) · 2.56 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
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 iter
import (
"fmt"
"reflect"
"runtime"
"testing"
)
var (
nilValues = []interface{}{
(*interface{})(nil), (**interface{})(nil), (***interface{})(nil),
(func())(nil), (*func())(nil), (**func())(nil), (***func())(nil),
(chan int)(nil), (*chan int)(nil), (**chan int)(nil), (***chan int)(nil),
([]int)(nil), (*[]int)(nil), (**[]int)(nil), (***[]int)(nil),
(map[int]int)(nil), (*map[int]int)(nil), (**map[int]int)(nil),
(***map[int]int)(nil),
}
)
func TestIndirect(t *testing.T) {
type testindirectCircular *testindirectCircular
teq := func(t testing.TB, exp, got interface{}) {
if !reflect.DeepEqual(exp, got) {
t.Errorf("DeepEqual failed:\n exp: %#v\n got: %#v", exp, got)
}
}
t.Run("Basic", func(t *testing.T) {
int64v := int64(123)
int64vp := &int64v
int64vpp := &int64vp
int64vppp := &int64vpp
int64vpppp := &int64vppp
teq(t, indirect(int64v), int64v)
teq(t, indirect(int64vp), int64v)
teq(t, indirect(int64vpp), int64v)
teq(t, indirect(int64vppp), int64v)
teq(t, indirect(int64vpppp), int64v)
})
t.Run("Nils", func(t *testing.T) {
for _, n := range nilValues {
indirect(n)
}
})
t.Run("Circular", func(t *testing.T) {
var circular testindirectCircular
circular = &circular
teq(t, indirect(circular), circular)
})
}
func TestRecoverFn(t *testing.T) {
t.Run("CallsFunc", func(t *testing.T) {
var called bool
err := recoverFn(func() error {
called = true
return nil
})
if err != nil {
t.Error("expected no error in recoverFn()")
}
if !called {
t.Error("Expected recoverFn() to call func")
}
})
t.Run("PropagatesError", func(t *testing.T) {
err := fmt.Errorf("expect this error")
rerr := recoverFn(func() error {
return err
})
if err != rerr {
t.Error("expected recoverFn() to propagate")
}
})
t.Run("PropagatesPanicError", func(t *testing.T) {
err := fmt.Errorf("expect this error")
rerr := recoverFn(func() error {
panic(err)
})
if err != rerr {
t.Error("Expected recoverFn() to propagate")
}
})
t.Run("PropagatesRuntimeError", func(t *testing.T) {
err := recoverFn(func() error {
sl := []int{}
_ = sl[0]
return nil
})
if err == nil {
t.Error("expected runtime error to propagate")
}
if _, ok := err.(runtime.Error); !ok {
t.Error("expected runtime error to retain type type")
}
})
t.Run("PropagatesString", func(t *testing.T) {
exp := "panic: string type panic"
rerr := recoverFn(func() error {
panic("string type panic")
})
if exp != rerr.Error() {
t.Errorf("expected recoverFn() to return %v, got: %v", exp, rerr)
}
})
}