-
Notifications
You must be signed in to change notification settings - Fork 2
/
debug_test.go
101 lines (90 loc) · 2.59 KB
/
debug_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
package nject
import (
"fmt"
"regexp"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func debugOn(t *testing.T) {
debugOutputMu.Lock()
debuglnHook = func(stuff ...interface{}) {
t.Log(stuff...)
}
debugfHook = func(format string, stuff ...interface{}) {
t.Logf(format+"\n", stuff...)
}
debugOutputMu.Unlock()
atomic.StoreUint32(&debug, 1)
}
func debugOff() {
debugOutputMu.Lock()
debuglnHook = nil
debugfHook = nil
debugOutputMu.Unlock()
atomic.StoreUint32(&debug, 0)
}
func wrapTest(t *testing.T, inner func(*testing.T)) {
namedWrapTest(t, "", inner)
}
func namedWrapTest(t *testing.T, name string, inner func(*testing.T)) {
if !t.Run("1st attempt"+name, func(t *testing.T) { inner(t) }) {
t.Run("2nd attempt"+name, func(t *testing.T) {
debugOn(t)
defer debugOff()
inner(t)
})
}
}
func TestDetailedError(t *testing.T) {
t.Parallel()
type MyType1 struct {
Int int
}
type MyType2 []MyType1
type MyType3 *MyType1
type MyType4 interface {
String() string
}
type MyType5 interface {
unimplementable()
}
err := Run("expected-to-fail",
Desired(func() MyType1 { return MyType1{} }),
Shun(func(m MyType1) MyType3 { return &m }),
Required(func(m MyType3) MyType2 { return []MyType1{*m} }),
Cacheable(func() int { return 4 }),
MustCache(func() string { return "foo" }),
Cluster("c1",
Singleton(func(i int) int64 { return int64(i) }),
Loose(func(m MyType4) string { return m.String() }),
),
Cluster("c2",
Reorder(func() time.Time { return time.Now() }),
NotCacheable(func(i int) int32 { return int32(i) }),
),
func(_ MyType1, _ MyType3) {},
// CallsInner(func(i func()) { i() }),
Memoize(func(i int32) int32 { return i }),
OverridesError(func(i func()) error { return nil }),
MustConsume(func(i int32) int64 { return int64(i) }),
ConsumptionOptional(func(i int64) float64 { return float64(i) }),
func(m MyType5) error { return nil },
NonFinal(func() {}),
)
require.Error(t, err, "mess from the above")
detailed := DetailedError(err)
require.NotEqual(t, err.Error(), detailed, "detailed should have more")
t.Log("detailed error", detailed)
index := strings.Index(detailed, "func TestRegression")
require.NotEqual(t, -1, index, "contains 'func TestRegression'")
detailed = detailed[index:]
for _, word := range strings.Split("Desired Shun Required Cacheable MustCache Cluster Memoize OverridesError MustConsume ConsumptionOptional NonFinal", " ") {
re := regexp.MustCompile(fmt.Sprintf(`\b%s\(` /*)*/, word))
if !re.MatchString(detailed) {
t.Errorf("did not find %s( in reproduce output", word) // )
}
}
}