This repository has been archived by the owner on Jan 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob_test.go
89 lines (83 loc) · 2.03 KB
/
job_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
package xgraph
import (
"context"
"io"
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
)
type testCase struct {
Name string
Args []interface{}
Func interface{}
Expect []interface{}
}
func (tc testCase) runTest(t *testing.T) {
args := make([]reflect.Value, len(tc.Args))
for i, v := range tc.Args {
args[i] = reflect.ValueOf(v)
}
vals := reflect.ValueOf(tc.Func).Call(args)
valint := make([]interface{}, len(vals))
for i, v := range vals {
valint[i] = v.Interface()
}
if !reflect.DeepEqual(valint, tc.Expect) {
t.Errorf("Expected %v but got %v for test %q", spew.Sdump(tc.Expect), spew.Sdump(valint), tc.Name)
}
}
func (tc testCase) genTest(t *testing.T) {
t.Run(tc.Name, func(t *testing.T) {
tc.runTest(t)
})
}
func TestBasicJob(t *testing.T) {
tests := []testCase{
{
Name: "name",
Func: BasicJob{JobName: "test"}.Name,
Expect: []interface{}{"test"},
},
{
Name: "run",
Args: []interface{}{context.Background()},
Func: BasicJob{RunCallback: func() error { return nil }}.Run,
Expect: []interface{}{nil},
},
{
Name: "run-error-propogate",
Args: []interface{}{context.Background()},
Func: BasicJob{RunCallback: func() error { return io.EOF }}.Run,
Expect: []interface{}{io.EOF},
},
{
Name: "run-missing-callback",
Args: []interface{}{context.Background()},
Func: BasicJob{}.Run,
Expect: []interface{}{ErrMissingCallback},
},
{
Name: "shouldrun-default",
Func: BasicJob{}.ShouldRun,
Expect: []interface{}{true, nil},
},
{
Name: "shouldrun-custom",
Func: BasicJob{ShouldRunCallback: func() (bool, error) { return false, io.EOF }}.ShouldRun,
Expect: []interface{}{false, io.EOF},
},
{
Name: "dependencies",
Func: BasicJob{Deps: []string{"dep1", "dep2"}}.Dependencies,
Expect: []interface{}{[]string{"dep1", "dep2"}, nil},
},
{
Name: "dependencies-defailt",
Func: BasicJob{}.Dependencies,
Expect: []interface{}{[]string{}, nil},
},
}
for _, tv := range tests {
tv.genTest(t)
}
}