forked from scaleway/scaleway-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_interceptor_test.go
More file actions
69 lines (59 loc) · 1.6 KB
/
command_interceptor_test.go
File metadata and controls
69 lines (59 loc) · 1.6 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
package core_test
import (
"context"
"testing"
"github.com/alecthomas/assert"
"github.com/scaleway/scaleway-cli/v2/core"
)
func Test_CombineCommandInterceptor(t *testing.T) {
runner := func(context.Context, interface{}) (interface{}, error) {
return []string{"runner"}, nil
}
newInterceptor := func(name string) core.CommandInterceptor {
return func(ctx context.Context, args interface{}, runner core.CommandRunner) (interface{}, error) {
res, _ := runner(ctx, args)
return append([]string{name}, res.([]string)...), nil
}
}
type TestCase struct {
Interceptors []core.CommandInterceptor
Expected []string
}
run := func(tc *TestCase) func(t *testing.T) {
return func(t *testing.T) {
t.Helper()
interceptor := core.CombineCommandInterceptor(tc.Interceptors...)
res, _ := interceptor(nil, nil, runner)
assert.Equal(t, tc.Expected, res)
}
}
t.Run("simple", run(&TestCase{
Interceptors: []core.CommandInterceptor{
newInterceptor("A"),
},
Expected: []string{"A", "runner"},
}))
t.Run("with two interceptor", run(&TestCase{
Interceptors: []core.CommandInterceptor{
newInterceptor("A"),
newInterceptor("B"),
},
Expected: []string{"A", "B", "runner"},
}))
t.Run("with nil", run(&TestCase{
Interceptors: []core.CommandInterceptor{
newInterceptor("A"),
nil,
newInterceptor("B"),
},
Expected: []string{"A", "B", "runner"},
}))
t.Run("with three interceptor", run(&TestCase{
Interceptors: []core.CommandInterceptor{
newInterceptor("A"),
newInterceptor("B"),
newInterceptor("C"),
},
Expected: []string{"A", "B", "C", "runner"},
}))
}