-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute_test.go
70 lines (66 loc) · 2.19 KB
/
route_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
package clif_test
import (
"context"
"errors"
"testing"
"github.com/google/go-cmp/cmp"
"impractical.co/clif"
)
func TestRoute(t *testing.T) {
t.Parallel()
type testCase struct {
app clif.Application
input []string
expectedCmdName string
expectedFlags clif.FlagSet
expectedArgs []string
expectedErr error
}
cases := map[string]testCase{
"basic": {
input: []string{"help"},
app: clif.Application{Commands: []clif.Command{{Name: "help", Handler: funcCommandHandler(func(_ context.Context, _ *clif.Response) {})}}},
expectedCmdName: "help",
expectedFlags: clif.FlagSet{},
},
"list-flags": {
input: []string{"hello", "--name=foo", "--name", "bar", "--name", "baaz"},
app: clif.Application{Commands: []clif.Command{{Name: "hello", Flags: []clif.FlagDef{{Name: "--name", AllowMultiple: true}}, Handler: funcCommandHandler(func(_ context.Context, _ *clif.Response) {})}}},
expectedCmdName: "hello",
expectedFlags: clif.FlagSet{
"name": {
{Set: true, Raw: "foo"},
{Set: true, Raw: "bar"},
{Set: true, Raw: "baaz"},
},
},
},
}
for name, testCase := range cases {
t.Run(name, func(t *testing.T) {
t.Parallel()
res, err := clif.Route(context.Background(), testCase.app, testCase.input)
if err != nil && testCase.expectedErr == nil {
t.Fatalf("Unexpected error: %+v", err)
}
if err == nil && testCase.expectedErr != nil {
t.Fatal("Expected error, didn't get one")
}
if err != nil && testCase.expectedErr != nil && !errors.Is(err, testCase.expectedErr) {
t.Fatalf("Expected error %+v, got error %+v", testCase.expectedErr, err)
}
if err != nil && testCase.expectedErr != nil {
return
}
if res.Command.Name != testCase.expectedCmdName {
t.Errorf("Expected command %q, got %q", testCase.expectedCmdName, res.Command.Name)
}
if diff := cmp.Diff(testCase.expectedFlags, res.Flags); diff != "" {
t.Errorf("Unexpected diff comparing flags (-expected, +got): %s", diff)
}
if diff := cmp.Diff(testCase.expectedArgs, res.Args); diff != "" {
t.Errorf("Unexpected diff comparing args (-expected, +got): %s", diff)
}
})
}
}