-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtesting_test.go
108 lines (90 loc) · 3.35 KB
/
testing_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 kod
import (
"context"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/go-kod/kod/internal/mock"
)
func Test_testRunner_sub(t *testing.T) {
t.Run("failure", func(t *testing.T) {
mock.ExpectFailure(t, func(tb testing.TB) {
tb.Helper()
r := &runner{}
err := r.sub(tb, nil)
if err != nil {
tb.FailNow()
}
})
})
}
func Test_checkRunFunc(t *testing.T) {
t.Run("not a func", func(t *testing.T) {
_, _, err := checkRunFunc(context.Background(), 0)
assert.EqualError(t, err, "not a func")
})
t.Run("must not be variadic", func(t *testing.T) {
_, _, err := checkRunFunc(context.Background(), func(t *testing.T, a ...int) {
})
assert.EqualError(t, err, "must not be variadic")
})
// must have no return outputs
t.Run("must have no return outputs", func(t *testing.T) {
_, _, err := checkRunFunc(context.Background(), func(t *testing.T, a int) int {
return 0
})
assert.EqualError(t, err, "must have no return outputs")
})
t.Run("must have at least two args", func(t *testing.T) {
_, _, err := checkRunFunc(context.Background(), func() {
})
assert.EqualError(t, err, "must have at least two args")
})
// function first argument type *testing.T does not match first kod.Run argument context.Context
t.Run("function first argument type *testing.T does not match first kod.Run argument context.Context", func(t *testing.T) {
_, _, err := checkRunFunc(context.Background(), func(t *testing.T, a *testing.T, b *testing.T) {
})
assert.EqualError(t, err, "function first argument type *testing.T does not match first kod.Run argument context.Context")
})
t.Run("function argument %d type %v must be a component interface or pointer to component implementation", func(t *testing.T) {
_, _, err := checkRunFunc(context.Background(), func(ctx context.Context, t int) {
})
assert.EqualError(t, err, "function argument 1 type int must be a component interface or pointer to component implementation")
})
t.Run("ok", func(t *testing.T) {
_, _, err := checkRunFunc(context.Background(), func(ctx context.Context, a *testComponent) {
})
assert.Nil(t, err)
})
}
// extractComponentInterfaceType
func Test_extractComponentInterfaceType(t *testing.T) {
t.Run("not a pointer", func(t *testing.T) {
_, err := extractComponentInterfaceType(reflect.TypeOf(0))
assert.EqualError(t, err, "type int is not a struct")
})
t.Run("not a struct pointer", func(t *testing.T) {
_, err := extractComponentInterfaceType(reflect.TypeOf(&testing.T{}))
assert.EqualError(t, err, "type *testing.T is not a struct")
})
t.Run("not a component interface", func(t *testing.T) {
_, err := extractComponentInterfaceType(reflect.TypeOf(&struct{}{}))
assert.EqualError(t, err, "type *struct {} is not a struct")
})
t.Run("not a struct", func(t *testing.T) {
_, err := extractComponentInterfaceType(reflect.TypeOf(&struct {
Implements[testing.T]
}{}))
assert.EqualError(t, err, "type *struct { kod.Implements[testing.T] } is not a struct")
})
t.Run("type struct {} does not embed kod.Implements", func(t *testing.T) {
_, err := extractComponentInterfaceType(reflect.TypeOf(struct{}{}))
assert.EqualError(t, err, "type struct {} does not embed kod.Implements")
})
t.Run("ok", func(t *testing.T) {
_, err := extractComponentInterfaceType(reflect.TypeOf(struct {
Implements[testing.T]
}{}))
assert.Nil(t, err)
})
}